如何在conanfile.py安装阶段修改Conan包解决UE4与OpenCV冲突
解决UE4与OpenCV 3.4.0的int64/uint64定义冲突(Conan配方实现)
问题背景
我们的项目基于Unreal Engine 4.26.2和OpenCV 3.4.0(需保留该旧版本),正在将原有OpenCV构建Shell脚本转换为Conan配方。构建最终应用时,遇到OpenCV与UE4的int64和uint64定义冲突的问题。
原有Shell脚本通过修补OpenCV安装目录下的头文件解决该冲突,具体补丁内容如下:
--- opencv2/core/hal/interface.old.h 2019-11-26 12:34:40.260333132 +0300 +++ opencv2/core/hal/interface.h 2019-11-26 17:13:27.232585976 +0300 @@ -58,8 +58,8 @@ # define CV_BIG_INT(n) n##I64 # define CV_BIG_UINT(n) n##UI64 #else - typedef int64_t int64; - typedef uint64_t uint64; +// typedef int64_t int64; +// typedef uint64_t uint64; # define CV_BIG_INT(n) n##LL # define CV_BIG_UINT(n) n##ULL #endif
原有Shell脚本核心逻辑
原有脚本先克隆代码、编译安装OpenCV,最后对安装目录的头文件打补丁:
git clone --depth=1 https://github.com/opencv/opencv.git -b 3.4.0 git clone --depth=1 https://github.com/opencv/opencv_contrib.git -b 3.4.0 mkdir build cd build cmake .. ..... make make install patch -N -p4 $INSTALL_DIR/include/opencv2/core/hal/interface.h $SCRIPT_DIR/opencv_02.patch
现有Conan配方及修改方案
现有Conan配方已实现克隆、编译安装逻辑,需在cmake.install()后添加补丁操作,对安装目录的头文件进行修改。
修改后的完整Conan配方代码
from conans import ConanFile, CMake, tools import os class OpenCVUE4Conan(ConanFile): name = "opencv-ue4" version = "3.4.0" url = "" description = "OpenCV custom build for UE4" license = "BSD" settings = "os", "compiler", "build_type", "arch" generators = "cmake" # 新增冲突修复补丁到导出列表 exports_sources = 'patches/cmakes.patch', 'patches/check_function.patch', 'patches/opencv_02.patch' def requirements(self): self.requires("ue4util/ue4@adamrehn/profile") self.requires("zlib/ue4@adamrehn/{}".format(self.channel)) self.requires("UElibPNG/ue4@adamrehn/{}".format(self.channel)) def cmake_flags(self): flags = [ "-DOPENCV_ENABLE_NONFREE=OFF", # 其他编译参数 ] return flags def source(self): self.run("git clone --depth=1 https://github.com/opencv/opencv.git -b {}".format(self.version)) self.run("git clone --depth=1 https://github.com/opencv/opencv_contrib.git -b {}".format(self.version)) def build(self): # 应用编译前的补丁,跳过冲突修复补丁 for p in self.exports_sources: if p.endswith(".patch") and not p.endswith("opencv_02.patch"): tools.patch(base_path='opencv', patch_file=p, fuzz=True) cmake = CMake(self) cmake.configure(source_folder="opencv", args=self.cmake_flags()) cmake.build() cmake.install() # 对安装目录的头文件应用冲突修复补丁 install_include_dir = os.path.join(self.package_folder, "include") target_header = os.path.join(install_include_dir, "opencv2", "core", "hal", "interface.h") tools.patch(file_path=target_header, patch_file="patches/opencv_02.patch", fuzz=True)
修改说明
- 新增补丁文件导出:将
opencv_02.patch添加到exports_sources列表,确保Conan能读取到该补丁文件。 - 区分补丁时机:编译前只应用原有两个补丁,避免提前修改源码影响编译流程;安装完成后再对安装目录的头文件打冲突修复补丁,和原有Shell脚本逻辑完全对齐。
- 定位目标文件:通过
self.package_folder获取Conan的安装目录,精准定位到需要修改的interface.h头文件,执行补丁操作。
内容的提问来源于stack exchange,提问作者wl2776
相关产品推荐
相关产品推荐

