### Example Project Setup Source: https://github.com/cmake/cmake/blob/master/Help/variable/CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO.rst Defines a project with libraries and exports them for installation. This sets up the targets that `CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO` will operate on. ```cmake project(Example VERSION 1.2.3 SPDX_LICENSE "BSD-3-Clause") add_library(foo ...) add_library(bar ...) install(TARGETS foo EXPORT required-targets) install(TARGETS bar EXPORT optional-targets) install(EXPORT required-targets FILE example-targets.cmake ...) install(EXPORT optional-targets FILE example-optional-targets.cmake ...) ``` -------------------------------- ### Custom InnoSetup File Install Instruction Example Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/innosetup.rst Example of customizing the destination directory and flags for a file installation. ```cmake set(CPACK_INNOSETUP_CUSTOM_INSTALL_INSTRUCTIONS "my_file.txt;Source: \"my_file.txt\"\; DestDir: \"{userdocs}\"\; Flags: ignoreversion uninsneveruninstall") ``` -------------------------------- ### Basic CMakeLists.txt Example Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CMP0173/CMakeLists.txt A minimal CMakeLists.txt file demonstrating basic project setup. ```cmake cmake_minimum_required(VERSION 3.30) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Custom InnoSetup Install Instructions with Components Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/innosetup.rst Example demonstrating how to customize install instructions for specific files and directories, including component assignment. Note the need for escaping. ```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") ``` -------------------------------- ### CMakeLists.txt Example Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CMP0119/CMakeLists.txt A basic CMakeLists.txt file demonstrating the project setup and inclusion of a test script. ```cmake cmake_minimum_required(VERSION 3.19) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Help/guide/importing-exporting/MathFunctionsComponents/CMakeLists.txt Initializes CMake, sets the project name, and includes standard directories for installation. ```cmake cmake_minimum_required(VERSION 3.15) project(MathFunctionsComponents) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/cmake/cmake/blob/master/Tests/CMakeCommands/target_sources/CMakeLists.txt Initializes CMake version, sets a policy, and defines a project. This is a standard starting point for CMake scripts. ```cmake cmake_minimum_required(VERSION 3.12) cmake_policy(SET CMP0076 NEW) project(target_sources) ``` -------------------------------- ### Install Code Example in CMake Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/add_subdirectory/CMP0082-ExcludeFromAll/CMakeLists.txt Demonstrates how to install custom code that executes during the installation process. This specific example prints a message indicating exclusion. ```cmake install(CODE "message(STATUS \"exclude\")") ``` -------------------------------- ### Basic Project Setup and Options Source: https://github.com/cmake/cmake/blob/master/Tests/Fuzzing/corpus/listfile/complex.txt Sets up the minimum CMake version, project name, C++ standard, and a build option for tests. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.16) project(ComplexProject VERSION 1.0.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) option(BUILD_TESTS "Build tests" ON) if(BUILD_TESTS) enable_testing() add_subdirectory(tests) endif() ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/FindDoxygen/AllTarget/CMakeLists.txt Initializes CMake, sets the project name and version, and enables testing. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.10) project(TestFindDoxygen VERSION 1.0 LANGUAGES NONE) enable_testing() ``` -------------------------------- ### Configure Inno Setup Menu Links Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/innosetup.rst Use CPACK_INNOSETUP_MENU_LINKS to add custom links to the Inno Setup start menu. Specify the link's 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") ``` -------------------------------- ### CMakeLists.txt Example Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CMP0115/CMakeLists.txt A basic CMakeLists.txt file demonstrating the setup for a CMake project. It includes setting the minimum required version, defining the project, and including a test CMake script. ```cmake cmake_minimum_required(VERSION 3.18) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Default InnoSetup File Install Instruction Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/innosetup.rst This is the default Inno Setup instruction CPack creates for every file. ```text Source: "absolute\path\to\my_file.txt"; DestDir: "{app}"; Flags: ignoreversion ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/FileSet-SOURCES/CMakeLists.txt This snippet initializes CMake, defines the project, and includes another CMake script. It's a fundamental starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 4.0) project(${RunCMake_TEST} LANGUAGES NONE) include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Complete/SimpleTest/CMakeLists.txt Initializes a CMake project with a specified version and defines an interface library. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.23) project(SimpleTest VERSION 0.0.1 ) ``` -------------------------------- ### Default InnoSetup Directory Install Instruction Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/innosetup.rst This is the default Inno Setup instruction CPack creates for every directory. ```text Name: "{app}\my_folder" ``` -------------------------------- ### Basic Fortran Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/VSGNUFortran/subdir/fortran/CMakeLists.txt Initializes a CMake project with Fortran and C languages. This is a standard starting point for multi-language CMake projects. ```cmake cmake_minimum_required(VERSION 3.10) project(FortranHello Fortran C) ``` -------------------------------- ### Basic CMake Project Setup for Package Creation Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake-packages.7.rst Sets up a basic CMake project, enabling include directories and defining the package version. This is a starting point for creating a package. ```cmake project(UpstreamLib) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON) set(Upstream_VERSION 3.4.1) include(GenerateExportHeader) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CpsExportImportBuild/CMakeLists.txt Sets the minimum CMake version, defines the project name, and includes another CMake file. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 4.0.20250220) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/BuiltinTargets/CMakeLists.txt Defines the minimum required CMake version, names the project, and includes a test module. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.28) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Install Project with Configuration Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake.1.rst For multi-configuration generators, specify the configuration to install. This is part of the install command options. ```shell cmake --install --config ``` -------------------------------- ### Install a Project Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake.1.rst Command to install a project from a specified build directory. Options can be provided to customize the installation process. ```console cmake --install [] ``` -------------------------------- ### Install Project with Strip Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake.1.rst Strip binaries before installing. This is a simple flag for the install command. ```shell cmake --install --strip ``` -------------------------------- ### Set NSIS Menu Links Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/nsis.rst Configure links to be displayed in the application's Start Menu. Links can be URLs or paths relative to the installation prefix. This example shows how to set help documentation, a website URL, and a CMake web site link. ```cmake set(CPACK_NSIS_MENU_LINKS "doc/cmake-@CMake_VERSION_MAJOR@.@CMake_VERSION_MINOR@/cmake.html" "CMake Help" "https://cmake.org" "CMake Web Site") ``` -------------------------------- ### Get Install Prefix Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake-generator-expressions.7.rst Retrieves the install prefix when the target is exported via install(EXPORT), or when evaluated in INSTALL_NAME_DIR or INSTALL_NAME_DIR argument of install(RUNTIME_DEPENDENCY_SET). Also evaluates to the install prefix in install(CODE) or install(SCRIPT) arguments. ```cmake $ ``` -------------------------------- ### Example of CTest Fixture Setup Source: https://github.com/cmake/cmake/blob/master/Help/prop_test/FIXTURES_REQUIRED.rst This example demonstrates how to define tests, fixtures, and their relationships using CTest properties. It includes setup, cleanup, required fixtures, and resource locking for a database test scenario. ```cmake add_test(NAME testsDone COMMAND emailResults) add_test(NAME fooOnly COMMAND testFoo) add_test(NAME dbOnly COMMAND testDb) add_test(NAME dbWithFoo COMMAND testDbWithFoo) add_test(NAME createDB COMMAND initDB) add_test(NAME setupUsers COMMAND userCreation) add_test(NAME cleanupDB COMMAND deleteDB) add_test(NAME cleanupFoo COMMAND removeFoos) set_tests_properties(setupUsers PROPERTIES DEPENDS createDB) set_tests_properties(createDB PROPERTIES FIXTURES_SETUP DB) set_tests_properties(setupUsers PROPERTIES FIXTURES_SETUP DB) set_tests_properties(cleanupDB PROPERTIES FIXTURES_CLEANUP DB) set_tests_properties(cleanupFoo PROPERTIES FIXTURES_CLEANUP Foo) set_tests_properties(testsDone PROPERTIES FIXTURES_CLEANUP "DB;Foo") set_tests_properties(fooOnly PROPERTIES FIXTURES_REQUIRED Foo) set_tests_properties(dbOnly PROPERTIES FIXTURES_REQUIRED DB) set_tests_properties(dbWithFoo PROPERTIES FIXTURES_REQUIRED "DB;Foo") set_tests_properties(dbOnly dbWithFoo createDB setupUsers cleanupDB PROPERTIES RESOURCE_LOCK DbAccess) ``` -------------------------------- ### Basic Qt4 Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/Qt4Targets/CMakeLists.txt Sets up the minimum CMake version, project name, finds the Qt4 package, and enables automatic MOC and include directories. ```cmake cmake_minimum_required(VERSION 3.10) project(Qt4Targets) find_package(Qt4 REQUIRED) set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) ``` -------------------------------- ### Install C++ Code Snippet Source: https://github.com/cmake/cmake/blob/master/Help/variable/CPACK_CUSTOM_INSTALL_VARIABLES.rst Example of an installation script that conditionally compresses a file based on a variable. ```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() ]]) ``` -------------------------------- ### Define and Get Property Example Source: https://github.com/cmake/cmake/blob/master/Help/command/define_property.rst This example demonstrates defining a custom target property and then checking if it is defined and retrieving its documentation using get_property. ```cmake # Initial definition define_property(TARGET PROPERTY MY_NEW_PROP BRIEF_DOCS "My new custom property" ) # Later examination get_property(my_new_prop_exists TARGET NONE PROPERTY MY_NEW_PROP DEFINED ) if(my_new_prop_exists) get_property(my_new_prop_docs TARGET NONE PROPERTY MY_NEW_PROP BRIEF_DOCS ) # ${my_new_prop_docs} is now set to "My new custom property" endif() ``` -------------------------------- ### Example: Generate and Build with FASTBuild Source: https://github.com/cmake/cmake/blob/master/Help/generator/FASTBuild.rst This example shows how to generate the fbuild.bff file and then use FASTBuild to build a specific target. ```shell cmake [] -G FASTBuild -B -DCMAKE_BUILD_TYPE=Release cmake --build --target my_app ``` -------------------------------- ### Setup KWSYS Header Install Rules Source: https://github.com/cmake/cmake/blob/master/Source/kwsys/CMakeLists.txt Configures options for installing KWSYS headers. Allows specifying a component for development files if the KWSYS_INSTALL_COMPONENT_NAME_DEVELOPMENT variable is set. ```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() ``` -------------------------------- ### CMakeLists.txt Basic Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/FetchContent_find_package/CMakeLists.txt Initializes CMake version and project name. Cleans up previous downloads to ensure a fresh test environment. ```cmake cmake_minimum_required(VERSION 3.23) project(${RunCMake_TEST} NONE) # Tests assume no previous downloads in the output directory file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/_deps) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Link Executable with Transitive Dependencies (Install Tree Variant) Source: https://github.com/cmake/cmake/blob/master/Tests/ExportImport/Import/A/CMakeLists.txt Another example of linking an executable with transitive dependencies imported from the install tree, demonstrating consistent behavior. ```cmake add_executable(imp_testLib11 imp_testLib11.c) target_link_libraries(imp_testLib11 PRIVATE exp_testLib11) ``` -------------------------------- ### Example: Static Plugins with CMake Source: https://github.com/cmake/cmake/blob/master/Help/prop_tgt/INTERFACE_LINK_LIBRARIES_DIRECT.rst Demonstrates how to set up static libraries and their plugins using CMake, ensuring correct linking order for dependencies. ```cmake # Core library used by other components. add_library(Core STATIC core.cpp) # Foo is a static library for use by applications. # Implementation of Foo depends on Core. add_library(Foo STATIC foo.cpp foo_plugin_helper.cpp) target_link_libraries(Foo PRIVATE Core) ``` -------------------------------- ### Build TutorialProject with CMake Presets Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Miscellaneous Features.rst Build the TutorialProject after its dependencies have been configured and installed. ```console cmake --preset tutorial cmake --build build ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/Autogen_6/incremental_build/CMakeLists.txt Sets the minimum CMake version and project name. Finds the required Qt components. ```cmake cmake_minimum_required(VERSION 3.10) project(UicIncrementalBuild LANGUAGES CXX) find_package(Qt${with_qt_version} REQUIRED COMPONENTS Core Widgets Gui) ``` -------------------------------- ### CMake Example for Link Libraries Strategy Source: https://github.com/cmake/cmake/blob/master/Help/prop_tgt/LINK_LIBRARIES_STRATEGY.rst This example demonstrates the setup for evaluating different link libraries ordering strategies. It defines static libraries A, B, and C, and an executable 'main' that links against them. ```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 Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/GeneratorExpressionShortCircuit/CMakeLists.txt This is a standard CMakeLists.txt file for setting up a test project. It includes the necessary CMake modules and defines the project name. ```cmake cmake_minimum_required(VERSION 3.26) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Setup KWSYS Library Install Rules Source: https://github.com/cmake/cmake/blob/master/Source/kwsys/CMakeLists.txt Defines rules for installing KWSYS libraries. Includes options for specifying an export name, destination directory, and component for runtime files. Also configures rules for installing namelinks for shared libraries, assigning them to the development component. ```cmake set(KWSYS_INSTALL_LIBRARY_RULE) set(KWSYS_INSTALL_NAMELINK_RULE) if(KWSYS_INSTALL_LIB_DIR) if(KWSYS_INSTALL_EXPORT_NAME) list(APPEND KWSYS_INSTALL_LIBRARY_RULE EXPORT ${KWSYS_INSTALL_EXPORT_NAME}) endif() # Install the shared library to the lib directory. set(KWSYS_INSTALL_LIBRARY_RULE ${KWSYS_INSTALL_LIBRARY_RULE} LIBRARY DESTINATION ${KWSYS_INSTALL_LIB_DIR} NAMELINK_SKIP ) # Assign the shared library to the runtime component. if(KWSYS_INSTALL_COMPONENT_NAME_RUNTIME) set(KWSYS_INSTALL_LIBRARY_RULE ${KWSYS_INSTALL_LIBRARY_RULE} COMPONENT ${KWSYS_INSTALL_COMPONENT_NAME_RUNTIME} ) endif() if(KWSYS_BUILD_SHARED) set(KWSYS_INSTALL_NAMELINK_RULE ${KWSYS_INSTALL_NAMELINK_RULE} LIBRARY DESTINATION ${KWSYS_INSTALL_LIB_DIR} NAMELINK_ONLY ) # Assign the namelink to the development component. if(KWSYS_INSTALL_COMPONENT_NAME_DEVELOPMENT) set(KWSYS_INSTALL_NAMELINK_RULE ${KWSYS_INSTALL_NAMELINK_RULE} COMPONENT ${KWSYS_INSTALL_COMPONENT_NAME_DEVELOPMENT} ) endif() endif() endif() ``` -------------------------------- ### CMakeLists.txt - Main Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/CMakeOnly/TargetScope/CMakeLists.txt Sets up the minimum CMake version and project name. It then adds a subdirectory and checks for the visibility of targets defined within it. ```cmake cmake_minimum_required(VERSION 3.10) project(TargetScope NONE) add_subdirectory(Sub) if(TARGET SubLibLocal) message(FATAL_ERROR "SubLibLocal visible in top directory") endif() if(NOT TARGET SubLibGlobal) message(FATAL_ERROR "SubLibGlobal not visible in top directory") endif() add_subdirectory(Sib) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/CommandLength/CMakeLists.txt Sets up a minimal CMake project and adds an executable. It also configures a post-build custom command to create a directory. ```cmake cmake_minimum_required(VERSION 3.10) project(CommandLength C) add_executable(CommandLength test.c) add_custom_command(TARGET CommandLength POST_BUILD VERBATIM COMMAND ${CMAKE_COMMAND} -E make_directory log) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/cmake/cmake/blob/master/Tests/CTestTestCycle/CMakeLists.txt Sets up a basic CMake project with an executable and three tests. This forms the foundation for defining test dependencies. ```cmake cmake_minimum_required(VERSION 3.10) project(CTestTestCycle) include(CTest) add_executable (simple simple.cxx) add_test (one simple) add_test (two simple) add_test (three simple) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/FileAPIExternalSource/CMakeLists.txt Initializes a C++ project and enables the C++ language. This is a standard starting point for most CMake projects. ```cmake project(External) enable_language(CXX) ``` -------------------------------- ### Get List Elements by Index Source: https://github.com/cmake/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 [ ...] ) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/add_test/CMakeLists.txt This snippet demonstrates the fundamental commands to initialize a CMake project. It sets the minimum required version, defines the project name, and includes another CMake script. ```cmake cmake_minimum_required(VERSION 3.18) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE) ``` -------------------------------- ### Setup Local Git Repository Source: https://github.com/cmake/cmake/blob/master/Tests/ExternalProject/CMakeLists.txt Use ExternalProject_Add to set up a local Git repository from a tarball. The CONFIGURE_COMMAND is used here to verify Git installation. ```cmake set(proj SetupLocalGITRepository) ExternalProject_Add(${proj} SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/LocalRepositories/GIT URL ${CMAKE_CURRENT_SOURCE_DIR}/gitrepo.tgz BUILD_COMMAND "" CONFIGURE_COMMAND "${GIT_EXECUTABLE}" --version INSTALL_COMMAND "" ) set_property(TARGET ${proj} PROPERTY FOLDER "SetupRepos/Local/Deeply/Nested/For/Testing") ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CTestCommandExpandLists/CMakeLists.txt This snippet shows the minimum required commands to initialize a CMake project and include a test script. ```cmake cmake_minimum_required(VERSION 3.14) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### Basic CMakeLists.txt Setup Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Getting Started with CMake.rst Essential commands for any CMake project's root CMakeLists.txt file. Ensures compatibility and defines the project name. ```cmake cmake_minimum_required(VERSION 3.23) project(MyProjectName) ``` -------------------------------- ### Create Symbolic Link with CPack Source: https://github.com/cmake/cmake/blob/master/Help/cpack_gen/rpm.rst Example of creating a symbolic link using CPack's execute_process command and installing it. This is useful for packaging symlinks. ```cmake execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION COMPONENT libraries) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/QtAutogen/UicInterface/CMakeLists.txt Initializes the CMake project and includes necessary helper scripts for Qt autogen features. ```cmake cmake_minimum_required(VERSION 3.16) project(UicInterface) include("../AutogenGuiTest.cmake") ``` -------------------------------- ### File Get Runtime Dependencies Test Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CMakeLists.txt Adds a test case for `file(GET_RUNTIME_DEPENDENCIES)` functionality, passing installation bug and compiler ID information. ```cmake add_RunCMake_test(file-GET_RUNTIME_DEPENDENCIES -DCMake_INSTALL_NAME_TOOL_BUG=${CMake_INSTALL_NAME_TOOL_BUG} -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID} ) ``` -------------------------------- ### Configure Qt Help Post Commands Source: https://github.com/cmake/cmake/blob/master/Utilities/Sphinx/CMakeLists.txt Sets up commands for generating Qt Help documentation, including finding necessary tools like Python and qhelpgenerator, and applying workarounds for CSS and name fixing. ```cmake find_package(Python REQUIRED) find_program(QHELPGENERATOR_EXECUTABLE NAMES qhelpgenerator-qt5 qhelpgenerator DOC "qhelpgenerator tool" ) if(NOT QHELPGENERATOR_EXECUTABLE) message(FATAL_ERROR "QHELPGENERATOR_EXECUTABLE (qhelpgenerator) not found!") endif() list(APPEND doc_formats qthelp) set(qthelp_post_commands # Workaround for assistant prior to # https://codereview.qt-project.org/#change,82250 in Qt 4. COMMAND ${CMAKE_COMMAND} "-DCSS_DIR=${CMAKE_CURRENT_BINARY_DIR}/qthelp/_static" -P "${CMAKE_CURRENT_SOURCE_DIR}/apply_qthelp_css_workaround.cmake" # Workaround sphinx configurability: # https://github.com/sphinx-doc/sphinx/issues/1448 COMMAND ${CMAKE_COMMAND} "-DQTHELP_DIR=${CMAKE_CURRENT_BINARY_DIR}/qthelp/" -P "${CMAKE_CURRENT_SOURCE_DIR}/fixup_qthelp_names.cmake" # Create proper identifiers. Workaround for # https://github.com/sphinx-doc/sphinx/issues/1491 COMMAND "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/create_identifiers.py" "${CMAKE_CURRENT_BINARY_DIR}/qthelp/" COMMAND ${QHELPGENERATOR_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/qthelp/CMake.qhcp ) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/ExportImport/Import/CMakeLists.txt Initializes CMake version, sets project name, and includes subdirectories for importing functionality. ```cmake cmake_minimum_required (VERSION 3.10) if(POLICY CMP0129) cmake_policy (SET CMP0129 NEW) endif() project(Import C CXX) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/ExternalOBJ/Object/CMakeLists.txt Sets the minimum required CMake version and defines the project name. This is a standard starting point for most CMake projects. ```cmake cmake_minimum_required(VERSION 3.10) project(Object) ``` -------------------------------- ### Project and Qt Setup Source: https://github.com/cmake/cmake/blob/master/Tests/QtAutogen/MacOsFW/CMakeLists.txt Initializes the CMake project and finds the required Qt version. Sets C++ standard based on QT_TEST_VERSION. ```cmake cmake_minimum_required(VERSION 3.16) project(MacOsFW) include("../AutogenGuiTest.cmake") find_package(Qt${QT_TEST_VERSION}Test REQUIRED) if(QT_TEST_VERSION EQUAL 5) set(CMAKE_CXX_STANDARD 11) elseif(QT_TEST_VERSION EQUAL 6) set(CMAKE_CXX_STANDARD 17) else() message(FATAL_ERROR "Unsupported Qt version: ${QT_TEST_VERSION}") endif() ``` -------------------------------- ### Basic Project Setup and Testing Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/Instrumentation/project/CMakeLists.txt Initializes the CMake project, enables testing, and defines the main executable and library. Links the executable to the library. ```cmake cmake_minimum_required(VERSION 4.3) project(instrumentation C) enable_testing() ``` ```cmake add_executable(main main.c) add_library(lib lib.c) target_link_libraries(main lib) ``` -------------------------------- ### CMake Minimum Version and Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/VSWinStorePhone/CMakeLists.txt Sets the minimum required CMake version and defines the project name. This is a standard starting point for CMake scripts. ```cmake cmake_minimum_required(VERSION 3.10) project(VSWinStorePhone) ``` -------------------------------- ### Initial CMake Setup Source: https://github.com/cmake/cmake/blob/master/Tests/QtAutogen/RerunMocPlugin/CMakeLists.txt Sets the minimum CMake version, project name, and includes core testing utilities. Defines a dummy executable for clean target generation. ```cmake cmake_minimum_required(VERSION 3.16) project(RerunMocPlugin) include("../AutogenCoreTest.cmake") # Tests Q_PLUGIN_METADATA and CMAKE_AUTOMOC_DEPEND_FILTERS # json file change detection # Dummy executable to generate a clean target add_executable(dummy dummy.cpp) ``` -------------------------------- ### MathFunctions Project CMakeLists.txt - Initial Setup Source: https://github.com/cmake/cmake/blob/master/Help/guide/importing-exporting/index.rst Initial CMakeLists.txt for the MathFunctions project, setting up the project name, version, and including subdirectories for components. ```cmake cmake_minimum_required(VERSION 3.16) project(MathFunctions VERSION 1.0 LANGUAGES CXX) add_subdirectory(Addition) add_subdirectory(SquareRoot) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/MSManifest/CMakeLists.txt Sets the minimum required CMake version and defines the project name and language. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.10) project(MSManifest C) ``` -------------------------------- ### Conditional Compile Options Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CXXModulesCompile/exp-usage-build/CMakeLists.txt Sets compile options conditionally based on the compiler. This example sets constexpr depth, with different values for build, install, and local interfaces. ```cmake if (MSVC) set(variable_flag "-constexpr:depth") else () set(variable_flag "-fconstexpr-depth=") endif () target_compile_options(export_usage PRIVATE "${variable_flag}100" "$" "$" "$") ``` -------------------------------- ### Configure iOS Project with Xcode Generator Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake-toolchains.7.rst Example of configuring a CMake project for iOS using the Xcode generator, specifying architectures, deployment target, and installation prefix. ```cmake $ cmake -S. -B_builds -GXcode \ -DCMAKE_SYSTEM_NAME=iOS \ "-DCMAKE_OSX_ARCHITECTURES=armv7;armv7s;arm64;i386;x86_64" \ -DCMAKE_OSX_DEPLOYMENT_TARGET=9.3 \ -DCMAKE_INSTALL_PREFIX=`pwd`/_install \ -DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO \ -DCMAKE_IOS_INSTALL_COMBINED=YES ``` -------------------------------- ### Build and Install Project Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Installation Commands and Concepts.rst Commands to build and install the project. Ensure the build directory is clean before rebuilding. ```console cmake --build build cmake --install build --prefix install ``` -------------------------------- ### Add Tutorial Subdirectory and Install Targets Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Step10/TutorialProject/CMakeLists.txt Adds the 'Tutorial' subdirectory and installs the 'Tutorial' target if TUTORIAL_BUILD_UTILITIES is enabled. ```cmake if(TUTORIAL_BUILD_UTILITIES) add_subdirectory(Tutorial) install( TARGETS Tutorial EXPORT TutorialTargets ) endif() ``` -------------------------------- ### Non-Relocatable Link Libraries and Include Directories Source: https://github.com/cmake/cmake/blob/master/Help/manual/cmake-packages.7.rst Example of how not to specify link libraries and include directories for a relocatable package, as it uses absolute paths that may not exist after installation. ```cmake target_link_libraries(ClimbingStats INTERFACE ${Foo_LIBRARIES} ${Bar_LIBRARIES} ) target_include_directories(ClimbingStats INTERFACE "$") ``` -------------------------------- ### Install Sphinx Qt Help Documentation Source: https://github.com/cmake/cmake/blob/master/Utilities/Sphinx/CMakeLists.txt Conditionally installs the generated Qt Help file (.qch) to the installation destination if SPHINX_QTHELP is enabled. ```cmake CMake_OPTIONAL_COMPONENT(sphinx-qthelp) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/qthelp/CMake.qch DESTINATION ${CMake_INSTALL_DOC_DIR} ${COMPONENT} ) ``` -------------------------------- ### Basic ExternalProject_Add Configuration Source: https://github.com/cmake/cmake/blob/master/Tests/ExternalProject/CMakeLists.txt This snippet shows a basic setup for ExternalProject_Add without explicit logging enabled. It defines commands for download, patch, update, configure, build, and install. ```cmake set(proj ExternalProject-no-log) set(download_cmd "") set(patch_cmd "") set(update_cmd "") set(configure_cmd "") set(build_cmd "") set(install_cmd "") ExternalProject_Add(${proj} DOWNLOAD_COMMAND "${download_cmd}" COMMAND "${CMAKE_COMMAND}" -E echo "download" PATCH_COMMAND "${patch_cmd}" COMMAND "${CMAKE_COMMAND}" -E echo "patch" UPDATE_COMMAND "${update_cmd}" COMMAND "${CMAKE_COMMAND}" -E echo "update" CONFIGURE_COMMAND "${configure_cmd}" COMMAND "${CMAKE_COMMAND}" -E echo "configure" BUILD_COMMAND "${build_cmd}" COMMAND "${CMAKE_COMMAND}" -E echo "build" INSTALL_COMMAND "${install_cmd}" COMMAND "${CMAKE_COMMAND}" -E echo "install" ) ``` -------------------------------- ### Run the Executable Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Getting Started with CMake.rst Execute the built target (e.g., `Tutorial`) with command-line arguments. The exact invocation may vary depending on your shell environment. ```console Tutorial 4294967296 Tutorial 10 Tutorial ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/target_compile_definitions/CMakeLists.txt Sets the minimum required CMake version and defines the project name. Languages are set to NONE as this example focuses on configuration rather than compilation. ```cmake cmake_minimum_required(VERSION 3.11) project(${RunCMake_TEST} LANGUAGES NONE) ``` -------------------------------- ### Downstream Project CMakeLists.txt - Initial Setup Source: https://github.com/cmake/cmake/blob/master/Help/guide/importing-exporting/index.rst Basic CMakeLists.txt for a downstream project, including minimum required version, project name, and C++ standard specification. ```cmake cmake_minimum_required(VERSION 3.16) project(Downstream VERSION 1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` -------------------------------- ### Set Default Directory Permissions Source: https://github.com/cmake/cmake/blob/master/Help/variable/CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS.rst Sets the default permissions for implicitly created directories during installation. This example includes owner read, write, execute, and group read permissions. ```cmake set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ ) ``` -------------------------------- ### Basic Java Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/Java/CMakeLists.txt Initializes a CMake project for Java and finds the necessary Java components. ```cmake project(hello Java) cmake_minimum_required(VERSION 3.10) set(CMAKE_VERBOSE_MAKEFILE 1) include(CTest) find_package(Java COMPONENTS Development) include (UseJava) ``` -------------------------------- ### Build and Install Project Source: https://github.com/cmake/cmake/blob/master/Help/guide/tutorial/Installation Commands and Concepts.rst Standard CMake commands to configure, build, and install a project. For multi-config generators, specify the configuration. ```console cmake --preset tutorial cmake --build build ``` ```console cmake --install build --prefix install ``` -------------------------------- ### Minimal CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/FetchContent_find_package/AddedProject/CMakeLists.txt This snippet sets the minimum CMake version, defines a project with no languages, and prints a confirmation message. It's a foundational example for new CMake projects. ```cmake cmake_minimum_required(VERSION 3.13...3.23) project(AddedProject LANGUAGES NONE) message(STATUS "Confirmation project has been added") ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/ARCHIVER-prefix/CMakeLists.txt Defines the minimum and maximum CMake versions required, names the project, and includes another CMake script. Use this for standard project initialization. ```cmake cmake_minimum_required(VERSION 3.31...4.0) project(${RunCMake_TEST} LANGUAGES NONE) include(${RunCMake_TEST}.cmake) ``` -------------------------------- ### CMake Project Setup and GTK2 Find Source: https://github.com/cmake/cmake/blob/master/Tests/FindGTK2/atk/CMakeLists.txt Configures the minimum CMake version, names the project, and finds the required GTK2 components including ATK. Ensure GTK2 is installed and discoverable by CMake. ```cmake cmake_minimum_required(VERSION 3.10) project(atk C) find_package(GTK2 COMPONENTS gtk REQUIRED) ``` -------------------------------- ### Qt4 Integration Setup Source: https://github.com/cmake/cmake/blob/master/Tests/CMakeLists.txt Sets up Qt4 testing, finding the package if not already defined. ```cmake if(NOT DEFINED CMake_TEST_Qt4) set(CMake_TEST_Qt4 1) endif() if(CMake_TEST_Qt4 AND NOT Qt4_FOUND) find_package(Qt4 QUIET) endif() ``` -------------------------------- ### Find and Link CUPS Library with CMake Source: https://github.com/cmake/cmake/blob/master/Tests/FindCups/Test/CMakeLists.txt This snippet shows the basic setup for finding the CUPS library and linking an executable against it using modern CMake targets. Ensure CUPS is installed on the system. ```cmake cmake_minimum_required(VERSION 3.10) project(TestFindCups C) include(CTest) find_package(Cups REQUIRED) add_definitions(-DCMAKE_EXPECTED_CUPS_VERSION="${Cups_VERSION}") add_executable(test_tgt main.c) target_link_libraries(test_tgt Cups::Cups) add_test(NAME test_tgt COMMAND test_tgt) ``` -------------------------------- ### CMake Project Setup and Library/Executable Build Source: https://github.com/cmake/cmake/blob/master/Tests/BundleGeneratorTest/CMakeLists.txt Configures the minimum CMake version, names the project, and defines build targets for a shared library and an executable. It also specifies installation destinations for these targets. ```cmake cmake_minimum_required(VERSION 3.10) project(BundleGeneratorTest) ``` ```cmake # Build a shared library and install it in lib/ add_library(Library SHARED Library.cxx) install(TARGETS Library DESTINATION lib) ``` ```cmake # Build an executable and install it in bin/ add_executable(Executable Executable.cxx) target_link_libraries(Executable Library) install(TARGETS Executable DESTINATION bin) ``` -------------------------------- ### CMakeLists.txt Example Source: https://github.com/cmake/cmake/blob/master/Tests/RunCMake/CMP0129/CMakeLists.txt Example CMakeLists.txt file demonstrating the use of include() with NO_POLICY_SCOPE. ```cmake cmake_minimum_required(VERSION 3.22) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE) ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/cmake/cmake/blob/master/Tests/FindMatlab/failure_reports/CMakeLists.txt Initializes CMake, enables testing, and defines the project name. Sets up variables for debugging and test execution based on MCR availability. ```cmake cmake_minimum_required(VERSION 3.10) enable_testing() project(failure_reports) ``` ```cmake set(MATLAB_FIND_DEBUG TRUE) ``` ```cmake if(IS_MCR) set(RUN_UNIT_TESTS FALSE) else() set(RUN_UNIT_TESTS TRUE) set(components MAIN_PROGRAM) endif() ``` ```cmake if(NOT "${MCR_ROOT}" STREQUAL "") set(Matlab_ROOT_DIR "${MCR_ROOT}") if(NOT EXISTS "${MCR_ROOT}") message(FATAL_ERROR "MCR does not exist ${MCR_ROOT}") endif() endif() ``` -------------------------------- ### Expat CMake Configuration Options Source: https://github.com/cmake/cmake/blob/master/Utilities/cmexpat/README.md These are common CMake variables used to configure the Expat build. They control features like build type, installation paths, documentation, examples, and library types. ```cmake // Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ... CMAKE_BUILD_TYPE:STRING= ``` ```cmake // Install path prefix, prepended onto install directories. CMAKE_INSTALL_PREFIX:PATH=/usr/local ``` ```cmake // Path to a program. DOCBOOK_TO_MAN:FILEPATH=/usr/bin/docbook2x-man ``` ```cmake // Build man page for xmlwf EXPAT_BUILD_DOCS:BOOL=ON ``` ```cmake // Build the examples for expat library EXPAT_BUILD_EXAMPLES:BOOL=ON ``` ```cmake // Build fuzzers for the expat library EXPAT_BUILD_FUZZERS:BOOL=OFF ``` ```cmake // Build pkg-config file EXPAT_BUILD_PKGCONFIG:BOOL=ON ``` ```cmake // Build the tests for expat library EXPAT_BUILD_TESTS:BOOL=ON ``` ```cmake // Build the xmlwf tool for expat library EXPAT_BUILD_TOOLS:BOOL=ON ``` ```cmake // Character type to use (char|ushort|wchar_t) [default=char] EXPAT_CHAR_TYPE:STRING=char ``` ```cmake // Install expat files in cmake install target EXPAT_ENABLE_INSTALL:BOOL=ON ``` ```cmake // Use /MT flag (static CRT) when compiling in MSVC EXPAT_MSVC_STATIC_CRT:BOOL=OFF ``` ```cmake // Build fuzzers via OSS-Fuzz for the expat library EXPAT_OSSFUZZ_BUILD:BOOL=OFF ``` ```cmake // Build a shared expat library EXPAT_SHARED_LIBS:BOOL=ON ``` ```cmake // Treat all compiler warnings as errors EXPAT_WARNINGS_AS_ERRORS:BOOL=OFF ``` ```cmake // Make use of getrandom function (ON|OFF|AUTO) [default=AUTO] EXPAT_WITH_GETRANDOM:STRING=AUTO ``` ```cmake // Utilize libbsd (for arc4random_buf) EXPAT_WITH_LIBBSD:BOOL=OFF ``` ```cmake // Make use of syscall SYS_getrandom (ON|OFF|AUTO) [default=AUTO] EXPAT_WITH_SYS_GETRANDOM:STRING=AUTO ```