conan new

从预定义的或用户定义的模板创建新的配方(包含 conanfile.py 和其他相关文件)。

conan new

$ conan new -h
usage: conan new [-h] [--out-file OUT_FILE]
                 [-v [{quiet,error,warning,notice,status,verbose,debug,v,trace,vv}]]
                 [-cc CORE_CONF] [-d DEFINE] [-f] [-o OUTPUT]
                 template

Create a new example recipe and source files from a template.

positional arguments:
  template              Template name, either a predefined built-in or a user-
                        provided one. Available built-in templates: basic,
                        cmake_lib, cmake_exe, header_lib, meson_lib,
                        meson_exe, msbuild_lib, msbuild_exe, bazel_lib,
                        bazel_exe, autotools_lib, autotools_exe, premake_lib,
                        premake_exe, local_recipes_index, workspace. E.g.
                        'conan new cmake_lib -d name=hello -d version=0.1'.
                        You can define your own templates too by inputting an
                        absolute path as your template, or a path relative to
                        your conan home folder.

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 [{quiet,error,warning,notice,status,verbose,debug,v,trace,vv}]
                        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
  -d DEFINE, --define DEFINE
                        Define a template argument as key=value, e.g., -d
                        name=mypkg
  -f, --force           Overwrite file if it already exists
  -o OUTPUT, --output OUTPUT
                        Output folder for the generated files

conan new 命令在当前工作目录中创建一个新的配方,以及额外的示例文件,例如 CMakeLists.txttest_package 文件夹(如果需要),以便用作您自己项目的基础,或帮助进行调试过程。

请注意,每个模板都有一些必需和一些[可选]用户定义的变量,用于自定义生成的文件。

可用的模板有

  • basic:创建一个简单的配方,其中包含一些示例代码和有用的注释,是避免编写样板代码的一个很好的起点。

    它的变量是:[name], [version], [description], [requires1, requires2, …], [tool_requires1, tool_requires2, …]

  • alias:创建定义目标配方别名的最小配方

    它的变量是:name, [version], target

  • cmake_lib:创建一个 cmake 库目标,该目标定义一个名为 name 的函数,它会将有关编译环境的一些信息打印到 stdout。您可以以以下形式向此模板添加依赖项:

    conan new cmake_lib -d name=ai -d version=1.0 -d requires=math/3.14 -d requires=magic/0.0

    这将为 math/3.14magic/0.0 添加依赖项到 requirements() 方法,将在 CMake 中添加必要的 find_package``s ,并在 生成的 ai() 函数中添加对 math()magic() 的调用。

    它的变量是:name, version, [requires1, requires2, …], [tool_requires1, tool_requires2, …]

  • cmake_exe:创建一个 cmake 可执行文件目标,该目标定义一个名为 name 的函数,它会将有关编译环境的一些信息打印到 stdout。您可以以以下形式向此模板添加依赖项:

    conan new cmake_exe -d name=game -d version=1.0 -d requires=math/3.14 -d requires=ai/1.0

    这将为 math/3.14ai/1.0 添加依赖项到 requirements() 方法,将在 CMake 中添加必要的 find_package``s ,并在 生成的 game() 函数中添加对 math()ai() 的调用。

    它的变量是:name, version, [requires1, requires2, …], [tool_requires1, tool_requires2, …]

  • header_lib 创建一个仅包含头文件的库,该库定义一个名为 name 的函数,它会将一些输出打印到 stdout。

    您可以以以下形式向此模板添加依赖项:

    conan new header_lib -d name=foo -d version=1.0 -d requires=math/3.14 -d requires=magic/0.0

    这将为 math/3.14ai/1.0 添加依赖项到 requirements() 方法,并添加对 math()ai() 在生成的 foo() 函数中的调用。

    它的变量是:name, version, [requires1, requires2, …]

  • autotools_lib:创建一个 Autotools 库。

    它的变量是:name, version

  • autotools_exe:创建一个 Autotools 可执行文件

    它的变量是:name, version

  • bazel_libBazel 集成 BazelDeps, BazelToolchain, Bazel 处于实验阶段。创建一个 Bazel 库。

    它的变量是:name, version

  • bazel_exeBazel 集成 BazelDeps, BazelToolchain, Bazel 处于实验阶段。创建一个 Bazel 可执行文件

    它的变量是:name, version

  • meson_lib:创建一个 Meson 库。

    它的变量是:name, version

  • meson_exe:创建一个 Meson 可执行文件

    它的变量是:name, version

  • msbuild_lib:创建一个 MSBuild 库。

    它的变量是:name, version

  • msbuild_exe:创建一个 MSBuild 可执行文件

    它的变量是:name, version

  • workspace:创建一个现成的工作区,其中包含三个可编辑项:libalibb(需要 liba)和 app1(需要 libb),以及顶层 CMakeLists.txtconanws.ymlconanws.py,这些文件描述了工作区。

    您可以传递一个 requires 变量,例如 -d requires=mymath/0.1,以将外部依赖项添加到 liba

默认情况下,如果用户未提供,所有 nameversion 变量都设置为 mypkg0.1

警告

预定义内置模板的输出不稳定。它可能会在未来的版本中更改,以适应最新的工具或最佳实践。

示例

$ conan new basic

生成一个基本的 conanfile.py,该文件不实现任何自定义功能

$ conan new basic -d name=mygame -d requires=math/1.0 -d requires=ai/1.3

生成一个用于 mygameconanfile.py,该文件依赖于包 math/1.0ai/1.3

$ conan new cmake_lib

创建一个具有默认包 name = "mypkg" 和默认包版本 version = "0.1" 的基本 CMake 库

$ conan new cmake_exe -d name=game -d version=1.0 -d requires=math/3.14 -d requires=ai/1.0

生成必要的文件以创建 CMake 可执行文件目标。这将为 math/3.14ai/1.0 添加依赖项到 requirements() 方法,将在 CMake 中添加必要的 find_package,并在生成的 game() 函数中添加对 math()ai() 的调用。

自定义模板

也可以创建您自己的模板。Conan 主目录中的模板应位于 templates/command/new 文件夹中,每个模板应具有一个与模板同名的文件夹。如果我们在 templates/command/new/mytemplate 文件夹中创建,则该命令将使用以下方式调用:

$ conan new mytemplate

与 Conan 主目录中的其他文件一样,您可以使用 conan config install <url> 管理这些模板,将它们放在 git 仓库或 http 服务器中,并与您的团队共享。也可以从任何文件夹使用模板,只需将完整路径传递给模板即可 conan new <full_path>,但通常以在 Conan 主目录中管理它们更方便。

该文件夹可以包含任意数量的文件。可以使用 Jinja2 语法对文件名和文件内容进行模板化。命令 -d/--define 参数将定义模板的 key=value 输入。

文件内容如下所示(Jinja2 语法):

# File "templates/command/new/mytemplate/conanfile.py"
from conan import ConanFile

class Conan(ConanFile):
    name = "{{name}}"
    version = "{{version}}"
    license = "{{license}}"

并且需要传递这些值

$ conan new mytemplate -d name=pkg -d version=0.1 -d license=MIT

它将在当前文件夹中生成一个文件

# File "<cwd>/conanfile.py"
from conan import ConanFile

class Conan(ConanFile):
    name = "pkg"
    version = "0.1"
    license = "MIT"

有一些特殊的 -d/--defines 名称。 name 始终是必需的。 conan_version 定义始终会自动定义。如果存在 requirestool_requires 定义,它们将自动转换为列表。 package_name 始终被定义,默认等于 name

对于参数化的文件名,文件名本身支持 Jinja2 语法。例如,如果我们将一个名为 {{name}} 的文件(带有括号)存储在模板文件夹 templates/command/new/mytemplate/ 中,而不是上面的 conanfile.py

文件:“templates/command/new/mytemplate/{{name}}”
{{contents}}

然后,执行

$ conan new mytemplate -d name=file.txt -d contents=hello!

将在当前目录中创建一个名为 file.txt 的文件,其中包含字符串 hello!

如果模板中存在不应使用 Jinja2 渲染的文件,例如图像文件,则应将它们的文件名添加到模板目录中的名为 not_templates 的文件中,每行一个文件名。因此,我们可以有一个包含

templates/command/new/mytemplate
                         |- not_templates
                         |- conanfile.py
                         |- image.png
                         |- image2.png

并且 not_templates 包含字符串 *.png,那么 conan new mytemplate ... 将仅通过 Jinja2 渲染 conanfile.py,但两个图像将按原样复制。