Skip to content

Commit b196cc6

Browse files
committed
Improve the generated meson cross files
The main benefits of the new cross files are: - The toolchain path is isolated into a constant, so when the path needs to be updated only one line needs to change. - It is easier to override certain configuration options.
1 parent 8ff8f02 commit b196cc6

File tree

5 files changed

+118
-60
lines changed

5 files changed

+118
-60
lines changed

azure-pipelines.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
"lowrisc-toolchain-gcc-rv32imc" \
3535
"riscv32-unknown-elf" \
3636
"/tools/riscv" \
37-
"-march=rv32imc" "-mabi=ilp32" "-mcmodel=medany"
37+
"rv32imc" "ilp32" "medany"
3838
displayName: 'Build GCC toolchain'
3939
env:
4040
ARTIFACT_STAGING_DIR: $(Build.ArtifactStagingDirectory)
@@ -45,7 +45,7 @@ jobs:
4545
"lowrisc-toolchain-rv32imc" \
4646
"riscv32-unknown-elf" \
4747
"/tools/riscv" \
48-
"-march=rv32imc" "-mabi=ilp32" "-mcmodel=medany"
48+
"rv32imc" "ilp32" "medany"
4949
displayName: "Build Clang toolchain"
5050
env:
5151
ARTIFACT_STAGING_DIR: $(Build.ArtifactStagingDirectory)
@@ -77,8 +77,8 @@ jobs:
7777
"lowrisc-toolchain-gcc-rv32imcb" \
7878
"riscv32-unknown-elf" \
7979
"/tools/riscv" \
80-
"-march=rv32imc_zba_zbb_zbc_zbs" \
81-
"-mabi=ilp32" "-mcmodel=medany"
80+
"rv32imc_zba_zbb_zbc_zbs" \
81+
"ilp32" "medany"
8282
displayName: 'Build GCC toolchain'
8383
env:
8484
ARTIFACT_STAGING_DIR: $(Build.ArtifactStagingDirectory)
@@ -89,8 +89,8 @@ jobs:
8989
"lowrisc-toolchain-rv32imcb" \
9090
"riscv32-unknown-elf" \
9191
"/tools/riscv" \
92-
"-march=rv32imc_zba0p93_zbb0p93_zbc0p93_zbs0p93" \
93-
"-mabi=ilp32" "-mcmodel=medany" "-menable-experimental-extensions"
92+
"rv32imc_zba0p93_zbb0p93_zbc0p93_zbs0p93" \
93+
"ilp32" "medany" "-menable-experimental-extensions"
9494
displayName: "Build Clang toolchain"
9595
env:
9696
ARTIFACT_STAGING_DIR: $(Build.ArtifactStagingDirectory)
@@ -119,7 +119,7 @@ jobs:
119119
"lowrisc-toolchain-gcc-rv64imac" \
120120
"riscv64-unknown-elf" \
121121
"/tools/riscv" \
122-
"-march=rv64imac" "-mabi=lp64" "-mcmodel=medany"
122+
"rv64imac" "lp64" "medany"
123123
displayName: 'Build GCC toolchain'
124124
env:
125125
ARTIFACT_STAGING_DIR: $(Build.ArtifactStagingDirectory)
@@ -130,7 +130,7 @@ jobs:
130130
"lowrisc-toolchain-rv64imac" \
131131
"riscv64-unknown-elf" \
132132
"/tools/riscv" \
133-
"-march=rv64imac" "-mabi=lp64" "-mcmodel=medany"
133+
"rv64imac" "lp64" "medany"
134134
displayName: "Build Clang toolchain"
135135
env:
136136
ARTIFACT_STAGING_DIR: $(Build.ArtifactStagingDirectory)

build-clang-with-args.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ set -x
2020
set -o pipefail
2121

2222
if ! [ "$#" -ge 3 ]; then
23-
echo "Usage: $0 <config_name> <target> <dest_dir> <cflags...>"
23+
echo "Usage: $0 <config_name> <target> <dest_dir> <march> <mabi> <mcmodel> <cflags...>"
2424
exit 2
2525
fi;
2626

@@ -33,8 +33,14 @@ toolchain_name="${1}"
3333
toolchain_target="${2}"
3434
# This is the directory where we want the toolchain to added to
3535
toolchain_dest="${3}"
36+
# -march option default value
37+
march="${4}"
38+
# -mabi option default value
39+
mabi="${5}"
40+
# -mcmodel option default value
41+
mcmodel="${6}"
3642
# Remaining cflags for build configurations
37-
toolchain_cflags=("${@:4}")
43+
toolchain_cflags=("${@:7}")
3844

3945
build_top_dir="${PWD}"
4046

@@ -137,7 +143,8 @@ cd "${build_top_dir}"
137143
"${build_top_dir}/generate-clang-cmake-toolchain.sh" \
138144
"${toolchain_target}" "${toolchain_dest}" "${toolchain_cflags[@]}"
139145
"${build_top_dir}/generate-clang-meson-cross-file.sh" \
140-
"${toolchain_target}" "${toolchain_dest}" "${toolchain_cflags[@]}"
146+
"${toolchain_target}" "${toolchain_dest}" ${march} ${mabi} ${mcmodel} \
147+
"${toolchain_cflags[@]}"
141148

142149
# Copy LLVM licenses into toolchain
143150
mkdir -p "${toolchain_dest}/share/licenses/llvm"
@@ -171,7 +178,7 @@ Crosstool-ng version:
171178
(git: ${CROSSTOOL_NG_URL} ${CROSSTOOL_NG_VERSION})
172179
173180
C Flags:
174-
${toolchain_cflags[@]}
181+
-march=${march} -mabi=${mabi} -mcmodel=${mcmodel} ${toolchain_cflags[@]}
175182
176183
Built at ${build_date} on $(hostname)
177184
BUILDINFO

build-gcc-with-args.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ toolchain_name="${1}"
2929
toolchain_target="${2}"
3030
# This is the directory where we want the toolchain to be installed.
3131
toolchain_dest="${3}"
32+
# -march option default value
33+
march="${4}"
34+
# -mabi option default value
35+
mabi="${5}"
36+
# -mcmodel option default value
37+
mcmodel="${6}"
3238
# Remaining cflags for build configurations
33-
toolchain_cflags=("${@:4}")
39+
toolchain_cflags=("${@:7}")
3440

3541
build_top_dir="${PWD}"
3642

@@ -91,7 +97,8 @@ cd "${build_top_dir}"
9197
"${build_top_dir}/generate-gcc-cmake-toolchain.sh" \
9298
"${toolchain_target}" "${toolchain_dest}" "${toolchain_cflags[@]}"
9399
"${build_top_dir}/generate-gcc-meson-cross-file.sh" \
94-
"${toolchain_target}" "${toolchain_dest}" "${toolchain_cflags[@]}"
100+
"${toolchain_target}" "${toolchain_dest}" ${march} ${mabi} ${mcmodel} \
101+
"${toolchain_cflags[@]}"
95102

96103
ls -l "${toolchain_dest}"
97104

@@ -116,7 +123,7 @@ Crosstool-ng version:
116123
(git: ${CROSSTOOL_NG_URL} ${CROSSTOOL_NG_VERSION})
117124
118125
C Flags:
119-
${toolchain_cflags[@]}
126+
-march=${march} -mabi=${mabi} -mcmodel=${mcmodel} ${toolchain_cflags[@]}
120127
121128
Built at ${build_date} on $(hostname)
122129
BUILDINFO

generate-clang-meson-cross-file.sh

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
## generate-gcc-meson-cross-file.sh
77
#
8-
# This generates a cross file to configure meson for cross-compiling with clang.
8+
# This generates a cross file to configure meson for cross-compiling with Clang.
99
#
10-
# Docs: https://mesonbuild.com/Cross-compilation.html
10+
# Docs:
11+
# - https://mesonbuild.com/Cross-compilation.html
12+
# - https://mesonbuild.com/Machine-files.html
1113

1214
set -e
1315
set -x
1416
set -o pipefail
1517

1618
if ! [ "$#" -ge 2 ]; then
17-
echo "Usage: $0 <target> <prefix_dir> <cflags...>"
19+
echo "Usage: $0 <target> <prefix_dir> <march> <mabi> <mcmodel> <cflags...>"
1820
exit 2
1921
fi;
2022

@@ -23,16 +25,23 @@ fi;
2325
toolchain_target="${1}"
2426
# This is the directory where the toolchain has been installed.
2527
toolchain_dest="${2}"
28+
# -march option default value
29+
march="${3}"
30+
# -mabi option default value
31+
mabi="${4}"
32+
# -mcmodel option default value
33+
mcmodel="${5}"
2634
# Remaining cflags for build configurations
27-
toolchain_cflags=("${@:3}")
35+
toolchain_cflags=("${@:6}")
2836

2937
# Meson uses the driver when both compiling and linking, which may need flags to
3038
# identify exactly how to set up paths and defaults for both.
3139
#
32-
# In particular, the clang driver requires a `--gcc-toolchain=<path>` argument
33-
# to find the right libraries if there are system versions of the risc-v
34-
# toolchains installed.
35-
meson_driver_flags="'--gcc-toolchain=${toolchain_dest}'"
40+
# The clang driver requires a `--gcc-toolchain=<path>` argument to find the
41+
# right libraries if there are system versions of the risc-v toolchains
42+
# installed. That is added automatically to the meson cross file constant
43+
# `default_args`, so it doesn't need to be manually specified here.
44+
meson_driver_flags=""
3645
for flag in "${toolchain_cflags[@]}"; do
3746
if [ -z "${meson_driver_flags}" ]; then
3847
meson_driver_flags+="'${flag}'";
@@ -47,7 +56,7 @@ system_name=""
4756

4857
case "${toolchain_target}" in
4958
riscv*-*-linux-gnu)
50-
sysroot_config="sys_root = '${toolchain_dest}/${toolchain_target}/sysroot'";
59+
sysroot_config="sys_root = toolchain_path / '${toolchain_target}/sysroot'";
5160
system_name="linux";
5261
;;
5362
riscv*-*-elf)
@@ -57,32 +66,46 @@ esac;
5766

5867
tee "${config_dest}" <<CONFIG
5968
# Autogenerated by ${0} on $(date -u)
60-
# Problems? Bug reporting instructions in ${toolchain_dest}/buildinfo
69+
# Problems? See the bug reporting instructions in the file buildinfo.
6170
#
62-
# If you have relocated this toolchain, change all occurences of '${toolchain_dest}'
63-
# to point to the new location of the toolchain.
71+
# If you have relocated this toolchain, update "toolchain_path" accordingly.
72+
73+
[constants]
74+
toolchain_path = '${toolchain_dest}'
75+
toolchain_opt = '--gcc-toolchain=' + toolchain_path
76+
march = '${march}'
77+
mabi = '${mabi}'
78+
mcmodel = '${mcmodel}'
79+
default_extra_args = [${meson_driver_flags}]
80+
default_args = [toolchain_opt, '-march=' + march, '-mabi=' + mabi, '-mcmodel=' + mcmodel] + default_extra_args
81+
default_c_args = default_args
82+
default_c_link_args = default_args
83+
default_cpp_args = default_args
84+
default_cpp_link_args = default_args
6485
6586
[binaries]
66-
c = '${toolchain_dest}/bin/${toolchain_target}-clang'
67-
cpp = '${toolchain_dest}/bin/${toolchain_target}-clang++'
68-
ar = '${toolchain_dest}/bin/${toolchain_target}-ar'
69-
ld = '${toolchain_dest}/bin/${toolchain_target}-ld'
70-
c_ld = '${toolchain_dest}/bin/${toolchain_target}-ld'
71-
cpp_ld = '${toolchain_dest}/bin/${toolchain_target}-ld'
72-
objdump = '${toolchain_dest}/bin/${toolchain_target}-objdump'
73-
objcopy = '${toolchain_dest}/bin/${toolchain_target}-objcopy'
74-
strip = '${toolchain_dest}/bin/${toolchain_target}-strip'
75-
as = '${toolchain_dest}/bin/${toolchain_target}-as'
87+
c = toolchain_path / 'bin/riscv32-unknown-elf-clang'
88+
cpp = toolchain_path / 'bin/riscv32-unknown-elf-clang++'
89+
ar = toolchain_path / 'bin/riscv32-unknown-elf-ar'
90+
ld = toolchain_path / 'bin/riscv32-unknown-elf-ld'
91+
c_ld = toolchain_path / 'bin/riscv32-unknown-elf-ld'
92+
cpp_ld = toolchain_path / 'bin/riscv32-unknown-elf-ld'
93+
objdump = toolchain_path / 'bin/riscv32-unknown-elf-objdump'
94+
objcopy = toolchain_path / 'bin/riscv32-unknown-elf-objcopy'
95+
strip = toolchain_path / 'bin/riscv32-unknown-elf-strip'
96+
as = toolchain_path / 'bin/riscv32-unknown-elf-as'
7697
7798
[properties]
7899
needs_exe_wrapper = true
79100
has_function_printf = false
80-
c_args = [${meson_driver_flags}]
81-
c_link_args = [${meson_driver_flags}]
82-
cpp_args = [${meson_driver_flags}]
83-
cpp_link_args = [${meson_driver_flags}]
84101
${sysroot_config}
85102
103+
[built-in options]
104+
c_args = default_c_args
105+
c_link_args = default_c_link_args
106+
cpp_args = default_cpp_args
107+
cpp_link_args = default_cpp_link_args
108+
86109
[host_machine]
87110
system = '${system_name}'
88111
cpu_family = '${toolchain_target%%-*}'

generate-gcc-meson-cross-file.sh

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
#
88
# This generates a cross file to configure meson for cross-compiling with GCC.
99
#
10-
# Docs: https://mesonbuild.com/Cross-compilation.html
10+
# Docs:
11+
# - https://mesonbuild.com/Cross-compilation.html
12+
# - https://mesonbuild.com/Machine-files.html
1113

1214
set -e
1315
set -x
1416
set -o pipefail
1517

1618
if ! [ "$#" -ge 2 ]; then
17-
echo "Usage: $0 <target> <prefix_dir> <cflags...>"
19+
echo "Usage: $0 <target> <prefix_dir> <march> <mabi> <mcmodel> <cflags...>"
1820
exit 2
1921
fi;
2022

@@ -23,8 +25,14 @@ fi;
2325
toolchain_target="${1}"
2426
# This is the directory where the toolchain has been installed.
2527
toolchain_dest="${2}"
28+
# -march option default value
29+
march="${3}"
30+
# -mabi option default value
31+
mabi="${4}"
32+
# -mcmodel option default value
33+
mcmodel="${5}"
2634
# Remaining cflags for build configurations
27-
toolchain_cflags=("${@:3}")
35+
toolchain_cflags=("${@:6}")
2836

2937
# Meson uses the driver when both compiling and linking, which may need flags to
3038
# identify exactly how to set up paths and defaults for both.
@@ -43,7 +51,7 @@ system_name=""
4351

4452
case "${toolchain_target}" in
4553
riscv*-*-linux-gnu)
46-
sysroot_config="sys_root = '${toolchain_dest}/${toolchain_target}/sysroot'";
54+
sysroot_config="sys_root = toolchain_path / '${toolchain_target}/sysroot'";
4755
system_name="linux";
4856
;;
4957
riscv*-*-elf)
@@ -53,30 +61,43 @@ esac;
5361

5462
tee "${config_dest}" <<CONFIG
5563
# Autogenerated by ${0} on $(date -u)
56-
# Problems? Bug reporting instructions in ${toolchain_dest}/buildinfo
64+
# Problems? See the bug reporting instructions in the file buildinfo.
5765
#
58-
# If you have relocated this toolchain, change all occurences of '${toolchain_dest}'
59-
# to point to the new location of the toolchain.
66+
# If you have relocated this toolchain, update "toolchain_path" accordingly.
67+
68+
[constants]
69+
toolchain_path = '${toolchain_dest}'
70+
march = '${march}'
71+
mabi = '${mabi}'
72+
mcmodel = '${mcmodel}'
73+
default_extra_args = [${meson_driver_flags}]
74+
default_args = ['-march=' + march, '-mabi=' + mabi, '-mcmodel=' + mcmodel] + default_extra_args
75+
default_c_args = default_args
76+
default_c_link_args = default_args
77+
default_cpp_args = default_args
78+
default_cpp_link_args = default_args
6079
6180
[binaries]
62-
c = '${toolchain_dest}/bin/${toolchain_target}-gcc'
63-
cpp = '${toolchain_dest}/bin/${toolchain_target}-g++'
64-
ar = '${toolchain_dest}/bin/${toolchain_target}-ar'
65-
ld = '${toolchain_dest}/bin/${toolchain_target}-ld'
66-
objdump = '${toolchain_dest}/bin/${toolchain_target}-objdump'
67-
objcopy = '${toolchain_dest}/bin/${toolchain_target}-objcopy'
68-
strip = '${toolchain_dest}/bin/${toolchain_target}-strip'
69-
as = '${toolchain_dest}/bin/${toolchain_target}-as'
81+
c = toolchain_path / 'bin/riscv32-unknown-elf-gcc'
82+
cpp = toolchain_path / 'bin/riscv32-unknown-elf-g++'
83+
ar = toolchain_path / 'bin/riscv32-unknown-elf-ar'
84+
ld = toolchain_path / 'bin/riscv32-unknown-elf-ld'
85+
objdump = toolchain_path / 'bin/riscv32-unknown-elf-objdump'
86+
objcopy = toolchain_path / 'bin/riscv32-unknown-elf-objcopy'
87+
strip = toolchain_path / 'bin/riscv32-unknown-elf-strip'
88+
as = toolchain_path / 'bin/riscv32-unknown-elf-as'
7089
7190
[properties]
7291
needs_exe_wrapper = true
7392
has_function_printf = false
74-
c_args = [${meson_driver_flags}]
75-
c_link_args = [${meson_driver_flags}]
76-
cpp_args = [${meson_driver_flags}]
77-
cpp_link_args = [${meson_driver_flags}]
7893
${sysroot_config}
7994
95+
[built-in options]
96+
c_args = default_c_args
97+
c_link_args = default_c_link_args
98+
cpp_args = default_cpp_args
99+
cpp_link_args = default_cpp_link_args
100+
80101
[host_machine]
81102
system = '${system_name}'
82103
cpu_family = '${toolchain_target%%-*}'

0 commit comments

Comments
 (0)