跳到主要内容

构建及安装指导

1. 准备构建环境

检查系统中构建工具是否已安装,并能正常使用。

名称推荐版本说明
Gcc>=7.3.0Linux
Python>=3.5Linux
CMake>=3.16Linux
Sctp无版本限制Linux

2. 准备源码

方式一

  1. 下载openHiTLS代码,含业务代码、构建脚本、测试代码 仓库地址:https://gitcode.com/openhitls
  2. openHiTLS依赖于libboundscheck库,构建之前需将其下载至openHiTLS/platform/Secure_C 仓库地址:https://gitee.com/openeuler/libboundscheck.git

方式二

使用git submodule的方式下载,可以直接下载源码和依赖的Securec库,下载命令

git clone --recurse-submodules https://gitcode.com/openhitls/openhitls.git

3. openHiTLS构建及安装

进入openHiTLS后代码目录结构如下:

└── openHiTLS
├── bsl
├── CMakeLists.txt
├── config
├── configure.py
├── crypto
├── docs
├── include
├── LICENSE
├── platform
├── README-en.md
├── README.md
├── script
├── testcode
├── tls
└── x509

其中:

  • bsl:存放基础功能相关代码;
  • CMakeLists.txt:构建入口文件
  • configure.py:提供构建配置命令行功能;
  • config、script:存放构建相关脚本;
  • crypto:存放密码学算法能力相关代码;
  • platform:存放其他依赖的代码;
  • testcode:存放测试工程类代码。
  • tls:存放安全传输相关代码;
  • x509:存放x509证书功能相关代码;

源码构建调用CMake进行构建,具体方法下面介绍。

3.1 CMake构建

openHiTLS提供CMake构建方式,可通过configure.py进行配置,之后建议新建build目录用于存放构建过程中的产生的临时文件,进入build目录使用“cmake .. && make”的方式执行构建。configure.py的配置可以通过python3 ./configure.py –help查询,相关参数如下:

脚本参数参数说明执行方式
--help显示脚本的帮助信息python3 configure.py --help
-m生成moudules.cmake文件python3 configure.py -m
--build_dir指定编译的临时目录python3 configure.py --build_dir build
--output_dir指定编译目标的输出路径python3 configure.py --output_dir output
--feature_config指定编译特性配置文件python3 configure.py --feature_config path/to/xxx.json
--compile_config指定编译参数配置文件python3 configure.py --compile_config path/to/xxx.json
--enable指定构建特性python3 configure.py --enable hitls_crypto hitls_tls hitls_pse
--asm_type汇编类型python3 configure.py --lib_type static --asm_type armv8
--endian大小端构建python3 configure.py --endian little
--lib_type选择构建静态、动态库或者objectpython3 configure.py --lib_type static
--add_options添加编译选项python3 configure.py --add_options "-O0 -g3"
--del_options移除编译选项python3 configure.py --del_options"-O2"
--add_link_flags添加链接选项python3 configure.py --add_link_flags="-pie"
--del_link_flags移除链接选项python3 configure.py --del_options="-O2 -Werror"

configure.py脚本会直接基于顶层的compile.json和feature.json配置文件修改已有配置。

CMake构建的总体执行步骤如下:

cd openHiTLS
mkdir -p ./build
cd ./build
python3 ../configure.py #修改配置,详见3.1.1节
cmake ..
make -j

构建结果会输出在openHiTLS/build目录下。

3.1.1 常用的配置命令

# 关闭某个特性
python3 ../configure.py --disable [feature]::[module]

# 默认配置,当文件不存在时,则生成文件,否则没有任何动作
python3 ../configure.py -m

# 增删编译选项
# 注意:如果原本存在该编译选项,想要更新,必须先用--del_options再用--add_options添加,如本例子中,原本优化是O0要改为O2
python3 ../configure.py --del_options="-O2 -D_FORTIFY_SOURCE=2" --add_options="-O0 -g"

# 增删链接选项
python3 ../configure.py --add_link_flags="-lxxx" --del_link_flags="-lxxx"

# 只生成静态库
python3 ../configure.py --lib_type static

# 只生成动态库
python3 ../configure.py --lib_type shared

# 只生成object文件
python3 ../configure.py --lib_type object

# 动态库、静态库、object均生成
python3 ../configure.py --lib_type shared static object

3.1.2 交叉编译

交叉编译openHiTLS需要使用CMake的-DCMAKE_TOOLCHAIN_FILE参数将交叉编译配置传入,openHiTLS代码目录conf/toolchain中存放着模版配置文件usr_gcc.toolchain.cmake,可进行适配编译器后使用如下命令:

cd openHiTLS
mkdir -p ./build
cd ./build
python3 ../configure.py #修改配置,详见3.1.1节
cmake -DCMAKE_TOOLCHAIN_FILE=usr_gcc.toolchain.cmake ..
make -j

3.2 构建结果安装

安装openHiTLS的构建结果只需要输入如下命令:

make install

头文件默认安装至/usr/local/include,库文件默认安装至/usr/local/lib。若需要自定义安装路径,在cmake配置阶段使用如下命令:

cmake -DCMAKE_INSTALL_PREFIX=<自定义路径> ..

欢迎关注openHiTLS微信公众号