blob: 284347dedc37070677bf8e95dd02627d5eb32352 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001SUMMARY = "Rust compiler and runtime libaries"
2HOMEPAGE = "http://www.rust-lang.org"
3SECTION = "devel"
4LICENSE = "MIT | Apache-2.0"
5LIC_FILES_CHKSUM = "file://COPYRIGHT;md5=93a95682d51b4cb0a633a97046940ef0"
6
7inherit rust
8inherit cargo_common
9
10DEPENDS += "file-native python3-native"
11DEPENDS:append:class-native = " rust-llvm-native"
Patrick Williams92b42cb2022-09-03 06:53:57 -050012DEPENDS:append:class-nativesdk = " nativesdk-rust-llvm"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050013
14S = "${RUSTSRC}"
15
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050016export FORCE_CRATE_HASH="${BB_TASKHASH}"
17
18RUST_ALTERNATE_EXE_PATH ?= "${STAGING_LIBDIR}/llvm-rust/bin/llvm-config"
Patrick Williams92b42cb2022-09-03 06:53:57 -050019RUST_ALTERNATE_EXE_PATH_NATIVE = "${STAGING_LIBDIR_NATIVE}/llvm-rust/bin/llvm-config"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050020
21# We don't want to use bitbakes vendoring because the rust sources do their
22# own vendoring.
23CARGO_DISABLE_BITBAKE_VENDORING = "1"
24
25# We can't use RUST_BUILD_SYS here because that may be "musl" if
26# TCLIBC="musl". Snapshots are always -unknown-linux-gnu
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050027setup_cargo_environment () {
28 # The first step is to build bootstrap and some early stage tools,
29 # these are build for the same target as the snapshot, e.g.
30 # x86_64-unknown-linux-gnu.
31 # Later stages are build for the native target (i.e. target.x86_64-linux)
32 cargo_common_do_configure
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050033}
34
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050035inherit rust-target-config
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050036
37do_rust_setup_snapshot () {
38 for installer in "${WORKDIR}/rust-snapshot-components/"*"/install.sh"; do
39 "${installer}" --prefix="${WORKDIR}/rust-snapshot" --disable-ldconfig
40 done
41
42 # Some versions of rust (e.g. 1.18.0) tries to find cargo in stage0/bin/cargo
43 # and fail without it there.
44 mkdir -p ${RUSTSRC}/build/${BUILD_SYS}
45 ln -sf ${WORKDIR}/rust-snapshot/ ${RUSTSRC}/build/${BUILD_SYS}/stage0
46
47 # Need to use uninative's loader if enabled/present since the library paths
48 # are used internally by rust and result in symbol mismatches if we don't
49 if [ ! -z "${UNINATIVE_LOADER}" -a -e "${UNINATIVE_LOADER}" ]; then
50 for bin in cargo rustc rustdoc; do
51 patchelf-uninative ${WORKDIR}/rust-snapshot/bin/$bin --set-interpreter ${UNINATIVE_LOADER}
52 done
53 fi
54}
55addtask rust_setup_snapshot after do_unpack before do_configure
56do_rust_setup_snapshot[dirs] += "${WORKDIR}/rust-snapshot"
Andrew Geisslereff27472021-10-29 15:35:00 -050057do_rust_setup_snapshot[vardepsexclude] += "UNINATIVE_LOADER"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050058
59python do_configure() {
60 import json
61 try:
62 import configparser
63 except ImportError:
64 import ConfigParser as configparser
65
66 # toml is rather similar to standard ini like format except it likes values
67 # that look more JSON like. So for our purposes simply escaping all values
68 # as JSON seem to work fine.
69
70 e = lambda s: json.dumps(s)
71
72 config = configparser.RawConfigParser()
73
74 # [target.ARCH-poky-linux]
Patrick Williams92b42cb2022-09-03 06:53:57 -050075 host_section = "target.{}".format(d.getVar('RUST_HOST_SYS', True))
76 config.add_section(host_section)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050077
Patrick Williams92b42cb2022-09-03 06:53:57 -050078 llvm_config_target = d.expand("${RUST_ALTERNATE_EXE_PATH}")
79 llvm_config_build = d.expand("${RUST_ALTERNATE_EXE_PATH_NATIVE}")
80 config.set(host_section, "llvm-config", e(llvm_config_target))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050081
Patrick Williams92b42cb2022-09-03 06:53:57 -050082 config.set(host_section, "cxx", e(d.expand("${RUST_TARGET_CXX}")))
83 config.set(host_section, "cc", e(d.expand("${RUST_TARGET_CC}")))
84 if "musl" in host_section:
85 config.set(host_section, "musl-root", e(d.expand("${STAGING_DIR_HOST}${exec_prefix}")))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050086
87 # If we don't do this rust-native will compile it's own llvm for BUILD.
88 # [target.${BUILD_ARCH}-unknown-linux-gnu]
Patrick Williams92b42cb2022-09-03 06:53:57 -050089 build_section = "target.{}".format(d.getVar('RUST_BUILD_SYS', True))
90 if build_section != host_section:
91 config.add_section(build_section)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050092
Patrick Williams92b42cb2022-09-03 06:53:57 -050093 config.set(build_section, "llvm-config", e(llvm_config_build))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050094
Patrick Williams92b42cb2022-09-03 06:53:57 -050095 config.set(build_section, "cxx", e(d.expand("${RUST_BUILD_CXX}")))
96 config.set(build_section, "cc", e(d.expand("${RUST_BUILD_CC}")))
97
98 target_section = "target.{}".format(d.getVar('RUST_TARGET_SYS', True))
99 if target_section != host_section and target_section != build_section:
100 config.add_section(target_section)
101
102 config.set(target_section, "llvm-config", e(llvm_config_target))
103
104 config.set(target_section, "cxx", e(d.expand("${RUST_TARGET_CXX}")))
105 config.set(target_section, "cc", e(d.expand("${RUST_TARGET_CC}")))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500106
Andrew Geissler615f2f12022-07-15 14:00:58 -0500107 # [llvm]
108 config.add_section("llvm")
109 config.set("llvm", "static-libstdcpp", e(False))
110
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500111 # [rust]
112 config.add_section("rust")
113 config.set("rust", "rpath", e(True))
114 config.set("rust", "channel", e("stable"))
115
116 # Whether or not to optimize the compiler and standard library
117 config.set("rust", "optimize", e(True))
118
119 # [build]
120 config.add_section("build")
121 config.set("build", "submodules", e(False))
122 config.set("build", "docs", e(False))
123
124 rustc = d.expand("${WORKDIR}/rust-snapshot/bin/rustc")
125 config.set("build", "rustc", e(rustc))
126
127 # Support for the profiler runtime to generate e.g. coverage report,
128 # PGO etc.
129 config.set("build", "profiler", e(True))
130
131 cargo = d.expand("${WORKDIR}/rust-snapshot/bin/cargo")
132 config.set("build", "cargo", e(cargo))
133
134 config.set("build", "vendor", e(True))
135
136 if not "targets" in locals():
Patrick Williams92b42cb2022-09-03 06:53:57 -0500137 targets = [d.getVar("RUST_TARGET_SYS", True)]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500138 config.set("build", "target", e(targets))
139
140 if not "hosts" in locals():
Patrick Williams92b42cb2022-09-03 06:53:57 -0500141 hosts = [d.getVar("RUST_HOST_SYS", True)]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500142 config.set("build", "host", e(hosts))
143
144 # We can't use BUILD_SYS since that is something the rust snapshot knows
145 # nothing about when trying to build some stage0 tools (like fabricate)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500146 config.set("build", "build", e(d.getVar("RUST_BUILD_SYS", True)))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500147
148 # [install]
149 config.add_section("install")
150 # ./x.py install doesn't have any notion of "destdir"
151 # but we can prepend ${D} to all the directories instead
152 config.set("install", "prefix", e(d.getVar("D", True) + d.getVar("prefix", True)))
153 config.set("install", "bindir", e(d.getVar("D", True) + d.getVar("bindir", True)))
154 config.set("install", "libdir", e(d.getVar("D", True) + d.getVar("libdir", True)))
155 config.set("install", "datadir", e(d.getVar("D", True) + d.getVar("datadir", True)))
156 config.set("install", "mandir", e(d.getVar("D", True) + d.getVar("mandir", True)))
157
158 with open("config.toml", "w") as f:
159 f.write('changelog-seen = 2\n\n')
160 config.write(f)
161
162 # set up ${WORKDIR}/cargo_home
163 bb.build.exec_func("setup_cargo_environment", d)
164}
165
166
167rust_runx () {
168 echo "COMPILE ${PN}" "$@"
169
170 # CFLAGS, LDFLAGS, CXXFLAGS, CPPFLAGS are used by rust's build for a
171 # wide range of targets (not just TARGET). Yocto's settings for them will
172 # be inappropriate, avoid using.
173 unset CFLAGS
174 unset LDFLAGS
175 unset CXXFLAGS
176 unset CPPFLAGS
177
Patrick Williams92b42cb2022-09-03 06:53:57 -0500178 export RUSTFLAGS="${RUST_DEBUG_REMAP}"
179
180 # Copy the natively built llvm-config into the target so we can run it. Horrible,
181 # but works!
182 if [ ${RUST_ALTERNATE_EXE_PATH_NATIVE} != ${RUST_ALTERNATE_EXE_PATH} ]; then
183 mkdir -p `dirname ${RUST_ALTERNATE_EXE_PATH}`
184 cp ${RUST_ALTERNATE_EXE_PATH_NATIVE} ${RUST_ALTERNATE_EXE_PATH}
185 chrpath -d ${RUST_ALTERNATE_EXE_PATH}
186 fi
187
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500188 oe_cargo_fix_env
189
190 python3 src/bootstrap/bootstrap.py ${@oe.utils.parallel_make_argument(d, '-j %d')} "$@" --verbose
191}
192rust_runx[vardepsexclude] += "PARALLEL_MAKE"
193
194do_compile () {
195 rust_runx build
196}
197
198rust_do_install () {
199 mkdir -p ${D}${bindir}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500200 cp build/${RUST_HOST_SYS}/stage2/bin/* ${D}${bindir}
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500201
202 mkdir -p ${D}${libdir}/rustlib
Patrick Williams92b42cb2022-09-03 06:53:57 -0500203 cp -pRd build/${RUST_HOST_SYS}/stage2/lib/* ${D}${libdir}
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500204 # Remove absolute symlink so bitbake doesn't complain
205 rm -f ${D}${libdir}/rustlib/src/rust
206}
207
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500208do_install () {
209 rust_do_install
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500210}