15

I'm using CMake to generate my projects IDE/makefiles, but I still need to call custom "scripts" to manipulate my compiled files or even generate code.

In previous projects I've been using Python and it was OK, but now I'm having serious trouble managing a lot of dependencies in two very big projects I'm working on so I want to minimize the dependencies everywhere.

Someone suggested to me to use C++ to write my build scripts instead of adding a language dependency just for that. The projects themeselves already use C++ so there are several advantages that I can see:

  • to build the whole project, only a C++ compiler and CMake would be necessary, nothing else (all the other dependencies are C or C++);
  • C++ type safety (when using modern C++) makes everything easier to get "correct";
  • it's also the language I know the better so I'm more at ease with it even if I'm able to write some good Python code;
  • potential gain in execution speed (but i don't think it will really be perceptible);

However, I think there might be some drawbacks and I'm not sure of the real impact as I didn't try yet:

  • might be longer to write the code (that said I'm not sure because I'm efficient enough in C++ to write something that work quickly, so maybe for this system it wouldn't be so long to write) (compilation time shouldn't be a problem for this case);
  • I must assume that all the text files I'll read as input are in UTF-8, I'm not sure it can be easilly checked at runtime in C++ and the language will not check it for you;
  • libraries in C++ are harder to manage than in scripting languages;

I lack experience and forsight so maybe I'm missing advantages and drawbacks. So the question is: does it make sense to use C++ for this? do you have experiences to report and do you see advantages and disadvantages that might be important?

9
  • 1
    Depending on a single language interpreter for the build only doesn't seem too bad, especially since it's Python -- very portable, widespread on Linux and easily available on Windows. If you need additional Python libraries, you can use virtualenv and it won't add anything but one Python file (which you could ship) internet connection and a bit of plumbing to set up the virtualenv, install them in the, and run your actual build scripts in that environment. You could get down to an extra script in your build and an internet connection as requirement.
    – user7043
    Apr 4, 2012 at 14:58
  • You can guesstimate the encoding of text with a fairly good reliability.
    – DeadMG
    Apr 4, 2012 at 15:00
  • @DeadMG Yeah, but doing so by hand if far too much work (if you want a good guess and not limit yourself to "is this ascii or unicode, and if it is unicode, how many bits and what byte order?") and adding another dependency (if there's a free and free-standing library for this at all) defeats the point of dropping to C++ to stop depending on Python.
    – user7043
    Apr 4, 2012 at 15:01
  • @delnan Yes but it's still on more language to install. From your description, any additional dependency would'nt seem "too bad" but I'm considering it because there is a lot to manage already so maybe it can help to limit the languages used too. I also use an embedded scripting language (implemented in C++ directly in the project), javascript/HTML/CSS, XML and other formats that are already a lot to keep in mind.
    – Klaim
    Apr 4, 2012 at 15:01
  • 2
    what will you use to build your c++ build script?
    – jk.
    Apr 4, 2012 at 15:14

4 Answers 4

24

Just use Python.

I develop in C++ and do my build scripts in Python, and I would find it painful to do build scripts in C++:

  • Python makes it trivial to manipulate dictionaries, lists, nested dictionaries of dictionaries of lists, etc. (For example, one of my scripts uses a multi-level hierarchy of all of my tools, tools' versions, and tools' versions' paths.) C++ can do the same with templates and custom classes, but it's much more verbose (which translates to more lines of code, which generally translates to lower productivity).
  • Python provides high-level libraries and routines like its XML and JSON handling, subprocess, and os.walk. Again, C++ can do this, but it's a lot more work to find the libraries, learn their APIs, correctly assemble the calls (which are often lower level), etc.
  • Build scripts are a non-value-added activity (to borrow a term from lean). It's better to use as high-level a language as possible, to get them done as quickly as possible, to get back to work which benefits your users.
  • In my experience, build scripts tend to grow in unforeseen ways. Even if a task seems initially simple for C++, it can get complicated in a hurry. When a new requirement comes up, it's often a lot simpler to tack on handling in a Python script than it is to do it in C++ (which may require finding or reading up on new library APIs, etc.).

Regarding the advantages which you list for C++:

  • Adding a single dependency (Python) shouldn't significantly complicate your build. It's already standard on most Linux installations, for example. Thanks to Python's "batteries included" libraries, it may even be easier to manage than the C++ libraries that your build scripts would depend on.
  • The type safety that C++ gives is most useful for large projects, not small scripts.
  • Python complements C++ very well (high-level versus lower-level, dynamically typed versus statically typed, etc.) and can even integrate with C++ very well (thanks to SWIG and Boost.Python) if you later want to do that, so it's worth learning for a C++ programmer.
  • As you said, execution speed should be a nonissue.
1
  • We did this (python builds) after a year of strugglign with cmake on a cross paltform project. In the end we didn't even bother trying to do dependancy checking (the most complex part of a build) . Since for automated builds you want to rebuild everything anyway and in C++ there are so many complex dependancies Apr 4, 2012 at 15:16
1

I believe this to be case-specific question. I'd claim there's no correct answer whether it makes sense or not to use C++ for build scripts, the only way to figure it out is to try it out in practice.

Personally I'd see Python superior to C++ because of better expressiveness of the language and easier(personal opinion, of course) standard library tools to manage the task of manipulating binary files and generating code. Of course, sophisticated libraries developed for the task might be available, but if not, then I'd personally definitely bet for Python to be the "more often correct" answer - in general case.

1

Don't write scripts yourself, you are duplicating effort and re-inventing wheels.

Use something like SCONS or even Maven 3 which has support for C++ build systems.

A good build system, regardless of language should not require custom logic in the form of scripts to build a functioning executable artifact.

If you have to write scripts for a build system to customize it, it isn't a good build system. Writing a plugin for a build system is a different story, but still that should be very particular to an environment/hardware mostly, and should be something that is rarely resorted to.

6
  • 1
    As I explained, I m using cmake already but I still need custom scripts to be done. I m talking about those custom scripts, not the build process itself.
    – Klaim
    Apr 5, 2012 at 1:35
  • 1
    I love Maven -- the script needed to build a "Hell World" C program is longer than the program!
    – quant_dev
    Apr 5, 2012 at 9:24
  • @quant_dev, I remember the first time I looked into using Maven, on their own web page, they recommend to get started with someone that already knows maven, since its hard to setup the first time. That was the first sign that I wanted nothing at all to do with it!
    – Brady
    May 28, 2012 at 8:19
  • I liked SCons, it'a bit slow but very easy to use.
    – quant_dev
    May 28, 2012 at 10:17
  • 1
    @JarrodRoberson I used "scripts" too liberally. I mean its configuration files. I'm not sure if being declarative makes Maven automagically more portable and easier to maintain. It does make it less flexible though, which can be a pain in large projects.
    – quant_dev
    May 28, 2012 at 15:58
0

To focus on the question:

Does it make sense to write build scripts in C++?

The answer is a simple no.

The currently accepted answer zooms into python, and lists a whole bunch of valid concerns, but I would like to add a language-independent reason:

You really don't want to script anything in any compiled language if you can help it:

  • You'll need a script to bootstrap the script!
  • Or you'd need to check in the compiled script into source control (and keep it in sync with the checked in sources!)
  • The script suddenly also has a build configuration for the script attached. (That needs to be maintained!)

In addition, going with the other answer:

Probably you also don't want to use a "raw" scripting language to author your build scripts (I get that the C++ intricacies would be handled by CMake).

What you probably should do is pick one of the Build. Systems. out. there. and see whether one fits your bill wrt. scriptability / extendability / play-nice-with-CMake / crossplatformness.

Not the answer you're looking for? Browse other questions tagged or ask your own question.