Meson can't find cmake subdirectory - meson-build

In one of my meson.build files, I have code that I need to generate a dependency for spdlog.
libspd = cmake.subproject('spdlog')
spd_dep = libspd.dependency('spd_dep')
The meson.build in question resides in a directory called vendor, and the spdlog directory also sits in it.
./
...
vendor/
meson.build
spdlog/
CMakeLists.txt
...
However, when I configure the project, meson throws this error:
Neither a subproject directory nor a spdlog.wrap file was found.. Why is this? Clearly, the spdlog directory is in the same directory as in meson.build in question, right?
Also, I know I should be using wrapfiles, but due to limitations that is impossible. I've also asked on IRC, but no-one responded, and I'm on a deadline.

Meson has predefined location for subprojects - All subprojects must be in subprojects directory. So, you should have structure:
vendor/
meson.build
subprojects/
spdlog/
CMakeLists.txt

Related

How to specify CMake Preset for subdirectories?

I have a big library with a lot of options that I recently converted to CMake from make, with a lot of options. I use configuration presets to have multiple profiles. The library is developed by my team, while the applications building over it are developed by other teams. The library provides common functionalities and is a must to be consumed by the apps. We were seeking to provide sane default options via presets.
Right now, I want to build a toy app over it.
I have the following directory structure where a CMakePresets.json file is located in the subdirectory (my_lib/, which is actually a git submodule).
main.cpp
CMakeLists.txt // Top-Level CMakeLists
my_lib/
------> lib.c // simplified view of the functionalities here
------> lib.h
------> CMakeLists.txt
------> CMakePresets.json
# Top level CMakeLists.txt
project(toy_app)
add_subdirectory(my_complex_lib)
set(SOURCES main.cpp)
add_executable(toy_app ${SOURCES})
target_link_libraries(toy_app my_lib)
Running cmake --list-presets at the top level won't detect sub-directories presets.
The question is how to specify the preset from the top-level invocation or even provide a default one in the CMakelists inside the library?
Trial #1
I have found the idea of default preset was rejected ( https://gitlab.kitware.com/cmake/cmake/-/issues/21417 )
Trial #2
I tried to add CMakePresets.json at the top level and included the one inside my_lib and it worked.
Running cmake --list-presets on the top level can find presets right now.
I wonder if there are other solutions?
As you indicate in your question post "Trial #2", yes, you can include other json files following the CMakePresets.json schema into CMakePresets.json files. See the docs on includes for more info. Here's an example from the docs:
{
...
"include": [
"otherThings.json",
"moreThings.json"
],
...
}
So as you found out, you can keep your main CMakePresets.json file in the subdirectory and create another one in the project's root directory that includes the subdirectory one, or you can do it the other way around: Put your main one in the project's root directory and create one that just includes it in the subdirectory.
This could be useful if you wanted to generate buildsystems for both the root directory and the subdirectory. I.e. you want your subdirectory's CMake config to be able to be used as a standalone project. In that case you might be making it a git submodule of the root project, or using a monorepo structure for the users of various parts of your project.
If you're going with a git submodule, then you couldn't put a main CMakePresets.json file in the project's root directory and include it in the submodule because the submodule repo should stand alone without knowing about what uses it as a submodule. In that case, you could put the main CMakePresets.json in the repo that gets used as a submodule, or just make two "copies" of the same CMakePresets.json file- one for each repo.
If you wanted to have a utility CMakePreset.json file with comon things used by multiple repos, you could create a repo with just that utility file and include that repo as a submodule to all your other repos and then use the CMakePresets.json's "include" mechanism to pull in those utility preset configurations into the repo's own CMakePresets.json file.

Building Documentation for Nested projects with CMake

I'm working on a project that contains other projects as libraries using git
submodules. These sub-projects are under active development as well, so the root
cmake file will run the sub-project cmake files as well.
Example Project Structure:
current_project/
src/
include/
docs/
libs/
sub_module_1/
src/
include/
docs/
sub_module_2/
src/
include/
docs/
sub_module_3/
src/
include/
docs/
src/
The actual building portion works fine. In some of the sub projects, the custom target docs is used to
generate the documentation. Since there are multiple docs targets defined for each
subproject, cmake complains. I don't have control over the sub-projects, and
therefore cannot simply edit the cmake files for those. Is there a way to, without
editing the sub-projects (as the changes will be overwritten when I do a git
update), either have cmake combine all the commands for each sub-project and run all
of them, or have it generate a prefix (probably the project name) for the docs
target for each project?
Edit: I see that CMake has projects as a defined term. I'm not referring to cmake projects. When I am saying project, I am referring to libraries and programs that, while related, are independent of each other.

CMake: include a directory into parent from within a subdirectory (aka library)

I have a small project with this structure:
myproject/
mylib/
include/
src/
myprog/
include/
src/
I added a CmakeLists.txt file into myproject and added the subdirectory mylib and myprog. The subdirectories got a CMakeLists, too. Now, when I run cmake, both modules are built correctly, unless I want to use mylib in myprogram. I've found solutions where the global CmakeLists defines an include_directories. But I would prefer to define this in mylib such that mylib/include is added to the project' include path. I would like a project of submodules where each submodule defines it's sources and includes and the project's CMakeLists only connects the modules together. How can I do that? Is that what I try to achieve recommanded? (cmake newbie)
You want target_include_directories. See http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html

what does "<cmake_binary_dir>/bin" mean?

I want to populate with some script files in the /bin subdirectory of CMake directory, but can not find the /bin file.
What can I give guys some advice?
Thanks
I have to disagree with gvd's answer, since CMAKE_BINARY_DIR should always point to the top level directory of the build tree. If you need the location of the build tree where the current CMakelists.txt goes to, use CMAKE_CURRENT_BINARY_DIR
CMAKE_BINARY_DIR
if you are building in-source, this is the same as CMAKE_SOURCE_DIR,
otherwise this is the top level directory of your build tree
CMAKE_CURRENT_BINARY_DIR
if you are building in-source, this is the same as CMAKE_CURRENT_SOURCE_DIR,
otherwise this is the directory where the compiled or generated files from the
current CMakeLists.txt will go to
So the answer is: cmake_binary_dir/bin points to toplevelBuilddir/bin
CMAKE_BINARY_DIR is the build folder location of your CMakeLists.txt file.
Say you have:
MyProject/application/CMakeLists.txt
and your build output is in MyProject/build
when using CMAKE_BINARY_DIR in the above CMakeLists.txt, it points to MyProject/build/application/
Have a look at this list of CMake variables for more variables.

CMake : produce Makefiles in each subdirectory?

Assuming the following configuration :
parentdir1/parentdir2/myproject
parentdir1/parentdir2/myproject/CMakeLists.txt
parentdir1/parentdir2/myproject/src
parentdir1/parentdir2/myproject/src/dir1
parentdir1/parentdir2/myproject/src/dir2
parentdir1/parentdir2/myproject/src/dir3
Currently, my CMakeLists produces only one Makefile in parentdir1/parentdir2/myproject.
How to produce a Makefile in each subdirectory (to compile the associated directory) ?
Thank you very much.
CMake allows you to have a CMakeLists.txt file in any subdirectory under the root directory of your source tree.
Then use the add_subdirectory command in CMakeLists.txt to add the subdirectories. Then CMake will look for CMakeLists.txt in those subdirectories.
See the documentation for the add_subdirectory command here:
http://cmake.org/cmake/help/cmake-2-8-docs.html#command:add_subdirectory
For more info, check out the CMake FAQ and documentation on www.cmake.org.