9 ms·
Using CMake and Managing Dependencies
- xvilka 5y agoI wish CMake would have kept the engine but change the syntax to something more sensible.
- flohofwoe 5y agoAgreed. What cmake does under the hood is actually very sensible and useful, but the arcane scripting language makes even simple things hard. Adding a "proper" alternative scripting language would be a good reason to bump the version number to 4.0
- klapatsibalo 5y agoLet's make a language that compiles to CMake! :D
- xvilka 5y agoLet's call it Autocmake.
- MaxBarraclough 5y agoOr perhaps CTools.
- rualca 5y ago...or CMake Development Kit (CDK)
- hoseja 5y agoIt is serviceable after time, and if you don't need nothing too fancy. Just remember that basically everything is a string.
- wmkn 5y agoCMake as a library with bindings for various scripting languages (e.g. Python) would make a lot of sense to me. Have the power of a proper scripting language and let the CMake library deal with generating the build system. I once looked into extracting the CMake build system generation code and make it into a standalone library. But I soon realized that it’s not exactly a weekend project.
- mathstuf 5y agoAgain, the way the language works is baked deep into the project. The stringly-typed nature is everywhere. Using Python isn't going to magically fix that list elements that contain `;` "don't work right". Policy scoping is weird and is not lexical (e.g., policies "stick" to function definitions, so calling a function can cross a policy boundary). It's one of those "ah, that sounds nice" things that is just too much work once you get down to it (and we have enough compatibility to handle without dealing with the vagaries of umpteen languages as it is).
- rualca 5y ago> CMake as a library with bindings for various scripting languages (e.g. Python) would make a lot of sense to me. Well, if your thing is fragmentation and bit rot and maintenance hell, sure. There were plenty of competing build systems for C++ that provided Python bindings, such as SCons. For some reason, CMake with its awful BASIC-like DSL became the de facto standard.
- umvi 5y agoThey should have kept it as a frontend and backend like llvm so people could use any language to interface with it
- therealjumbo 5y agoCouldn't they still do this? I think there was a project a ways back to add a lua frontend to CMake. It's been since abandoned, but I don't see why it couldn't be revitalized. I guess the project just doesn't see the value in doing it, or they don't have enough manpower.
- mathstuf 5y agoSee my sibling comment. The way CMake's language has evolved is very organic. There is a declarative core now with targets and usage requirements, but due to backwards compatibility guarantees, actually making it the only thing available is nigh impossible. Lua was also deemed not suitable in the long run because of our backwards compatibility guarantees. We'd need to ship N Lua interpreters internally because Lua is not compatible between 5.1, 5.2, 5.3, etc. Or say "we only support Lua 5.2, we cannot support 5.4" which…distros really dislike.
- enriquto 5y ago10 lua interpreters would still be less than 1% of the cmake code base. Shipping all of them would be orders of magnitude better than the current clusterfuck that cmake is. But in reality you would only need one.
- cprecioso 5y agoThat's kinda Meson I guess. I found it super pleasant - once you grasp the concepts and terminology; but the website is very informative.
- xvilka 5y agoMeson has different engine and requires Python. CMake is already nearly perfect in everything except syntax. Moreover, it requires only C++ to be built so bootstrapping is easier than in case of Meson.
- ironman1478 5y agoPython doesn't seem like a huge barrier to entry to me. CMake might be great in many ways, but the syntax is truly awful. It's just not intuitive to many people. I think due to how spacing works in CMake you can have incorrect things run and not fail. That's a huge issue, especially when onboarding people who aren't familiar with CMake. Meson has some warts, especially around documentation but it's just easier and is just generally predictable. I've converted some CMake projects internally to meson and people were able to contribute and make changes with minimal learning and training, because the structure is intuitive. That means a lot.
- dgellow 5y agoSo much this. Their DSL is so full of weird details.
- mxcrossr 5y agoAlso I wish they could nuke from orbit all the tutorials and stack exchange answers that are completely wrong about how to use it!
- gspr 5y agoWhy people think it's a good idea for the build systems to reach out and grab code from the internet is beyond me.
- hoseja 5y agoIt's not a build system, it's a meta build system.
- mathstuf 5y ago[ Full disclosure: CMake developer. ] Eh. CMake is a build system. It supports multiple build tools, but it's not like one can just tell CMake "here's some literal Make code, please put it into the generated files". The generated builds are still restricted to the semantics that CMake itself provides via its interface (e.g., no dyndep support in `add_custom_command` because only the Ninja generator supports that reasonably well; it's possible with Make, but I don't know how easy it would be to make it work for arbitrary custom commands).
- iamcreasy 5y ago[Not a C/C++ dev] I am under the assumption CMake do not perform build itself, it produces the build file for Make, Ninja etc. and have those tools run the build file to produce the final binary. If that's correct, wouldn't it make CMake a meta build system?
- mathstuf 5y agoRight, I'm being more semantic here. CMake is a build system with the concepts of targets, linking, compiling, languages, toolchains, etc. The fact that it gets "rendered" into a `build.ninja`, `.vcxproj`, `.xcodebuild`, or `Makefile` is (largely) ambivalent (I call these "build tools" or "build executors"). Other instances would be `gn` or Meson. They're the build system; ninja is "just" the build tool they use to actually implement their semantics. Still others like build2, Boost.build, or the Bazel family are both: the build system and the build tool. One cannot use CMake to write arbitrary Makefiles using all the fancy features available through macro expansion, rule templates, etc. All you have are CMake's semantics that can then be executed by a tool that implements POSIX makefile execution behaviors. A (probably poor) analogy is a network resource. It can be provided over HTTP, Gopher, or whatever. The semantics of the resource are the same, but the transport mechanism is just how it interacts with the rest of the world.
- vadersb 5y agoVery nice tutorial on the subject! Probably it's a noob question, but - with such CMake setup, will it be possible to step into dependencies' source code when debugging?
- gomoboo 5y agoExcellent guide. I wish I had had it when struggling to learn CMake initially. There’s good content there for those past the basics as well. For example, I had no idea FetchContent could be used to download release zips. I will be trying that out tonight.
- varajelle 5y agoI think FetchContent is quite recent and only really usable since cmake 3.14 or so, released in 2019. So maybe that was just not yet possible when you learnt cmake initially.
- mr_sturd 5y agoI've much preferred, in recent years, to use Conan for fetching, building and importing external dependencies. Being able to use Python to manage the individual packages made it somewhat easier to get stuck in to; avoiding, as much as possible, the dark art that is writing scripts for CMake.
- slavik81 5y agoI really wanted to use Conan, but the deal-breaker for me was discovering that Conan can only create one CMake target for a package, and that it must be called 'packagename::packagename'. Conan's packages could never work like native installs of the dependencies I wanted to use.
- hamstergene 5y agoThe author of the article will realize their mistake when they try to consume a library that consumes its own dependencies via the same FetchContent mechanism, and one of those dependencies will be the same as what author’s executable needs. Or when they need to compile a build tool (like protobuf compiler) that needs to target build host rather than what CMake currently targets. All such ad-hoc solutions are not scalable, and as the project grows the moment will come when you’ll have to throw all of that away and do everything differently. Dependency management is a lot harder problem than just downloading and unpacking a zip. Why not leave the job to specialized tools from the start? The argument against package managers is poor. At least with Conan, your project's CMake list does not have to depend on the package manager; instead one can consume dependencies from Conan and use `cmake_paths` generator and `find_library()` to link to them, allowing consumers of your project to use something other than Conan. EDIT: Also, many open-source projects are not based on CMake themselves but have very weird very custom build scripts e.g. OpenSSL, FFmpeg, that can easily kill several days of your time each just to get going. All while package managers' recipes already have all of that figured out, just provide `feature1=yes feature2=no ...` and they will handle the rest.
- gabereiser 5y agoThis. For some of my native projects I’ve resorted to building a “fat lib” or amalgamated build of all dependencies into a static library. It’s a pattern I first saw when dealing with gameplay3d. It works for me. I then include the fat lib as a dependency to my library and let the compiler prune my lib of unused while leaving the static dependency lib unchanged. CMake on the surface looks good, is easy to work with, but once you start having dependencies of dependencies (some of which you need too) you run into issues that you have to script your way out of. I’ve tried vpkg as well and it has the same kind of issues. What does the future hold for native development when so much of your initial time is spent fighting the tools?
- halayli 5y agoI feel that if the author took the time to learn the problem domain well and why conan & vcpkg happen to exist, then this article wouldn't have been written. It's not like people that started conan/vcpkg didn't know about cmake's FetchContent and friends. Just a simple case of switching branches where some dependencies are different is going to trigger a lot of issues. And it's a big waste of time waiting for everything to compile instead of having a package server like jfrog hosting compiled libraries & source and making sure all your flags and toolchain setup is applied. And dealing with library versions, and making them available to other dependencies outside your project. And having projects that use cmake and others that use autotools, and some that are custom etc. Those issues are a few obvious ones that you'll see if you barely scratch the surface. Unfortunately, this article will stay on the internet and spread a very problematic way of dealing with dependencies.
- rualca 5y ago> I feel that if the author took the time to learn the problem domain well and why conan & vcpkg happen to exist, then this article wouldn't have been written. It's not like people that started conan/vcpkg didn't know about cmake's FetchContent and friends. Exactly this. I would add that the author, for some reason, left out the most basic and I dare standard way to manage third-party dependencies: have a setup/provision script that downloads and installs dependencies locally. You know, what Conan does, but ad-hoc. In Linux it might be just a call to the package manager to install stuff from a djstro's dependencies, a kin to NPM's npm install. In Windows it might even be a big whole zip file bundling prebuilt binaries.