如何配置conanfile.py使用本地已克隆的源码文件
如何配置conanfile.py使用本地源码文件
核心思路
默认情况下,Conan会将conanfile.py所在目录的本地文件作为项目源码,无需额外克隆操作。只需移除示例中用于远程拉取源码的配置,并确保本地源码目录结构正确即可。
具体步骤
移除SCM配置(若存在)
多数示例中的scm字段用于从远程仓库克隆源码,直接删除该部分即可避免不必要的克隆行为:# 删掉这类SCM配置 # scm = { # "type": "git", # "url": "https://github.com/xxx/xxx.git", # "revision": "master" # }确保源码目录结构正确
你的源码文件(如CMakeLists.txt、src/、include/等)需与conanfile.py处于同一目录(或其子目录),Conan默认会扫描当前目录下的文件(排除Conan生成的build/、conan/等文件夹)。在build()方法中直接引用本地源码
根据Conan版本不同,配置方式略有差异:Conan 1.x示例
from conans import ConanFile, CMake class MyProjectConan(ConanFile): name = "my_project" version = "1.0.0" settings = "os", "compiler", "build_type", "arch" generators = "cmake" def build(self): cmake = CMake(self) # 直接指向本地的CMakeLists.txt所在路径 cmake.configure(source_folder=".") cmake.build()Conan 2.x示例
from conan import ConanFile from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps class MyProjectConan(ConanFile): name = "my_project" version = "1.0.0" settings = "os", "compiler", "build_type", "arch" generators = "CMakeDeps", "CMakeToolchain" def build(self): cmake = CMake(self) # Conan 2.x默认使用当前目录作为源码目录,无需额外指定 # 若源码在子目录,可添加参数:cmake.configure(source_folder="./src") cmake.configure() cmake.build()可选:过滤需要包含的源码文件
若不想将当前目录下所有文件都纳入构建,可通过配置指定要包含的文件/目录:Conan 1.x
exports_sources = "CMakeLists.txt", "src/*", "include/*"Conan 2.x
from conan.tools.files import copy def export_sources(self): copy(self, "CMakeLists.txt", self.recipe_folder, self.export_sources_folder) copy(self, "src/*", self.recipe_folder, self.export_sources_folder) copy(self, "include/*", self.recipe_folder, self.export_sources_folder)
内容的提问来源于stack exchange,提问作者montjet
相关产品推荐
相关产品推荐

