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?

Klaim
  • 14,902
  • 4
  • 51
  • 62

4 Answers4

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.
Josh Kelley
  • 11,131
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.

zxcdw
  • 5,115
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.

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.

Martin Ba
  • 7,862