CMake build and project directories - cmake

I wonder to know why projects structures has folders like bin and lib but normally (at least in tutorials I saw), people creates a folder named build and use cmake ...
Is it the right way to use ${CMAKE_BINARY_DIR}/bin or ${CMAKE_SOURCE_DIR}/bin to build a project?

You're right, people using CMake often do build in a different directory (e.g. one called build). That's called "out of source" building, and it's useful because it helps keep built artifacts out of your source tree so you don't check them in.
As for bin and lib, those are the conventional names on Unix-like systems for directories storing executable files and libraries respectively. It's good to keep your build artifacts separated this way because it makes it clear where to look for things you can run vs. things you can build against. A common setup in CMake would be to have a build directory containing bin and lib within--if your build rules are set up properly, CMake will create bin and lib when you run your build.

Related

Generating compile_commands.json without generating build files

I'd like to generate a compile_commands.json file for use with the clangd language server. However, EXPORT_COMPILE_COMMANDS only works for the make and ninja build systems. When building a project that uses a different build system it would be convenient to also be able to generate compile_commands.json files as if I was using make or ninja without actually generating any build files that interfere with the build system that I'm using to perform the build.
What is the most convenient way to do this with cmake?
I think your only option here is to have a different build folder with Ninja or Makefile to generates the compile_commands.json and have a different build folder for your "actual" build.
The thing is, CMake is a generator, and it doesn't support mixed builds; and in fact, it should not. If they do that, you will end up having random artifacts from different build systems inside the build folder that might eventually conflicts with each others.
That being said, you are aware that what you get in Ninja-based compile_commands.json is not going to be fully relevant to your "actual" build system that you want to use. I can see it being useful, but not the same for sure.

How to parse whole project but build only subfolder that have dependancies on other subfolder, using cmake

I'm porting a project to linux and cmake. It is part of a huge project, each subproject in a folder. I want to build only my project and its dependencies.
I can comment the other projects from the main CMakeList.txt file, but that doesn't work because there are a few dependencies to other projects (which may also depend on other projects...)
I don't want to modify all the cmake files from other projects to build only the small pieces that I need because is very error prone and time consuming. I could checkin something by mistake which I was not supposed to, for example.
My main problem is that the other projects break continuisly (almost everybody else is just working in windows, so the linux build is not taken good care of) and is slowing me down a lot.
My question is: Is there a way to tell cmake: run through all the project, but compile only what is needed to compile certain subfolder?
You can do:
add_subdirectory(dir EXCLUDE_FROM_ALL)
and then all targets within that subdirectory (and recursive) are by default marked with EXCLUDE_FROM_ALL, that way they will not build by default.
You may also specify the specific targets to be build with:
cmake --build the_build_dir --target this_target -t another_target

How do I generate a clean (ready to release) build in CMake?

For the record, because this seems like there should be an obvious, well-known way to do it: I can't find any docs on how to do this - all I find is people looking to do a "clean" of their built files when I search for this.
When you build out-of-source in CMake, the resulting directory does not exclusively contain your built executable and other files. CMake leaves all its cache and makefiles, as well as other intermediate files, lying around - for example, the CMakeFiles directory, the CMakeCache.txt, Makefiles, .cbp, .ilk, .pdb, etc. - meaning if I wanted to release the results of said build, I'd need to (automatically or manually) either remove excess cmake files, or copy all my generated files elsewhere manually, before I am ready to release. This would be tedious for projects that require a specific structure in the output directory, or have many output files (e.g. an assets folder full of dozens of built asset files - now polluted with CMakeFiles et al.)
When building with CMake, can I not only perform an out-of-source build where intermediate files (as seen above) end up out-of-source, but where the resulting cmake binary directory is also separate and kept free of polluting by CMake and extra compiler output? (e.g. resulting in a build/output directory that cleanly contains all the actual built files with proper directory structure.)

How to make a CMake package?

I'm attempting to make a CMake package for Crypto++ inclusion in CMake projects, this will end up in the noloader/cryptopp-cmake repo if it gets done.
The ultimate goal is to come up with a working cross-platform FindCryptoPP.cmake file which can be dropped in the Crypto++ source directory to do things like:
find_package(CryptoPP REQUIRED)
target_link_libraries(libbiocoin cryptopp-static)
Or:
find_package(CryptoPP REQUIRED)
target_link_libraries(libbiocoin cryptopp-shared)
In a finished application and have it "just work."
My current best solution within a CMake application is to build Crypto++ for the platform, stick the resulting archive or library in a lib directory, reference that within the CMakeLists.txt and pull it in that way, but of course that requires packaging a binary distribution of the compiled Crypto++ for every platform targeted by the application, which would be nasty to maintain and generally bad even if it weren't crypto code.
It's better to provide a CMake configuration file. find_package will look for a configuration file if no FindFoo.cmake find script is provided. One advantage over a find script is that you won't end with different, maybe conflicting versions of the find script.
See https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html, especially the section Create Layout.

CMake: automatically find target dependencies in other CMake projects

If we have a case of highly decentralized development environment, where there are many repositories and projects, is there an existing functionality in CMake that automatically finds dependencies between targets without a top level CMake file?
The workflow is something like this, you specify a directory and all targets are default-configured in the given tree. Then you can go and build any of the projects. I am looking for a behavior similar to that when you build the Android OS.
There is no build-time dependency tracking in CMake across different projects. For this case you need to have a project on the top-level which adds all the subdirectories, so that the target names are available inside a single CMake project.
I am aware of one helper script around CMake which provides the required inter-project dependencies: https://github.com/aldebaran/qibuild
I would say that is getting close to a mature code base. However, it requires additional descriptor files for each project. Might be worth to have a look at it.