前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >conan入门(十六):profile template功能实现不同平台下profile的统一

conan入门(十六):profile template功能实现不同平台下profile的统一

作者头像
10km
发布2022-04-13 12:17:30
6250
发布2022-04-13 12:17:30
举报
文章被收录于专栏:10km的专栏10km的专栏

conan: profile template功能实现不同平台下profile的统一

之前我写过的两篇博客《conan入门(十):Windows下Android NDK交叉编译Boost》,.《conan入门(十一):Linux下Android NDK交叉编译Boost》中介绍了在Linux和Windows下NDK交叉编译boost的过程

在这两篇博客中针对Linux和Windows平台我定义了不同的profile文件,因为Linux和Windows的路径换行符不同,而且Linux和Windows下clang编译器可执行文件的后缀也不同(Windows下为.cmd)。更重要的是不同的平台下Android NDK的安装位置也不同。

但因为这些平台的微小差异就要定义不同的profile,也是不方便维护的。如果我把这个profile给我的同事,他必须根据平台和NDK安装位置,修改profile才能正常使用。

有没有办法使用不同平台使用同一个profile来实现NDK交叉编译呢?

有的,这就要用到Conan profile文件支持的模板功能(template)–《Profile templates》

从Conan 1.38 开始,可以使用jinja2模板引擎进行配置文件。通过使用.jinja扩展名命名配置文件来启用此功能。当conan加载带有.jinja扩展名的配置文件时,立即解析并渲染模板生成标准的profile。

jinja2支持基本的if-else条件判断以及字符操作,也就是说可以完全使用jinja2语法改造动态生成适应当前平台的profile

所以代价就是要学会使用jinja2模板

在网上找到了jinja2模板的使用文档–《Template Designer Documentation》,花了点时间学习了一下,将原来的android_armv7a_clang 模板改名为android_armv7a_clang.jinja,以下是profile完整内容:

android_armv7a_clang.jinja

代码语言:javascript
复制
include(default)
# 获取当前平台名并转为小写,linux,windows,darwin....
{% set osname = platform.system() | lower %}
# 获取当前CPU架构名称:x86,x86_64,
# 如果在windows平台返回的是AMD64则转为x86_64
{% set arch = {"AMD64": "x86_64"}.get(platform.machine(), platform.machine()) %}
{% if platform.system() == "Windows" %}
{% set exe_suffix = ".cmd" %}
{% endif %}
# 从环境变量ANDROID_NDK中读取Android NDK安装位置
android_ndk={{ os.getenv("ANDROID_NDK") }}
target_host=armv7a-linux-androideabi
api_level=16
[settings]
arch=armv7
build_type=Release
compiler=clang
compiler.libcxx=c++_static
#compiler.libcxx=c++_shared
compiler.version=8
os=Android
os.api_level=$api_level
#[tool_requires]
[options]
{% if platform.system() == "Windows" %}
boost:addr2line_location=$android_ndk\toolchains\llvm\prebuilt\windows-x86_64\bin\x86_64-linux-android-addr2line.exe
{% endif %}
boost:without_stacktrace=True
[env]
# 根据前面的osname和arch变量拼接生成交叉编译器路径
{% set bin_path = "$android_ndk/toolchains/llvm/prebuilt/"~osname~"-"~arch~"/bin" %}
{% if platform.system() == "Windows" %}
# windows下替换路径分割符
PATH=[{{ bin_path | replace("/","\\") }}]
{% else %}
PATH=[{{ bin_path }}]
{% endif %}
CHOST=$target_host
AR=arm-linux-androideabi-ar
AS=arm-linux-androideabi-as
RANLIB=arm-linux-androideabi-ranlib
CC=$target_host$api_level-clang{{ exe_suffix }}
CXX=$target_host$api_level-clang++{{ exe_suffix }}
LD=arm-linux-androideabi-ld
STRIP=arm-linux-androideabi-strip
{% set toolchain = "$android_ndk/build/cmake/android.toolchain.cmake" %}
{% if platform.system() == "Windows" %}
# windows下替换路径分割符
CONAN_CMAKE_TOOLCHAIN_FILE={{ toolchain | replace("/","\\") }}
{% else %}
CONAN_CMAKE_TOOLCHAIN_FILE={{ toolchain }}
{% endif %}
CONAN_CMAKE_GENERATOR="Unix Makefiles"
[conf]
tools.android:ndk_path=$android_ndk

只要正确定义了ANDROID_NDK环境变量,android_armv7a_clang.jinja 在Windows,Linux,macOS下都可以正常使用

代码语言:javascript
复制
$ conan install boost/1.69.0@ -pr:h android_armv7a.jinja -pr:b default --build boost --build zlib --build bzip2 --build  libiconv

参考资料

conan 文档:《Profile templates》

jinja2语法:《Template Designer Documentation》

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • conan: profile template功能实现不同平台下profile的统一
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档