conan install

$ conan install -h
usage: conan install [-h] [-v [V]] [-cc CORE_CONF] [-f FORMAT]
                     [--out-file OUT_FILE] [--name NAME] [--version VERSION]
                     [--user USER] [--channel CHANNEL] [--requires REQUIRES]
                     [--tool-requires TOOL_REQUIRES] [-b BUILD]
                     [-r REMOTE | -nr] [-u [UPDATE]] [-pr PROFILE]
                     [-pr:b PROFILE_BUILD] [-pr:h PROFILE_HOST]
                     [-pr:a PROFILE_ALL] [-o OPTIONS] [-o:b OPTIONS_BUILD]
                     [-o:h OPTIONS_HOST] [-o:a OPTIONS_ALL] [-s SETTINGS]
                     [-s:b SETTINGS_BUILD] [-s:h SETTINGS_HOST]
                     [-s:a SETTINGS_ALL] [-c CONF] [-c:b CONF_BUILD]
                     [-c:h CONF_HOST] [-c:a CONF_ALL] [-l LOCKFILE]
                     [--lockfile-partial] [--lockfile-out LOCKFILE_OUT]
                     [--lockfile-clean]
                     [--lockfile-overrides LOCKFILE_OVERRIDES] [-g GENERATOR]
                     [-of OUTPUT_FOLDER] [-d DEPLOYER]
                     [--deployer-folder DEPLOYER_FOLDER]
                     [--deployer-package DEPLOYER_PACKAGE] [--build-require]
                     [--envs-generation {false}]
                     [path]

Install the requirements specified in a recipe (conanfile.py or conanfile.txt).

It can also be used to install packages without a conanfile, using the
--requires and --tool-requires arguments.

If any requirement is not found in the local cache, it will iterate the remotes
looking for it. When the full dependency graph is computed, and all dependencies
recipes have been found, it will look for binary packages matching the current settings.
If no binary package is found for some or several dependencies, it will error,
unless the '--build' argument is used to build it from source.

After installation of packages, the generators and deployers will be called.

positional arguments:
  path                  Path to a folder containing a recipe (conanfile.py or
                        conanfile.txt) or to a recipe file. e.g.,
                        ./my_project/conanfile.txt.

options:
  -h, --help            show this help message and exit
  -v [V]                Level of detail of the output. Valid options from less
                        verbose to more verbose: -vquiet, -verror, -vwarning,
                        -vnotice, -vstatus, -v or -vverbose, -vv or -vdebug,
                        -vvv or -vtrace
  -cc CORE_CONF, --core-conf CORE_CONF
                        Define core configuration, overwriting global.conf
                        values. E.g.: -cc core:non_interactive=True
  -f FORMAT, --format FORMAT
                        Select the output format: json
  --out-file OUT_FILE   Write the output of the command to the specified file
                        instead of stdout.
  --name NAME           Provide a package name if not specified in conanfile
  --version VERSION     Provide a package version if not specified in
                        conanfile
  --user USER           Provide a user if not specified in conanfile
  --channel CHANNEL     Provide a channel if not specified in conanfile
  --requires REQUIRES   Directly provide requires instead of a conanfile
  --tool-requires TOOL_REQUIRES
                        Directly provide tool-requires instead of a conanfile
  -b BUILD, --build BUILD
                        Optional, specify which packages to build from source.
                        Combining multiple '--build' options on one command
                        line is allowed. Possible values: --build=never
                        Disallow build for all packages, use binary packages
                        or fail if a binary package is not found, it cannot be
                        combined with other '--build' options. --build=missing
                        Build packages from source whose binary package is not
                        found. --build=cascade Build packages from source that
                        have at least one dependency being built from source.
                        --build=[pattern] Build packages from source whose
                        package reference matches the pattern. The pattern
                        uses 'fnmatch' style wildcards, so '--build="*"' will
                        build everything from source. --build=~[pattern]
                        Excluded packages, which will not be built from the
                        source, whose package reference matches the pattern.
                        The pattern uses 'fnmatch' style wildcards.
                        --build=missing:[pattern] Build from source if a
                        compatible binary does not exist, only for packages
                        matching pattern. --build=compatible:[pattern]
                        (Experimental) Build from source if a compatible
                        binary does not exist, and the requested package is
                        invalid, the closest package binary following the
                        defined compatibility policies (method and
                        compatibility.py)
  -r REMOTE, --remote REMOTE
                        Look in the specified remote or remotes server
  -nr, --no-remote      Do not use remote, resolve exclusively in the cache
  -u [UPDATE], --update [UPDATE]
                        Will install newer versions and/or revisions in the
                        local cache for the given reference name, or all
                        references in the graph if no argument is supplied.
                        When using version ranges, it will install the latest
                        version that satisfies the range. It will update to
                        the latest revision for the resolved version range.
  -pr PROFILE, --profile PROFILE
                        Apply the specified profile. By default, or if
                        specifying -pr:h (--profile:host), it applies to the
                        host context. Use -pr:b (--profile:build) to specify
                        the build context, or -pr:a (--profile:all) to specify
                        both contexts at once
  -pr:b PROFILE_BUILD, --profile:build PROFILE_BUILD
  -pr:h PROFILE_HOST, --profile:host PROFILE_HOST
  -pr:a PROFILE_ALL, --profile:all PROFILE_ALL
  -o OPTIONS, --options OPTIONS
                        Apply the specified options. By default, or if
                        specifying -o:h (--options:host), it applies to the
                        host context. Use -o:b (--options:build) to specify
                        the build context, or -o:a (--options:all) to specify
                        both contexts at once. Example:
                        -o="pkg/*:with_qt=True"
  -o:b OPTIONS_BUILD, --options:build OPTIONS_BUILD
  -o:h OPTIONS_HOST, --options:host OPTIONS_HOST
  -o:a OPTIONS_ALL, --options:all OPTIONS_ALL
  -s SETTINGS, --settings SETTINGS
                        Apply the specified settings. By default, or if
                        specifying -s:h (--settings:host), it applies to the
                        host context. Use -s:b (--settings:build) to specify
                        the build context, or -s:a (--settings:all) to specify
                        both contexts at once. Example: -s="compiler=gcc"
  -s:b SETTINGS_BUILD, --settings:build SETTINGS_BUILD
  -s:h SETTINGS_HOST, --settings:host SETTINGS_HOST
  -s:a SETTINGS_ALL, --settings:all SETTINGS_ALL
  -c CONF, --conf CONF  Apply the specified conf. By default, or if specifying
                        -c:h (--conf:host), it applies to the host context.
                        Use -c:b (--conf:build) to specify the build context,
                        or -c:a (--conf:all) to specify both contexts at once.
                        Example:
                        -c="tools.cmake.cmaketoolchain:generator=Xcode"
  -c:b CONF_BUILD, --conf:build CONF_BUILD
  -c:h CONF_HOST, --conf:host CONF_HOST
  -c:a CONF_ALL, --conf:all CONF_ALL
  -l LOCKFILE, --lockfile LOCKFILE
                        Path to a lockfile. Use --lockfile="" to avoid
                        automatic use of existing 'conan.lock' file
  --lockfile-partial    Do not raise an error if some dependency is not found
                        in lockfile
  --lockfile-out LOCKFILE_OUT
                        Filename of the updated lockfile
  --lockfile-clean      Remove unused entries from the lockfile
  --lockfile-overrides LOCKFILE_OVERRIDES
                        Overwrite lockfile overrides
  -g GENERATOR, --generator GENERATOR
                        Generators to use
  -of OUTPUT_FOLDER, --output-folder OUTPUT_FOLDER
                        The root output folder for generated and build files
  -d DEPLOYER, --deployer DEPLOYER
                        Deploy using the provided deployer to the output
                        folder. Built-in deployers: 'full_deploy',
                        'direct_deploy', 'runtime_deploy'
  --deployer-folder DEPLOYER_FOLDER
                        Deployer output folder, base build folder by default
                        if not set
  --deployer-package DEPLOYER_PACKAGE
                        Execute the deploy() method of the packages matching
                        the provided patterns
  --build-require       Whether the provided path is a build-require
  --envs-generation {false}
                        Generation strategy for virtual environment files for
                        the root

conan install 命令是 Conan 的主要命令之一,用于解析和安装依赖项。

此命令执行以下操作

  • 计算整个依赖关系图,针对由设置、选项、配置文件和配置定义的当前配置。 它解析版本范围、传递依赖项、条件需求等,以构建依赖关系图。

  • 评估图中每个软件包的二进制文件的存在情况,无论是否有预编译的二进制文件可供下载,或者是否应从源代码构建它们(如 --build 参数所指示的)。 如果缺少二进制文件,它不会重新计算依赖关系图,以尝试回退到包含该配置的二进制文件的先前版本。 如果需要特定的依赖项版本,则应明确要求。

  • 下载预编译的二进制文件,或在本地缓存中从源代码构建二进制文件,以正确的顺序进行依赖关系图。

  • 创建 “generators” 所需的文件,以便构建系统和其他工具可以找到本地安装的依赖项

  • 可选地,执行所需的 deployers

另请参阅

Conanfile 路径或 –requires

conan install 命令可以使用 2 个不同的信息来源。 第一个使用本地 conanfile.pyconanfile.txt,其中包含要使用的依赖项和生成器的定义。

$ conan install .  # there is a conanfile.txt or a conanfile.py in the cwd
$ conan install conanfile.py  # also works, direct reference file
$ conan install myconan.txt  # explicit custom name
$ conan install myfolder  # there is a conanfile in "myfolder" folder

即使可以使用自定义名称,但在一般情况下,建议使用默认的 conanfile.py 名称,该名称位于存储库根目录中,因此用户可以进行直接的 git clone ... `` + ``conan install .

另一种可能性是根本没有 conanfile,并在命令行中直接定义要安装的需求

# Install the zlib/1.2.13 library
$ conan install --requires=zlib/1.2.13
# Install the zlib/1.2.13 and bzip2/1.0.8 libraries
$ conan install --requires=zlib/1.2.13 --requires=bzip2/1.0.8
# Install the cmake/3.23.5 and ninja/1.11.0 tools
$ conan install --tool-requires=cmake/3.23.5 --tool-requires=ninja/1.11.0
# Install the zlib/1.2.13 library and ninja/1.11.0 tool
$ conan install --requires=zlib/1.2.13 --tool-requires=ninja/1.11.0

在一般情况下,建议使用 conanfile 而不是在命令行中定义内容。

配置文件、设置、选项、配置

有几个参数用于定义将要使用的有效配置文件,包括 “build” 和 “host” 上下文。

默认情况下,参数引用 “host” 上下文,因此 --settings:host, -s:h--settings, -s 完全等效。 此外,默认情况下,conan install 命令将对 “build” 和 “host” 上下文使用 default 配置文件。 这意味着如果尚未创建带有 “default” 名称的配置文件,则会出错。

配置文件的多个定义可以作为参数传递,它们将从左到右复合(右侧具有最高优先级)

# The values of myprofile3 will have higher priority
$ conan install . -pr=myprofile1 -pr=myprofile2 -pr=myprofile3

注意

在各种位置搜索配置文件,有关更多信息,请参见此处

如果在命令行中提供了 settingsoptionsconf 的任何值,它们会创建一个配置文件,该配置文件与提供的其他 -pr(如果未指定,则为 “default” 配置文件)复合,优先级更高,无论参数的顺序如何。

# the final "host" profile will always be build_type=Debug, even if "myprofile"
# says "build_type=Release"
$ conan install . -pr=myprofile -s build_type=Debug

生成器和部署器

-g 参数允许在命令行中定义要使用的不同内置生成器

$ conan install --requires=zlib/1.2.13 -g CMakeDeps -g CMakeToolchain

请注意,在一般情况下,推荐的方法是在 conanfile 中定义 generators,并且仅对于 --requires 用例,它才更需要作为命令行参数。

生成器旨在为构建系统创建文件以定位依赖项,而 deployers 的主要用例是将文件从 Conan 缓存复制到用户空间,并对依赖关系图执行任何其他自定义操作,例如收集许可证、生成报告、将二进制文件部署到系统等。 deployers 的语法是

# does a full copy of the dependencies binaries to the current user folder
$ conan install . --deployer=full_deploy

有 3 个内置的 deployers

  • full_deploy 在本地文件夹中完整复制依赖项二进制文件,并具有最小的文件夹结构,以避免不同软件包的文件和工件之间的冲突

  • direct_deploy 仅复制直接依赖项,但不包括传递依赖项。

  • runtime_deploy 将依赖项的所有共享库和可执行文件(例如 .so.dll.dylib 文件)部署到平面目录结构中。 默认情况下会复制库的符号链接,并且可以通过布尔配置 tools.deployer:symlinks 进行管理。(自 Conan 2.5.0 起可用)

某些生成器可能具有重新定义目标 “package folder” 的能力。 这意味着如果使用了其他生成器(如 CMakeDeps),并且它指向软件包,则它将指向本地部署的副本,而不是 Conan 缓存中的原始软件包。 请参阅 创建用于开发人员使用的与 Conan 无关的依赖项部署 中的完整示例。

也可以编写自定义用户部署器,这是一个强大的扩展点。 在 Deployers 中阅读有关自定义部署器的更多信息。

也可以使用 --deployer-package 调用包 recipe 的 deploy() 方法

# Execute deploy() method of every recipe that defines it
$ conan install --requires=pkg/0.1 --deployer-package="*"
# Execute deploy() method only for "pkg" (any version) recipes
$ conan install --requires=pkg/0.1 --deployer-package="pkg/*"
# Execute deploy() method for all packages except the "zlib" (transitive dep) one
$ conan install --requires=pkg/0.1 --deployer-package="*" --deployer-package="~zlib/*"

--deployer-package 参数是一个模式,它接受多个值,所有与任何定义的模式匹配的包引用都将执行其 deploy() 方法。 这包括否定模式,例如 --deployer-package=~pkg/* 将对除 pkg recipe 的包之外的所有包执行 deploy() 方法。 --deployer-folder 参数也会影响此部署的输出位置。 请参阅 deploy() 方法

如果多个部署的包部署到同一位置,则它们有责任在具有相同文件名时不要相互覆盖其二进制文件。 例如,如果多个包 deploy() 一个名为 “License.txt” 的文件,则每个 recipe 负责创建一个具有包名称和/或版本的中间文件夹,使其唯一,因此其他 recipes 的 deploy() 方法不会覆盖先前部署的 “License.txt” 文件。

名称、版本、用户、频道

conan install 命令为 --name, --version, --user, --channel 提供了可选参数。 在大多数情况下,这些参数可能不是必需的。 对于 conanfile.txt 永远不需要,对于 conanfile.py 仅在 recipe 中未定义它们的情况下才需要

from conan import ConanFile
from conan.tools.scm import Version

class Pkg(ConanFile):
    name = "mypkg"

    def requirements(self):
        if Version(self.version) >= "3.23":
            self.requires("...")
# If we don't specify ``--version``, it will be None and it will fail
$ conan install . --version=3.24

锁定文件

conan install 命令具有多个参数来加载和生成锁定文件。 默认情况下,如果 conan.lock 文件位于 recipe 旁边,或者如果未提供路径,则位于当前工作目录中,则将用作输入锁定文件。

默认情况下,锁定文件是严格的,这意味着如果存在某些 requires 并且无法在锁定文件中找到匹配的锁定引用,则会出错并停止。 对于预计锁定文件不完整的情况,因为可能存在新的依赖项,可以使用 --lockfile-partial 参数。

默认情况下,conan install 不会生成输出锁定文件,但如果提供了 --lockfile-out 参数,指向文件名,例如 --lockfile-out=result.lock,则将从当前依赖关系图生成锁定文件。 如果提供了 --lockfile-clean 参数,则当前依赖关系图中未使用的所有版本和修订版本将从生成的锁定文件中删除。

假设我们已经有一个 conan.lock 输入锁定文件,但是我们刚刚添加了一个新的 requires = "newpkg/1.0" 到一个新的依赖项。 我们可以解析依赖项,锁定所有先前锁定的版本,同时允许解析新的依赖项(以前未在锁定文件中存在),并将其存储在一个新的位置,或者覆盖现有的锁定文件。

# --lockfile=conan.lock is the default, not necessary
$ conan install . --lockfile=conan.lock --lockfile-partial --lockfile-out=conan.lock

此外,大多数锁定文件操作最好通过 conan lock 命令进行管理。

另请参阅

更新

conan install 命令有一个 --update 参数,它会强制重新评估依赖关系图中所选的项目,如果使用版本范围,则允许将依赖项更新到最新版本,或者在给定的锁定文件中未锁定这些版本时,更新到同一版本的最新修订版。 传递 --update 将检查依赖关系图中的每个包,但也可以将包名称传递给 --update 参数(可以通过不同的名称多次添加到命令中),仅更新这些包,从而避免重新评估整个图。

$ conan install . --update  # Update all packages in the graph
$ conan install . --update=openssl  # Update only the openssl package
$ conan install . --update=openssl --update=boost  # Update both openssl and boost packages

请注意,--update 参数将在命令中指定的所有远程仓库中查找可能的较新版本,并且不会在找到第一个较新版本时停止。

构建模式

conan install --build=<mode> 参数控制从源代码构建包的行为。 默认行为是在没有现有二进制文件时失败,并显示“缺少二进制文件”错误消息,除非对于定义了 build_policy = "missing" 策略的包,但可以使用 --build 参数更改此行为。

可能的值是:

--build=never      Disallow build for all packages, use binary packages or fail if a binary
                   package is not found, it cannot be combined with other '--build' options.
--build=missing    Build packages from source whose binary package is not found.
--build=cascade    Build packages from source that have at least one dependency being built from
                   source.
--build=[pattern]  Build packages from source whose package reference matches the pattern. The
                   pattern uses 'fnmatch' style wildcards, so '--build="*"' will build everything
                   from source.
--build=~[pattern] Excluded packages, which will not be built from the source, whose package
                   reference matches the pattern. The pattern uses 'fnmatch' style wildcards.
--build=missing:[pattern] Build from source if a compatible binary does not exist, only for
                          packages matching pattern.
--build=compatible:[pattern] (Experimental) Build from source if a compatible binary does not
                             exist, and the requested package is invalid, the closest package
                             binary following the defined compatibility policies (method and
                             compatibility.py)

--build=never 策略可用于强制永不从源代码构建,即使对于定义了 build_policy = "missing" 策略的包也是如此。

--build=compatible:[pattern] 是一种**实验性**的新模式,允许使用与当前配置不同的配置构建缺少的二进制文件。 例如,如果当前配置文件具有 compiler.cppstd=14,但某些包引发“无效”配置错误,因为它至少需要 compiler.cppstd=17,并且二进制兼容性(例如在 compatibility.py 插件中定义)允许将其作为兼容的二进制文件,那么 Conan 将从源代码构建该依赖项包,并应用 compiler.cppstd=17

--build=[pattern] 使用模式,因此它应该使用类似 --build="zlib/*" 的东西来匹配 zlib 包的任何版本,因为执行 --build=zlib 将不起作用。

注意

最佳实践

使用 --build="*" 或任何其他 --build="pkg/*" 或类似模式强制重建现有二进制文件不是推荐的做法。 如果二进制文件已经存在,则没有理由从源代码重建它。 CI 管道应特别注意不要这样做,并且通常更推荐使用 --build=missing--build=missing:[pattern]

--build=cascade 模式部分是旧的,在大多数情况下不应使用。 package_id 计算应该是决定需要构建的内容的驱动因素。 此模式仅在特殊情况下(例如从损坏的系统恢复)保留在 Conan 2 中,但不建议用于正常的生产用途。