Skip to content
Snippets Groups Projects
Unverified Commit 6e606dc1 authored by Tim Kaune's avatar Tim Kaune
Browse files

Transform LoadStaticSharedTargets into an installable CMake project

You can now build and install LoadStaticSharedTargets like any CMake
library and also discover it the same way via find_package().
parent 5d585da1
No related branches found
No related tags found
No related merge requests found
#[[
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 ()
{
"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
}
}
]
}
# 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
......
#[[
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}"
)
#[[
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}")
#[[
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)
#[[
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment