11.4 以Conda包的形式发布一个简单的项目

NOTE:此示例代码可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-11/recipe-04 中找到。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

虽然PyPI是发布Python包的标准平台,但Anaconda (https://anaconda.org )更为可能更为流行,因为它不仅允许使用Python接口发布Python或混合项目,还允许对非Python项目进行打包和依赖关系管理。这个示例中,我们将为一个非常简单的C示例项目准备一个Conda包,该项目使用CMake配置和构建,除了C之外没有依赖关系。下一个示例中,我们将会来看看一个更复杂的Conda包。

准备工作

我们的目标是打包以下示例代码(example.cpp):

  1. #include <iostream>
  2. int main() {
  3. std::cout << "hello from your conda package!" << std::endl;
  4. return 0;
  5. }

具体实施

  1. CMakeLists.txt文件给出了最低版本要求、项目名称和支持的语言:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-04 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
  1. 使用example.cpp构建hello-conda可执行目标:
    add_executable(hello-conda "")
    target_sources(hello-conda
    PRIVATE
       example.cpp
    )
    
  1. 使用CMakeLists.txt定义安装目标:
    nstall(
    TARGETS
       hello-conda
    DESTINATION
       bin
    )
    
  1. 将在一个名为meta.yaml的文件中,对Conda包进行描述。我们将把它放在conda-recipe目录下,文件结构如下:
    .
    ├── CMakeLists.txt
    ├── conda-recipe
    │    └── meta.yaml
    └── example.cpp
    
  1. meta.yaml包含如下内容: ```yaml package: name: conda-example-simple version: “0.0.0”

source: path: .. / # this can be changed to git-url

build: number: 0 binary_relocation: true script:

- cmake -H. -Bbuild_conda -G "${CMAKE_GENERATOR}" -DCMAKE_INSTALL_PREFIX=${PREFIX} # [not win]
- cmake -H. -Bbuild_conda -G "%CMAKE_GENERATOR%" -DCMAKE_INSTALL_PREFIX="%LIBRARY_PREFIX%" # [win]
- cmake - -build build_conda - -target install

requirements: build:

- cmake >=3.5
- { { compiler('cxx') } }

about: home: http://www.example.com license: MIT summary: “Summary in here …”



6. 
现在来构建包:
```shell
$ conda build conda-recipe
  1. 过程中屏幕上看到大量输出,但是一旦构建完成,就可以对包进行安装。首先,在本地进行测试:
    $ conda install --use-local conda-example-simple
    
  1. 现在准备测试安装包,打开一个新的终端(假设Anaconda处于激活状态),并输入以下内容: ```shell $ hello-conda

hello from your conda package!



9. 
测试成功后,再移除包装:
```shell
$ conda remove conda-example-simple

工作原理

CMakeLists.txt中,安装目标是这个示例的一个基本组件:

install(
  TARGETS
      hello-conda
  DESTINATION
      bin
  )

目标的二进制文件会安装到${CMAKE_INSTALL_PREFIX}/bin中。变量由Conda定义,并且构建步骤中定义在meta.yaml

build:
  number: 0
  binary_relocation: true
  script:
    - cmake -H. -Bbuild_conda -G "${CMAKE_GENERATOR}" -DCMAKE_INSTALL_PREFIX=${PREFIX} # [not win]
    - cmake -H. -Bbuild_conda -G "%CMAKE_GENERATOR%" -DCMAKE_INSTALL_PREFIX="%LIBRARY_PREFIX%" # [win]
    - cmake - -build build_conda - -target install

将安装目录设置为${prefix} (Conda的内部变量),然后构建并安装项目。调用构建目录命名为build_conda的动机与前面的示例类似:特定的构建目录名可能已经命名为build

更多信息

配置文件meta.yaml可为任何项目指定构建、测试和安装步骤。详情请参考官方文档:https://conda.io/docs/user-guide/tasks/build-packages/define-metadata.html

要将Conda包上传到Anaconda云,请遵循官方的Anaconda文档: https://docs.anaconda.com/anaconda-cloud/user-guide/

此外,也可以考虑将Miniconda,作为Anaconda的轻量级替代品:https://conda.io/miniconda.html