Introduce a CMAKE_FIND_PACKAGE_IGNORE_PACKAGES variable
For a while I have been following Daniel Pfeiffers suggestion on redefining find_package()
.
Now I stumbled upon @craig.scott article on discouraging this practice
https://crascit.com/2018/09/14/do-not-redefine-cmake-commands/
and I see the point.
However the power that this solution provides is really important to us! It probably shouldn't be set by libraries, for other libraries, but for quick integrations it can be really helpful:
-
Projects Samples
When I write CMake files for my projects, I want that they build as part of the project, but users should also be able to just take the file and get going, using it against an installed version of the library.
libA src CMakeLists.txt samples CMakeLists.txt CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.22) project(libA) set(CMAKE_FIND_PACKAGE_IGNORE_PACKAGES libA) add_subdirectory(src) add_subdirectory(samples)
src/CMakeListst.txt
add_library(libA ...) add_library(libA::libA ALIAS libA)
samples/CMakeLists.txt
project(test) find_package(libA REQUIRED) add_executable(test) target_link_libraries(test PRIVATE libA)
Telling CMake to ignore to search for my own project, allows me to write cleaner and reusable samples.
-
Bug fixing / co-development of libraries Let's say I am developing an app that uses a library, and I now find a bug in said library. With the given pattern, it's incredibly easy to fix the bug and test in the context off the app. The setup is the basically the same as in the above usecase
Integrated build:
- CMakeList.txt - libA (clone libA repo) - CMakeLists.txt - app (clone app repo) - CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.22) project(integrated_build) set(CMAKE_FIND_PACKAGE_IGNORE_PACKAGES libA) add_subdirectory(libA) add_subdirectory(app)
I can achieve a build this without even changing a single line of code in the respective projects. I know I can achieve the same thing by doing a build, then install, then setting the libA_DIR variable, but it's so much more complicated. I will always have to run the install in between.
This way, you also achieve real co-development, and it provides a complete different layer of integration possiblities.
How do you feel about adding a CMAKE_FIND_PACKAGE_IGNORE_PACKAGES
variable, that allows this behavior, without users having to go to the undocumented behavior?
If not, are there other patterns I can use, especially for the samples usecase?