conan lock create

$ conan lock create -h
usage: conan lock create [-h] [--out-file OUT_FILE] [-v [V]] [-cc CORE_CONF]
                         [--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]
                         [--build-require]
                         [path]

Create a lockfile from a conanfile or a reference.

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
  --out-file OUT_FILE   Write the output of the command to the specified file
                        instead of stdout.
  -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
  --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
  --build-require       Whether the provided reference is a build-require

conan lock create 命令为 path--requires 中指定的 recipe 或引用创建 lockfile。此命令将计算依赖关系图,评估哪些二进制文件存在或需要构建,但它不会尝试安装或从源代码构建这些二进制文件。在这方面,它等同于 conan graph info 命令。此命令接受的大多数参数与 conan graph info (以及 conan install, conan create) 相同,因为 conan lock create 为给定配置创建或更新 lockfile。

可以从头开始创建 lockfile,从本地 conanfile 或 requires 计算新的依赖关系图,例如对于这个 conanfile.txt

conanfile.txt
[requires]
fmt/9.0.0

[tool_requires]
cmake/3.23.5

我们可以运行

$ conan lock create .

$ cat conan.lock
{
    "version": "0.5",
    "requires": [
        "fmt/9.0.0#ca4ae2047ef0ccd7d2210d8d91bd0e02%1675126491.773"
    ],
    "build_requires": [
        "cmake/3.23.5#5f184bc602682bcea668356d75e7563b%1676913225.027"
    ],
    "python_requires": []
}

conan lock create 接受 --lockfile 输入 lockfile (如果找到默认的 conan.lock,它将自动使用),然后它将在 --lockfile-out 中添加新信息 (默认情况下,也是 conan.lock)。例如,如果我们更改上面的 conanfile.txt,移除 tool_requires,将 fmt 更新到 9.1.0 并添加一个新的依赖项到 zlib/1.2.13

conanfile.txt
[requires]
fmt/9.1.0
zlib/1.2.13

[tool_requires]

我们将看到 conan lock create 如何使用新配置扩展现有 lockfile,但它不会从中删除未使用的版本或包

$ conan lock create .  # will use the existing conan.lock as base, and rewrite it
# use --lockfile and --lockfile-out to change that behavior

$ cat conan.lock
{
  "version": "0.5",
  "requires": [
      "zlib/1.2.13#13c96f538b52e1600c40b88994de240f%1667396813.733",
      "fmt/9.1.0#e747928f85b03f48aaf227ff897d9634%1675126490.952",
      "fmt/9.0.0#ca4ae2047ef0ccd7d2210d8d91bd0e02%1675126491.773"
  ],
  "build_requires": [
      "cmake/3.23.5#5f184bc602682bcea668356d75e7563b%1676913225.027"
  ],
  "python_requires": []
}

此行为对于能够捕获可能具有不同依赖关系图的多个不同配置(Linux/Windows、shared/static、Debug/Release 等)非常重要。请参阅 lockfiles 教程,以阅读有关多配置 lockfile 的更多信息。

如果我们想修剪未使用的版本和包,我们可以使用 --lockfile-clean 参数强制执行

$ conan lock create . --lockfile-clean
# will use the existing conan.lock as base, and rewrite it, cleaning unused versions
$ cat conan.lock
{
    "version": "0.5",
    "requires": [
        "zlib/1.2.13#13c96f538b52e1600c40b88994de240f%1667396813.733",
        "fmt/9.1.0#e747928f85b03f48aaf227ff897d9634%1675126490.952"
    ],
    "build_requires": [],
    "python_requires": []
}

另请参阅

lockfiles 教程部分 提供了更多关于 lockfile 的示例和动手解释。