Skip to content

archbuild 的 profile alias 介绍 配置 必要的代码修改

tsuibin edited this page Aug 1, 2021 · 1 revision

用户在使用archbuild时,archbuild会检查是从哪个链接文件调用的,然后读取相应的配置文件,以riscv64为例,这里我们看到setarch会读取 "@pkgdatadir@/setarch-aliases.d/${arch}" 下的 $arch 文件,对应的是 /usr/share/devtools/setarch-aliases.d/ 目录 那么我们可以通过在该目录下创建riscv64文件来改变set_arch的值,至于为什么要这么做,是因为到目前位置setarch 还没 riscv64 的支持, setarch.c 起初是想直接添加riscv64的架构支持,比如这样

#if defined(__riscv64__) || defined(__riscv__)
                {PER_LINUX,	"riscv",	"riscv"},
                {PER_LINUX,	"riscv32",	"riscv32"},
		{PER_LINUX,	"riscv64",	"riscv64"},
#endif

但是测试了gcc 对riscv64宏定义的支持情况后,发现并没不像猜测的那样, 测试代码如下:

#include <stdio.h>

int main(void)
{
#if defined(__riscv64__)
        printf("__riscv64__: ok \n");
#endif

#if defined(__riscv__)
        printf("__riscv__: ok \n");
#endif

#if defined(__riscv)
        printf("__riscv: ok \n");
#endif

#if defined(__riscv32__)
        printf("__riscv32__: ok \n");
#endif

        return 0;
}

输出:

[root@riscv64linux ~]# ./a.out
__riscv: ok

通过查阅 C/C++ preprocessor definitions ,发现riscv64目前支持的宏定义如下

__riscv: defined for any RISC-V target. Older versions of the GCC toolchain defined __riscv__.
__riscv_xlen: 32 for RV32 and 64 for RV64.
__riscv_float_abi_soft, __riscv_float_abi_single, __riscv_float_abi_double: one of these three will be defined, depending on target ABI.
__riscv_cmodel_medlow, __riscv_cmodel_medany: one of these two will be defined, depending on the target code model.
__riscv_mul: defined when targeting the 'M' ISA extension.
__riscv_muldiv: defined when targeting the 'M' ISA extension and -mno-div has not been used.
__riscv_div: defined when targeting the 'M' ISA extension and -mno-div has not been used.
__riscv_atomic: defined when targeting the 'A' ISA extension.
__riscv_flen: 32 when targeting the 'F' ISA extension (but not 'D') and 64 when targeting 'FD'.
__riscv_fdiv: defined when targeting the 'F' or 'D' ISA extensions and -mno-fdiv has not been used.
__riscv_fsqrt: defined when targeting the 'F' or 'D' ISA extensions and -mno-fdiv has not been used.
__riscv_compressed: defined when targeting the 'C' ISA extension.

而且目前x86的机器上也并不会编译这个部分,只是在模拟器和原生环境中构建时会用到,所以给util-linux/sys-utils/setarch 提交pr的计划暂且搁置,等一切清晰明了之后再回头来做。当前通过另外一个方案来解决setarch不识别架构的问题,如下:

[tsuibin@archlinux devtools]$ cat /usr/share/devtools/setarch-aliases.d/riscv64
x86_64

从该处源码得到的临时解决方案:

if [[ -f "@pkgdatadir@/setarch-aliases.d/${arch}" ]]; then
	read -r set_arch < "@pkgdatadir@/setarch-aliases.d/${arch}"
else
	set_arch="${arch}"
fi

这里会设置set_arch的值 , set_arch 是 setarch的重要参数之一 。

	setarch "${set_arch}" mkarchroot \
		-C "${pacman_config}" \
		-M "${makepkg_config}" \
		"${chroots}/${repo}-${arch}/root" \
		"${base_packages[@]}" || abort

我们在这段代码中可以看到除了set_arch 以外,在mkarchroot时,还读入了 pacman_config 和 makepkg_config,以riscv64架构为例,这两个文件对应的是在 /usr/share/devtools目录下

[tsuibin@archlinux devtools]$ ls -l /usr/share/devtools
total 56
-rw-r--r-- 1 root root 6258 Jul 31 16:20 makepkg-riscv64.conf
-rw-r--r-- 1 root root 5788 Jul 31 16:40 makepkg-x86_64.conf
-rw-r--r-- 1 root root 2790 Jul 31 16:20 pacman-extra-riscv64.conf
-rw-r--r-- 1 root root 2722 Jul 31 16:40 pacman-extra.conf
-rw-r--r-- 1 root root 2873 Jul 31 16:40 pacman-gnome-unstable.conf
-rw-r--r-- 1 root root 2769 Jul 31 16:40 pacman-kde-unstable.conf
-rw-r--r-- 1 root root 3094 Jul 31 16:40 pacman-multilib-staging.conf
-rw-r--r-- 1 root root 2937 Jul 31 16:40 pacman-multilib-testing.conf
-rw-r--r-- 1 root root 2944 Jul 31 16:40 pacman-multilib.conf
-rw-r--r-- 1 root root 2820 Jul 31 16:40 pacman-staging.conf
-rw-r--r-- 1 root root 2718 Jul 31 16:40 pacman-testing.conf
drwxr-xr-x 2 root root 4096 Jul 31 16:42 setarch-aliases.d

对应的是 makepkg-riscv64.conf 和 pacman-extra-riscv64.conf 文件,这两个文件默认是不存在的,是通过 makepkg-x86_64.conf 和 pacman-extra.conf 文件修改而来 在 makepkg-riscv64.conf 中, 我们需要修改编译工具链,架构,参数等,几个比较重要的修改如下:

 CARCH="riscv64"
 CHOST="riscv64-unknown-linux-gnu"

这里修改架构和编译工具

 CFLAGS="-march=rv64gc -mabi=lp64d -O2 -pipe -fno-plt -fexceptions \
         -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
         -fstack-clash-protection"
 CXXFLAGS="$CFLAGS -Wp,-D_GLIBCXX_ASSERTIONS"

这里修改编译参数

COMPRESSZST=(zstd -c -z -q -)

包格式参数

其中pacman-extra-riscv.conf中要把archlinux的源更换为archriscv的源,这里可以使用 https://mirrors.wsyu.edu.cn/archriscv/

Architecture = riscv64

修改架构信息

[core]
Server = https://mirrors.wsyu.edu.cn/archriscv/repo/$repo

[extra]
Server = https://mirrors.wsyu.edu.cn/archriscv/repo/$repo

[community]
Server = https://mirrors.wsyu.edu.cn/archriscv/repo/$repo

修改软件源