// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \page creator-build-settings-cmake.html
    \previouspage creator-reference.html
    \nextpage creator-build-settings-qmake.html

    \title Building projects with CMake

    \quotation
    \l {Build with CMake}{How to: Build with CMake}
    \endquotation

    \brief Settings for building applications with CMake.

    To specify build settings for the selected \l{Managing kits}{kit}, go to
    \uicontrol Projects > \uicontrol {Build Settings}.

    Configuring medium-sized to large CMake projects in \QC can be a
    challenge due to the number of variables that you need to pass to
    CMake to configure the project correctly. To make this easier,
    \QC creates an initial configuration for you based on the \l{Managing kits}
    {kit preferences} and displays it in \uicontrol {Initial Configuration} in
    the \uicontrol {Build Settings} of the project.
    Or, you can use CMake presets to configure CMake.

    The \uicontrol Configure field displays the effective CMake call that
    \QC constructs using the values of the \uicontrol {Build directory} and
    \uicontrol {Build type} fields.

    \image {qtcreator-build-settings-cmake-configure.webp} {CMake configure command}

    You can specify additional CMake options, such as \c {--find-debug},
    \c {--trace-expand}, or \c {--warn-uninitialized}, in
    \uicontrol {Additional CMake options}. For more information about
    the options, select the link in the field name or see \l{CMake: cmake(1)}.

    Select \uicontrol {Kit Configuration} to edit the CMake settings for the
    build and run kit selected for the project.

    You can use \l{CMake presets} files to specify common
    configure, build, and test options and share them with others.

    \section1 Initial configuration

    \image {qtcreator-build-settings-cmake-initial.webp} {Initial CMake configuration}

    \uicontrol {Initial Configuration} lists the variables that \QC uses to
    configure the CMake project for the first time. It shows the default values
    that come from the kit's CMake configuration in italics. \QC saves the
    initial configuration list of variables in a file \c CMakeLists.txt.user
    located in a directory \c .qtcreator under the project's source directory.

    \section1 Current configuration

    \image {qtcreator-build-settings-cmake-current.webp} {Current CMake configuration}

    \uicontrol {Current Configuration} lists the CMake variables in the
    \c cmake-file-api JSON export in the \c {.cmake/api/v1/reply} directory.
    It shows the variables that come from the initial configuration in italics
    and mismatched values in red.

    After selecting the \uicontrol {Run CMake} button, you can view and
    change the actual values of the variables that \QC passes to CMake.
    The  \uicontrol Key column lists variable names, and the \uicontrol Value
    column lists their current values. For more information about the variables,
    select \uicontrol Help in the context menu or see
    \l{CMake: cmake-variables(7)}. For more information about Qt-specific
    variables, see \l{CMake Variable Reference}.

    \section1 Multi-config support

    \QC supports \l{CMake: Multi-config generators}{Multi-config generators},
    such as Xcode, Visual Studio, and Ninja Multi-Config. This means that you
    need to configure CMake only once and have only one build directory, and
    can switch between build types faster.

    However, this also means that \QC can no longer simply parse the first CMake
    file-api JSON export. Therefore, the value of the \uicontrol {Build type}
    field must match that of the \c CMAKE_BUILD_TYPE variable for the single
    configuration generators (Ninja, Makefile) to determine, which generator
    to use.

    When developing with Qt 6 for iOS, only the Xcode generator is supported.

    \section1 Modifying variable values

    You can view and edit the actual values of the variables that
    are passed to CMake in \uicontrol {Initial Configuration} or
    \uicontrol {Current Configuration}.

    \image {qtcreator-build-settings-cmake-current.webp} {Current CMake configuration}

    You can select several variables and apply an action to them. To clear
    the selection, click anywhere in the view.

    To batch edit variable values, select \uicontrol {Batch Edit}.

    \image {qtcreator-edit-cmake-configuration.webp} {Edit CMake Configuration dialog}

    To build using the current configuration, select \uicontrol {Run CMake}.
    While building, the button text changes to \uicontrol {Stop CMake}. Select
    the button to cancel the current build.

    \section2 Adding variables

    To add variables, select \uicontrol Add, and then select the type of
    the variable that you are adding: \uicontrol Boolean, \uicontrol String,
    \uicontrol Directory, or \uicontrol File.

    To change the type of the selected variable, right-click the
    variable name in the \uicontrol Key column, and then select
    \uicontrol {Force to bool}, \uicontrol {Force to file},
    \uicontrol {Force to directory}, or \uicontrol {Force to string}
    in the context menu.

    To copy the name or value of the selected variable to the clipboard,
    select \uicontrol Copy in the context menu.

    \section2 Changing variable values

    To change the value of a variable, double-click it, or select it,
    and then select \uicontrol Edit. If the initial, current, and kit
    configuration get out of sync, select \uicontrol {Apply Kit Value} or
    \uicontrol {Apply Initial Configuration Value} in the context menu in
    \uicontrol {Initial Configuration} or \uicontrol {Current Configuration}.

    To reset all the changes that you made, select \uicontrol Reset.

    The variable values that you change are passed via \c -D<option>=<value>
    to CMake, which stores the options in the \c CMakeCache.txt file. This means
    that if you remove the build directory, all the custom variables that are
    not part of the initial CMake configuration are also removed.

    To reconfigure a project using the changed variable values, go to
    \uicontrol Build and select \uicontrol {Clear CMake Configuration}, which
    removes the \c CMakeCache.txt file. This enables you to do a full rebuild.

    \section2 Removing variables

    To remove the selected variables, select \uicontrol Unset. To undo
    the removal, select \uicontrol Set.

    \section2 Viewing advanced variables

    To view all variables, select \uicontrol Advanced.

    To view all variables by default, go to \preferences > \uicontrol CMake >
    \uicontrol General and select \uicontrol {Show advanced options by default}.

    \image {qtcreator-preferences-cmake-general.webp} {General tab in CMake preferences}

    \section1 Re-configuring with initial variables

    To reset CMake variables to the initial ones, select
    \uicontrol {Re-configure with Initial Variables} in
    \uicontrol {Initial Configuration}. \QC deletes the current CMake
    configuration and runs CMake. The initial configuration values are
    stored in the \c CMakeLists.txt.user file, so deleting a build directory
    does not delete the initial configuration.

    To be asked before \QC resets the changes, go to \preferences >
    \uicontrol CMake > \uicontrol General and select
    \uicontrol {Ask before re-configuring with initial parameters}.

    \section1 Viewing CMake output

    Output from CMake is displayed next to the \uicontrol {Build Settings},
    \uicontrol {Deploy Settings}, and \uicontrol {Run Settings} tabs in the
    \uicontrol Projects mode.

    \image {qtcreator-build-cmake-output.webp} {CMake output in the Projects mode}

    To clear the output from CMake, select
    \inlineimage {icons/clean_pane_small.png} {Clear} (\uicontrol Clear).

    To always clear old output when you run CMake, go to \preferences >
    \uicontrol CMake > \uicontrol General and select
    \uicontrol {Clear old CMake output on a new run}.

    You can enter a string in the \uicontrol Filter field to filter output.
    To specify filtering options, select
    \inlineimage {icons/magnifier.png} {Filter menu}.
    You can filter output by using regular expressions or
    case-sensitivity. Select \uicontrol {Show Non-matching Lines} to
    hide the lines that match the filter. You can also set the number of
    preceding and subsequent lines to show.

    Select \key {Ctrl+F} to \l{Search in current file}{search} for a string in
    the output.

    To increase or decrease the output text size, select \imageplus
    (\uicontrol {Zoom In}) or \key Ctrl++ or \imageminus (\uicontrol {Zoom Out})
    or \key Ctrl+-.

    To hide the output, select
    \inlineimage {icons/rightsidebaricon.png} {Hide Right Sidebar}
    (\uicontrol {Hide Right Sidebar}) or \key {Alt+Shift+0}.

    \section1 CLICOLOR_FORCE environment variable

    \QC sets the environment variable \c CLICOLOR_FORCE to \e 1 to show
    ANSI-colored output for CMake. This might affect the process output.

    If the output looks incorrect or different from earlier \QC versions, try
    setting \c CLICOLOR_FORCE to \e 0 in \uicontrol {Use Build Environment}.

    \image {qtcreator-build-configuration-set-clicolor-force.webp} {Build Environment with CLICOLOR_FORCE set}

    \section1 CMake build steps

    \QC builds CMake projects by running \c {cmake . --build}, which then runs
    the CMake generator specified in the project configuration: \c make,
    \c mingw32-make, \c nmake, or \c ninja, for example. The CMake generator
    produces project files for \QC. Multi-config generators are also supported.

    You can add arguments to pass to CMake and the generator and targets for
    the build command in \uicontrol {Build Steps}.

    \image {qtcreator-cmake-build-steps.webp} {CMake build steps}

    \note While the other CMake generators are installed together with Qt,
    you usually need to install Ninja yourself.

    To install the application to a staging directory,
    select \uicontrol {Install into staging directory} and specify
    the path to the staging directory.
    The option runs the \c install target of CMake with \c DESTDIR set to the
    specified directory. The target location for deployment configurations is not affected.
    Use this option for example if you develop for a remote target device and do not
    want to install into the system directories of the build device, or to use
    separate installation directories on the build device for different build
    configurations.

    \section1 Using Ninja as a CMake generator

    To use \l {Ninja} with CMake, you must install it and select it as the CMake
    generator in the build and run kit:

    \list 1
        \li Install Ninja.
        \li Add the path to the Ninja executable to the value of the PATH system
            variable.
        \li Go to \uicontrol Projects > \uicontrol {Build Settings}, and select
            \uicontrol {Kit Configuration}.
            \image {qtcreator-cmake-kit-configuration.png} {Kit CMake Configuration dialog}
        \li Select \uicontrol Change next to the \uicontrol {CMake generator}
            field to open the \uicontrol {CMake Generator} dialog.
            \image {qtcreator-cmake-generator.webp} {CMake Generator dialog}
        \li In \uicontrol Generator, select \uicontrol Ninja.
        \li Select \uicontrol OK to save your changes and close the dialog.
        \li Select \uicontrol Close to close the
            \uicontrol {Kit CMake Configuration} dialog
            and return to \uicontrol {Build Settings}.
    \endlist

    \note To make sure that old build artifacts don't get in the way
    the first time you build the project after the change, go to
    \uicontrol Build and select \uicontrol {Rebuild Project}. This cleans up the
    build directory and performs a new build.

    \section1 Using CMake with package managers

    To automatically set up the \l {Conan Package Manager}{Conan} or
    \l {Set up vcpkg package manager}{vcpkg} package manager for use with CMake:

    \list 1
        \li Create a CMake script file that installs dependencies from a
            \c conanfile.txt, \c conanfile.py, or \c vcpkg.json file in
            the project source directory.
        \li Set the path to the script as the value of the
            \c CMAKE_PROJECT_INCLUDE_BEFORE variable.
        \li Go to \preferences > \uicontrol CMake > \uicontrol General and select
            \uicontrol {Package manager auto setup}.
    \endlist

    \section1 QTC_RUN environment variable

    \QC sets the environment variable \c QTC_RUN to \c 1 when executing
    the \c cmake process.

    This enables the CMake code to detect if it's being executed from \QC.

    \section1 CMake clean steps

    When building with CMake, you can add arguments to pass to CMake and the
    generator and targets for the clean command in \uicontrol {Clean Steps}.

    \image {qtcreator-cmake-clean-steps.webp} {CMake clean steps}

    The build errors and warnings are parsed and displayed in \l Issues.

    \sa {Activate kits for a project}, {Add custom output parsers},
    {Configure projects for building}, {Configure projects for running},
    {Build with CMake}{How to: Build with CMake}, {Open projects}, {CMake}
*/

/*!
    \page creator-how-to-use-junction-points.html
    \previouspage creator-how-tos.html

    \ingroup creator-how-to-build-with-cmake

    \title Use junction points on Windows

    Long project paths on Windows might lead to builds failing.

    To overcome these issues:

    \list
        \li Use source directory names with short absolute paths. For example,
            use \c {C:\dev} instead of \c {C:\Users\My Name\Projects}.
        \li Use short build directory paths.
        \li Use the Ninja generator for CMake.
        \li Use junction points on Windows for the source and build directories.
    \endlist

    \section1 Junction points

    At a \l {Junction points}{junction point}, a directory serves as an alias to
    another directory on the computer.

    To use the junction points for CMake configure, build, and install
    operations, go to \preferences > \uicontrol CMake > \uicontrol General and
    select \uicontrol {Use junctions for CMake configuration and build operations}.

    \image {qtcreator-preferences-cmake-general.webp} {General tab in CMake preferences}

    The build system configures, compiles, and installs the project through
    the junction points making command line commands shorter.

    \details {Set environment variables}
    \QC stores junction points in \c {C:\ProgramData\QtCreator\Links}. To use
    another path, set it as the value of the \c QTC_CMAKE_JUNCTIONS_DIR
    environment variable.

    Set the \c QTC_CMAKE_JUNCTIONS_HASH_LENGTH environment variable to shorten
    the MD5 hash key length from its default length value of 32.
    \enddetails

    \sa {Build with CMake}{How to: Build with CMake}
*/
