如果我们希望包配方与打包的源代码位于同一存储库中,这可能是一种合适的方法。
首先,让我们获取初始源代码并创建基本包配方:
$ conan new hello/0.1 -t -s
这将生成以下文件:
conanfile.py
src
CMakeLists.txt
hello.cpp
hello.h
test_package
CMakeLists.txt
conanfile.py
example.cpp
将使用之前的示例(hello)源代码创建src文件夹,现在让我们来看看conanfile.py:
from conans import ConanFile, CMake
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
license = "<Put the package license here>"
url = "<Package recipe repository url here, for issues about the package>"
description = "<Description of hello here>"
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False]}
default_options = {"shared": False}
generators = "cmake"
exports_sources = "src/*"
def build(self):
cmake = CMake(self)
cmake.configure(source_folder="src")
cmake.build()
# Explicit way:
# self.run('cmake "%s/src" %s' % (self.source_folder, cmake.command_line))
# self.run("cmake --build . %s" % cmake.build_config)
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.dll", dst="bin", keep_path=False)
self.copy("*.dylib*", dst="lib", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["hello"]
有两个重要的变化:
- 添加了
exports_source
字段,指示Conan将本地src文件夹中的所有文件复制到包配方中。 - 删除了
source()
方法,因为不再需要检索外部源。
另外,您可以注意到两行CMake:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
它们不会添加到包配方中,因为它们可以直接添加到 src/CMakeLists.txt 文件中。
如前所述,只需为 user 和 channel demo/testing 创建包:
$ conan create . demo/testing
...
hello/0.1@demo/testing test package: Running test()
Hello world Release!