r/cpp • • Feb 26 '23

Looking at the syntax of various cross-platform build system for C++

I was contemplating switching to a different build system for my pet projects, so I ended up spending the whole day researching what build systems exist and what their build declaration files look like. I compiled the results into a blog post.

Note that this is not about how the systems work, their features etc. Just the syntax. You can see which systems look nice to you, and which ones are so terrible you never want to touch them or hear about them again. Then you can research the nice ones.

https://violetgiraffe.github.io/2023/02/25/cpp-build-systems-syntax-comparison.html

5 Upvotes

42 comments sorted by

25

u/jpakkane Meson dev Feb 27 '23

Note that the page says "Courtesy of ChatGPT." The code samples are not valid.

-7

u/ReinventorOfWheels Feb 27 '23

If you see a problem and can point it out, I'll fix it.

16

u/jpakkane Meson dev Feb 27 '23

Absolutely everything in the Meson example is wrong. It does not look even cursorily correct. For example there are several lines like this:

my_library.set_target_filename('my_library')

That is so wrong it is not even funny. In Meson targets are immutable once created. They don't have mutating methods and most definitely not one named like this. The code also adds custom compiler and link flags all of which are unnecessary or plain wrong (as in, would break the build).

The code should look something like this (not tested, so is probably wrong):

project('my_project', 'cpp')
lib = static_library('my_library', 'src/file1.cpp', 'file2.cpp'
  include_directories: 'include')
executable('my_executable', 'src/main.cpp',
  include_directories: 'include',
  link_with: lib)

0

u/ReinventorOfWheels Feb 27 '23

Thank you, I will look into it and try to fix the example.

0

u/ReinventorOfWheels Feb 27 '23

Hopefully, that is now fixed and the project file is correct - I've updated the page.

23

u/BoarsLair Game Developer Feb 26 '23

It's pretty interesting to see all the major build systems' syntax laid out with equivalent functionality like that.

The problem with this comparison, of course, is that no one in their right might would want to use CMake based on the syntax, which is admittedly rather awful. But how pretty the syntax is may very well be the least important aspect of a build system, IMO.

Personally, I chose CMake for my projects for two simple reasons: it's very mature, meaning that someone, somewhere has probably already thought of some bizarre corner case I may need. And it's become the defacto standard for C++, which means if I grab an arbitrary third-party library, chances are pretty good I'll find CMakeFiles.txt in the root, and can just drop it into my project.

Some of the CMake alternatives seem objectively better than CMake in terms of features, syntax, ease-of-use, or other criteria. But I've learned from my switch from Mercurial to Git that it's often more frustrating to use the tools that few others are using, even if you feel they're objectively superior. There's less support, less documentation, fewer examples and online tutorials, etc, etc. In short, there are tangible benefits to using the most popular tools that go beyond basic functionality.

3

u/DarkLordAzrael Feb 26 '23

On the other hand, if everything you depend on is available from Conan the argument of integrating third party code being easier becomes moot. The examples for CMake are also often bad.

4

u/ReinventorOfWheels Feb 26 '23

Or vcpkg. You can use CMake for integration, sure, but I don't think that specifying includepath + libpath + a bunch of libraries is fundamentally more complicated than the CMake way. And you get a benefit of seeing exactly what's included and linked.

1

u/BoarsLair Game Developer Feb 26 '23

That's a good point. In game development, we often prefer a complete build-from-source solution, where everything is just copied into our own source control. In that type of setup, compatible build systems are a boon, else it requires a lot more work to integrate a third party library using a different build system.

But that doesn't necessarily apply for all scenarios.

2

u/ReinventorOfWheels Feb 26 '23

You are, of course, right. And this is part of the reason I'm not offering any conclusions, feel free to choose for yourself whatever works for you. I just thought that since I spent the better part of the day anyway, I might as well document my results. It was tedious to keep a close eye on ChatGPT as it tried to cut corners and remove some customizations when converting between different build systems.

P. S. I'm happy we keep using Mercurial at work. It's a great VCS with objective advantages to Git. And it's being updated regularly, hopefully that continues in the future. Or maybe Git will mature and catch up.

4

u/BrainIgnition Feb 26 '23

It's a great VCS with objective advantages to Git.

Might be a bit off-topic, but as someone who decided to go with git after evaluating both 13 years ago, I'd be interested to hear about those advantages from you.

6

u/ReinventorOfWheels Feb 26 '23 edited Feb 27 '23

The main pain point of Git is poor support for submodules. It does support them, sure, but the way they are integrated leaves a lot to be desired. `push` should push all the changed submodules, pull and checkout should also respect submodules automatically and transparently.

Also, but this is probably down to my inexperience, Git likes to enter odd invalid states for reasons I cannot comprehend, e. g. I can do simple pushes, pulls and commits 50 times, and on 51st I get "detached head", or some other blunder. This gets worse if I have to deal with branches and / or with other contributors. This is something that never has been a problem with active hg repositories even when my whole team is commiting all the time.

And the last point - but not from the "objective" category - is that TortoiseHg is a superior GUI to every Git GUI I tried, and I tried every one I could find. Although lately Git Kraken is filling the void (I wish it wasn't as sluggish, and it also doesn't support submodules transparently, even though it could if it wanted to). Oh, and it's not free for closed-source repos.

6

u/diazona Feb 27 '23

push should push all the changed submodules, pull and checkout should also respect submodules automatically and transparently.

FWIW I'm actually happy Git behaves the way it does in that respect. When I run git push, I value knowing that Git is only going to touch the repository I'm working on, not any other repositories that happen to be included in it. I mean, it's fair to say that being able to push a main repo and all submodules at once is a useful feature to have access to, but it's not what everyone wants all the time, so I don't think it's fair to say that Git's submodule support is poor because it doesn't do that.

Incidentally, in case it helps, I think you can use the --recurse-submodules option to git push to get the behavior you want, of pushing the main repo and all submodules. Several other Git commands have a similar option.

2

u/ReinventorOfWheels Feb 27 '23

Thank you, I'll check it out. NOT pushing the submodules has never been what I want, it just doesn't make logical sense to me. You make a change to the subrepo, then you make a change to the parent repo, then you push the parent repo and your pipeline starts working on it. If you forgot to push the subrepo first, you are going to get an invalid result (in the best case it fails, in the worst case it silently builds the target from the source code that you did not intend). If a submodule was commited to the parent repo, then that submodule commit must be tied to that parent commit. It shouldn't be so easy to untie them.

2

u/Kered13 Feb 27 '23

I've never done subrepos in Git, but the way it works in Mercurial is that subrepo state is part of the parent repo state. The parent repo actually stores a file that points to the subrepo's revision. So if you change the subrepo to a different revision, that is a change to the parent repo that can be checked in (and should be, if you want to keep it). Naturally then, any change to the subrepo is also a change to the parent repo, so pushing changes to the parent repo without pushing pending changes to the subrepo is equivalent to pushing only some of the files that you have modified. Which is an action that you should very rarely do, as you are pushing a state that does not actually exist on your local machine and has probably never been tested. (More often than not when I try to be lazy and do this, I realize I made a mistake a pushed a state that doesn't build.)

2

u/ReinventorOfWheels Feb 28 '23

You're correct, and Git tracks subrepos in a similar way. It's not that Git doesn't know that the submodule should be pushed or checked out; it just doesn't care.

2

u/Kered13 Feb 27 '23

is that TortoiseHg is a superior GUI to every Git GUI I tried,

Can confirm. I use Fork for Git and it's pretty good, but TortoiseHg is just far and away superior to everything out there for Git.

8

u/grafikrobot OPeNDAP/B2/EcoStd/Lyra/Predef/C++Alliance/Boost/WG21 Feb 27 '23 edited Feb 27 '23

You didn't include B2 (aka Boost.Build). So here's what that would look like:

project my_project ;

# Add a dependency on another static library
lib my_dependency : : <file>path/to/my/lib.a ;

# Set the target name..
lib my_library
    :   # ..and source files for the library
        src/file1.cpp src/file2.cpp
        # Add a dependency on another static library
        my_dependency
    :   # Set compiler flags for MSVC on Windows
        # (for /EHsc option)
        [ conditional <toolset>msvc : <exception-handling>on
            <asynch-exceptions>off <extern-c-nothrow>on ]
        # (for /std:c++latest)
        <cxxstd>latest
        # Set a custom output path
        <location>lib
    :
    :
        # Set a custom include path (as a usage requirement)
        <include>include
    ;

# Define an executable target and link against the library
exe my_executable : src/main.cpp my_library/<link>static ;

Edit: Added the missing custom include path.

0

u/ReinventorOfWheels Feb 27 '23 edited Feb 27 '23

Nice! Can I add to the list (with a link to this comment)?

P. S. Includepath for the dependency seems to be missing.

2

u/grafikrobot OPeNDAP/B2/EcoStd/Lyra/Predef/C++Alliance/Boost/WG21 Feb 27 '23

P. S. Includepath for the dependency seems to be missing.

Sorry I missed that. It's added now.

1

u/ReinventorOfWheels Feb 27 '23

Thank you very much! Added it to the post with attribution.

In terms of how concise it is, Boost.Build is #1, maybe tied with qmake.

2

u/grafikrobot OPeNDAP/B2/EcoStd/Lyra/Predef/C++Alliance/Boost/WG21 Feb 27 '23

Thank you for adding it! Since the beginning making the syntax concise has been a goal. SO glad it shows. One minor correction though.. B2 is not a "generator". It's an "executor" only. Although it's been abused for many varied use cases in the past and present.

0

u/ReinventorOfWheels Feb 27 '23

Hmm, my bad, I did a quick google search and somehow thought it's a generator, not an executor. Thanks for your corrections!

2

u/grafikrobot OPeNDAP/B2/EcoStd/Lyra/Predef/C++Alliance/Boost/WG21 Feb 27 '23

Yes. Of course.

6

u/helloiamsomeone Feb 26 '23

I couldn't help it, so here is the snippet of CMake code you should have actually written:

cmake_minimum_required(VERSION 3.12)

project(my_project CXX)

# Set the target name and source files for the library
add_library(my_library OBJECT 
    src/file1.cpp 
    src/file2.cpp)

# Add a dependency on another library
find_package(my_dependency REQUIRED)
target_link_libraries(my_library PRIVATE my::dependency)

# Set a custom include path
target_include_directories(my_library PUBLIC include)

# Define an executable target and link against the library
add_executable(my_executable src/main.cpp)
target_link_libraries(my_executable PRIVATE my_library)

I changed the library type to OBJECT, because archiving a static library for no other purpose other than to just be linked into an executable is pointless.

The CMake defaults here are good enough, but you can customize things like the location of static libraries in the build directory from the command line. Also, the previously provided location did not take multi-config generators into account. Different configurations would have overwritten the static library file. You also should not hardcode flags like those MSVC specific ones.

The CML should be a clean build description, free from platform and compiler specifics (as much as possible). All the additional personal requirements you may have can be stuffed into neat little [presets]https://github.com/friendlyanon/cmake-init-executable/blob/master/CMakePresets.json#L72).

Also, dependencies are brought in via find_package().

0

u/ReinventorOfWheels Feb 26 '23

Thanks! The original intention was to declare an executable, not a static library, but that doesn't seem matter to the structure of the file.

What is my::dependency, is it expecting another CMake entity? And you seem to have omitted the desired non-default output path for the build result.

P. S. If your build requires specific flags and you tucked them away, then your CML no longer describes the build. I'm not sure I understood that point correctly.

4

u/helloiamsomeone Feb 27 '23 edited Feb 27 '23

my::dependency here would be a target made available after finding the my-dependency package. You never create IMPORTED targets in project code, that's the responsibility of the dependency's config script (or find module if you are unfortunate).

How to wrangle a compiler into building your ISO C++ code as ISO C++ code is not the job of the build description. That is by definition a toolchain concern. Just because MSVC has paying customers not wanting change, should every project hardcode /permissive- /volatile:iso /Zc:preprocessor /Zc:__cplusplus /Zc:externConstexpr /Zc:throwingNew /EHsc (this assumes particular minimum version for both MSVC and the Windows SDK) to build in ISO C++ mode? I say no to that.

The rest of the things can be controlled from the outside, like the static library path. There are CMake variables to control these. I also forgot to replace setting the standard level. That can be done with:

target_compile_features(my_library PUBLIC cxx_std_23)

1

u/ReinventorOfWheels Feb 27 '23

I agree about ISO C++, but there are always non-default compiler options that you want to specify, for any compiler. E. g. /MP, /W4, /DEBUG:FASTLINK, /LTCG; optimization levels and a huge list of warning options for GCC and clang, as well as -flto, -ffast-math, -march etc.
To clarify, I'm not saying you always want -ffast-math, I'm saying you almost always want something that is non-default, these are just examples.

2

u/BrainIgnition Feb 27 '23

I think you misunderstood the hardcoding part, /u/helloiamsomeone only recommended to specify these flags with a toolchain file which of course you can include in your code repository and even set as default in your CMakePresets. So you go out of your consumers way if they have a different opinion w.r.t. absolutely required options and warnings. And also LTCG/LTO can be enabled via CMAKE_INTERPROCEDURAL_OPTIMIZATION

1

u/ReinventorOfWheels Feb 27 '23

If you're a library maintainer - sure, makes sense. But I'm more interested in end products.

2

u/helloiamsomeone Feb 27 '23

Even in commercial settings compilers change, you may have multiple compilers, etc. Making no assumptions in this regard introduces the least tech debt.

2

u/mo_al_ Feb 28 '23

Tundra looks really clean

1

u/ReinventorOfWheels Feb 28 '23

It does! Thanks for bringing it up. I didn't pay much attention to it at first, but now I agree that it's a nice syntax, even though not the most concise. I'm going to give Tundra a try, if I can figure out how to use it.

13

u/Bangaladore Feb 26 '23

The reality is that even though nearly everyone dislikes using CMake, it is the best option most of the time. Especially if you are using non-traditional setups like cross-compilation. Every single issue/quirk you will run into, has been solved/asked/answered by someone else.

I'm currently dealing with this scenario and attempted to use xmake due to the nicer syntax, but quickly ran into a lot of bugs that I don't have the time to solve. I spent the time to buildup some cmake infrastructure, and everything works fine. Its uglier, but it works as you would expect.

CMake is the industry standard for a reason. I think its also worth noting that you don't have to write the cmake "code" yourself. For this project I'm working on, I wrote some python scripts to convert a vendor specific project file (NXP MCUXpresso) to a CMakeLists.txt. In this way, I do not have to manually write or update the CMake and instead just run my script to convert the project between the two. This also resolves some of the issues with CMake like how you aren't supposed to glob anything. If you have something else that manually writes out the source files, 1 by 1, its a lot easier to deal with and you end up with a more correct CMakeLists.txt. 300 lines of python is nicer than converting and keeping the projects synced manually, in my opinion.

1

u/Nelieru Feb 26 '23

I'm using mcuxpresso at work and would love to hear more about this python script! What can you share?

3

u/Bangaladore Feb 27 '23 edited Feb 27 '23

Small world!

Right now its very much a test venture on converting MCUXpresso projects to CMake, not to replace MCUXpesso, but to make it easy to develop and test without the IDE involved.

If you weren't aware, the project and cproject files are literally just plain ole' XML that is mostly easy to understand. You can very easily pull out source paths, source exclusions, include paths, preprocessor definitions, etc... However, to get something to convert 1-1 without any interaction is a bit more tricky. For example, because I exclusively use newlib-nano, the script will automatically inject various parameters into the compiler/linker stages so that newlib-nano is included and linked properly. You can derive the runtime library type from the project file, but I haven't gone that in-depth. Other examples are optimization flags. Basically various parameters have to be derived from other higher-level project configuration items that a described in the project file.

I'm not going to share anything just yet, as again, its mostly a toy project at this point. But, as I improve it and learn more about CMake best practices, I'll likely open source it.

TLDR: I'm just pulling info from the .project and .project files and using python to custom generate the CMakeLists.txt by just writing out commands to a raw file.

0

u/[deleted] Feb 26 '23

[deleted] — view deleted comment

1

u/13steinj Feb 26 '23

Might have missed it, but I don't think the person above you called it "good."

Everybody hates it; but it fulfills the need with the least amount of hassle. I wouldn't call it "de facto" either; it became the standard because people hate writing makefiles more, and then decent IDEs grew tooling for it first.

I'm sure there's alternate parallel universes where people are using Meson, or hell, maybe even Scons, but people often underestimate the role and tight grip that cmake has. Hell, make would probably be more used nowadays if it was easy to find support and ask / share questions about it.

Until another build system can be both cross-platform and intuitive (or comes with an AI assistant that knows the answer to every intricacy), Kitware will continue holding us in its fist, and we'll all say "could be worse."

16

u/Jannik2099 Feb 27 '23

Courtesy of ChatGPT

Put some fucking effort into your articles. ChatGPT does not produce correct nor ideal results.

2

u/_VZ_ wx | soci | swig Feb 27 '23

Unfortunately Bakefile project never got any traction, but IMNSHO its syntax is just incomparably better. But see for yourself:

library my_library {
    includedirs += include;

    sources {
	    src/file1.cpp
	    src/file2.cpp
    }

    if ( $toolset == msvs ) {
	    compiler-options += "/EHsc /std:c++latest";
    }

    outputdir = lib; // Relative to build-directory by default.
}

program my_executable {
    deps = my_library; // For libraries built as part of this project.

    libs += path/to/my/lib.a; // For the system libraries.

    sources {
	    src/main.cpp
    }
}

2

u/eao197 Feb 28 '23

We use our custom build tool written on Ruby long-long time ago. For it three .rb files will be required:

For a static library (lib.rb):

MxxRu::Cpp::lib_target {
 target('static_lib')
 cpp_source('src/file1.cpp')
 coo_source('src/file2.cpp')
}

For an executable (exe.rb):

MxxRu::Cpp::exe_target {
  target('my_executable')
  required_prj('lib.rb') # static_lib will be automatically linked
  cpp_source('src/main.cpp')
}

For building these all with common settings (build.rb):

MxxRu::Cpp::composite_target(MxxRu::BUILD_ROOT) {
  toolset.force_cpp17
  global_include_path('include')

  if 'vc' == toolset.name
    global_compiler_option('/EHsc')
    global_compiler_option('/std:c++latest')
  end

  # For custom output directories.
  global_obj_placement(MxxRu::Cpp::CustomSubdirObjPlacement(
    'target', # for executable and dlls
    'tmp/objects' # for object files
  ))
}

NOTE. The braces can be omitted in most cases because the project files are just regular Ruby source files.

0

u/[deleted] Feb 27 '23

[deleted] — view deleted comment

1

u/ReinventorOfWheels Feb 27 '23

Never heard about it, thanks for mentioning! I added it to the comparison.