C++ – Does It Make Sense to Write Build Scripts in C++?

build-systembuildsccmake

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?

Best Answer

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.
Related Topic