blob: 956301023a05708fb344110c8c25bc71e212d000 [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}")))
Andrew Geissler87f5cff2022-09-30 13:13:31 -050084 config.set(host_section, "linker", e(d.expand("${RUST_TARGET_CCLD}")))
Patrick Williams92b42cb2022-09-03 06:53:57 -050085 if "musl" in host_section:
86 config.set(host_section, "musl-root", e(d.expand("${STAGING_DIR_HOST}${exec_prefix}")))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050087
88 # If we don't do this rust-native will compile it's own llvm for BUILD.
89 # [target.${BUILD_ARCH}-unknown-linux-gnu]
Patrick Williams92b42cb2022-09-03 06:53:57 -050090 build_section = "target.{}".format(d.getVar('RUST_BUILD_SYS', True))
91 if build_section != host_section:
92 config.add_section(build_section)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050093
Patrick Williams92b42cb2022-09-03 06:53:57 -050094 config.set(build_section, "llvm-config", e(llvm_config_build))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050095
Patrick Williams92b42cb2022-09-03 06:53:57 -050096 config.set(build_section, "cxx", e(d.expand("${RUST_BUILD_CXX}")))
97 config.set(build_section, "cc", e(d.expand("${RUST_BUILD_CC}")))
Andrew Geissler87f5cff2022-09-30 13:13:31 -050098 config.set(build_section, "linker", e(d.expand("${RUST_BUILD_CCLD}")))
Patrick Williams92b42cb2022-09-03 06:53:57 -050099
100 target_section = "target.{}".format(d.getVar('RUST_TARGET_SYS', True))
101 if target_section != host_section and target_section != build_section:
102 config.add_section(target_section)
103
104 config.set(target_section, "llvm-config", e(llvm_config_target))
105
106 config.set(target_section, "cxx", e(d.expand("${RUST_TARGET_CXX}")))
107 config.set(target_section, "cc", e(d.expand("${RUST_TARGET_CC}")))
Andrew Geissler87f5cff2022-09-30 13:13:31 -0500108 config.set(target_section, "linker", e(d.expand("${RUST_TARGET_CCLD}")))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500109
Andrew Geissler615f2f12022-07-15 14:00:58 -0500110 # [llvm]
111 config.add_section("llvm")
112 config.set("llvm", "static-libstdcpp", e(False))
Andrew Geissler87f5cff2022-09-30 13:13:31 -0500113 if "llvm" in (d.getVar('TC_CXX_RUNTIME') or ""):
114 config.set("llvm", "use-libcxx", e(True))
Andrew Geissler615f2f12022-07-15 14:00:58 -0500115
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500116 # [rust]
117 config.add_section("rust")
118 config.set("rust", "rpath", e(True))
119 config.set("rust", "channel", e("stable"))
120
121 # Whether or not to optimize the compiler and standard library
122 config.set("rust", "optimize", e(True))
123
Andrew Geissler87f5cff2022-09-30 13:13:31 -0500124 # Emits extraneous output from tests to ensure that failures of the test
125 # harness are debuggable just from logfiles
126 config.set("rust", "verbose-tests", e(True))
127
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500128 # [build]
129 config.add_section("build")
130 config.set("build", "submodules", e(False))
131 config.set("build", "docs", e(False))
132
133 rustc = d.expand("${WORKDIR}/rust-snapshot/bin/rustc")
134 config.set("build", "rustc", e(rustc))
135
136 # Support for the profiler runtime to generate e.g. coverage report,
137 # PGO etc.
138 config.set("build", "profiler", e(True))
139
140 cargo = d.expand("${WORKDIR}/rust-snapshot/bin/cargo")
141 config.set("build", "cargo", e(cargo))
142
143 config.set("build", "vendor", e(True))
144
145 if not "targets" in locals():
Patrick Williams92b42cb2022-09-03 06:53:57 -0500146 targets = [d.getVar("RUST_TARGET_SYS", True)]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500147 config.set("build", "target", e(targets))
148
149 if not "hosts" in locals():
Patrick Williams92b42cb2022-09-03 06:53:57 -0500150 hosts = [d.getVar("RUST_HOST_SYS", True)]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500151 config.set("build", "host", e(hosts))
152
153 # We can't use BUILD_SYS since that is something the rust snapshot knows
154 # nothing about when trying to build some stage0 tools (like fabricate)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500155 config.set("build", "build", e(d.getVar("RUST_BUILD_SYS", True)))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500156
157 # [install]
158 config.add_section("install")
159 # ./x.py install doesn't have any notion of "destdir"
160 # but we can prepend ${D} to all the directories instead
161 config.set("install", "prefix", e(d.getVar("D", True) + d.getVar("prefix", True)))
162 config.set("install", "bindir", e(d.getVar("D", True) + d.getVar("bindir", True)))
163 config.set("install", "libdir", e(d.getVar("D", True) + d.getVar("libdir", True)))
164 config.set("install", "datadir", e(d.getVar("D", True) + d.getVar("datadir", True)))
165 config.set("install", "mandir", e(d.getVar("D", True) + d.getVar("mandir", True)))
166
167 with open("config.toml", "w") as f:
168 f.write('changelog-seen = 2\n\n')
169 config.write(f)
170
171 # set up ${WORKDIR}/cargo_home
172 bb.build.exec_func("setup_cargo_environment", d)
173}
174
175
176rust_runx () {
177 echo "COMPILE ${PN}" "$@"
178
179 # CFLAGS, LDFLAGS, CXXFLAGS, CPPFLAGS are used by rust's build for a
180 # wide range of targets (not just TARGET). Yocto's settings for them will
181 # be inappropriate, avoid using.
182 unset CFLAGS
183 unset LDFLAGS
184 unset CXXFLAGS
185 unset CPPFLAGS
186
Patrick Williams92b42cb2022-09-03 06:53:57 -0500187 export RUSTFLAGS="${RUST_DEBUG_REMAP}"
188
189 # Copy the natively built llvm-config into the target so we can run it. Horrible,
190 # but works!
191 if [ ${RUST_ALTERNATE_EXE_PATH_NATIVE} != ${RUST_ALTERNATE_EXE_PATH} ]; then
192 mkdir -p `dirname ${RUST_ALTERNATE_EXE_PATH}`
193 cp ${RUST_ALTERNATE_EXE_PATH_NATIVE} ${RUST_ALTERNATE_EXE_PATH}
194 chrpath -d ${RUST_ALTERNATE_EXE_PATH}
195 fi
196
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500197 oe_cargo_fix_env
198
199 python3 src/bootstrap/bootstrap.py ${@oe.utils.parallel_make_argument(d, '-j %d')} "$@" --verbose
200}
201rust_runx[vardepsexclude] += "PARALLEL_MAKE"
202
203do_compile () {
204 rust_runx build
205}
206
207rust_do_install () {
208 mkdir -p ${D}${bindir}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500209 cp build/${RUST_HOST_SYS}/stage2/bin/* ${D}${bindir}
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500210
211 mkdir -p ${D}${libdir}/rustlib
Patrick Williams92b42cb2022-09-03 06:53:57 -0500212 cp -pRd build/${RUST_HOST_SYS}/stage2/lib/* ${D}${libdir}
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500213 # Remove absolute symlink so bitbake doesn't complain
214 rm -f ${D}${libdir}/rustlib/src/rust
215}
216
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500217do_install () {
218 rust_do_install
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500219}