diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..081872a34bef1be02a5840c1a5f16f348284b7e4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,54 @@ +#[[ +MIT License + +CMake build script for LoadStaticSharedTargets module +Copyright (c) 2024 Tim Kaune + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + +cmake_minimum_required(VERSION 3.23) + +# If CMake >=3.24 is used, set policies up to v3.24 to NEW +if (NOT ${CMAKE_VERSION} VERSION_LESS 3.24) + cmake_policy(VERSION 3.24) +endif() + +project(LoadStaticSharedTargets VERSION 1.3.0 LANGUAGES NONE) + +include(GNUInstallDirs) + +# LoadStaticSharedTargets is a CMake script, which we never want to debug +# If using a multi config generator +if (GENERATOR_IS_MULTI_CONFIG) + set(CMAKE_CONFIGURATION_TYPES "Release") + set(CMAKE_DEFAULT_BUILD_TYPE "Release") +# If using a single config generator +else () + set(CMAKE_BUILD_TYPE "Release") +endif () + +add_subdirectory(src) + +string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level) +option(LoadStaticSharedTargets_INCLUDE_PACKAGING "Include packaging rules for LoadStaticSharedTargets" "${is_top_level}") + +if (LoadStaticSharedTargets_INCLUDE_PACKAGING) + add_subdirectory(packaging) +endif () diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000000000000000000000000000000000000..c3c1288a33e0e839c674d2e82ae9ecb06e37eeb0 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,19 @@ +{ + "version": 4, + "cmakeMinimumRequired": { + "major": 3, + "minor": 23, + "patch": 0 + }, + "configurePresets": [ + { + "name": "default", + "displayName": "Default Release Build", + "generator": "Unix Makefiles", + "binaryDir": "${sourceDir}/build", + "warnings": { + "dev": false + } + } + ] +} diff --git a/README.md b/README.md index b440c034b252724ffbf4471568becf942fa7fd37..65dae6185e6ffa923b52791e88b02f887029859e 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,35 @@ -# LoadStaticSharedTargets.cmake # +# LoadStaticSharedTargets # -This project contains a CMake macro for loading static or shared exported -library targets in a CMake package config file. Static or shared targets can be +This CMake module contains a macro for loading static or shared exported library +targets in a CMake package config file. Static or shared targets can be explicitly (via `COMPONENTS`) or implicitly (via `BUILD_SHARED_LIBS`) imported with the `find_package()` command. -## Usage ## +## Prerequisites ## -In your cmake project, do the following: +What you need: + +- [Git](https://git-scm.com/) (optional) for getting the code and contributing + to the project +- [CMake](https://cmake.org/) for building the project + +## How to build ## + +1. Clone the git repository or download the source code archive and unpack it to + an arbitrary directory (e.g. `load-static-shared-targets`). +2. For building the module, type `cmake --preset default`. This will populate the + `build` directory with a CMake build tree. +3. Now, you can build with `cmake --build ./build`. You should see some + informative terminal output. +4. Finally, install the built artifacts to the `bin` folder with `cmake + --install ./build --prefix ./bin`. You can also specify a different install + prefix, e. g. if you want to install system-wide: `cmake --install ./build + --prefix /usr/local` (might require `sudo`). + +Now, the `LoadStaticSharedTargets` module is installed and you can use it in +other projects. The installed `LoadStaticSharedTargets` package is discoverable +by CMake as __LoadStaticSharedTargets__. In the `CMakeLists.txt` of the other +project, do the following: ```cmake include(FetchContent) @@ -15,26 +37,21 @@ include(FetchContent) FetchContent_Declare( LoadStaticSharedTargets GIT_REPOSITORY "https://github.com/lepus2589/LoadStaticSharedTargets.git" - GIT_TAG v1.2 + GIT_TAG v1.3 + FIND_PACKAGE_ARGS NAMES LoadStaticSharedTargets CONFIG ) +set(LoadStaticSharedTargets_INCLUDE_PACKAGING TRUE) FetchContent_MakeAvailable(LoadStaticSharedTargets) -FetchContent_GetProperties( - LoadStaticSharedTargets - SOURCE_DIR LoadStaticSharedTargets_SOURCE_DIR - POPULATED LoadStaticSharedTargets_POPULATED -) - -install( - FILES - "${LoadStaticSharedTargets_SOURCE_DIR}/src/cmake/LoadStaticSharedTargets.cmake" - DESTINATION "${YourProject_INSTALL_CMAKEDIR}" -) ``` -In your projects package config CMake file, you can now use the macro like this: +This discovers an installed version of the LoadStaticSharedTargets module or +adds it to the other project's install targets. + +In the other project's package config CMake file, you can now use the module like this: ```cmake -include("${CMAKE_CURRENT_LIST_DIR}/LoadStaticSharedTargets.cmake") +find_package(LoadStaticSharedTargets REQUIRED CONFIG) +include(LoadStaticSharedTargets) load_static_shared_targets( STATIC_TARGETS @@ -44,6 +61,20 @@ load_static_shared_targets( ) ``` +This adds the LoadStaticSharedTargets module to the `CMAKE_MODULE_PATH` variable +which enables the include by name only. + +Then, to help CMake discover the LoadStaticSharedTargets package, call CMake for +the other project like this: + +```shell +$ cmake -B ./build -DCMAKE_PREFIX_PATH=/path/to/LoadStaticSharedTargets/install/prefix +``` + +Replace the path to the LoadStaticSharedTargets install prefix with the actual +path on your system! If LoadStaticSharedTargets is installed in a proper +system-wide location, the `CMAKE_PREFIX_PATH` shouldn't be necessary. + ## Further Reading ## The idea for this code was taken from diff --git a/packaging/CMakeLists.txt b/packaging/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..9f309814ac837fbd04029109fb37dc0826defca4 --- /dev/null +++ b/packaging/CMakeLists.txt @@ -0,0 +1,51 @@ +#[[ +MIT License + +CMake build script for LoadStaticSharedTargets module +Copyright (c) 2024 Tim Kaune + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + +include(CMakePackageConfigHelpers) + +if (NOT DEFINED LoadStaticSharedTargets_INSTALL_CMAKEDIR) + set(LoadStaticSharedTargets_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" + CACHE STRING "Path to LoadStaticSharedTargets CMake files") +endif () + +install( + FILES + "../LICENSE" + RENAME copyright + DESTINATION "${CMAKE_INSTALL_DOCDIR}" +) + +write_basic_package_version_file( + LoadStaticSharedTargetsConfigVersion.cmake + COMPATIBILITY SameMajorVersion +) + +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/LoadStaticSharedTargetsConfigVersion.cmake" + "LoadStaticSharedTargetsConfig.cmake" + "../src/cmake/LoadStaticSharedTargets.cmake" + DESTINATION "${LoadStaticSharedTargets_INSTALL_CMAKEDIR}" +) diff --git a/packaging/LoadStaticSharedTargetsConfig.cmake b/packaging/LoadStaticSharedTargetsConfig.cmake new file mode 100644 index 0000000000000000000000000000000000000000..3dabe24756cf68907921cddd25c3eeb962bee5ff --- /dev/null +++ b/packaging/LoadStaticSharedTargetsConfig.cmake @@ -0,0 +1,34 @@ +#[[ +MIT License + +CMake build script for LoadStaticSharedTargets module +Copyright (c) 2024 Tim Kaune + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + +cmake_minimum_required(VERSION 3.23) + +# If CMake >=3.24 is used, set policies up to v3.24 to NEW +if (NOT ${CMAKE_VERSION} VERSION_LESS 3.24) + cmake_policy(VERSION 3.24) +endif() + +# include(LoadStaticSharedTargets) should work after this +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..728c97b5e8fde2896e5e6331805f4fa56c8b176f --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,28 @@ +#[[ +MIT License + +CMake build script for LoadStaticSharedTargets module +Copyright (c) 2024 Tim Kaune + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + +add_custom_target(LoadStaticSharedTargets ALL) + +add_subdirectory(cmake) diff --git a/src/cmake/CMakeLists.txt b/src/cmake/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..7307f4f295ebfe813f6668b208e2077766802f97 --- /dev/null +++ b/src/cmake/CMakeLists.txt @@ -0,0 +1,26 @@ +#[[ +MIT License + +CMake build script for LoadStaticSharedTargets module +Copyright (c) 2024 Tim Kaune + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + +target_sources(LoadStaticSharedTargets PRIVATE LoadStaticSharedTargets.cmake)