7 ms·
You can handle The Diamond with CMake
- wheybags 6y agoThis is a really weird way of doing things. You can just add_subdirectory from the top level all the way down as intended. Then if you only want to build A, you just do "make A" instead of parameterless "make". Am I missing something that makes this approach bad? IMO this is the idiomatic way to do it.
- BeeOnRope 6y agoHow would you lay out the directory structure for the diamond dependency shape discussed in the post? Specifically, where would "base" live?
- sjburt 6y agoroot/ base/ a/ b/ As long as root has a CMakeLists.txt and includes base, a, and b, cmake will figure it out. After the build tree has been built with cmake, you can even run make from the build/root/a/ directory and base/ and a/ will be built. The author, for some reason, does not want a cmakelists.txt in root/ and wants to run cmake from a or b alone so they have to use a more convoluted approach.
- qznc 6y agoOne reason against the CMakeLists.txt at the root: When you generate a Visual Studio "solution", every target becomes a "project". At work this results in a few hundred projects and VS gets slow. Someone build a Python script to trim it down. This is treating symptoms instead of root causes and adds accidental complexity. Of course, the root CMake also needs to parse all sub directories which requires time. However, this has not been a big problem in practice so far. Only on the "annoying" level.
- wheybags 6y agoThat sounds like you've just got too many projects. Why not merge them? You also save wasted time on linking static libraries (at least on Linux this is significant, dunno about windows).
- ihnorton 6y agoMany (if not most) libraries don’t work correctly with add_subdirectory.
- wheybags 6y agoMy normal strategy is too just dump most of their build system and remake it myself in cmake. Even if they use cmake, I often end up commenting out most of it. If you keep things super simple in your cmake, it all works very well. Writing super simple cmake is also pretty easy, as well. Regardless, this article was talking about internal dependencies, not external ones.
- junon 6y agoThis doesn't work all the time. The problem is that there's a difference with how targets are built up vs. how find_package() specifies you should detect system deps, and they're inherently incompatible with each other.
- wheybags 6y agoThis is talking about internal dependencies though, not system ones. That said, my strategy for system deps is to just not use them and vendor everything I can, rewriting their build system in cmake if I have to.
- junon 6y agoYes, exactly. It doesn't always work like you'd expect. Custom find modules can use custom variables and thus you have to define them manually in every case. This macro gets you half the way there, at least.
- cheesedoodle 6y agoI really appreciate the advice from Daniel Pfeifer [0] where you treat all your build-tree sub-projects as out of tree dependencies, by overriding the `find_package` macro. This way, you can just find project dependencies using the `find_package` as if you were referring to something pre-built. Save a lot of hassle. macro(find_package) if(NOT "${ARG0}" IN_LIST as_subproject) _find_package(${ARGV}) endif() endmacro() [0] C++Now 2017: Daniel Pfeifer “Effective CMake" https://www.youtube.com/watch?v=bsXLMQ6WgIk&vl=en https://www.youtube.com/watch?v=bsXLMQ6WgIk&vl=en slides: https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf https://github.com/boostcon/cppnow_presentations_2017/blob/m...
- junon 6y agoThis requires a lot of extra work and doesn't often work like you'd expect. However, this is a shortcoming of CMake not having a good dependency model aside from the old (and IMO not far from obsolete) system dependencies model that Linux popularized. It made sense then but things have changed, and dependency injection is something CMake fails miserably at. We shouldn't have to do macro/function overrides, but I'm glad they exist in CMake. Current status quo is "it's really annoying and a huge headache to do, but at least CMake allows me to do it, unlike others".
- cheesedoodle 6y agoIf you mean the extra work is exporting your libraries and having a list of all your sub_projects in the root CMakeLists.txt, then yes. I think it's minimal work, as I would like to have all my libraries imported and linked anyway: find_package(MyLib) target_link_libraries(${PROJECT_NAME} PRIVATE MyLib::MyLib) This looks tidy any more explicit, avoids transitive leakage and more. What is it that does not often work as you expect?
- junon 6y agoBecause you're ignoring how find_package works and how dependencies use the variables inside of them. Not every find script creates some sort of target.
- xaedes 6y agoI like to have a meta project with the root CMakeLists.txt. In this meta project I either just add_subdirectory all necessary components manually, or I use Findxyz.cmake scripts for each module with that "header guard". They check if target is already defined, if not they add_subdirectory the required component. For each component (i.e. cmake project) I can then just find_package (module mode) all dependencies and target_link them. Like any other dependency. For consistency reasons I like to put cmake find modules for all external (installed) library as well and then let the find modules use find_package in config mode for installed libraries, and add_subdirectory for my own components, or for projects I want to include with source and build along with the other stuff. This way I have one point where I can control the source of all dependencies. Another way would be to just install all components as libraries in the system with proper cmake config files for find_package in config mode. I dont like to clutter my system with all kinds of application specific libraries, so that is not the way for me.
- hedora 6y agoOther than the fact that it generates VC++ projects, I haven’t been able to find any advantage of cmake over plain make. (I think auto downloading stuff during the build is a bug, not a feature. I’d also rather never use find_package. Many people disagree. That’s OK.) Are there any reasonable raw make based solutions for VC++?
- banachtarski 6y agoYou probably feel the way you do because: - you don’t target different platforms - you don’t target different compilers - you don’t cross compile and need to maintain multiple toolchains - you like juggling different compiler flags - you work on small codebases - you haven’t bothered to sit down with cmake for the few hours needed to understand why a cmake project might be more maintainable than a make one
- hedora 6y ago100% of your assumptions are wrong. I have years of experience with cmake, and all the other things you mentioned. I work in environments where the correctness of the binaries is important, and cmake fights that at every possible step. The documentation is poorly organized and overly verbose. 99% of the details in the docs are irrelevant 99% of the time, and the important details are missing or relegated to a non-discoverable page elsewhere on their site.
- Galanwe 6y agoThough I agree with your points, and do not like CMake either (I tend to much prefer autotools or plain make) there has been a time when I saw the use of it: maintaining a software with developers both on Windows using VS and Linux using make.
- banachtarski 6y agoPlease substantiate the claim about how "cmake fights that at every possible step." There's so much hyperbole in your statement that it really does seem like you've spent "years of experience with cmake" futzing around.
- randrews 6y agoThe article-title-rewriting thing has actually made this sound _more_ clickbaity. The original title prepends the word "how" which is much better.
- qznc 6y ago(author here) Initially my draft had the title "CMake and the Diamond". When the article was finished, I decided to make it more clickbaity because I'm not Paul Graham or Scott Alexander. Rumors are that words like "How to" and "You" make it look more interesting. I weakened it intentionally by adding the "can" because I don't claim that this approach is the one best solution.
- randrews 6y agoI think the article title is fine, I'm saying that the HN thing that removes numbers and "how to" and so on (to prevent clickbait titles) has done the opposite here. :)
- theamk 6y ago> A problem you might not notice initially is that CMake has no namespacing. This means it gets littered with pre- or postfixes like ${PROJECT_NAME} It is 2021, and we still have C++ developers program in a language which lacks namespaces, types and relies heavily on generated variable names. How did we end up there?
- sebastienbarre 6y ago"CMake is a conservative and popular build system [...] . Yet, it does not scale well to large projects". CMake was literally funded by NLM back then to handle a large open source project, ITK (then VTK). There are many, many other examples of large projects using CMake (MySQL, KDE, Minecraft, Second Life, Netflix internally, etc).
- ftrobro 6y agoCMake 3.10 and newer has the command "include_guard(GLOBAL)", for me that has solved any problems I previously had with diamond shaped dependencies. https://cmake.org/cmake/help/latest/command/include_guard.html https://cmake.org/cmake/help/latest/command/include_guard.ht...