![]() ![]() ![]() Now we just need to create a new release in GitHub’s UI, wait for everything to finish execute, and automatically have a packaged source file that people can download via FetchContent. Set ( package_files include/ src/ CMakeLists.txt LICENSE ) add_custom_command ( OUTPUT $ What to download?įetchContent_Declare can download the project from many different sources, but not all sources take the same time.Īt least from GitHub, cloning the git repository takes a lot longer than downloading and extracting the zipped sources: This is done by calling cmake_minimum_required(VERSION 3.18) inside the if() block. Option ( MY_PROJECT_BUILD_EXAMPLES "whether or not examples should be built" ON ) option ( MY_PROJECT_BUILD_TESTS "whether or not tests should be built" ON ) if ( MY_PROJECT_BUILD_EXAMPLES ) add_subdirectory ( examples ) endif () if ( MY_PROJECT_BUILD_TESTS ) enable_testing () add_subdirectory ( tests ) endif () …īy bifurcating the CMakeLists.txt in that way, we can even use different CMake versions for downstream consumers and library developers.įor example, lexy requires version 3.8 to consume it, but 3.18 to develop it. Project ( my_project LANGUAGES CXX ) # define build options useful for all useĪdd_subdirectory ( src ) if ( CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR ) # We're in the root, define additional targets for developers. In the library’s root CMakeLists.txt, it can be detected by comparing CMAKE_CURRENT_SOURCE_DIR with CMAKE_SOURCE_DIR: they’re only the same if it is the real root of the source tree.Īs such, we only define test targets, when this is not the case: It is therefore a good idea to prevent that by only exposing those helper targets when not used as a subdirectory. However, this includes targets that are not useful for downstream consumers like unit tests, documentation builders, and so on.Ĭrucially, this includes the dependencies of those targets – when using a library, I don’t want CMake to download that libraries testing framework! This makes all targets of the project available in the parent, so you can link against them and use them. If a project is used via FetchContent, CMake will automatically call add_subdirectory(). ![]() Let’s look at the library side of things for the remainder of the post. Very simple and straightforward, refer to CMake’s documentation for more details. This will automatically download the project and make the targets available so you can link against them and have them built as necessary.įetchContent_Declare ( lexy URL ) FetchContent_MakeAvailable ( lexy ) # Link against lexy's targets now. Hide developer targets like tests, provide a zip archive that contains only the source files relevant downstream, and use GitHub actions to create it automatically.įetchContent is a CMake module that makes downloading or “fetching” dependencies really trivial.Īll you need is to let CMake know where the sources are with a call to FetchContent_Declare() and then include them as a subproject with FetchContent_MakeAvailable(). If you’re a library writer, there are ways you can structure your CMake project to improve the experience for end users that use FetchContent: If all you need is to get the source code of a library, include in your CMake project, and have it compiled from source with the rest of your project, CMake’s FetchContent module can do it for you. ![]() If you’re working on an executable project in C++, as opposed to a C++ library, using a package manager to get your dependencies might be overkill: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |