### 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 [ ...]