conan是个包管理工具,不仅仅支持cmake编译,还支持很多常用的构建工具如configure/make,msbuild,VisualStudo,meson,本文以NXP的Embedded RPC为例说明conan中如何使用make来构建项目。
NXP的eRPC
(Embedded RPC) 是用于多芯片嵌入式系统和异构多核 SoC 的开源远程过程调用 (RPC) 框架。目前只支持make构建,我的一个项目中用到了它,因为访问github比较慢,我fork了一份代码到国内码云仓库:https://gitee.com/l0km/erpc.git
以下的python脚本是为编译eRPC
编译器(erpcgen)而设计,使用了AutoToolsBuildEnvironment
对象基于命令执行make
来编译项目,
conanfile-erpcgen.py
from conans import ConanFile, AutoToolsBuildEnvironment,tools
import os
class ErpcgenConan(ConanFile):
name = "erpcgen"
version = "1.7.4-patch"
# Optional metadata
license = "BSD-2-Clause"
author = "guyadong 10km0811@sohu.com"
url = "https://gitee.com/l0km/erpcex"
description = "erpcgen base NXP Embedded RPC"
topics = ("embedded","erpc","nxp","erpcgen")
requires = "boost/[>=1.69.0]"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "erpc/*"
@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
if self.settings.compiler == "Visual Studio":
# 不支持Visual Studio编译,报错退出
raise Exception("Unsupport Visual Studio,plese use MinGw compiler for Windows")
def build_requirements(self):
if self._settings_build.os == "Windows":
self.build_requirements("winflexbison/2.5.24")
if not tools.get_env("CONAN_BASH_PATH"):
# Windows下需要在MSYS2环境编译
self.build_requirements("msys2/cci.latest")
else:
self.tool_requires("flex/2.6.4")
self.tool_requires("bison/3.7.6")
def package(self):
env_build = AutoToolsBuildEnvironment(self)
#env_build.fpic = True
# 安装路径
prefix = self.package_folder
print("BOOST_ROOT=" + self.deps_env_info["boost"].BOOST_ROOT)
if self._settings_build.os == "Windows":
# erpcgen中使用环境变量FLEX定义flex,BISON定义bison,
# 与winflexbison加载时定义的变量名不一样,所以这里要
# 定义环境变量 FLEX=$LEX,BISON=$YACC
os.environ['FLEX'] = self.deps_env_info["winflexbison"].LEX
os.environ['BISON'] = self.deps_env_info["winflexbison"].YACC
# 将 winflexbison的include文件夹添加到INCLUDE环境变量,否则会找不到
os.environ['INCLUDES'] = self.deps_cpp_info["winflexbison"].include_paths[0]
# 转为unix格式路径
prefix = tools.unix_path(self.package_folder)
else:
# 定义环境变量 FLEX=$LEX,BISON=$BISON_ROOT/bin/bison
os.environ['FLEX'] = self.deps_env_info["flex"].LEX
os.environ['BISON'] = self.deps_env_info["bison"].BISON_ROOT + "/bin/bison"
# 将 flex的include文件夹添加到INCLUDE环境变量,否则会使用系统安装的flex的include
os.environ['INCLUDES'] = self.deps_cpp_info["flex"].include_paths[0]
with tools.environment_append(env_build.vars):
# VERBOSE=1 编译时输出命令
self.run("VERBOSE=0 build="+ str(self.settings.build_type) + " make -C erpc/erpcgen install PREFIX=" + prefix, win_bash=True)
以上脚本的的完整代码代码位于码云仓库:https://gitee.com/l0km/erpcex/blob/master/conanfile-erpcgen.py
以上脚本在Windows和Linux(Ubuntu 16.04)下都通过了测试,因为eRPC项目本身设计的限制,Windows下不支持Visual Studio编译器,只能用MinGW编译器。
在Windows编译时需要依赖MSYS2提供的bash shell环境,而msys2/cci.latest
本身也提供了默认MinGW编译器,这有可能与你当前系统安装的编译版本不同,所以需要要通过环境变量CC,CXX
等强制指定使用你自己的MinGW编译器,你可以在执行conan create
命令时使用-e
参数来定义CC,CXX
环境变量,但用起来挺麻烦的,所以为了简化在Windows下的编译时需要在$HOME/.conan/profiles
下增加一下支持MinGW编译的profile文件,如下:
profiles/mingw
# 此profile文件假设已经将MinGW编译器bin文件夹添加到了Windows系统搜索路径(环境变量PATH)
# 否则就要以全路径来定义AR,RANLIB,CC,CXX
include(default)
# 编译器名前缀,为执行gcc -dumpmachine显示的结果
compile_prefix=x86_64-w64-mingw32
[settings]
compiler=gcc
# MinGW编译器版本号,需要根据你的MinGW编译器的实际的版本号来修改
compiler.version=5.2
compiler.libcxx=libstdc++11
build_type=Release
[options]
[build_requires]
[env]
AR=$compile_prefix-gcc-ar
RANLIB=$compile_prefix-gcc-ranlib
CC=$compile_prefix-gcc
CXX=$compile_prefix-g++
有了这个profile,Windows下执行conan编译就很简单:
$ cd erpcex
$ conan create conanfile-erpcgen.py -pr mingw