现在我们就利用之前学习到的conan的使用方法,将chromium中的base库打包成一个conan包。
1. 准备源码
chromium本身是通过gn来编译的,这个目前conan并不支持。所以需要把它从chromium代码中抽离出来,这是一个复杂的工作。所幸的是,已有大神做了做方面工作:https://github.com/shaoyuan1943/chromium-base 。
拿到源码,我们当然需要在各个平台进行验证,看是否能成功编译。
从README中了解到:
- 编译Windows版本,需直接使用sln工程文件进行编译;
- 编译Linux版本,需使用作者写好的编译sh脚本。
在Linux上单独使用CMakeLists.txt进行编译是无法编译通过的,查看它的实现,发现里面由很多非预定义的变量。看了 build_base_linux.sh脚本后,确定该脚本主要功能就是设置这些变量:
因此,确定我们使用conan编译的方案:
- windows上直接使用MSBuild进行编译;
- Linux上使用cmake进行编译,但是要设置好一些变量。
2. 编写conanfile.py
经过反复测试,conanfile.py的内容如下:
#!/usr/bin/python # -*- coding: UTF-8 -*- from conans import ConanFile, MSBuild, CMake, tools class ChromiumbaseConan(ConanFile): name = "chromium-base" version = "1.0.1" license = "" author = "google" url = "https://github.com/shaoyuan1943/chromium-base" description = "Base library from chromium source code" topics = ("chromium", "base") settings = "os", "compiler", "build_type", "arch" options = {"shared": [True, False]} default_options = {"shared": True} def configure(self): if self.settings.os == "Windows": self.generators = "visual_studio", # Trailing comma, or ["visual_studio"] else: self.generators = "cmake", # Note the trailing comma def source(self): self.run("git clone -b conan https://github.com/243286065/chromium-base.git") if self.settings.os == "Linux": # 解决本身的依赖问题 tools.replace_in_file("chromium-base/src/CMakeLists.txt", "project(base)", '''project(base) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup()''') # 按照要求修改CMakeLists.txt tools.replace_in_file("chromium-base/src/CMakeLists.txt", '''set(BASE_INCLUDE_PLATFORM_DIRECTORIES /usr/local/include/glib-2.0 /usr/local/lib/glib-2.0/include # for glibconfig.h )''', "") # CMakeLists.txt默认是编译shared版本,需要根据配置自动选择 if self.options.shared == False: tools.replace_in_file("chromium-base/src/CMakeLists.txt", "set(BASE_BUILD_CONFIGURATION_TYPE SHARED)", '''set(BASE_BUILD_CONFIGURATION_TYPE)''') def requirements(self): if self.settings.os == "Linux": self.requires("glib/2.56.1@bincrafters/stable") self.requires("libevent/2.1.10@bincrafters/stable") def build(self): if self.settings.os == "Linux": cmake = CMake(self) cmake.definitions["CMAKE_BUILD_TYPE"] = self.settings.build_type if self.settings.arch in ["x86_64", "x64"]: cmake.definitions["BASE_ARCH_PLATFORM"] = "x64" else: cmake.definitions["BASE_ARCH_PLATFORM"] = "x86" cmake.definitions["BASE_BUILD_PLATFORM"] = self.settings.os cmake.configure(source_folder="chromium-base/src") cmake.build() elif self.settings.os == "Windows": msbuild = MSBuild(self) msbuild.build("chromium-base/src/sln/base.sln", targets=["base"], build_type=self.settings.build_type) def package(self): self.copy("*", dst="include/base", src="chromium-base/src/base") 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 = ["chromium-base"]
configure
函数解决指定编译器的配置问题;source
函数是准备源码,这里同样是从github上下载。对于Linux平台,提供替换字符串的方法,修改CMakeLists.txt文件,为编译做好准备。requirements
是解决Linux上依赖glib的问题;build
即进行编译,在Linux上需要设置好一些变量;Windows上需要指定编译目标工程;package
即做打包工作,把库文件和头文件导出;package_info
: 声明包的信息。
window上由于是指定的sln文件,目前我还没想到怎么修改它是编译动态库还是静态库。
3. 打包
编写好conanfile.py后,就可以进行打包:
conan create . google/stable
打包成功后,上传到服务器:
conan upload chromium-base/1.0.1@google/stable -r develope --all
注意chromium-base依赖C++17,因此在Linux上重新编译包时,请带上 -s compiler.libcxx=libstdc++11 参数
4. 测试
在Ubuntu和Windows上测试通过。
项目地址:https://github.com/243286065/chromium-base/tree/conan
来源:https://www.cnblogs.com/xl2432/p/11925860.html