mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-17 15:32:10 +08:00
Help: Add guides for user interaction
Add some prose to the documentation index page to guide readers to the major manuals and guides. Add a new "User Interaction Guide" to help the class of new user who wishes to build a project with CMake for the first time, such as after cloning a repo from a git repository. Add a new "Using Dependencies Guide" to help the class of new user who wishes to consume a SDK provided by a third party and needs a starting point. This is a different type of user to the user who wishes to create their own project from scratch (addressed by the `cmake-buildsystem(7)` manual) as each will encounter needs for information discovery in a different order.
This commit is contained in:
BIN
Help/guide/user-interaction/GUI-Add-Entry.png
Normal file
BIN
Help/guide/user-interaction/GUI-Add-Entry.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
BIN
Help/guide/user-interaction/GUI-Choose-Generator.png
Normal file
BIN
Help/guide/user-interaction/GUI-Choose-Generator.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
BIN
Help/guide/user-interaction/GUI-Configure-Dialog.png
Normal file
BIN
Help/guide/user-interaction/GUI-Configure-Dialog.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
BIN
Help/guide/user-interaction/GUI-Source-Binary.png
Normal file
BIN
Help/guide/user-interaction/GUI-Source-Binary.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
686
Help/guide/user-interaction/index.rst
Normal file
686
Help/guide/user-interaction/index.rst
Normal file
@@ -0,0 +1,686 @@
|
|||||||
|
User Interaction Guide
|
||||||
|
**********************
|
||||||
|
|
||||||
|
.. only:: html
|
||||||
|
|
||||||
|
.. contents::
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
============
|
||||||
|
|
||||||
|
Where a software package supplies a CMake-based buildsystem
|
||||||
|
with the source of their software, the consumer of the
|
||||||
|
software is required to run a CMake user interaction tool
|
||||||
|
in order to build it.
|
||||||
|
|
||||||
|
Well-behaved CMake-based buildsystems do not create any
|
||||||
|
output in the source directory, so typically, the user
|
||||||
|
performs an out-of-source build and performs the build
|
||||||
|
there. First, CMake must be instructed to generate a
|
||||||
|
suitable buildsystem, then the user invokes a build tool
|
||||||
|
to process that generated buildsystem. The generated
|
||||||
|
buildsystem is specific to the machine used to generate
|
||||||
|
it and is not redistributable. Each consumer of a provided
|
||||||
|
source software package is required to use CMake to
|
||||||
|
generate a buildsystem specific to their system.
|
||||||
|
|
||||||
|
Generated buildsystems should generally be treated as
|
||||||
|
read-only. The CMake files as a primary artifact should
|
||||||
|
completely specify the buildsystem and there should be no
|
||||||
|
reason to populate properties manually in an IDE for
|
||||||
|
example after generating the buildsystem. CMake will
|
||||||
|
periodically rewrite the generated buildsystem, so
|
||||||
|
modifications by users will be overwritten.
|
||||||
|
|
||||||
|
The features and user interfaces described in this manual
|
||||||
|
are available for all CMake-based build systems by virtue
|
||||||
|
of providing CMake files.
|
||||||
|
|
||||||
|
The CMake tooling may report errors to the user when
|
||||||
|
processing provided CMake files, such as reporting that
|
||||||
|
the compiler is not supported, or the compiler does not
|
||||||
|
support a required compile option, or a dependency can
|
||||||
|
not be found. These errors must be resolved by the user
|
||||||
|
by choosing a different compiler,
|
||||||
|
:guide:`installing dependencies <Using Dependencies Guide>`,
|
||||||
|
or instructing CMake where to find them, etc.
|
||||||
|
|
||||||
|
Command Line cmake tool
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
A simple but typical use of :manual:`cmake(1)` with a fresh
|
||||||
|
copy of software source code is to create a build directory
|
||||||
|
and invoke cmake there:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cd some_software-1.4.2
|
||||||
|
$ mkdir build
|
||||||
|
$ cd build
|
||||||
|
$ cmake .. -DCMAKE_INSTALL_PREFIX=/opt/the/prefix
|
||||||
|
$ cmake --build .
|
||||||
|
$ cmake --build . --target install
|
||||||
|
|
||||||
|
It is recommended to build in a separate directory to the
|
||||||
|
source because that keeps the source directory pristine,
|
||||||
|
allows for building a single source with multiple
|
||||||
|
toolchains, and allows easy clearing of build artifacts by
|
||||||
|
simply deleting the build directory.
|
||||||
|
|
||||||
|
The CMake tooling may report warnings which are intended
|
||||||
|
for the provider of the software, not intended for the
|
||||||
|
consumer of the software. Such warnings end with "This
|
||||||
|
warning is for project developers". Users may disable
|
||||||
|
such warnings by passing the ``-Wno-dev`` flag to
|
||||||
|
:manual:`cmake(1)`.
|
||||||
|
|
||||||
|
cmake-gui tool
|
||||||
|
--------------
|
||||||
|
|
||||||
|
Users more accustomed to GUI interfaces may use the
|
||||||
|
:manual:`cmake-gui(1)` tool to invoke CMake and generate
|
||||||
|
a buildsystem.
|
||||||
|
|
||||||
|
The source and binary directories must first be
|
||||||
|
populated. It is always advised to use different
|
||||||
|
directories for the source and the build.
|
||||||
|
|
||||||
|
.. image:: /guide/user-interaction/GUI-Source-Binary.png
|
||||||
|
|
||||||
|
Generating a Buildsystem
|
||||||
|
========================
|
||||||
|
|
||||||
|
There are several user interface tools which may be used
|
||||||
|
to generate a buildsystem from CMake files. The
|
||||||
|
:manual:`ccmake(1)` and :manual:`cmake-gui(1)` tools guide
|
||||||
|
the user through setting the various necessary options.
|
||||||
|
The :manual:`cmake(1)` tool can be invoked to specify
|
||||||
|
options on the command line. This manual describes options
|
||||||
|
which may be set using any of the user interface tools,
|
||||||
|
though the mode of setting an option is different for each
|
||||||
|
tool.
|
||||||
|
|
||||||
|
Command line environment
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
When invoking :manual:`cmake(1)` with a command line
|
||||||
|
buildsystem such as ``Makefiles`` or ``Ninja``, it is
|
||||||
|
necessary to use the correct build environment to
|
||||||
|
ensure that build tools are available. CMake must be
|
||||||
|
able to find the appropriate
|
||||||
|
:variable:`build tool <CMAKE_MAKE_PROGRAM>`,
|
||||||
|
compiler, linker and other tools as needed.
|
||||||
|
|
||||||
|
On Linux systems, the appropriate tools are often
|
||||||
|
provided in system-wide locations and may be readily
|
||||||
|
installed through the system package manager. Other
|
||||||
|
toolchains provided by the user or installed in
|
||||||
|
non-default locations can also be used.
|
||||||
|
|
||||||
|
When cross-compiling, some platforms may require
|
||||||
|
environment variables to be set or may provide
|
||||||
|
scripts to set the environment.
|
||||||
|
|
||||||
|
Visual Studio ships multiple command prompts and
|
||||||
|
``vcvarsall.bat`` scripts for setting up the
|
||||||
|
correct environments for command line buildsystems. While
|
||||||
|
not strictly necessary to use a corresponding
|
||||||
|
command line environment when using a Visual Studio
|
||||||
|
generator, doing so has no disadvantages.
|
||||||
|
|
||||||
|
When using Xcode, there can be more than one Xcode
|
||||||
|
version installed. Which one to use can be selected
|
||||||
|
in a number of different ways, but the most common
|
||||||
|
methods are:
|
||||||
|
|
||||||
|
* Setting the default version in the preferences
|
||||||
|
of the Xcode IDE.
|
||||||
|
* Setting the default version via the ``xcode-select``
|
||||||
|
command line tool.
|
||||||
|
* Overriding the default version by setting the
|
||||||
|
``DEVELOPER_DIR`` environment variable when running
|
||||||
|
CMake and the build tool.
|
||||||
|
|
||||||
|
Command line ``-G`` option
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
CMake chooses a generator by default based on the
|
||||||
|
platform. Usually, the default generator is sufficient
|
||||||
|
to allow the user to proceed to build the software.
|
||||||
|
|
||||||
|
The user may override the default generator with
|
||||||
|
the ``-G`` option:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake .. -G Ninja
|
||||||
|
|
||||||
|
The output of ``cmake --help`` includes a list of
|
||||||
|
:manual:`generators <cmake-generators(7)>` available
|
||||||
|
for the user to choose from. Note that generator
|
||||||
|
names are case sensitive.
|
||||||
|
|
||||||
|
On Unix-like systems (including Mac OS X), the
|
||||||
|
:generator:`Unix Makefiles` generator is used by
|
||||||
|
default. A variant of that generator can also be used
|
||||||
|
on Windows in various environments, such as the
|
||||||
|
:generator:`NMake Makefiles` and
|
||||||
|
:generator:`MinGW Makefiles` generator. These generators
|
||||||
|
generate a ``Makefile`` variant which can be executed
|
||||||
|
with ``make``, ``gmake``, ``nmake`` or similar tools.
|
||||||
|
See the individual generator documentation for more
|
||||||
|
information on targeted environments and tools.
|
||||||
|
|
||||||
|
The :generator:`Ninja` generator is available on all
|
||||||
|
major platforms. ``ninja`` is a build tool similar
|
||||||
|
in use-cases to ``make``, but with a focus on
|
||||||
|
performance and efficiency.
|
||||||
|
|
||||||
|
On Windows, :manual:`cmake(1)` can be used to generate
|
||||||
|
solutions for the Visual Studio IDE. Visual Studio
|
||||||
|
versions may be specified by the product name of the
|
||||||
|
IDE, which includes a four-digit year. Aliases are
|
||||||
|
provided for other means by which Visual Studio
|
||||||
|
versions are sometimes referred to, such as two
|
||||||
|
digits which correspond to the product version of the
|
||||||
|
VisualC++ compiler, or a combination of the two:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake .. -G "Visual Studio 2019"
|
||||||
|
$ cmake .. -G "Visual Studio 16"
|
||||||
|
$ cmake .. -G "Visual Studio 16 2019"
|
||||||
|
|
||||||
|
Visual Studio generators can target different architectures.
|
||||||
|
One can specify the target architecture using the `-A` option:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
cmake .. -G "Visual Studio 2019" -A x64
|
||||||
|
cmake .. -G "Visual Studio 16" -A ARM
|
||||||
|
cmake .. -G "Visual Studio 16 2019" -A ARM64
|
||||||
|
|
||||||
|
On Apple, the :generator:`Xcode` generator may be used to
|
||||||
|
generate project files for the Xcode IDE.
|
||||||
|
|
||||||
|
Some IDEs such as KDevelop4, QtCreator and CLion have
|
||||||
|
native support for CMake-based buildsystems. Those IDEs
|
||||||
|
provide user interface for selecting an underlying
|
||||||
|
generator to use, typically a choice between a ``Makefile``
|
||||||
|
or a ``Ninja`` based generator.
|
||||||
|
|
||||||
|
Note that it is not possible to change the generator
|
||||||
|
with ``-G`` after the first invocation of CMake. To
|
||||||
|
change the generator, the build directory must be
|
||||||
|
deleted and the build must be started from scratch.
|
||||||
|
|
||||||
|
When generating Visual Studio project and solutions
|
||||||
|
files several other options are available to use when
|
||||||
|
initially running :manual:`cmake(1)`.
|
||||||
|
|
||||||
|
The Visual Studio toolset can be specified with the
|
||||||
|
``-T`` option:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ # Build with the clang-cl toolset
|
||||||
|
$ cmake.exe .. -G "Visual Studio 16 2019" -A x64 -T LLVM
|
||||||
|
$ # Build targeting Windows XP
|
||||||
|
$ cmake.exe .. -G "Visual Studio 16 2019" -A x64 -T v120_xp
|
||||||
|
|
||||||
|
Whereas the ``-A`` option specifies the _target_
|
||||||
|
architecture, the ``-T`` option can be used to specify
|
||||||
|
details of the toolchain used. For example, `-Thost=x64`
|
||||||
|
can be given to select the 64-bit version of the host
|
||||||
|
tools. The following demonstrates how to use 64-bit
|
||||||
|
tools and also build for a 64-bit target architecture:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake .. -G "Visual Studio 16 2019" -A x64 -Thost=x64
|
||||||
|
|
||||||
|
Choosing a generator in cmake-gui
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
The "Configure" button triggers a new dialog to
|
||||||
|
select the CMake generator to use.
|
||||||
|
|
||||||
|
.. image:: /guide/user-interaction/GUI-Configure-Dialog.png
|
||||||
|
|
||||||
|
All generators available on the command line are also
|
||||||
|
available in :manual:`cmake-gui(1)`.
|
||||||
|
|
||||||
|
.. image:: /guide/user-interaction/GUI-Choose-Generator.png
|
||||||
|
|
||||||
|
When choosing a Visual Studio generator, further options
|
||||||
|
are available to set an architecture to generate for.
|
||||||
|
|
||||||
|
.. image:: /manual/VS-Choose-Arch.png
|
||||||
|
|
||||||
|
.. _`Setting Build Variables`:
|
||||||
|
|
||||||
|
Setting Build Variables
|
||||||
|
=======================
|
||||||
|
|
||||||
|
Software projects often require variables to be
|
||||||
|
set on the command line when invoking CMake. Some of
|
||||||
|
the most commonly used CMake variables are listed in
|
||||||
|
the table below:
|
||||||
|
|
||||||
|
========================================== ============================================================
|
||||||
|
Variable Meaning
|
||||||
|
========================================== ============================================================
|
||||||
|
:variable:`CMAKE_PREFIX_PATH` Path to search for
|
||||||
|
:guide:`dependent packages <Using Dependencies Guide>`
|
||||||
|
:variable:`CMAKE_MODULE_PATH` Path to search for additional CMake modules
|
||||||
|
:variable:`CMAKE_BUILD_TYPE` Build configuration, such as
|
||||||
|
``Debug`` or ``Release``, determining
|
||||||
|
debug/optimization flags. This is only
|
||||||
|
relevant for single-configuration buildsystems such
|
||||||
|
as ``Makefile`` and ``Ninja``. Multi-configuration
|
||||||
|
buildsystems such as those for Visual Studio and Xcode
|
||||||
|
ignore this setting.
|
||||||
|
:variable:`CMAKE_INSTALL_PREFIX` Location to install the
|
||||||
|
software to with the
|
||||||
|
``install`` build target
|
||||||
|
:variable:`CMAKE_TOOLCHAIN_FILE` File containing cross-compiling
|
||||||
|
data such as
|
||||||
|
:manual:`toolchains and sysroots <cmake-toolchains(7)>`.
|
||||||
|
:variable:`BUILD_SHARED_LIBS` Whether to build shared
|
||||||
|
instead of static libraries
|
||||||
|
for :command:`add_library`
|
||||||
|
commands used without a type
|
||||||
|
:variable:`CMAKE_EXPORT_COMPILE_COMMANDS` Generate a ``compile_commands.json``
|
||||||
|
file for use with clang-based tools
|
||||||
|
========================================== ============================================================
|
||||||
|
|
||||||
|
Other project-specific variables may be available
|
||||||
|
to control builds, such as enabling or disabling
|
||||||
|
components of the project.
|
||||||
|
|
||||||
|
There is no convention provided by CMake for how
|
||||||
|
such variables are named between different
|
||||||
|
provided buildsystems, except that variables with
|
||||||
|
the prefix ``CMAKE_`` usually refer to options
|
||||||
|
provided by CMake itself and should not be used
|
||||||
|
in third-party options, which should use
|
||||||
|
their own prefix instead. The
|
||||||
|
:manual:`cmake-gui(1)` tool can display options
|
||||||
|
in groups defined by their prefix, so it makes
|
||||||
|
sense for third parties to ensure that they use a
|
||||||
|
self-consistent prefix.
|
||||||
|
|
||||||
|
Setting variables on the command line
|
||||||
|
-------------------------------------
|
||||||
|
|
||||||
|
CMake variables can be set on the command line either
|
||||||
|
when creating the initial build:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ mkdir build
|
||||||
|
$ cd build
|
||||||
|
$ cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Debug
|
||||||
|
|
||||||
|
or later on a subsequent invocation of
|
||||||
|
:manual:`cmake(1)`:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cd build
|
||||||
|
$ cmake . -DCMAKE_BUILD_TYPE=Debug
|
||||||
|
|
||||||
|
The ``-U`` flag may be used to unset variables
|
||||||
|
on the :manual:`cmake(1)` command line:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cd build
|
||||||
|
$ cmake . -UMyPackage_DIR
|
||||||
|
|
||||||
|
A CMake buildsystem which was initially created
|
||||||
|
on the command line can be modified using the
|
||||||
|
:manual:`cmake-gui(1)` and vice-versa.
|
||||||
|
|
||||||
|
The :manual:`cmake(1)` tool allows specifying a
|
||||||
|
file to use to populate the initial cache using
|
||||||
|
the ``-C`` option. This can be useful to simplify
|
||||||
|
commands and scripts which repeatedly require the
|
||||||
|
same cache entries.
|
||||||
|
|
||||||
|
Setting variables with cmake-gui
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
Variables may be set in the cmake-gui using the "Add Entry"
|
||||||
|
button. This triggers a new dialog to set the value of
|
||||||
|
the variable.
|
||||||
|
|
||||||
|
.. image:: /guide/user-interaction/GUI-Add-Entry.png
|
||||||
|
|
||||||
|
The main view of the :manual:`cmake-gui(1)` user interface
|
||||||
|
can be used to edit existing variables.
|
||||||
|
|
||||||
|
The CMake Cache
|
||||||
|
---------------
|
||||||
|
|
||||||
|
When CMake is executed, it needs to find the locations of
|
||||||
|
compilers, tools and dependencies. It also needs to be
|
||||||
|
able to consistently re-generate a buildsystem to use the
|
||||||
|
same compile/link flags and paths to dependencies. Such
|
||||||
|
parameters are also required to be configurable by the
|
||||||
|
user because they are paths and options specific to the
|
||||||
|
users system.
|
||||||
|
|
||||||
|
When it is first executed, CMake generates a
|
||||||
|
``CMakeCache.txt`` file in the build directory containing
|
||||||
|
key-value pairs for such artifacts. The cache file can be
|
||||||
|
viewed or edited by the user by running the
|
||||||
|
:manual:`cmake-gui(1)` or :manual:`ccmake(1)` tool. The
|
||||||
|
tools provide an interactive interface for re-configuring
|
||||||
|
the provided software and re-generating the buildsystem,
|
||||||
|
as is needed after editing cached values. Each cache
|
||||||
|
entry may have an associated short help text which is
|
||||||
|
displayed in the user interface tools.
|
||||||
|
|
||||||
|
The cache entries may also have a type to signify how it
|
||||||
|
should be presented in the user interface. For example,
|
||||||
|
a cache entry of type ``BOOL`` can be edited by a
|
||||||
|
checkbox in a user interface, a ``STRING`` can be edited
|
||||||
|
in a text field, and a ``FILEPATH`` while similar to a
|
||||||
|
``STRING`` should also provide a way to locate filesystem
|
||||||
|
paths using a file dialog. An entry of type ``STRING``
|
||||||
|
may provide a restricted list of allowed values which are
|
||||||
|
then provided in a drop-down menu in the
|
||||||
|
:manual:`cmake-gui(1)` user interface (see the
|
||||||
|
:prop_cache:`STRINGS` cache property).
|
||||||
|
|
||||||
|
The CMake files shipped with a software package may also
|
||||||
|
define boolean toggle options using the :command:`option`
|
||||||
|
command. The command creates a cache entry which has a
|
||||||
|
help text and a default value. Such cache entries are
|
||||||
|
typically specific to the provided software and affect
|
||||||
|
the configuration of the build, such as whether tests
|
||||||
|
and examples are built, whether to build with exceptions
|
||||||
|
enabled etc.
|
||||||
|
|
||||||
|
Invoking the Buildsystem
|
||||||
|
========================
|
||||||
|
|
||||||
|
After generating the buildsystem, the software can be
|
||||||
|
built by invoking the particular build tool. In the
|
||||||
|
case of the IDE generators, this can involve loading
|
||||||
|
the generated project file into the IDE to invoke the
|
||||||
|
build.
|
||||||
|
|
||||||
|
CMake is aware of the specific build tool needed to invoke
|
||||||
|
a build so in general, to build a buildsystem or project
|
||||||
|
from the command line after generating, the following
|
||||||
|
command may be invoked in the build directory:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake --build .
|
||||||
|
|
||||||
|
The ``--build`` flag enables a particular mode of
|
||||||
|
operation for the :manual:`cmake(1)` tool. It invokes
|
||||||
|
the :variable:`CMAKE_MAKE_PROGRAM` command associated
|
||||||
|
with the :manual:`generator <cmake-generators(7)>`, or
|
||||||
|
the build tool configured by the user.
|
||||||
|
|
||||||
|
The ``--build`` mode also accepts the parameter
|
||||||
|
``--target`` to specify a particular target to build,
|
||||||
|
for example a particular library, executable or
|
||||||
|
custom target, or a particular special target like
|
||||||
|
``install``:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake --build . --target myexe
|
||||||
|
|
||||||
|
The ``--build`` mode also accepts a ``--config`` parameter
|
||||||
|
in the case of multi-config generators to specify which
|
||||||
|
particular configuration to build:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake --build . --target myexe --config Release
|
||||||
|
|
||||||
|
The ``--config`` option has no effect if the generator
|
||||||
|
generates a buildsystem specific to a configuration which
|
||||||
|
is chosen when invoking cmake with the
|
||||||
|
:variable:`CMAKE_BUILD_TYPE` variable.
|
||||||
|
|
||||||
|
Some buildsystems omit details of command lines invoked
|
||||||
|
during the build. The ``--verbose`` flag can be used to
|
||||||
|
cause those command lines to be shown:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake --build . --target myexe --verbose
|
||||||
|
|
||||||
|
The ``--build`` mode can also pass particular command
|
||||||
|
line options to the underlying build tool by listing
|
||||||
|
them after ``--``. This can be useful to specify
|
||||||
|
options to the build tool, such as to continue the
|
||||||
|
build after a failed job, where CMake does not
|
||||||
|
provide a high-level user interface.
|
||||||
|
|
||||||
|
For all generators, it is possible to run the underlying
|
||||||
|
build tool after invoking CMake. For example, ``make``
|
||||||
|
may be executed after generating with the
|
||||||
|
:generator:`Unix Makefiles` generator to invoke the build,
|
||||||
|
or ``ninja`` after generating with the :generator:`Ninja`
|
||||||
|
generator etc. The IDE buildsystems usually provide
|
||||||
|
command line tooling for building a project which can
|
||||||
|
also be invoked.
|
||||||
|
|
||||||
|
Selecting a Target
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Each executable and library described in the CMake files
|
||||||
|
is a build target, and the buildsystem may describe
|
||||||
|
custom targets, either for internal use, or for user
|
||||||
|
consumption, for example to create documentation.
|
||||||
|
|
||||||
|
CMake provides some built-in targets for all buildsystems
|
||||||
|
providing CMake files.
|
||||||
|
|
||||||
|
``all``
|
||||||
|
The default target used by ``Makefile`` and ``Ninja``
|
||||||
|
generators. Builds all targets in the buildsystem,
|
||||||
|
except those which are excluded by their
|
||||||
|
:prop_tgt:`EXCLUDE_FROM_ALL` target property or
|
||||||
|
:prop_dir:`EXCLUDE_FROM_ALL` directory property. The
|
||||||
|
name ``ALL_BUILD`` is used for this purpose for the
|
||||||
|
Xcode and Visual Studio generators.
|
||||||
|
``help``
|
||||||
|
Lists the targets available for build. This target is
|
||||||
|
available when using the :generator:`Unix Makefiles` or
|
||||||
|
:generator:`Ninja` generator, and the exact output is
|
||||||
|
tool-specific.
|
||||||
|
``clean``
|
||||||
|
Delete built object files and other output files. The
|
||||||
|
``Makefile`` based generators create a ``clean`` target
|
||||||
|
per directory, so that an individual directory can be
|
||||||
|
cleaned. The ``Ninja`` tool provides its own granular
|
||||||
|
``-t clean`` system.
|
||||||
|
``test``
|
||||||
|
Runs tests. This target is only automatically available
|
||||||
|
if the CMake files provide CTest-based tests. See also
|
||||||
|
`Running Tests`_.
|
||||||
|
``install``
|
||||||
|
Installs the software. This target is only automatically
|
||||||
|
available if the software defines install rules with the
|
||||||
|
:command:`install` command. See also
|
||||||
|
`Software Installation`_.
|
||||||
|
``package``
|
||||||
|
Creates a binary package. This target is only
|
||||||
|
automatically available if the CMake files provide
|
||||||
|
CPack-based packages.
|
||||||
|
``package_source``
|
||||||
|
Creates a source package. This target is only
|
||||||
|
automatically available if the CMake files provide
|
||||||
|
CPack-based packages.
|
||||||
|
|
||||||
|
For ``Makefile`` based systems, ``/fast`` variants of binary
|
||||||
|
build targets are provided. The ``/fast`` variants are used
|
||||||
|
to build the specified target without regard for its
|
||||||
|
dependencies. The dependencies are not checked and
|
||||||
|
are not rebuilt if out of date. The :generator:`Ninja`
|
||||||
|
generator is sufficiently fast at dependency checking that
|
||||||
|
such targets are not provided for that generator.
|
||||||
|
|
||||||
|
``Makefile`` based systems also provide build-targets to
|
||||||
|
preprocess, assemble and compile individual files in a
|
||||||
|
particular directory.
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ make foo.cpp.i
|
||||||
|
$ make foo.cpp.s
|
||||||
|
$ make foo.cpp.o
|
||||||
|
|
||||||
|
The file extension is built into the name of the target
|
||||||
|
because another file with the same name but a different
|
||||||
|
extension may exist. However, build-targets without the
|
||||||
|
file extension are also provided.
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ make foo.i
|
||||||
|
$ make foo.s
|
||||||
|
$ make foo.o
|
||||||
|
|
||||||
|
In buildsystems which contain ``foo.c`` and ``foo.cpp``,
|
||||||
|
building the ``foo.i`` target will preprocess both files.
|
||||||
|
|
||||||
|
Specifying a Build Program
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
The program invoked by the ``--build`` mode is determined
|
||||||
|
by the :variable:`CMAKE_MAKE_PROGRAM` variable. For most
|
||||||
|
generators, the particular program does not need to be
|
||||||
|
configured.
|
||||||
|
|
||||||
|
===================== =========================== ===========================
|
||||||
|
Generator Default make program Alternatives
|
||||||
|
===================== =========================== ===========================
|
||||||
|
XCode ``xcodebuild``
|
||||||
|
Unix Makefiles ``make``
|
||||||
|
NMake Makefiles ``nmake`` ``jom``
|
||||||
|
NMake Makefiles JOM ``jom`` ``nmake``
|
||||||
|
MinGW Makefiles ``mingw32-make``
|
||||||
|
MSYS Makefiles ``make``
|
||||||
|
Ninja ``ninja``
|
||||||
|
Visual Studio ``msbuild``
|
||||||
|
Watcom WMake ``wmake``
|
||||||
|
===================== =========================== ===========================
|
||||||
|
|
||||||
|
The ``jom`` tool is capable of reading makefiles of the
|
||||||
|
``NMake`` flavor and building in parallel, while the
|
||||||
|
``nmake`` tool always builds serially. After generating
|
||||||
|
with the :generator:`NMake Makefiles` generator a user
|
||||||
|
can run ``jom`` instead of ``nmake``. The ``--build``
|
||||||
|
mode would also use ``jom`` if the
|
||||||
|
:variable:`CMAKE_MAKE_PROGRAM` was set to ``jom`` while
|
||||||
|
using the :generator:`NMake Makefiles` generator, and
|
||||||
|
as a convenience, the :generator:`NMake Makefiles JOM`
|
||||||
|
generator is provided to find ``jom`` in the normal way
|
||||||
|
and use it as the :variable:`CMAKE_MAKE_PROGRAM`. For
|
||||||
|
completeness, ``nmake`` is an alternative tool which
|
||||||
|
can process the output of the
|
||||||
|
:generator:`NMake Makefiles JOM` generator, but doing
|
||||||
|
so would be a pessimisation.
|
||||||
|
|
||||||
|
Software Installation
|
||||||
|
=====================
|
||||||
|
|
||||||
|
The :variable:`CMAKE_INSTALL_PREFIX` variable can be
|
||||||
|
set in the CMake cache to specify where to install the
|
||||||
|
provided software. If the provided software has install
|
||||||
|
rules, specified using the :command:`install` command,
|
||||||
|
they will install artifacts into that prefix. On Windows,
|
||||||
|
the default installation location corresponds to the
|
||||||
|
``ProgramFiles`` system directory which may be
|
||||||
|
architecture specific. On Unix hosts, ``/usr/local`` is
|
||||||
|
the default installation location.
|
||||||
|
|
||||||
|
The :variable:`CMAKE_INSTALL_PREFIX` variable always
|
||||||
|
refers to the installation prefix on the target
|
||||||
|
filesystem.
|
||||||
|
|
||||||
|
In cross-compiling or packaging scenarios where the
|
||||||
|
sysroot is read-only or where the sysroot should otherwise
|
||||||
|
remain pristine, the :variable:`CMAKE_STAGING_PREFIX`
|
||||||
|
variable can be set to a location to actually install
|
||||||
|
the files.
|
||||||
|
|
||||||
|
The commands:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local \
|
||||||
|
-DCMAKE_SYSROOT=$HOME/root \
|
||||||
|
-DCMAKE_STAGING_PREFIX=/tmp/package
|
||||||
|
$ cmake --build .
|
||||||
|
$ cmake --build . --target install
|
||||||
|
|
||||||
|
result in files being installed to paths such
|
||||||
|
as ``/tmp/package/lib/libfoo.so`` on the host machine.
|
||||||
|
The ``/usr/local`` location on the host machine is
|
||||||
|
not affected.
|
||||||
|
|
||||||
|
Some provided software may specify ``uninstall`` rules,
|
||||||
|
but CMake does not generate such rules by default itself.
|
||||||
|
|
||||||
|
Running Tests
|
||||||
|
=============
|
||||||
|
|
||||||
|
The :manual:`ctest(1)` tool is shipped with the CMake
|
||||||
|
distribution to execute provided tests and report
|
||||||
|
results. The ``test`` build-target is provided to run
|
||||||
|
all available tests, but the :manual:`ctest(1)` tool
|
||||||
|
allows granular control over which tests to run, how to
|
||||||
|
run them, and how to report results. Executing
|
||||||
|
:manual:`ctest(1)` in the build directory is equivalent
|
||||||
|
to running the ``test`` target:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ ctest
|
||||||
|
|
||||||
|
A regular expression can be passed to run only tests
|
||||||
|
which match the expression. To run only tests with
|
||||||
|
``Qt`` in their name:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ ctest -R Qt
|
||||||
|
|
||||||
|
Tests can be excluded by regular expression too. To
|
||||||
|
run only tests without ``Qt`` in their name:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ ctest -E Qt
|
||||||
|
|
||||||
|
Tests can be run in parallel by passing ``-j`` arguments
|
||||||
|
to :manual:`ctest(1)`:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ ctest -R Qt -j8
|
||||||
|
|
||||||
|
The environment variable :envvar:`CTEST_PARALLEL_LEVEL`
|
||||||
|
can alternatively be set to avoid the need to pass
|
||||||
|
``-j``.
|
||||||
|
|
||||||
|
By default :manual:`ctest(1)` does not print the output
|
||||||
|
from the tests. The command line argument ``-V`` (or
|
||||||
|
``--verbose``) enables verbose mode to print the
|
||||||
|
output from all tests.
|
||||||
|
The ``--output-on-failure`` option prints the test
|
||||||
|
output for failing tests only. The environment variable
|
||||||
|
:envvar:`CTEST_OUTPUT_ON_FAILURE`
|
||||||
|
can be set to ``1`` as an alternative to passing the
|
||||||
|
``--output-on-failure`` option to :manual:`ctest(1)`.
|
200
Help/guide/using-dependencies/index.rst
Normal file
200
Help/guide/using-dependencies/index.rst
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
Using Dependencies Guide
|
||||||
|
************************
|
||||||
|
|
||||||
|
.. only:: html
|
||||||
|
|
||||||
|
.. contents::
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
============
|
||||||
|
|
||||||
|
For developers wishing to use CMake to consume a third
|
||||||
|
party binary package, there are multiple possibilities
|
||||||
|
regarding how to optimally do so, depending on how
|
||||||
|
CMake-aware the third-party library is.
|
||||||
|
|
||||||
|
CMake files provided with a software package contain
|
||||||
|
instructions for finding each build dependency. Some
|
||||||
|
build dependencies are optional in that the build may
|
||||||
|
succeed with a different feature set if the dependency
|
||||||
|
is missing, and some dependencies are required. CMake
|
||||||
|
searches well-known locations for each dependency, and
|
||||||
|
the provided software may supply additional hints or
|
||||||
|
locations to CMake to find each dependency.
|
||||||
|
|
||||||
|
If a required dependency is not found by
|
||||||
|
:manual:`cmake(1)`, the cache is populated with an entry
|
||||||
|
which contains a ``NOTFOUND`` value. This value can be
|
||||||
|
replaced by specifying it on the command line, or in
|
||||||
|
the :manual:`ccmake(1)` or :manual:`cmake-gui(1)` tool.
|
||||||
|
See the :guide:`User Interaction Guide` for
|
||||||
|
more about setting cache entries.
|
||||||
|
|
||||||
|
Libraries providing Config-file packages
|
||||||
|
----------------------------------------
|
||||||
|
|
||||||
|
The most convenient way for a third-party to provide library
|
||||||
|
binaries for use with CMake is to provide
|
||||||
|
:ref:`Config File Packages`. These packages are text files
|
||||||
|
shipped with the library which instruct CMake how to use the
|
||||||
|
library binaries and associated headers, helper tools and
|
||||||
|
CMake macros provided by the library.
|
||||||
|
|
||||||
|
The config files can usually be found in a directory whose
|
||||||
|
name matches the pattern ``lib/cmake/<PackageName>``, though
|
||||||
|
they may be in other locations instead. The
|
||||||
|
``<PackageName>`` corresponds to use in CMake code with the
|
||||||
|
:command:`find_package` command such as
|
||||||
|
``find_package(PackageName REQUIRED)``.
|
||||||
|
|
||||||
|
The ``lib/cmake/<PackageName>`` directory will contain a
|
||||||
|
file which is either named ``<PackageName>Config.cmake``
|
||||||
|
or ``<PackageName>-config.cmake``. This is the entry point
|
||||||
|
to the package for CMake. A separate optional file named
|
||||||
|
``<PackageName>ConfigVersion.cmake`` may also exist in the
|
||||||
|
directory. This file is used by CMake to determine whether
|
||||||
|
the version of the third party package satisfies uses of the
|
||||||
|
:command:`find_package` command which specify version
|
||||||
|
constraints. It is optional to specify a version when using
|
||||||
|
:command:`find_package`, even if a ``ConfigVersion`` file is
|
||||||
|
present.
|
||||||
|
|
||||||
|
If the ``Config.cmake`` file is found and the
|
||||||
|
optionally-specified version is satisfied, then the CMake
|
||||||
|
:command:`find_package` command considers the package to be
|
||||||
|
found and the entire library package is assumed to be
|
||||||
|
complete as designed.
|
||||||
|
|
||||||
|
There may be additional files providing CMake macros or
|
||||||
|
:ref:`imported targets` for you to use. CMake does not
|
||||||
|
enforce any naming convention for these
|
||||||
|
files. They are related to the primary ``Config`` file by
|
||||||
|
use of the CMake :command:`include` command.
|
||||||
|
|
||||||
|
:guide:`Invoking CMake <User Interaction Guide>` with the
|
||||||
|
intent of using a package of third party binaries requires
|
||||||
|
that cmake :command:`find_package` commands succeed in finding
|
||||||
|
the package. If the location of the package is in a directory
|
||||||
|
known to CMake, the :command:`find_package` call should
|
||||||
|
succeed. The directories known to cmake are platform-specific.
|
||||||
|
For example, packages installed on Linux with a standard
|
||||||
|
system package manager will be found in the ``/usr`` prefix
|
||||||
|
automatically. Packages installed in ``Program Files`` on
|
||||||
|
Windows will similarly be found automatically.
|
||||||
|
|
||||||
|
Packages which are not found automatically are in locations
|
||||||
|
not predictable to CMake such as ``/opt/mylib`` or
|
||||||
|
``$HOME/dev/prefix``. This is a normal situation and CMake
|
||||||
|
provides several ways for users to specify where to find
|
||||||
|
such libraries.
|
||||||
|
|
||||||
|
The :variable:`CMAKE_PREFIX_PATH` variable may be
|
||||||
|
:ref:`set when invoking CMake <Setting Build Variables>`.
|
||||||
|
It is treated as a list of paths to search for
|
||||||
|
:ref:`Config File Packages`. A package installed in
|
||||||
|
``/opt/somepackage`` will typically install config files
|
||||||
|
such as
|
||||||
|
``/opt/somepackage/lib/cmake/somePackage/SomePackageConfig.cmake``.
|
||||||
|
In that case, ``/opt/somepackage`` should be added to
|
||||||
|
:variable:`CMAKE_PREFIX_PATH`.
|
||||||
|
|
||||||
|
The environment variable ``CMAKE_PREFIX_PATH`` may also be
|
||||||
|
populated with prefixes to search for packages. Like the
|
||||||
|
``PATH`` environment variable, this is a list and needs to use
|
||||||
|
the platform-specific environment variable list item separator
|
||||||
|
(``:`` on Unix and ``;`` on Windows).
|
||||||
|
|
||||||
|
The :variable:`CMAKE_PREFIX_PATH` variable provides convenience
|
||||||
|
in cases where multiple prefixes need to be specified, or when
|
||||||
|
multiple different package binaries are available in the same
|
||||||
|
prefix. Paths to packages may also be specified by setting
|
||||||
|
variables matching ``<PackageName>_DIR``, such as
|
||||||
|
``SomePackage_DIR``. Note that this is not a prefix but should
|
||||||
|
be a full path to a directory containing a config-style package
|
||||||
|
file, such as ``/opt/somepackage/lib/cmake/SomePackage/`` in
|
||||||
|
the above example.
|
||||||
|
|
||||||
|
Imported Targets from Packages
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
A third-party package which provides config-file packages may
|
||||||
|
also provide :ref:`Imported targets`. These will be
|
||||||
|
specified in files containing configuration-specific file
|
||||||
|
paths relevant to the package, such as debug and release
|
||||||
|
versions of libraries.
|
||||||
|
|
||||||
|
Often the third-party package documentation will point out the
|
||||||
|
names of imported targets available after a successful
|
||||||
|
``find_package`` for a library. Those imported target names
|
||||||
|
can be used with the :command:`target_link_libraries` command.
|
||||||
|
|
||||||
|
A complete example which makes a simple use of a third party
|
||||||
|
library might look like:
|
||||||
|
|
||||||
|
.. code-block:: cmake
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(MyExeProject VERSION 1.0.0)
|
||||||
|
|
||||||
|
find_package(SomePackage REQUIRED)
|
||||||
|
add_executable(MyExe main.cpp)
|
||||||
|
target_link_libraries(MyExe PRIVATE SomePrefix::LibName)
|
||||||
|
|
||||||
|
See :manual:`cmake-buildsystem(7)` for further information
|
||||||
|
about developing a CMake buildsystem.
|
||||||
|
|
||||||
|
Libraries not Providing Config-file Packages
|
||||||
|
--------------------------------------------
|
||||||
|
|
||||||
|
Third-party libraries which do not provide config-file packages
|
||||||
|
can still be found with the :command:`find_package` command, if
|
||||||
|
a ``FindSomePackage.cmake`` file is available.
|
||||||
|
|
||||||
|
These module-file packages are different to config-file packages
|
||||||
|
in that:
|
||||||
|
|
||||||
|
#. They should not be provided by the third party, except
|
||||||
|
perhaps in the form of documentation
|
||||||
|
#. The availability of a ``Find<PackageName>.cmake`` file does
|
||||||
|
not indicate the availability of the binaries themselves.
|
||||||
|
#. CMake does not search the :variable:`CMAKE_PREFIX_PATH` for
|
||||||
|
``Find<PackageName>.cmake`` files. Instead CMake searches
|
||||||
|
for such files in the :variable:`CMAKE_MODULE_PATH`
|
||||||
|
variable. It is common for users to set the
|
||||||
|
:variable:`CMAKE_MODULE_PATH` when running CMake, and it is
|
||||||
|
common for CMake projects to append to
|
||||||
|
:variable:`CMAKE_MODULE_PATH` to allow use of local
|
||||||
|
module-file packages.
|
||||||
|
#. CMake ships ``Find<PackageName>.cmake`` files for some
|
||||||
|
:manual:`third party packages <cmake-modules(7)>`
|
||||||
|
for convenience in cases where the third party does
|
||||||
|
not provide config-file packages directly. These files are
|
||||||
|
a maintenance burden for CMake, so new Find modules are
|
||||||
|
generally not added to CMake anymore. Third-parties should
|
||||||
|
provide config file packages instead of relying on a Find
|
||||||
|
module to be provided by CMake.
|
||||||
|
|
||||||
|
Module-file packages may also provide :ref:`Imported targets`.
|
||||||
|
A complete example which finds such a package might look
|
||||||
|
like:
|
||||||
|
|
||||||
|
.. code-block:: cmake
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(MyExeProject VERSION 1.0.0)
|
||||||
|
|
||||||
|
find_package(PNG REQUIRED)
|
||||||
|
|
||||||
|
# Add path to a FindSomePackage.cmake file
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
|
||||||
|
find_package(SomePackage REQUIRED)
|
||||||
|
|
||||||
|
add_executable(MyExe main.cpp)
|
||||||
|
target_link_libraries(MyExe PRIVATE
|
||||||
|
PNG::PNG
|
||||||
|
SomePrefix::LibName
|
||||||
|
)
|
||||||
|
|
||||||
|
The :variable:`<PackageName>_ROOT` variable is also
|
||||||
|
searched as a prefix for :command:`find_package` calls using
|
||||||
|
module-file packages such as ``FindSomePackage``.
|
@@ -1,5 +1,34 @@
|
|||||||
.. title:: CMake Reference Documentation
|
.. title:: CMake Reference Documentation
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
############
|
||||||
|
|
||||||
|
CMake is a tool to manage building of source code. Originally, CMake was
|
||||||
|
designed as a generator for various dialects of ``Makefile``, today
|
||||||
|
CMake generates modern buildsystems such as ``Ninja`` as well as project
|
||||||
|
files for IDEs such as Visual Studio and Xcode.
|
||||||
|
|
||||||
|
CMake is widely used for the C and C++ languages, but it may be used to
|
||||||
|
build source code of other languages too.
|
||||||
|
|
||||||
|
People encountering CMake for the first time may have different initial
|
||||||
|
goals. To learn how to build a source code package downloaded from the
|
||||||
|
internet, start with the :guide:`User Interaction Guide`.
|
||||||
|
This will detail the steps needed to run the :manual:`cmake(1)` or
|
||||||
|
:manual:`cmake-gui(1)` executable and how to choose a generator, and
|
||||||
|
how to complete the build.
|
||||||
|
|
||||||
|
The :guide:`Using Dependencies Guide` is aimed at developers
|
||||||
|
wishing to get started using a third-party library.
|
||||||
|
|
||||||
|
For developers starting a project using CMake, the :guide:`CMake Tutorial`
|
||||||
|
is a suitable starting point. The :manual:`cmake-buildsystem(7)`
|
||||||
|
manual is aimed at developers expanding their knowledge of maintaining
|
||||||
|
a buildsystem and becoming familiar with the build targets that
|
||||||
|
can be represented in CMake. The :manual:`cmake-packages(7)` manual
|
||||||
|
explains how to create packages which can easily be consumed by
|
||||||
|
third-party CMake-based buildsystems.
|
||||||
|
|
||||||
Command-Line Tools
|
Command-Line Tools
|
||||||
##################
|
##################
|
||||||
|
|
||||||
@@ -53,6 +82,8 @@ Reference Manuals
|
|||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|
||||||
/guide/tutorial/index
|
/guide/tutorial/index
|
||||||
|
/guide/user-interaction/index
|
||||||
|
/guide/using-dependencies/index
|
||||||
|
|
||||||
.. only:: html or text
|
.. only:: html or text
|
||||||
|
|
||||||
|
BIN
Help/manual/VS-Choose-Arch.png
Normal file
BIN
Help/manual/VS-Choose-Arch.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user