本文档翻译自:https://docs.conan.io/en/latest/creating_packages/package_repo.html#exporting-the-sources-with-the-recipe-exports-sources

    如果我们希望包配方与打包的源代码位于同一存储库中,这可能是一种合适的方法。
    首先,让我们获取初始源代码并创建基本包配方:

    1. $ conan new hello/0.1 -t -s

    这将生成以下文件:

    1. conanfile.py
    2. src
    3. CMakeLists.txt
    4. hello.cpp
    5. hello.h
    6. test_package
    7. CMakeLists.txt
    8. conanfile.py
    9. example.cpp

    将使用之前的示例(hello)源代码创建src文件夹,现在让我们来看看conanfile.py:

    1. from conans import ConanFile, CMake
    2. class HelloConan(ConanFile):
    3. name = "hello"
    4. version = "0.1"
    5. license = "<Put the package license here>"
    6. url = "<Package recipe repository url here, for issues about the package>"
    7. description = "<Description of hello here>"
    8. settings = "os", "compiler", "build_type", "arch"
    9. options = {"shared": [True, False]}
    10. default_options = {"shared": False}
    11. generators = "cmake"
    12. exports_sources = "src/*"
    13. def build(self):
    14. cmake = CMake(self)
    15. cmake.configure(source_folder="src")
    16. cmake.build()
    17. # Explicit way:
    18. # self.run('cmake "%s/src" %s' % (self.source_folder, cmake.command_line))
    19. # self.run("cmake --build . %s" % cmake.build_config)
    20. def package(self):
    21. self.copy("*.h", dst="include", src="src")
    22. self.copy("*.lib", dst="lib", keep_path=False)
    23. self.copy("*.dll", dst="bin", keep_path=False)
    24. self.copy("*.dylib*", dst="lib", keep_path=False)
    25. self.copy("*.so", dst="lib", keep_path=False)
    26. self.copy("*.a", dst="lib", keep_path=False)
    27. def package_info(self):
    28. self.cpp_info.libs = ["hello"]

    有两个重要的变化:

    • 添加了exports_source字段,指示Conan将本地src文件夹中的所有文件复制到包配方中。
    • 删除了source() 方法,因为不再需要检索外部源。

    另外,您可以注意到两行CMake:

    1. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
    2. conan_basic_setup()

    它们不会添加到包配方中,因为它们可以直接添加到 src/CMakeLists.txt 文件中。
    如前所述,只需为 user 和 channel demo/testing 创建包:

    1. $ conan create . demo/testing
    2. ...
    3. hello/0.1@demo/testing test package: Running test()
    4. Hello world Release!