### Install and Run Example Executable Source: https://github.com/kitware/cmake/blob/master/Help/guide/importing-exporting/index.rst Steps to build and install an example executable, then run it to generate a source file. This setup is a prerequisite for importing the executable into another CMake project. ```console $ cd Help/guide/importing-exporting/MyExe $ mkdir build $ cd build $ cmake .. $ cmake --build . $ cmake --install . --prefix $ /myexe $ ls [...] main.cc [...] ``` -------------------------------- ### Build and Install Library Source: https://github.com/kitware/cmake/blob/master/Help/manual/cmake-toolchains.7.rst Example CMakeLists.txt to build a library and install it. ```cmake project(foo) add_library(foo foo.cpp) install(TARGETS foo DESTINATION lib) ``` -------------------------------- ### Example CMake Target Setup Source: https://github.com/kitware/cmake/blob/master/Help/prop_tgt/LINK_LIBRARIES_STRATEGY.rst This example demonstrates setting up libraries and an executable with direct and indirect link dependencies to illustrate the effect of different LINK_LIBRARIES_STRATEGY values. ```cmake add_library(A STATIC ...) add_library(B STATIC ...) add_library(C STATIC ...) add_executable(main ...) target_link_libraries(B PRIVATE A) target_link_libraries(C PRIVATE A) target_link_libraries(main PRIVATE A B C) ``` -------------------------------- ### Basic CMakeLists.txt Example Source: https://github.com/kitware/cmake/blob/master/Help/manual/cmake-toolchains.7.rst A minimal CMakeLists.txt file to start a project. Ensure you have a compatible CMake version. ```cmake cmake_minimum_required(VERSION 3.14) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/kitware/cmake/blob/master/Help/guide/importing-exporting/MyExe/CMakeLists.txt Sets the minimum CMake version, project name, C++ standard, and adds an executable. Installs the executable after building. ```cmake cmake_minimum_required(VERSION 3.15) project(MyExe) # specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # Add executable add_executable(myexe main.cxx) # install executable install(TARGETS myexe) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/ExternalProjectLocal/Step5/CMakeLists.txt Initializes CMake, sets the project name, and defines version numbers. This is the standard starting point for any CMake project. ```cmake cmake_minimum_required (VERSION 3.31) project (Tutorial) ``` ```cmake # The version number. set (Tutorial_VERSION_MAJOR 1) set (Tutorial_VERSION_MINOR 0) ``` -------------------------------- ### Install Library with Runtime Dependencies Source: https://github.com/kitware/cmake/blob/master/Tests/ExportImport/Import/install-RUNTIME_DEPENDENCIES/CMakeLists.txt Installs a library and configures its runtime dependencies using include and exclude regexes, and specifies directories. This is a simpler configuration than the previous example. ```cmake install(TARGETS lib RUNTIME_DEPENDENCIES PRE_INCLUDE_REGEXES dep8 PRE_EXCLUDE_REGEXES ".*" DIRECTORIES "$" ${_framework_args} ) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/ExportImport/Import/CMakeLists.txt Sets the minimum CMake version, applies a policy, and defines a project. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required (VERSION 3.10) if(POLICY CMP0129) cmake_policy(SET CMP0129 NEW) endif() project(Import C CXX) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/CMakePresets/TraceFormatHuman-stderr.txt This snippet shows the minimum required version and project name declaration in a CMakeLists.txt file. It's a standard starting point for any CMake project. ```cmake cmake_minimum_required(VERSION 3.18 ) project(${RunCMake_TEST} NONE ) ``` -------------------------------- ### CMake Project Setup and Configuration Source: https://github.com/kitware/cmake/blob/master/Tests/QtAutogen/RerunUicOnFileChange/CMakeLists.txt Initializes the CMake project, includes necessary modules, and sets up project-specific directories and variables. This is the starting point for the build process. ```cmake cmake_minimum_required(VERSION 3.17) project(RerunUicOnFileChange) include("../AutogenGuiTest.cmake") # Utility variables set(testProjectTemplateDir "${CMAKE_CURRENT_SOURCE_DIR}/UicOnFileChange") set(testProjectSrc "${CMAKE_CURRENT_BINARY_DIR}/UicOnFileChange") set(testProjectBinDir "${CMAKE_CURRENT_BINARY_DIR}/UicOnFileChange-build") set(TEST_CONFIG "Release") ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/CMakeLib/DebuggerSample/CMakeLists.txt This snippet shows the fundamental structure of a CMakeLists.txt file, including setting the minimum version, defining the project, and displaying a message. It's a starting point for any CMake project. ```cmake cmake_minimum_required(VERSION 3.26) project(DebuggerSample NONE) message("Hello CMake Debugger") ``` -------------------------------- ### Basic CPack Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/CPackInnoSetupGenerator/CMakeLists.txt Initializes CPack with a minimum version and project name. This is a standard starting point for any CMake project using CPack. ```cmake cmake_minimum_required(VERSION 3.13) project(CPackInnoSetupGenerator VERSION 42.0 HOMEPAGE_URL "https://www.example.com") ``` -------------------------------- ### Inno Setup Specific Options Source: https://github.com/kitware/cmake/blob/master/Tests/CPackInnoSetupGenerator/CMakeLists.txt Customizes the Inno Setup installer behavior, including installation root, menu folder, and page visibility. This allows fine-grained control over the installer's user experience. ```cmake set(CPACK_INNOSETUP_INSTALL_ROOT "{autopf}\\Sheldon Cooper") set(CPACK_INNOSETUP_PROGRAM_MENU_FOLDER ".") set(CPACK_INNOSETUP_IGNORE_LICENSE_PAGE ON) set(CPACK_INNOSETUP_IGNORE_README_PAGE OFF) # Test if only readme page is shown set(CPACK_INNOSETUP_SETUP_AppComments ON) # Test if CPACK_INNOSETUP_USE_CMAKE_BOOL_FORMAT works set(CPACK_INNOSETUP_CUSTOM_INSTALL_INSTRUCTIONS "extras/empty" "Name: \"{userdocs}\\empty\"; Check: ReturnTrue; Components: accessories\\extras") set(CPACK_INNOSETUP_MENU_LINKS "https://www.example.com" "Web" "my_file.txt" "Text") set(CPACK_INNOSETUP_RUN_EXECUTABLES hello) set(CPACK_INNOSETUP_CREATE_UNINSTALL_LINK ON) # Test if this macro is available in the code file below containing the check function set(CPACK_INNOSETUP_DEFINE_PascalMacro "end;") set(CPACK_INNOSETUP_CODE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Code.pas") set(CPACK_INNOSETUP_EXECUTABLE "ISCC.exe") ``` -------------------------------- ### Build and Install Tutorial Project Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Installation Commands and Concepts.rst Commands to configure, build, and install the tutorial project. For multi-config generators, specify the configuration. ```console cmake --preset tutorial cmake --build build ``` ```console cmake --install build --prefix install ``` -------------------------------- ### Create and Configure an Application Bundle Source: https://github.com/kitware/cmake/blob/master/Tests/BundleTest/BundleSubDir/CMakeLists.txt Defines an executable target with the MACOSX_BUNDLE property and links it against a library. This example also shows how to set the output name for the bundle and install it. ```cmake project(BundleSubDir) add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/randomResourceFile.plist" COMMAND /bin/cp ARGS "${BundleTest_SOURCE_DIR}/randomResourceFile.plist.in" "${CMAKE_CURRENT_BINARY_DIR}/randomResourceFile.plist") set_source_files_properties( "${CMAKE_CURRENT_BINARY_DIR}/randomResourceFile.plist" PROPERTIES MACOSX_PACKAGE_LOCATION Resources ) set_source_files_properties( "${BundleTest_SOURCE_DIR}/SomeRandomFile.txt" "${BundleTest_SOURCE_DIR}/../../README.rst" PROPERTIES MACOSX_PACKAGE_LOCATION Other ) add_executable(SecondBundle MACOSX_BUNDLE "${BundleTest_SOURCE_DIR}/BundleTest.cxx" "${BundleTest_SOURCE_DIR}/SomeRandomFile.txt" "${BundleTest_SOURCE_DIR}/../../README.rst" "${CMAKE_CURRENT_BINARY_DIR}/randomResourceFile.plist" ) target_link_libraries(SecondBundle BundleTestLib) # Test bundle installation. install(TARGETS SecondBundle DESTINATION Applications) # Test whether bundles respect the output name. Since the library is # installed into a location that uses this output name this will fail if the # bundle does not respect the name. Also the executable will not be found by # the test driver if this does not work. set_target_properties(SecondBundle PROPERTIES OUTPUT_NAME SecondBundleExe) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/QtAutogen/MocInterfaceMacroNames/CMakeLists.txt Initializes a CMake project and enables the AUTOMOC feature. This is a standard starting point for projects using Qt's meta-object system. ```cmake cmake_minimum_required(VERSION 3.16) project(MocInterfaceMacroNames) include("../AutogenCoreTest.cmake") set(CMAKE_AUTOMOC ON) add_executable(dummy dummy.cpp) target_link_libraries(dummy PRIVATE static_lib interface_lib shared_lib) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/SubDirSpaces/CMakeLists.txt Initializes CMake version and project name. Includes logic to skip rpath on QNX systems and enables verbose Makefiles. ```cmake cmake_minimum_required(VERSION 3.10) project(SUBDIR) # Some systems do not seem to support rpath with spaces. if(CMAKE_SYSTEM_NAME MATCHES "QNX") set(CMAKE_SKIP_BUILD_RPATH 1) endif() # be able to see output from make on dashboards set(CMAKE_VERBOSE_MAKEFILE 1) message("${CMAKE_MAKE_PROGRAM}") ``` -------------------------------- ### Install Conditionally Built Tutorial Executable Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Installation Commands and Concepts.rst Conditionally install the 'Tutorial' executable if TUTORIAL_BUILD_UTILITIES is enabled. This is an example of installing a specific target. ```cmake if(TUTORIAL_BUILD_UTILITIES) add_subdirectory(Tutorial) install( TARGETS Tutorial ) endif() ``` -------------------------------- ### Install Multiple Components Source: https://github.com/kitware/cmake/blob/master/Help/manual/cmake.1.rst Demonstrates how to install multiple components using the --component option. ```shell cmake --install --component ``` ```shell cmake --install --component --component ``` -------------------------------- ### Install Code Block Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/Ninja/SubDirSource/CMakeLists.txt The `install(CODE ...)` command allows you to execute arbitrary CMake code during the installation process. This example shows a message being displayed during installation. ```cmake install(CODE [[ message(STATUS "Installing SubDirSource") ]]) ``` -------------------------------- ### Setup Header Installation Rules Source: https://github.com/kitware/cmake/blob/master/Source/kwsys/CMakeLists.txt Defines options for installing KWSYS headers, including the component name if specified. This allows for granular control over which components are installed. ```cmake set(KWSYS_INSTALL_INCLUDE_OPTIONS) if(KWSYS_INSTALL_COMPONENT_NAME_DEVELOPMENT) set(KWSYS_INSTALL_INCLUDE_OPTIONS ${KWSYS_INSTALL_INCLUDE_OPTIONS} COMPONENT ${KWSYS_INSTALL_COMPONENT_NAME_DEVELOPMENT} ) endif() ``` -------------------------------- ### Project Setup and Dummy Executable Source: https://github.com/kitware/cmake/blob/master/Tests/PolicyScope/CMakeLists.txt Basic CMake project setup including minimum version, project name, and adding a dummy executable for building and running purposes. ```cmake cmake_minimum_required(VERSION 3.30) project(PolicyScope C) #----------------------------------------------------------------------------- # Dummy executable so the project can build and run. add_executable(PolicyScope main.c) ``` -------------------------------- ### Configure Installation Prefix Source: https://github.com/kitware/cmake/blob/master/Utilities/cmexpat/README.md Use the --prefix option with the configure script to specify a custom installation directory. This example sets the installation path to /home/me/mystuff. ```bash ./configure --prefix=/home/me/mystuff ``` -------------------------------- ### Build and Install Project Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Installation Commands and Concepts.rst Standard commands to build and install the project. Ensure the 'install' target is used with a specified prefix. ```console cmake --build build cmake --install build --prefix install ``` -------------------------------- ### Install Custom Code Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/Ninja/SubDir/CMakeLists.txt The `install(CODE ...)` command allows you to execute custom CMake script code during the installation phase. This example prints a status message. ```cmake install(CODE [[ message(STATUS "Installing SubDir") ]]) ``` -------------------------------- ### Define External Project with Install Byproduct Source: https://github.com/kitware/cmake/blob/master/Tests/CustomCommandByproducts/CMakeLists.txt Use INSTALL_BYPRODUCTS to specify files installed by the external project. This example copies a library to the install directory and marks it as an install byproduct, also handling multi-configuration builds. ```cmake if(_isMultiConfig) set(cfg /${CMAKE_CFG_INTDIR}) else() set(cfg) endif() include(ExternalProject) ExternalProject_Add(ExtTargetInstallSubst SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/External" DOWNLOAD_COMMAND "" INSTALL_COMMAND "${CMAKE_COMMAND}" -E copy_directory "${cfg}" "${cfg}" BUILD_BYPRODUCTS "${cfg}/${CMAKE_STATIC_LIBRARY_PREFIX}ExternalLibrary${CMAKE_STATIC_LIBRARY_SUFFIX}" INSTALL_BYPRODUCTS "${cfg}/${CMAKE_STATIC_LIBRARY_PREFIX}ExternalLibrary${CMAKE_STATIC_LIBRARY_SUFFIX}" ) ExternalProject_Get_Property(ExtTargetInstallSubst install_dir) add_library(ExternalLibraryWithInstallDirSubstitution STATIC IMPORTED) set_property(TARGET ExternalLibraryWithInstallDirSubstitution PROPERTY IMPORTED_LOCATION ${install_dir}${cfg}/${CMAKE_STATIC_LIBRARY_PREFIX}ExternalLibrary${CMAKE_STATIC_LIBRARY_SUFFIX}) add_dependencies(ExternalLibraryWithInstallDirSubstitution ExtTargetInstallSubst) ``` -------------------------------- ### cmake_path GET command examples Source: https://github.com/kitware/cmake/blob/master/Help/dev/documentation.rst Demonstrates the usage of the cmake_path(GET) command to extract ROOT_NAME and ROOT_PATH from a path variable. ```cmake cmake_path(GET ROOT_NAME ) cmake_path(GET ROOT_PATH ) ``` -------------------------------- ### Install Targets for Export Source: https://github.com/kitware/cmake/blob/master/Help/variable/CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO.rst Example of installing targets for export in CMake. This prepares targets to be included in export sets for package distribution. ```cmake install(TARGETS foo EXPORT required-targets) install(TARGETS bar EXPORT optional-targets) ``` ```cmake install(EXPORT required-targets FILE example-targets.cmake ...) install(EXPORT optional-targets FILE example-optional-targets.cmake ...) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/Contracts/Trilinos/CMakeLists.txt Initializes CMake version and project name. Includes optional local override files. ```cmake cmake_minimum_required(VERSION 3.10) project(Trilinos) include(ExternalProject) include("${CMAKE_CURRENT_SOURCE_DIR}/LocalOverrides.cmake" OPTIONAL) include("${CMAKE_CURRENT_BINARY_DIR}/LocalOverrides.cmake" OPTIONAL) ``` -------------------------------- ### FindThreads C++ Example Source: https://github.com/kitware/cmake/blob/master/Tests/FindThreads/CXX-only/CMakeLists.txt Example CMakeLists.txt to find and link the Threads library for C++ projects. This setup is typically used on non-Windows systems. ```cmake cmake_minimum_required(VERSION 3.10) project(FindThreads_CXX-only CXX) find_package(Threads REQUIRED) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../../../Modules/CheckForPthreads.c ${CMAKE_CURRENT_BINARY_DIR}/CheckForPthreads.cxx) if (NOT WIN32) add_executable(thr ${CMAKE_CURRENT_BINARY_DIR}/CheckForPthreads.cxx) target_link_libraries(thr Threads::Threads) endif () ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/TestDriver/CMakeLists.txt Initializes CMake version and project name. Sets additional source files and include directories. ```cmake cmake_minimum_required(VERSION 3.10) project(TestDriverTest) set(Extra_SRCS testExtraStuff.cxx testExtraStuff2.cxx ) set(Extra_SRCS ${Extra_SRCS};testExtraStuff3.cxx ) include_directories(${TestDriverTest_SOURCE_DIR}) ``` -------------------------------- ### Configuring kwsys Install Rules in CMake Source: https://github.com/kitware/cmake/blob/master/Source/kwsys/CMakeLists.txt This snippet illustrates how to configure installation directories and component names for kwsys libraries and headers using CMake. These settings are relative to the installation prefix and should not start with '/'. ```cmake set(KWSYS_INSTALL_BIN_DIR bin) set(KWSYS_INSTALL_LIB_DIR lib) set(KWSYS_INSTALL_INCLUDE_DIR include) set(KWSYS_INSTALL_COMPONENT_NAME_RUNTIME Runtime) set(KWSYS_INSTALL_COMPONENT_NAME_DEVELOPMENT Development) ``` -------------------------------- ### Install Directory with Components Source: https://github.com/kitware/cmake/blob/master/Tests/CPackIFWGenerator/CMakeLists.txt Installs files from a directory with specific component assignments. Be mindful of Windows reserved names when naming components, as shown in the examples. ```cmake install( DIRECTORY . DESTINATION txt COMPONENT CON FILES_MATCHING PATTERN *.txt) ``` ```cmake install( DIRECTORY . DESTINATION txt COMPONENT Console FILES_MATCHING PATTERN *.txt) ``` ```cmake install( DIRECTORY . DESTINATION txt COMPONENT EndsWithDot. FILES_MATCHING PATTERN *.txt) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/SubDir/CMakeLists.txt Initializes CMake version and project name. Includes subdirectories and defines variables. ```cmake cmake_minimum_required(VERSION 3.10) project(SUBDIR) subdirs(Executable EXCLUDE_FROM_ALL Examples) set(DEFINED_AFTER_SUBDIRS_COMMAND 42) ``` -------------------------------- ### Project Setup and Includes Source: https://github.com/kitware/cmake/blob/master/Tests/QtAutogen/UicInterface/CMakeLists.txt Sets the minimum CMake version, project name, and includes necessary modules for GUI testing and Qt integration. ```cmake cmake_minimum_required(VERSION 3.16) project(UicInterface) include("../AutogenGuiTest.cmake") ``` -------------------------------- ### Set CPack Inno Setup Menu Links Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/innosetup.rst Configures menu links for the Inno Setup installer. Specify the link target, display text, and URL. ```cmake set(CPACK_INNOSETUP_MENU_LINKS "doc/cmake-@CMake_VERSION_MAJOR@.@CMake_VERSION_MINOR@/cmake.html" "CMake Help" "https://cmake.org" "CMake Web Site") ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/Instrumentation/project/CMakeLists.txt Initializes a CMake project for C, enables testing, and includes an optional instrumentation command file. ```cmake cmake_minimum_required(VERSION 4.3) project(instrumentation C) enable_testing() if (EXISTS ${INSTRUMENT_COMMAND_FILE}) include(${INSTRUMENT_COMMAND_FILE}) endif() ``` -------------------------------- ### MathFunctions CMakeLists.txt - Installation and Export Source: https://github.com/kitware/cmake/blob/master/Help/guide/importing-exporting/index.rst Installs the MathFunctions target and creates an export set named 'MathFunctionsTargets'. This setup facilitates importing the target into other CMake projects. ```cmake # install the target and create export-set install(TARGETS MathFunctions EXPORT MathFunctionsTargets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) # install header file ``` -------------------------------- ### Build Tutorial Project Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Miscellaneous Features.rst This console command configures and builds the TutorialProject using a CMake preset named 'tutorial'. Ensure you are in the correct directory before running. ```console cmake --preset tutorial cmake --build build ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/BundleUtilities/CMakeLists.txt This snippet shows the fundamental commands to initialize a CMake project. It sets the minimum required version, names the project, and includes another CMake script. ```cmake cmake_minimum_required(VERSION 3.12) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### CPack Component Installation Example Source: https://github.com/kitware/cmake/blob/master/Tests/CPackComponentsPrefix/CMakeLists.txt Configures CPack for component-based installation. Ensure CMAKE_INSTALL_PREFIX is set appropriately for your target environment. The CPACK_PACKAGE_CONTACT is mandatory for DEB generator. ```cmake cmake_minimum_required(VERSION 3.10) project(CPackComponentsPrefix NONE) install(FILES file-runtime.txt DESTINATION bin COMPONENT Runtime) install(FILES file-development.txt DESTINATION lib COMPONENT Development) set(CPACK_PACKAGE_CONTACT "None") # mandatory for DEB generator set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY 1) set(CPACK_COMPONENTS_ALL Development) set(CPACK_ARCHIVE_COMPONENT_INSTALL 1) set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/My-1.0") include(CPack) ``` -------------------------------- ### Custom InnoSetup Install Instruction for File Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/innosetup.rst Example of customizing the install instruction for a file, changing the destination directory and flags. Requires careful handling of escaping. ```cmake set(CPACK_INNOSETUP_CUSTOM_INSTALL_INSTRUCTIONS "my_file.txt;Source: \"my_file.txt\"\; DestDir: \"{userdocs}\"\; Flags: ignoreversion uninsneveruninstall") ``` -------------------------------- ### Build TutorialProject with Unpackaged Header Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Finding Dependencies.rst Build the TutorialProject to verify the inclusion of the unpackaged header. ```console cmake --build build ``` -------------------------------- ### Add Local External Project with Test Excluded (After Install) Source: https://github.com/kitware/cmake/blob/master/Tests/ExternalProjectLocal/CMakeLists.txt This example adds a local external project that runs tests after installation, with the tests excluded from the main build. It ensures tests are executed in the correct phase relative to installation. ```cmake set(proj TutorialStep5-Local-TestExcludeFromMainAfter) ExternalProject_Add(${proj} URL "${CMAKE_CURRENT_SOURCE_DIR}/Step5" CMAKE_ARGS --install-prefix= -G ${CMAKE_GENERATOR} CMAKE_CACHE_DEFAULT_ARGS -DUSE_MYMATH:BOOL=OFF TEST_AFTER_INSTALL 1 TEST_EXCLUDE_FROM_MAIN 1 STEP_TARGETS test LOG_TEST 1 ) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/OutOfSource/CMakeLists.txt Initializes a CMake project and adds a subdirectory. Ensure CMake version 3.10 or higher is used. ```cmake cmake_minimum_required(VERSION 3.10) project (OutOfSource) add_subdirectory(SubDir) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Help/guide/importing-exporting/MathFunctionsComponents/CMakeLists.txt Sets the minimum CMake version and project name. Includes standard practices for project initialization. ```cmake cmake_minimum_required(VERSION 3.15) project(MathFunctionsComponents) ``` -------------------------------- ### Install SimpleTest Library and Package Config Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Complete/SimpleTest/CMakeLists.txt Installs the SimpleTest library, its headers, and CMake package configuration files. This setup allows other projects to find and use SimpleTest. ```cmake include(GNUInstallDirs) include(CMakePackageConfigHelpers) install( TARGETS SimpleTest EXPORT SimpleTestTargets FILE_SET HEADERS ) install( EXPORT SimpleTestTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SimpleTest NAMESPACE SimpleTest:: ) write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/SimpleTestConfigVersion.cmake COMPATIBILITY ExactVersion ARCH_INDEPENDENT ) install( FILES cmake/simpletest_discover_impl.cmake cmake/simpletest_discover_tests.cmake cmake/SimpleTestConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/SimpleTestConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SimpleTest ) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/VSGNUFortran/subdir/fortran/CMakeLists.txt Initializes CMake for a project supporting Fortran and C. Specifies the minimum required CMake version. ```cmake cmake_minimum_required(VERSION 3.10) project(FortranHello Fortran C) ``` -------------------------------- ### Configure Package Information Installation Source: https://github.com/kitware/cmake/blob/master/Help/variable/CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO.rst Example CMake command-line arguments for configuring package information installation. This uses CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO and related variables to generate .cps files. ```bash -DCMAKE_EXPERIMENTAL_FIXME= -DCMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO=\ required-targets:Example/l;\ optional-targets:Example/laoptional -Drequired-targets_EXPORT_PACKAGE_INFO_VERSION=@Example_VERSION@ -Drequired-targets_EXPORT_PACKAGE_INFO_LICENSE=@Example_SPDX_LICENSE@ ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/kitware/cmake/blob/master/Tests/VSMARMASM/CMakeLists.txt This snippet shows the fundamental structure of a CMakeLists.txt file, including setting the minimum required version, project name, and adding an executable with source files. ```cmake cmake_minimum_required(VERSION 3.25) # Enable CMP0141 cmake_policy(SET CMP0184 NEW) project(VSMARMASM C ASM_MARMASM) add_executable(VSMARMASM main.c foo.asm) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/CXXModulesCompile/exp-builddb/CMakeLists.txt Initializes the CMake project and includes necessary rule and setup files. Sets the CMAKE_POLICY CMP0204 to NEW for MSVC ABI compatibility. ```cmake cmake_minimum_required(VERSION 3.29) project(cxx_modules_export_build_database CXX) include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake") include("${CMAKE_SOURCE_DIR}/../export-build-database-setup.cmake") # _MBCS default append for MSVC ABI workaround: see CMP00204 # So, force add the default value for all platforms to be consistent. cmap_policy(SET CMP00204 NEW) add_compile_definitions(_MBCS) ``` -------------------------------- ### Custom InnoSetup Install Instructions for Folders and Files Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/innosetup.rst Example demonstrating custom install instructions for both a folder and a file, including component assignment. Note the escaping requirements. ```cmake set(CPACK_INNOSETUP_CUSTOM_INSTALL_INSTRUCTIONS "component1/my_folder" "Name: \"{userdocs}\\\my_folder\"; Components: component1" "component2/my_folder2/my_file.txt" "Source: \"component2\\my_folder2\\my_file.txt\"; DestDir: \"{app}\\\my_folder2\\my_file.txt\"; Flags: ignoreversion uninsneveruninstall; Components: component2") ``` -------------------------------- ### Install Targets with Framework-Specific Destinations Source: https://github.com/kitware/cmake/blob/master/Tests/Framework/CMakeLists.txt Use the `install` command to specify installation directories for runtime, framework, library, archive, private headers, public headers, and resources. Note that some destinations are platform-specific. ```cmake install(TARGETS foo bar RUNTIME DESTINATION Applications/CMakeTestsFramework/bin FRAMEWORK DESTINATION Library/Frameworks LIBRARY DESTINATION lib ARCHIVE DESTINATION lib PRIVATE_HEADER DESTINATION share/foo-${foo_ver}/PrivateHeaders PUBLIC_HEADER DESTINATION include/foo-${foo_ver} RESOURCE DESTINATION share/foo-${foo_ver}/Resources ) ``` -------------------------------- ### WiX Installer Specific Settings Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/RunCPack/AppWiX/CMakeLists.txt Configures WiX specific installer properties, including upgrade GUID, uninstall option, and extra light flags for non-interactive sessions. ```cmake set(CPACK_WIX_UPGRADE_GUID "BF20CE5E-7F7C-401D-8F7C-AB45E8D170E6") set(CPACK_WIX_UNINSTALL "1") # Support non-interactive sessions (like CI). set(CPACK_WIX_LIGHT_EXTRA_FLAGS "-sval") ``` -------------------------------- ### Find Using Windows Registry Examples Source: https://github.com/kitware/cmake/blob/master/Help/manual/cmake-developer.7.rst Examples demonstrating how to query the Windows Registry using find_file and find_library commands with default and custom separators. ```cmake find_file(... PATHS "/root/[HKLM/Stuff;InstallDir]/lib[HKLM\\Stuff;Architecture]") # example using different specified separators find_library(... HINTS "/root/[{|}HKCU/Stuff|InstallDir]/lib[{@@}HKCU\\Stuff@@Architecture]") ``` -------------------------------- ### Install Library and Export Targets Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/CXXModulesCompile/imp-std-exp-no-std-build/CMakeLists.txt Installs the library and its C++ module file set, then exports the targets for use by other projects. This setup is essential for distributing and consuming the library and its modules. ```cmake install(TARGETS import_std_export_no_std EXPORT export ARCHIVE DESTINATION "lib" FILE_SET use_std DESTINATION "lib/cxx/miu") export(EXPORT export NAMESPACE CXXModules:: FILE "${CMAKE_CURRENT_BINARY_DIR}/import_std_export_no_std-targets.cmake") ``` -------------------------------- ### Install File Sets Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/FileAPI/fileset/CMakeLists.txt Use the `install` command with `FILE_SET` to specify which file sets should be installed and their destinations. Non-existent file sets are ignored. ```cmake install(TARGETS c_headers_1 FILE_SET HEADERS DESTINATION include COMPONENT Headers FILE_SET b DESTINATION include/dir FILE_SET c # Non-extant FILE_SET should be ignored FILE_SET d ) ``` -------------------------------- ### Install with Conditional Compression Logic Source: https://github.com/kitware/cmake/blob/master/Help/variable/CPACK_CUSTOM_INSTALL_VARIABLES.rst This CMake code installs a file and includes logic to conditionally compress it based on the ENABLE_COMPRESSION variable. This snippet demonstrates the setup that CPACK_CUSTOM_INSTALL_VARIABLES can influence. ```cmake install(FILES large.txt DESTINATION data) install(CODE [[ if(ENABLE_COMPRESSION) # "run-compressor" is a fictional tool that produces # large.txt.xz from large.txt and then removes the input file execute_process(COMMAND run-compressor $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/large.txt) endif() ]]) ``` -------------------------------- ### Install Targets with Runtime Dependencies Source: https://github.com/kitware/cmake/blob/master/Tests/ExportImport/Import/install-RUNTIME_DEPENDENCIES/CMakeLists.txt Installs targets and configures runtime dependencies using include, exclude, and directory patterns. This example shows complex filtering for runtime dependencies. ```cmake set(_framework_args) if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") set(_framework_args FRAMEWORK DESTINATION subdir/frameworks) endif() install(TARGETS exe1 exe2 lib mod sublib1 RUNTIME_DEPENDENCIES PRE_INCLUDE_REGEXES "$<1:dep([2-9]|1[012])>" PRE_EXCLUDE_REGEXES "$<1:.*>" POST_INCLUDE_REGEXES "$<1:(bin|lib)/(lib)?dep3>" POST_EXCLUDE_REGEXES "$<1:(bin|lib)/(lib)?dep[34]>" POST_INCLUDE_FILES "$" "$" POST_EXCLUDE_FILES "$" "$" DIRECTORIES "$" RUNTIME DESTINATION "$<1:subdir/bin>" LIBRARY DESTINATION "$<1:subdir/lib>" ${_framework_args} ) ``` -------------------------------- ### Install a Project with Configuration and Component Source: https://github.com/kitware/cmake/blob/master/Help/manual/cmake.1.rst Specifies the configuration and component for a component-based installation. Supports installing multiple components. ```shell cmake --install --config --component ``` -------------------------------- ### Setup KWSYS Installation Directories Source: https://github.com/kitware/cmake/blob/master/Source/kwsys/CMakeLists.txt Configures the installation directories for KWSYS headers, libraries, and binaries. It uses new KWSYS_INSTALL_* variable names and falls back to old names if not specified. ```cmake if(NOT KWSYS_INSTALL_INCLUDE_DIR) string(REGEX REPLACE "^/" "" KWSYS_INSTALL_INCLUDE_DIR "${KWSYS_HEADER_INSTALL_DIR}") endif() if(NOT KWSYS_INSTALL_LIB_DIR) string(REGEX REPLACE "^/" "" KWSYS_INSTALL_LIB_DIR "${KWSYS_LIBRARY_INSTALL_DIR}") endif() if(NOT KWSYS_INSTALL_BIN_DIR) string(REGEX REPLACE "^/" "" KWSYS_INSTALL_BIN_DIR "${KWSYS_LIBRARY_INSTALL_DIR}") endif() ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/CMP0171/CMakeLists.txt Sets the minimum CMake version, defines the project name and language, includes a test script, and enables testing. ```cmake cmake_minimum_required(VERSION 3.29) project(${RunCMake_TEST} LANGUAGES C) include(${RunCMake_TEST}.cmake) enable_testing() ``` -------------------------------- ### CPack Inno Setup Generator Configuration Source: https://github.com/kitware/cmake/blob/master/Tests/CPackInnoSetupGenerator/CMakeLists.txt Configures the CPack generator to use Inno Setup and sets various package properties. This includes name, vendor, install directory, and system name. ```cmake set(CPACK_GENERATOR "INNOSETUP") set(CPACK_PACKAGE_NAME "Hello, World!") # Test constant escape (like {cm:...}, see code documentation) set(CPACK_PACKAGE_VENDOR "Sheldon Cooper") set(CPACK_PACKAGE_INSTALL_DIRECTORY "hello_world") set(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "hello_world") set(CPACK_PACKAGE_FILE_NAME "hello_world_setup") set(CPACK_SYSTEM_NAME "win32") set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/my_bitmap.bmp") set(CPACK_VERBATIM_VARIABLES ON) set(CPACK_PACKAGE_EXECUTABLES "hello" "Hello, World!") set(CPACK_CREATE_DESKTOP_LINKS hello) ``` -------------------------------- ### Set Test Properties for Fixture Setup Source: https://github.com/kitware/cmake/blob/master/Help/prop_test/FIXTURES_SETUP.rst Example of setting the FIXTURES_SETUP and FIXTURES_REQUIRED properties for a test. The first example is valid, while the second demonstrates an error condition where a test cannot require the same fixture it is setting up. ```cmake set_tests_properties(setupFoo PROPERTIES FIXTURES_SETUP Foo FIXTURES_REQUIRED Bar ) ``` ```cmake set_tests_properties(setupFoo PROPERTIES FIXTURES_SETUP Foo FIXTURES_REQUIRED Foo ) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/MacRuntimePath/B/CMakeLists.txt Initializes CMake version and project name. Includes external CMake scripts for additional configuration. ```cmake cmake_minimum_required(VERSION 3.10) project(MacRuntimePath_B) ``` ```cmake include(${MacRuntimePath_B_BINARY_DIR}/../Root/lib/exp.cmake) ``` -------------------------------- ### Use Custom Macro in Inno Setup Extra Script Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/innosetup.rst Example of using a custom preprocessor macro defined via `CPACK_INNOSETUP_DEFINE_` within an extra Inno Setup script file. ```text AppComments={#emit "'My Macro' has the value: " + MyMacro} ``` -------------------------------- ### Basic Qt4 Executable Setup Source: https://github.com/kitware/cmake/blob/master/Tests/Qt4Targets/CMakeLists.txt Configures a basic Qt4 executable with specified source files and links against the Qt4::QtGui library. Ensure Qt4 is found using find_package. ```cmake cmake_minimum_required(VERSION 3.10) project(Qt4Targets) find_package(Qt4 REQUIRED) set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) add_executable(Qt4Targets WIN32 main.cpp) target_link_libraries(Qt4Targets Qt4::QtGui) ``` -------------------------------- ### Create and Install Directory Source: https://github.com/kitware/cmake/blob/master/Tests/CPackComponentsDEB/CMakeLists.txt Creates a directory and installs it into 'bin/' as part of the 'applications' component. The directory is left empty on purpose. Ensure not to add a trailing '/' to the directory name when specifying the destination. ```cmake if(EXISTS "./dirtest") execute_process(COMMAND ${CMAKE_COMMAND} -E rm -rf ./dirtest) endif() # NOTE: directory left empty on purpose execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ./dirtest) # NOTE: we should not add the trailing "/" to dirtest install(DIRECTORY ${CPackComponentsDEB_BINARY_DIR}/dirtest DESTINATION bin/ COMPONENT applications) ``` -------------------------------- ### Add CPackInnoSetupGenerator Test Source: https://github.com/kitware/cmake/blob/master/Tests/CMakeLists.txt Configures and adds a test for the CPackInnoSetupGenerator on Windows. This test verifies the Inno Setup installer generation process. ```cmake if(WIN32 AND CMake_TEST_CPACK_INNOSETUP) add_test(CPackInnoSetupGenerator ${CMAKE_CTEST_COMMAND} -C ${CTEST_CONFIGURATION_TYPE} --build-and-test "${CMake_SOURCE_DIR}/Tests/CPackInnoSetupGenerator" "${CMake_BINARY_DIR}/Tests/CPackInnoSetupGenerator" ${build_generator_args} --build-project CPackInnoSetupGenerator --build-options --test-command ${CMAKE_CMAKE_COMMAND} "-DCPackInnoSetupGenerator_BINARY_DIR:PATH=${CMake_BINARY_DIR}/Tests/CPackInnoSetupGenerator" "-Dconfig= ${CTEST_CONFIGURATION_TYPE}" -P "${CMake_SOURCE_DIR}/Tests/CPackInnoSetupGenerator/RunCPackVerifyResult.cmake") set_property(TEST CPackInnoSetupGenerator PROPERTY ATTACHED_FILES_ON_FAIL "${CMake_BINARY_DIR}/Tests/CPackInnoSetupGenerator/_CPack_Packages/win32/INNOSETUP/ISCCOutput.log") set_property(TEST CPackInnoSetupGenerator PROPERTY ATTACHED_FILES "${CMake_BINARY_DIR}/Tests/CPackInnoSetupGenerator/_CPack_Packages/win32/INNOSETUP/ISScript.iss") endif() ``` -------------------------------- ### Basic CUDA Toolkit Setup Source: https://github.com/kitware/cmake/blob/master/Tests/CudaOnly/StaticRuntimePlusToolkit/CMakeLists.txt Finds the CUDA Toolkit and links a common library against it. Ensure CUDAToolkit is installed and discoverable by CMake. ```cmake cmake_minimum_required(VERSION 3.18) project(StaticRuntimePlusToolkit CUDA) #Goal for this example: # Validate that with cuda we can use some components of the CUDA toolkit, and # specify the cuda runtime find_package(CUDAToolkit REQUIRED) add_library(Common OBJECT curand.cu nppif.cu) target_link_libraries(Common PRIVATE CUDA::toolkit) set_target_properties(Common PROPERTIES POSITION_INDEPENDENT_CODE ON) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/CTestTest/SmallAndFast/CMakeLists.txt Initializes CMake version and project name. Includes CTest for testing framework. ```cmake cmake_minimum_required(VERSION 3.10) project(SmallAndFast) include(CTest) ``` -------------------------------- ### Get Pretty Name from /etc/os-release Source: https://github.com/kitware/cmake/blob/master/Help/command/cmake_host_system_information.rst Retrieve the PRETTY_NAME field from the /etc/os-release file. This example demonstrates how to query distribution-specific information and display it. ```cmake cmake_host_system_information(RESULT PRETTY_NAME QUERY DISTRIB_PRETTY_NAME) message(STATUS "${PRETTY_NAME}") ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/CMakeOnly/ProjectInclude/CMakeLists.txt Initialize a CMake project and define its language support. Use LANGUAGES NONE if no specific language compilation is needed. ```cmake project(ProjectInclude LANGUAGES NONE) ``` -------------------------------- ### Get List Elements by Index Source: https://github.com/kitware/cmake/blob/master/Help/command/list.rst Retrieves one or more elements from a list based on their indices. Indices can be positive (from the start) or negative (from the end). ```cmake list(GET [ ...] ) ``` -------------------------------- ### Installing a Target Source: https://github.com/kitware/cmake/blob/master/Tests/RunCMake/Instrumentation/project/CMakeLists.txt Installs the 'main' executable. ```cmake install(TARGETS main) ``` -------------------------------- ### Basic CUDA Toolkit Setup Source: https://github.com/kitware/cmake/blob/master/Tests/CudaOnly/SharedRuntimePlusToolkit/CMakeLists.txt Finds the CUDA Toolkit and sets up a basic C++ project. Ensure CUDA is installed and findable by CMake. ```cmake cmake_minimum_required(VERSION 3.15) project(SharedRuntimePlusToolkit CUDA) #Goal for this example: # Validate that with c++ we can use some components of the CUDA toolkit, and # specify the cuda runtime find_package(CUDAToolkit REQUIRED) ``` -------------------------------- ### Install Project Source: https://github.com/kitware/cmake/blob/master/Help/guide/tutorial/Installation Commands and Concepts.rst Install the built project artifacts to a specified prefix. This command is used to deploy the project. ```console cmake --install build --prefix install ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/GhsMulti/GhsMultiIntegrity/GhsMultiIntegrityDDInt/CMakeLists.txt This snippet shows the basic structure of a CMakeLists.txt file, including setting the minimum version, project name, and adding subdirectories for different parts of the project. ```cmake cmake_minimum_required(VERSION 3.10) project(test) add_subdirectory(App) add_subdirectory(Int) add_subdirectory(Lib) ``` -------------------------------- ### Create Symbolic Link with CPack Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/rpm.rst Example of creating a symbolic link using CPack and installing it. Ensure the destination path is correctly specified. ```cmake execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION COMPONENT libraries) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/kitware/cmake/blob/master/Tests/EmptyProperty/CMakeLists.txt This snippet sets up a basic CMake project. It defines the minimum required version, names the project, and appends to the COMPILE_DEFINITIONS property. It also includes CTest and adds an executable target. ```cmake cmake_minimum_required(VERSION 3.10) project (EmptyProperty) set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS) include(CTest) add_executable(EmptyProperty EmptyProperty.cxx) ``` -------------------------------- ### Default InnoSetup Directory Instruction Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/innosetup.rst This is the default Inno Setup instruction CPack creates for every directory. It specifies the name of the directory within the installation. ```text Name: "{app}\my_folder" ``` -------------------------------- ### Default InnoSetup Install Instruction Source: https://github.com/kitware/cmake/blob/master/Help/cpack_gen/innosetup.rst This is the default Inno Setup instruction CPack creates for every file. It specifies the source, destination directory, and flags. ```text Source: "absolute\path\to\my_file.txt"; DestDir: "{app}"; Flags: ignoreversion ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/ISPC/DynamicLibrary/CMakeLists.txt Initializes CMake version and project name. Specifies CXX and ISPC as languages. ```cmake cmake_minimum_required(VERSION 3.18) project(ISPCDynamicLibrary CXX ISPC) ``` -------------------------------- ### CMake Project Setup Source: https://github.com/kitware/cmake/blob/master/Tests/Unset/CMakeLists.txt Basic CMake project setup including minimum version, project name, and executable definition. ```cmake add_executable(Unset unset.c) ```