### Quick Start: Build and Install CMake Project Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Demonstrates the fundamental steps to build and install a CMake project from its source directory. It involves creating a build directory, configuring with CMake, building the project, and then installing it. ```bash cd basic-static mkdir build && cd build cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=DEBUG cmake --build . cmake --install . ``` -------------------------------- ### CMake Project Setup and Library Installation Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-static/CMakeLists.txt This snippet shows the complete CMake configuration for creating and installing a static library. It covers minimum requirements, project definition, including external modules, defining a static library, managing sources and headers, setting compile features, and using the custom 'target_install_package' command for installation. ```CMake cmake_minimum_required(VERSION 3.25) project(basic_static_example VERSION 1.0.0) # Include target_install_package utilities set(TARGET_INSTALL_PACKAGE_DISABLE_INSTALL ON) include(../../CMakeLists.txt) # Create a static library add_library(math_lib STATIC) target_sources(math_lib PRIVATE src/math.cpp) # Declare public headers using FILE_SET (modern CMake approach) target_sources(math_lib PUBLIC FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES "include/math/calculator.h") # Set C++ standard target_compile_features(math_lib PUBLIC cxx_std_17) # Install the library as a package target_install_package(math_lib NAMESPACE Math:: VERSION ${PROJECT_VERSION}) ``` -------------------------------- ### Build and Install Command Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/tests/CMakeLists.txt Example command to configure, build, and install the project using CMake. It specifies installation prefix, logging, and build options. ```cmake cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=TRACE -Dtarget_install_package_BUILD_TESTS=ON && cmake --build . && cmake --install . ``` -------------------------------- ### CMake: Configure and Install Library Example Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/configure-files/CMakeLists.txt This CMake script demonstrates configuring header files using `target_configure_sources` and installing a library package. It sets up a C++ library with specific sources, headers, and configuration files, then prepares it for installation. ```cmake cmake_minimum_required(VERSION 3.25) project(configure_files_example VERSION 2.3.1) # Include target_install_package utilities set(TARGET_INSTALL_PACKAGE_DISABLE_INSTALL ON) include(../../CMakeLists.txt) # Set some project variables for configuration set(LIBRARY_DESCRIPTION "Example library demonstrating configure file usage") set(LIBRARY_AUTHOR "CMake Examples Team") set(ENABLE_LOGGING ON) set(MAX_BUFFER_SIZE 1024) # Create a library add_library(config_lib STATIC) target_sources(config_lib PRIVATE src/config_lib.cpp) # Declare regular headers using FILE_SET target_sources(config_lib PUBLIC FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include/config_lib" FILES "include/config_lib/library.h") target_sources( config_lib PRIVATE FILE_SET private_config_no_configure TYPE HEADERS BASE_DIRS include FILES include/config_lib/internal_non_config.h) # Configure template files and add them to the target target_configure_sources( config_lib PUBLIC OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/include/config_lib FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/include FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/config_lib/version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/config_lib/build_info.h.in) target_configure_sources( config_lib PRIVATE OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/include/config_lib FILE_SET private_config BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/include FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/config_lib/internal_config.h.in) # Set C++ standard target_compile_features(config_lib PUBLIC cxx_std_17) # Install the library as a package target_install_package(config_lib NAMESPACE Config:: VERSION ${PROJECT_VERSION}) ``` -------------------------------- ### CPack Usage Examples Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/experimental/cpack_example_experimental.md Illustrates how to use the CMake install command and CPack to install specific components or create distributable packages for the SDK. ```cmake # Usage examples: # # Install everything: # cmake --install . --prefix /usr/local # # Install only runtime: # cmake --install . --prefix /usr/local --component Runtime # # Install GUI components (will include Runtime and Development due to dependencies): # cmake --install . --prefix /usr/local --component GUI # # Create component packages: # cpack -G ZIP # Creates: MySDK-1.0.0-Runtime.zip, MySDK-1.0.0-Development.zip, etc. # # Create single package with selectable components (Windows): # cpack -G WIX # Creates installer where users can select components ``` -------------------------------- ### Build and Install CMake Package Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/configure-files/README.md Steps to configure, build, and install the CMake project. Includes setting the installation prefix and building the package. ```bash # Create build directory mkdir build && cd build # Configure with install prefix set to build directory cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=DEBUG # Build the library cmake --build . # Install the package cmake --install . ``` -------------------------------- ### Install Package Components Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-shared/README.md Commands to install the built CMake package. It shows how to install all components or specific ones like 'Runtime' or 'Development'. ```bash # Install everything cmake --install . # Or install specific components: # cmake --install . --component Runtime # Only shared library # cmake --install . --component Development # Headers and CMake configs ``` -------------------------------- ### Build Single Example with Multiple Configurations Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Commands to configure a single CMake example for multiple configurations and then build and install each configuration separately. This is useful for testing across different build types. ```bash # Configure once for all configurations cmake .. -G "Ninja Multi-Config" -DCMAKE_CONFIGURATION_TYPES="Debug;Release;RelWithDebInfo" -DCMAKE_INSTALL_PREFIX=./install # Build each configuration (CMake requires individual --config for each type) cmake --build . --config Debug && cmake --build . --config Release && cmake --build . --config RelWithDebInfo # Install each configuration cmake --install . --config Debug && cmake --install . --config Release && cmake --install . --config RelWithDebInfo ``` -------------------------------- ### Build and Install Example Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/dependency-aggregation/README.md Provides the bash commands required to build and install the CMake dependency aggregation example. It includes steps for creating a build directory, configuring with CMake, building the project, and installing it to a specified prefix. ```bash cd dependency-aggregation mkdir build && cd build cmake .. -DCMAKE_INSTALL_PREFIX=./install cmake --build . cmake --install . ``` -------------------------------- ### CMake Project Setup and Library Definitions Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/CMakeLists.txt Configures the CMake project, defines shared and static libraries (media_core, media_dev_tools), and an executable (asset_converter). It sets up sources, properties like POSITION_INDEPENDENT_CODE, and C++ standard features. ```cmake cmake_minimum_required(VERSION 3.25) project(components_example VERSION 1.0.0) # Include target_install_package utilities set(TARGET_INSTALL_PACKAGE_DISABLE_INSTALL ON) include(../../CMakeLists.txt) # Core runtime library (shared) add_library(media_core SHARED) target_sources(media_core PRIVATE src/media_core.cpp) target_sources(media_core PUBLIC FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES "include/media/core.h") set_target_properties( media_core PROPERTIES POSITION_INDEPENDENT_CODE ON VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) if(WIN32) set_target_properties(media_core PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() target_compile_features(media_core PUBLIC cxx_std_17) # Developer utilities (static) add_library(media_dev_tools STATIC) target_sources(media_dev_tools PRIVATE src/dev_tools.cpp) target_sources(media_dev_tools PUBLIC FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES "include/media/dev_tools.h") target_compile_features(media_dev_tools PUBLIC cxx_std_17) # Asset converter tool (executable) add_executable(asset_converter) target_sources(asset_converter PRIVATE src/asset_converter.cpp) ``` -------------------------------- ### Consumer Project CMakeLists.txt Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-shared/README.md Example CMakeLists.txt for a consumer project that finds and links against the installed 'string_utils' package. ```cmake # CMakeLists.txt cmake_minimum_required(VERSION 3.25) project(consumer) # Find the package find_package(string_utils 2.1 REQUIRED) # Create executable add_executable(test_app main.cpp) # Link with the installed library target_link_libraries(test_app PRIVATE Utils::string_utils) ``` -------------------------------- ### Build All Examples with Multiple Configurations Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md A command to build all provided CMake examples using a script that handles multiple build configurations (e.g., Debug, Release). ```bash ./build_all_examples.sh --multi-config ``` -------------------------------- ### Example: Static Library Packaging Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Demonstrates simple static library packaging, including FILE_SET for header installation and assigning development component names. ```cmake # Simple static library packaging # FILE_SET header installation # Development component assignment ``` -------------------------------- ### Install the Package Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-interface/README.md Installs the headers and CMake configuration files for the library to the specified installation prefix. This makes the library discoverable by other CMake projects. ```bash cmake --install . ``` -------------------------------- ### Build and Install Static Library (Bash) Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-static/README.md Steps to configure, build, and install the static library using CMake commands. It sets an installation prefix and logs build details. ```bash # Create build directory mkdir build && cd build # Configure with install prefix set to build directory cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=DEBUG # Build the library cmake --build . # Install to the specified prefix cmake --install . ``` -------------------------------- ### Contributing to Examples Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Guidelines for contributing new examples to the project, emphasizing the need for a README, clear feature demonstration, inclusion in build scripts, and updating the main CMakeLists.txt for package finding. ```cmake # 1. Include a README.md # 2. Demonstrate specific features clearly # 3. Add example dir to [build_all_examples.sh](build_all_examples.sh) # 4. Add find_package to this [CMakeLists.txt](CMakeLists.txt) ``` -------------------------------- ### CMake Consumer for Runtime Library Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/README.md Example CMakeLists.txt for a consumer application that finds and links against the installed `media_core` runtime library. It demonstrates how to use `find_package` and `target_link_libraries`. ```cmake # CMakeLists.txt cmake_minimum_required(VERSION 3.25) project(media_app) # Find the runtime library find_package(media_core REQUIRED COMPONENTS core) add_executable(my_app main.cpp) target_link_libraries(my_app PRIVATE Media::media_core) ``` -------------------------------- ### Consumer Project CMakeLists.txt Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Example CMakeLists.txt for a consumer project to find and link against an installed package. It demonstrates setting the CMAKE_PREFIX_PATH and using find_package. ```cmake cmake_minimum_required(VERSION 3.25) project(consumer) # Add install location to prefix path list(APPEND CMAKE_PREFIX_PATH "/path/to/example/build/install") # Find the installed package find_package( REQUIRED) # Create your application add_executable(my_app main.cpp) # Link with the installed library target_link_libraries(my_app PRIVATE ::) ``` -------------------------------- ### Build, Install, and Package Workflow Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/cpack-basic/README.md This sequence of bash commands outlines the typical workflow for configuring, building, installing, and generating packages using CMake and CPack. It includes setting the installation prefix, building targets, installing the project, and generating different package formats. ```bash # Create build directory mkdir build && cd build # Configure with install prefix cmake .. -DCMAKE_INSTALL_PREFIX=./install # Build all targets cmake --build . # Install all components cmake --install . # Verify installation structure find install/ -type f | sort # Generate all configured package types cpack # Generate specific package type cpack -G TGZ # Generate component packages (if supported) cpack -G TGZ -D CPACK_ARCHIVE_COMPONENT_INSTALL=ON # List generated packages ls -la *.tar.gz *.zip *.deb *.rpm 2>/dev/null || echo "No packages found" # Extract and verify package contents tar -tzf MyLibrary-*.tar.gz | head -20 ``` -------------------------------- ### Custom Target Installation with Components Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/CMakeLists.txt Utilizes the custom `target_install_package` function to define installation components for libraries and executables. This allows granular control over what gets installed into different package components like 'runtime', 'devel', and 'tools'. ```cmake # Install with custom components target_install_package( media_core NAMESPACE Media:: RUNTIME_COMPONENT "runtime" DEVELOPMENT_COMPONENT "devel") target_install_package(media_dev_tools NAMESPACE Media:: DEVELOPMENT_COMPONENT "devel") target_install_package( asset_converter NAMESPACE Media:: COMPONENT "tools" # Tools get their own component RUNTIME_COMPONENT "tools") ``` -------------------------------- ### Consumer Application Logic Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-interface/README.md A C++ main function demonstrating the usage of the installed header-only library. It includes examples of sorting and searching algorithms provided by the library. ```cpp #include "algorithms/sorting.hpp" #include "algorithms/searching.hpp" #include #include int main() { // Test sorting std::vector numbers = {64, 34, 25, 12, 22, 11, 90}; std::cout << "Original array: "; for (int n : numbers) std::cout << n << " "; std::cout << std::endl; // Test bubble sort auto bubble_sorted = numbers; algorithms::Sorting::bubbleSort(bubble_sorted); std::cout << "Bubble sorted: "; for (int n : bubble_sorted) std::cout << n << " "; std::cout << std::endl; // Test quick sort auto quick_sorted = numbers; algorithms::Sorting::quickSort(quick_sorted); std::cout << "Quick sorted: "; for (int n : quick_sorted) std::cout << n << " "; std::cout << std::endl; // Test searching auto result = algorithms::Searching::binarySearch(quick_sorted, 25); if (result) { std::cout << "Found 25 at index: " << *result << std::endl; } else { std::cout << "25 not found" << std::endl; } return 0; } ``` -------------------------------- ### Component-Based Installation Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Demonstrates how to install specific components of a CMake project. This allows users to install only the necessary parts, such as runtime or development files. ```bash # Install only runtime components cmake --install . --component Runtime # Install only development components cmake --install . --component Development # Install custom components (see components example) cmake --install . --component runtime --component devel ``` -------------------------------- ### Configure CMAKE_PREFIX_PATH for Examples Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/CMakeLists.txt Iterates through identified example directories and appends their respective 'build/install' subdirectories to the CMAKE_PREFIX_PATH. This allows CMake to find installed targets from these examples. ```cmake foreach(example_dir IN LISTS EXAMPLE_DIRECTORIES) message(VERBOSE "Adding example path: ${example_dir}/build/install") list(APPEND CMAKE_PREFIX_PATH "${example_dir}/build/install/") endforeach() ``` -------------------------------- ### Standard Build and Install Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Provides the standard CMake commands to configure, build, and install a project. This is a common workflow for most CMake-based projects. ```bash mkdir build && cd build cmake .. -DCMAKE_INSTALL_PREFIX=./install cmake --build . cmake --install . ``` -------------------------------- ### Example: Multi-Configuration Support Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md An example demonstrating how to configure a CMake package to support different build types (e.g., Debug, Release). ```cmake # Example showing configuration for different build types support ``` -------------------------------- ### Verify Installation Structure Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-static/README.md Shows the expected file structure within the installation directory after the build and install process. ```text install/ ├── include/ │ └── math/ │ └── calculator.h ├── lib/ │ └── libmath_lib.a └── share/ └── cmake/ └── math_lib/ ├── math_lib-config.cmake ├── math_lib-config-version.cmake └── math_lib-targets.cmake ``` -------------------------------- ### Install CMake Components Selectively Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/README.md Demonstrates installing specific components of a CMake project using the `cmake --install` command. This allows users to install only runtime, development, or tool components as needed. ```bash # Install all components cmake --install . # Install only runtime components (e.g., shared libraries) cmake --install . --component runtime # Install development components (e.g., headers, static libraries, CMake configs) cmake --install . --component devel # Install developer tools (e.g., executables) cmake --install . --component tools # Install multiple components (e.g., runtime and development) cmake --install . --component runtime --component devel ``` -------------------------------- ### CMakeLists.txt for Consumer Project Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/configure-files/README.md Example CMakeLists.txt for a project that finds and links against the installed 'config_lib' package. ```cmake # CMakeLists.txt cmake_minimum_required(VERSION 3.25) project(consumer) # Find the package find_package(config_lib 2.3 REQUIRED) # Create executable add_executable(test_app main.cpp) # Link with the library target_link_libraries(test_app PRIVATE Config::config_lib) ``` -------------------------------- ### Configure and Build CMake Project Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/README.md Steps to configure a CMake project with a custom installation prefix and build all targets. This sets up the project for subsequent component-based installation. ```bash # Create build directory mkdir build && cd build # Configure with install prefix set to build directory// Use --log-level=DEBUG for detailed output cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=DEBUG # Build all targets cmake --build . ``` -------------------------------- ### Configure and Build Package Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-shared/README.md Steps to configure a CMake project with a specific install prefix and build the package. It sets up the build environment and compiles the library. ```bash # Create build directory mkdir build && cd build # Configure with install prefix set to build directory cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=DEBUG # Build the library cmake --build . ``` -------------------------------- ### Install with Single-Config Generators (Bash) Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/multi-config/README.md Installs the project when using single-configuration generators. Each configuration's build directory is installed separately to its designated prefix. ```bash cmake --install build-debug --prefix install-debug cmake --install build-release --prefix install-release ``` -------------------------------- ### Asset Converter Tool Usage Examples Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/README.md Demonstrates the basic and advanced command-line usage of the asset_converter tool for media conversion. It shows how to convert audio files and utilize options like output format and quality settings, as well as how to access help. ```bash # Basic usage ./install/bin/asset_converter input.wav output.mp3 # With options ./install/bin/asset_converter -f mp3 -q 95 input.wav output.mp3 # Help ./install/bin/asset_converter --help ``` -------------------------------- ### CMake: Build and Install Shared Library with target_install_package Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/multi-config/CMakeLists.txt This snippet defines a shared library named math_utils, sets its properties including C++17 standard and versioning, and installs it using target_install_package with a specific namespace and debug postfix. It includes header declaration using FILE_SET. ```CMake cmake_minimum_required(VERSION 3.25) project(multi_config_example VERSION 1.0.0) # Include target_install_package utilities set(TARGET_INSTALL_PACKAGE_DISABLE_INSTALL ON) include(../../CMakeLists.txt) # Create a shared library add_library(math_utils SHARED) target_sources(math_utils PRIVATE src/math_utils.cpp) # Declare public headers using FILE_SET target_sources(math_utils PUBLIC FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES "include/utils/math_utils.h") # Set properties for shared library set_target_properties( math_utils PROPERTIES POSITION_INDEPENDENT_CODE ON VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) # Windows-specific: ensure import library is generated if(WIN32) set_target_properties(math_utils PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() # Set C++ standard target_compile_features(math_utils PUBLIC cxx_std_17) # Install the library as a package with multi-config support target_install_package(math_utils NAMESPACE Utils:: VERSION ${PROJECT_VERSION} DEBUG_POSTFIX "d") ``` -------------------------------- ### Modern CMake Practices Overview Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Key modern CMake practices discussed, including FILE_SET for header management (CMake 3.23+), generator expressions for build/install separation, component system for installation, and target dependencies for transitive dependency handling. ```cmake # FILE_SET: Modern header management (CMake 3.23+) # Generator Expressions: Build vs install interface separation # Component System: Flexible installation strategies # Target Dependencies: Proper transitive dependency handling ``` -------------------------------- ### Executable 'bin0' Configuration Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/tests/CMakeLists.txt Configures a separate executable 'bin0' with its sources, linking to 'fmt', including directories, and specifying its installation component as 'bin'. ```cmake add_executable(bin0) target_sources(bin0 PRIVATE src3.cpp) target_link_libraries(bin0 PRIVATE fmt) target_include_directories(bin0 PRIVATE include) target_install_package(bin0 RUNTIME_COMPONENT "bin") ``` -------------------------------- ### Debugging CMake Package Installation Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Shell commands for debugging CMake package installation processes, including enabling detailed logging and verifying installed files and configuration. ```bash # Enable detailed logging cmake .. -DPROJECT_LOG_COLORS=ON --log-level=DEBUG ``` ```bash # Verify installed files find install/ -type f | sort ``` ```bash # Check CMake config files cat install/share/cmake/*/*-config.cmake ``` ```bash # Test if package can be found cmake --find-package -DNAME= -DCOMPILER_ID=GNU -DLANGUAGE=CXX -DMODE=EXIST ``` -------------------------------- ### Install with Multi-Config Generators (Bash) Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/multi-config/README.md Installs both Debug and Release configurations when using a multi-config generator. Libraries can be installed to separate prefixes or the same prefix, leveraging the `DEBUG_POSTFIX` for distinct naming. ```bash # Install both configurations to separate prefixes cmake --install build --config Debug --prefix install-debug cmake --install build --config Release --prefix install-release # Or install both to the same prefix (different library names due to debug postfix) cmake --install build --config Debug --prefix install cmake --install build --config Release --prefix install ``` -------------------------------- ### Example: Configure Files and Substitution Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Demonstrates template file processing in CMake, including build-time variable substitution and the distinction between PUBLIC and PRIVATE configured headers. ```cmake # Template file processing # Build-time variable substitution # PUBLIC vs PRIVATE configured headers ``` -------------------------------- ### Example: Dependency Aggregation Mechanics Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md A minimal example focusing on dependency aggregation mechanics, using real dependencies (fmt, spdlog, cxxopts) via FetchContent. It highlights correct vs. problematic patterns and how dependencies are collected. ```cmake # Minimal focused example of dependency aggregation mechanics # Uses real dependencies (fmt, spdlog, cxxopts) via FetchContent # Demonstrates the difference between correct and problematic patterns # Shows exactly how dependencies are collected and aggregated ``` -------------------------------- ### Install game_engine with Dependencies using target_install_package Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/multi-target/CMakeLists.txt Installs the 'game_engine' library as a package. It specifies a namespace 'GameEngine::', sets the project version, and includes 'core_utils' and 'math_ops' as additional targets within the same package. ```cmake target_install_package( game_engine NAMESPACE GameEngine:: VERSION ${PROJECT_VERSION} ADDITIONAL_TARGETS core_utils math_ops) ``` -------------------------------- ### CMake Project Configuration and Dependencies Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/CMakeLists.txt Sets up the minimum CMake version and project details. It includes several helper CMake scripts for project guards, logging, and general utilities, which are essential for the project's build and installation process. ```cmake cmake_minimum_required(VERSION 3.25) project(target_install_package VERSION 5.3.0) # ~~~\n# Project dependencies, these have been included directly in this project for\n# ease of installation. They rarely change, and this is the the one repo that requires all of them.\n# ~~~\ninclude(${CMAKE_CURRENT_LIST_DIR}/cmake/list_file_include_guard.cmake) include(${CMAKE_CURRENT_LIST_DIR}/cmake/project_include_guard.cmake) include(${CMAKE_CURRENT_LIST_DIR}/cmake/project_log.cmake) project_include_guard() # Guard against multiple add_subdirectory (e.g git submodules) ``` -------------------------------- ### Consumer Project main.cpp Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-shared/README.md Example C++ source file for a consumer application that uses the 'string_utils' library to perform string manipulations. ```c++ // main.cpp #include "utils/string_utils.h" #include int main() { std::string text = "Hello, World!"; std::cout << "Original: " << text << std::endl; std::cout << "Upper: " << utils::StringUtils::toUpper(text) << std::endl; std::cout << "Lower: " << utils::StringUtils::toLower(text) << std::endl; auto words = utils::StringUtils::split(text, ' '); std::cout << "Split into " << words.size() << " words" << std::endl; return 0; } ``` -------------------------------- ### Configure and Build Project Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-interface/README.md Steps to configure the CMake project with a specified install prefix and then build it. For an interface library, the build step primarily ensures targets are correctly defined. ```bash mkdir build && cd build cmake .. -DCMAKE_INSTALL_PREFIX=./install -DPROJECT_LOG_COLORS=ON --log-level=DEBUG cmake --build . ``` -------------------------------- ### Consumer CMakeLists.txt for Installed Package (CMake) Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-static/README.md Configures a consumer project to find and link against the installed static library. It sets the CMAKE_PREFIX_PATH and uses find_package to locate the library. ```cmake # CMakeLists.txt cmake_minimum_required(VERSION 3.25) project(consumer) # Set CMAKE_PREFIX_PATH to find the installed package list(APPEND CMAKE_PREFIX_PATH "/path/to/build/install") # Find the package find_package(math_lib REQUIRED) # Create executable add_executable(test_app main.cpp) # Link with the installed library target_link_libraries(test_app PRIVATE Math::math_lib) ``` -------------------------------- ### CMake Interface Library Installation Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-interface/CMakeLists.txt This CMake script sets up a minimum required version and project name. It then creates an interface library named 'algorithms', collects all header files recursively from the 'include/algorithms' directory, declares them as interface sources, sets the C++ standard to C++17, and finally installs the library as a package using the custom `target_install_package` function. ```CMake cmake_minimum_required(VERSION 3.25) project(basic_interface_example VERSION 1.5.0) # Include target_install_package utilities set(TARGET_INSTALL_PACKAGE_DISABLE_INSTALL ON) include(../../CMakeLists.txt) # Create an interface (header-only) library add_library(algorithms INTERFACE) # Collect all headers file(GLOB_RECURSE ALGORITHM_HEADERS "include/algorithms/*.hpp") # Declare interface headers using FILE_SET target_sources(algorithms INTERFACE FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${ALGORITHM_HEADERS}) # Set C++ standard target_compile_features(algorithms INTERFACE cxx_std_17) # Install the interface library as a package target_install_package(algorithms NAMESPACE Algorithms:: VERSION ${PROJECT_VERSION}) ``` -------------------------------- ### CPM Package Management (fmtlib) Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/tests/CMakeLists.txt Demonstrates fetching the 'fmt' library using CPM (CMake Package Manager). It includes setting an install option and adding the package to the build. ```cmake include(get_cpm.cmake) set(FMT_INSTALL ON) cpmaddpackage("gh:fmtlib/fmt#11.1.4") # Use CMake's built-in property for position independent code instead of -fPIC. set_target_properties(fmt PROPERTIES POSITION_INDEPENDENT_CODE ON) ``` -------------------------------- ### Install C++20 Modules Library Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/cxx-modules/README.md Command to install the built C++20 module library and its associated CMake configuration files to the specified installation prefix. ```bash # Install modules and library cmake --install . ``` -------------------------------- ### Example: Multi-Target Export Pattern Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Presents the recommended pattern for multiple targets sharing a common export configuration. It covers dependency aggregation from PUBLIC_DEPENDENCIES, per-target component assignments, and correct usage of target_prepare_package() and finalize_package(). ```cmake # Recommended pattern for multiple targets with shared export # Dependency aggregation from multiple PUBLIC_DEPENDENCIES # Per-target component assignments within single export # Demonstrates correct target_prepare_package() + finalize_package() usage ``` -------------------------------- ### Example: Shared Library Packaging Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Illustrates packaging shared libraries with versioning (VERSION, SOVERSION), separating runtime and development components, and building for different platforms. ```cmake # Library versioning (VERSION, SOVERSION) # Runtime/Development component separation # Building and packaging shared libraries for different platforms ``` -------------------------------- ### Consumer Main Application Code (C++) Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/basic-static/README.md A simple C++ program that includes the library's header and uses its functionality. It demonstrates how to call methods from the installed library. ```cpp // main.cpp #include "math/calculator.h" #include int main() { std::cout << "5 + 3 = " << math::Calculator::add(5, 3) << std::endl; std::cout << "10 / 2 = " << math::Calculator::divide(10, 2) << std::endl; return 0; } ``` -------------------------------- ### Main Executable Configuration Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/tests/CMakeLists.txt Configures the main project executable, linking it against static1, shared1, and interface1. It also sets a compile definition and prepares the executable for installation. ```cmake add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE static1 shared1 interface1) target_compile_definitions(${PROJECT_NAME} PRIVATE "SHARED=1") # Need to re-enable because it is PRIVATE above target_install_package(${PROJECT_NAME}) ``` -------------------------------- ### Core Project Functions and Installation Logic Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/CMakeLists.txt Includes the project's core CMake functions and defines the main installation logic. It creates an INTERFACE library for the project and conditionally installs it based on project options, using the 'target_install_package' function to manage files and destinations. ```cmake # ~~~\n# This projects cmake functions!\n# ~~~\ninclude(${CMAKE_CURRENT_LIST_DIR}/install_package_helpers.cmake) include(${CMAKE_CURRENT_LIST_DIR}/target_configure_sources.cmake) include(${CMAKE_CURRENT_LIST_DIR}/target_install_package.cmake) include(${CMAKE_CURRENT_LIST_DIR}/target_configure_cpack.cmake) # Create an INTERFACE library to represent our modules add_library(${PROJECT_NAME} INTERFACE) # Only install if this is the main project or if explicitly enabled option(TARGET_INSTALL_PACKAGE_ENABLE_INSTALL "Create install configuration for ${PROJECT_NAME}" OFF) option(TARGET_INSTALL_PACKAGE_DISABLE_INSTALL "Do not create install configuration for ${PROJECT_NAME}" OFF) if(NOT TARGET_INSTALL_PACKAGE_DISABLE_INSTALL) if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR} OR TARGET_INSTALL_PACKAGE_ENABLE_INSTALL) # Use target_install_package to install itself and target_configure_sources target_install_package( ${PROJECT_NAME} ADDITIONAL_FILES ${CMAKE_CURRENT_LIST_DIR}/cmake/generic-config.cmake.in ADDITIONAL_FILES_DESTINATION "cmake" PUBLIC_CMAKE_FILES ${CMAKE_CURRENT_LIST_DIR}/cmake/list_file_include_guard.cmake ${CMAKE_CURRENT_LIST_DIR}/cmake/project_include_guard.cmake ${CMAKE_CURRENT_LIST_DIR}/cmake/project_log.cmake ${CMAKE_CURRENT_LIST_DIR}/install_package_helpers.cmake ${CMAKE_CURRENT_LIST_DIR}/target_configure_sources.cmake ${CMAKE_CURRENT_LIST_DIR}/target_install_package.cmake ${CMAKE_CURRENT_LIST_DIR}/target_configure_cpack.cmake # Use generic-config.cmake.in as the template CONFIG_TEMPLATE ${CMAKE_CURRENT_LIST_DIR}/cmake/generic-config.cmake.in # Set destination to standard CMake modules location CMAKE_CONFIG_DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME} # Override include destination to put modules in the cmake dir INCLUDE_DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME} RUNTIME_COMPONENT "CMakeUtilities" DEVELOPMENT_COMPONENT "CMakeUtilities") endif() endif() ``` -------------------------------- ### C++ Consumer Application Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/README.md A C++ application demonstrating how to use the installed `media_core` library. It includes conditional compilation for development tools and shows basic library usage. ```cpp // main.cpp #include "media/core.h" #ifdef MEDIA_DEV_TOOLS_AVAILABLE #include "media/dev_tools.h" #endif #include int main() { // Initialize media system if (!media::MediaCore::initialize()) { std::cerr << "Failed to initialize media core." << std::endl; return 1; } // Load and play media media::MediaCore::loadMedia("song.mp3", media::MediaType::AUDIO); media::MediaCore::playAudio("song.mp3"); #ifdef MEDIA_DEV_TOOLS_AVAILABLE // Use development tools if available auto info = media::DevTools::analyzeFile("song.mp3"); std::cout << "Duration: " << info.duration << " seconds" << std::endl; #endif media::MediaCore::shutdown(); return 0; } ``` -------------------------------- ### Consumer Project CMakeLists.txt Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/cpack-basic/README.md This CMakeLists.txt file defines a simple consumer project that finds and links against the installed `mylib` and `mylib_utils` packages. It demonstrates how to use `find_package` and `target_link_libraries` to integrate the installed library into another project. ```cmake # consumer/CMakeLists.txt cmake_minimum_required(VERSION 3.25) project(consumer) # Point to package installation list(APPEND CMAKE_PREFIX_PATH "/path/to/test-install") find_package(mylib REQUIRED) find_package(mylib_utils REQUIRED) add_executable(consumer main.cpp) target_link_libraries(consumer PRIVATE MyLib::mylib MyLib::mylib_utils) ``` -------------------------------- ### Template File Example Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/README.md An example of a C++ header template file used for generating version information during the CMake configuration process. ```cpp #pragma once #define MY_LIBRARY_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ #define MY_LIBRARY_VERSION_MINOR @PROJECT_VERSION_MINOR@ #define MY_LIBRARY_VERSION_PATCH @PROJECT_VERSION_PATCH@ #define MY_LIBRARY_VERSION "@PROJECT_VERSION@" ``` -------------------------------- ### Consumer Project main.cpp Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/cpack-basic/README.md This C++ source file is a simple example for a consumer project. It includes headers from the `mylib` package and uses functions from `mylib::Core` and `mylib::Utils` to demonstrate successful integration and usage of the installed library. ```cpp // consumer/main.cpp #include "mylib/core.h" #include "mylib/utils.h" #include int main() { mylib::Core::initialize(); auto parts = mylib::Utils::split("hello,world,test", ","); auto joined = mylib::Utils::join(parts, " | "); std::cout << "Result: " << joined << std::endl; mylib::Core::shutdown(); return 0; } ``` -------------------------------- ### Component-Specific Installation Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/cpack-basic/README.md These bash commands demonstrate how to install only specific components of a CMake project after it has been built. This allows users or automated systems to install only the necessary parts, such as runtime libraries, development headers, or tools. ```bash # Install runtime only cmake --install . --component Runtime # Install development files only cmake --install . --component Development # Install tools only cmake --install . --component Tools ``` -------------------------------- ### CMake SDK Packaging Script Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/experimental/cpack_example_experimental.md This CMake script defines libraries and executables, prepares them for packaging using custom functions, and configures CPack for component-based installation. It handles cross-platform generator settings and defines component dependencies and descriptions. ```cmake cmake_minimum_required(VERSION 3.23) project(MySDK VERSION 1.0.0) # Include your packaging functions include(target_install_package.cmake) # Example: SDK with multiple components # - Core library (always needed) # - GUI library (optional) # - Tools executable (optional) # Core library - goes into base Runtime/Development add_library(mycore src/core.cpp) target_include_directories(mycore PUBLIC $ $) target_sources(mycore PUBLIC FILE_SET HEADERS BASE_DIRS include FILES include/mycore.h) # GUI library - goes into base + "GUI" component add_library(mygui src/gui.cpp) target_link_libraries(mygui PUBLIC mycore) target_sources(mygui PUBLIC FILE_SET HEADERS BASE_DIRS include FILES include/mygui.h) # Tools - goes into base + "Tools" component add_executable(mytool src/tool.cpp) target_link_libraries(mytool PRIVATE mycore) # Prepare installations target_prepare_package(mycore EXPORT_NAME MySDK NAMESPACE MySDK:: VERSION ${PROJECT_VERSION} # No COMPONENT specified - only goes to base components ) target_prepare_package(mygui EXPORT_NAME MySDK NAMESPACE MySDK:: COMPONENT GUI # Goes to Runtime, Development, AND GUI ) target_prepare_package(mytool EXPORT_NAME MySDK NAMESPACE MySDK:: COMPONENT Tools # Goes to Runtime AND Tools RUNTIME_COMPONENT Runtime DEVELOPMENT_COMPONENT Development # Executables don't have dev files ) # Finalize the export finalize_package(EXPORT_NAME MySDK) # Configure CPack set(CPACK_PACKAGE_NAME "MySDK") set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}") set(CPACK_PACKAGE_VENDOR "MyCompany") # Enable component installation set(CPACK_COMPONENTS_ALL Runtime Development GUI Tools) set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP) # Define component relationships set(CPACK_COMPONENT_GUI_DEPENDS Runtime Development) set(CPACK_COMPONENT_TOOLS_DEPENDS Runtime) # Component descriptions set(CPACK_COMPONENT_RUNTIME_DESCRIPTION "Runtime libraries required to run applications") set(CPACK_COMPONENT_DEVELOPMENT_DESCRIPTION "Development headers and import libraries") set(CPACK_COMPONENT_GUI_DESCRIPTION "Optional GUI components") set(CPACK_COMPONENT_TOOLS_DESCRIPTION "Optional command-line tools") # Set default components set(CPACK_COMPONENTS_DEFAULT Runtime) # Generator-specific settings if(WIN32) set(CPACK_GENERATOR "ZIP;WIX") set(CPACK_WIX_COMPONENT_INSTALL ON) elseif(APPLE) set(CPACK_GENERATOR "TGZ;DragNDrop") else() set(CPACK_GENERATOR "TGZ;DEB;RPM") set(CPACK_DEB_COMPONENT_INSTALL ON) set(CPACK_RPM_COMPONENT_INSTALL ON) endif() include(CPack) ``` -------------------------------- ### CMakeLists.txt Configuration Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/tests/consumer/CMakeLists.txt Configures a CMake project, finds required packages (target_install_package, component-devel, shared1), defines an executable, and links it to the component-devel library. It also installs the target package. ```cmake cmake_minimum_required(VERSION 3.25) project(consumer) list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../build/install") find_package(target_install_package CONFIG REQUIRED) find_package(component-devel CONFIG REQUIRED) find_package(shared1 CONFIG REQUIRED) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE component-devel::component-devel) target_install_package(${PROJECT_NAME}) ``` -------------------------------- ### Configure and Build C++20 Modules Library Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/cxx-modules/README.md Demonstrates the CMake commands to configure a C++20 module project with the Ninja generator and then build the library. It sets the installation prefix and log level. ```bash # Create build directory mkdir build && cd build # Configure with Ninja generator (recommended for modules) cmake .. -G Ninja \ -DCMAKE_INSTALL_PREFIX=./install \ -DPROJECT_LOG_COLORS=ON \ --log-level=DEBUG # Build the library (modules will be scanned and compiled) cmake --build . ``` -------------------------------- ### Example: Interface Library Packaging Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Details packaging for header-only template libraries, which have no runtime dependencies and often contain template algorithm implementations. ```cmake # Header-only template library # No runtime dependencies # Template algorithm implementations ``` -------------------------------- ### CPack Configuration for Component Packaging Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/components/CMakeLists.txt Configures CPack to generate installable packages with defined components. It sets package name, version, generator, and display names/descriptions for each component, enabling a structured installation process. ```cmake # Basic CPack configuration to test component packaging set(CPACK_PACKAGE_NAME "MediaLibrary") set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}") set(CPACK_GENERATOR "TGZ") # Define components set(CPACK_COMPONENT_RUNTIME_DISPLAY_NAME "Runtime Libraries") set(CPACK_COMPONENT_RUNTIME_DESCRIPTION "Runtime libraries required to run applications") set(CPACK_COMPONENT_DEVEL_DISPLAY_NAME "Development Files") set(CPACK_COMPONENT_DEVEL_DESCRIPTION "Headers and development files") set(CPACK_COMPONENT_TOOLS_DISPLAY_NAME "Tools") set(CPACK_COMPONENT_TOOLS_DESCRIPTION "Asset conversion tools") # Custom components (these should be auto-created by our dual install) set(CPACK_COMPONENT_TOOLS-RUNTIME_DISPLAY_NAME "Tools Runtime") set(CPACK_COMPONENT_TOOLS-RUNTIME_DESCRIPTION "Runtime files for tools component") set(CPACK_COMPONENT_TOOLS-DEVELOPMENT_DISPLAY_NAME "Tools Development") set(CPACK_COMPONENT_TOOLS-DEVELOPMENT_DESCRIPTION "Development files for tools component") include(CPack) ``` -------------------------------- ### Example: Custom Component Names Source: https://github.com/jkammerland/target_install_package.cmake/blob/master/examples/README.md Illustrates using custom component names in a CMake package, mixing different target types (shared, static, executable), and managing per-target component assignments. ```cmake # Custom component names # Mixed target types (shared, static, executable) ```