blob: dc92cf5fd002bcb68e0e5ecff208f1b5049ca27e [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"
12
13S = "${RUSTSRC}"
14
15# We generate local targets, and need to be able to locate them
16export RUST_TARGET_PATH="${WORKDIR}/targets/"
17
18export FORCE_CRATE_HASH="${BB_TASKHASH}"
19
20RUST_ALTERNATE_EXE_PATH ?= "${STAGING_LIBDIR}/llvm-rust/bin/llvm-config"
21export YOCTO_ALTERNATE_EXE_PATH = "${RUST_ALTERNATE_EXE_PATH}"
22export YOCTO_ALTERNATE_MULTILIB_NAME = "/${BASELIB}"
23
24# We don't want to use bitbakes vendoring because the rust sources do their
25# own vendoring.
26CARGO_DISABLE_BITBAKE_VENDORING = "1"
27
28# We can't use RUST_BUILD_SYS here because that may be "musl" if
29# TCLIBC="musl". Snapshots are always -unknown-linux-gnu
30SNAPSHOT_BUILD_SYS = "${BUILD_ARCH}-unknown-linux-gnu"
31setup_cargo_environment () {
32 # The first step is to build bootstrap and some early stage tools,
33 # these are build for the same target as the snapshot, e.g.
34 # x86_64-unknown-linux-gnu.
35 # Later stages are build for the native target (i.e. target.x86_64-linux)
36 cargo_common_do_configure
37
38 printf '[target.%s]\n' "${SNAPSHOT_BUILD_SYS}" >> ${CARGO_HOME}/config
39 printf "linker = '%s'\n" "${RUST_BUILD_CCLD}" >> ${CARGO_HOME}/config
40}
41
42include rust-common.inc
43
44do_rust_setup_snapshot () {
45 for installer in "${WORKDIR}/rust-snapshot-components/"*"/install.sh"; do
46 "${installer}" --prefix="${WORKDIR}/rust-snapshot" --disable-ldconfig
47 done
48
49 # Some versions of rust (e.g. 1.18.0) tries to find cargo in stage0/bin/cargo
50 # and fail without it there.
51 mkdir -p ${RUSTSRC}/build/${BUILD_SYS}
52 ln -sf ${WORKDIR}/rust-snapshot/ ${RUSTSRC}/build/${BUILD_SYS}/stage0
53
54 # Need to use uninative's loader if enabled/present since the library paths
55 # are used internally by rust and result in symbol mismatches if we don't
56 if [ ! -z "${UNINATIVE_LOADER}" -a -e "${UNINATIVE_LOADER}" ]; then
57 for bin in cargo rustc rustdoc; do
58 patchelf-uninative ${WORKDIR}/rust-snapshot/bin/$bin --set-interpreter ${UNINATIVE_LOADER}
59 done
60 fi
61}
62addtask rust_setup_snapshot after do_unpack before do_configure
63do_rust_setup_snapshot[dirs] += "${WORKDIR}/rust-snapshot"
64
65python do_configure() {
66 import json
67 try:
68 import configparser
69 except ImportError:
70 import ConfigParser as configparser
71
72 # toml is rather similar to standard ini like format except it likes values
73 # that look more JSON like. So for our purposes simply escaping all values
74 # as JSON seem to work fine.
75
76 e = lambda s: json.dumps(s)
77
78 config = configparser.RawConfigParser()
79
80 # [target.ARCH-poky-linux]
81 target_section = "target.{}".format(d.getVar('TARGET_SYS', True))
82 config.add_section(target_section)
83
84 llvm_config = d.expand("${YOCTO_ALTERNATE_EXE_PATH}")
85 config.set(target_section, "llvm-config", e(llvm_config))
86
87 config.set(target_section, "cxx", e(d.expand("${RUST_TARGET_CXX}")))
88 config.set(target_section, "cc", e(d.expand("${RUST_TARGET_CC}")))
89
90 # If we don't do this rust-native will compile it's own llvm for BUILD.
91 # [target.${BUILD_ARCH}-unknown-linux-gnu]
92 target_section = "target.{}".format(d.getVar('SNAPSHOT_BUILD_SYS', True))
93 config.add_section(target_section)
94
95 config.set(target_section, "llvm-config", e(llvm_config))
96
97 config.set(target_section, "cxx", e(d.expand("${RUST_BUILD_CXX}")))
98 config.set(target_section, "cc", e(d.expand("${RUST_BUILD_CC}")))
99
100 # [rust]
101 config.add_section("rust")
102 config.set("rust", "rpath", e(True))
103 config.set("rust", "channel", e("stable"))
104
105 # Whether or not to optimize the compiler and standard library
106 config.set("rust", "optimize", e(True))
107
108 # [build]
109 config.add_section("build")
110 config.set("build", "submodules", e(False))
111 config.set("build", "docs", e(False))
112
113 rustc = d.expand("${WORKDIR}/rust-snapshot/bin/rustc")
114 config.set("build", "rustc", e(rustc))
115
116 # Support for the profiler runtime to generate e.g. coverage report,
117 # PGO etc.
118 config.set("build", "profiler", e(True))
119
120 cargo = d.expand("${WORKDIR}/rust-snapshot/bin/cargo")
121 config.set("build", "cargo", e(cargo))
122
123 config.set("build", "vendor", e(True))
124
125 if not "targets" in locals():
126 targets = [d.getVar("TARGET_SYS", True)]
127 config.set("build", "target", e(targets))
128
129 if not "hosts" in locals():
130 hosts = [d.getVar("HOST_SYS", True)]
131 config.set("build", "host", e(hosts))
132
133 # We can't use BUILD_SYS since that is something the rust snapshot knows
134 # nothing about when trying to build some stage0 tools (like fabricate)
135 config.set("build", "build", e(d.getVar("SNAPSHOT_BUILD_SYS", True)))
136
137 # [install]
138 config.add_section("install")
139 # ./x.py install doesn't have any notion of "destdir"
140 # but we can prepend ${D} to all the directories instead
141 config.set("install", "prefix", e(d.getVar("D", True) + d.getVar("prefix", True)))
142 config.set("install", "bindir", e(d.getVar("D", True) + d.getVar("bindir", True)))
143 config.set("install", "libdir", e(d.getVar("D", True) + d.getVar("libdir", True)))
144 config.set("install", "datadir", e(d.getVar("D", True) + d.getVar("datadir", True)))
145 config.set("install", "mandir", e(d.getVar("D", True) + d.getVar("mandir", True)))
146
147 with open("config.toml", "w") as f:
148 f.write('changelog-seen = 2\n\n')
149 config.write(f)
150
151 # set up ${WORKDIR}/cargo_home
152 bb.build.exec_func("setup_cargo_environment", d)
153}
154
155
156rust_runx () {
157 echo "COMPILE ${PN}" "$@"
158
159 # CFLAGS, LDFLAGS, CXXFLAGS, CPPFLAGS are used by rust's build for a
160 # wide range of targets (not just TARGET). Yocto's settings for them will
161 # be inappropriate, avoid using.
162 unset CFLAGS
163 unset LDFLAGS
164 unset CXXFLAGS
165 unset CPPFLAGS
166
167 oe_cargo_fix_env
168
169 python3 src/bootstrap/bootstrap.py ${@oe.utils.parallel_make_argument(d, '-j %d')} "$@" --verbose
170}
171rust_runx[vardepsexclude] += "PARALLEL_MAKE"
172
173do_compile () {
174 rust_runx build
175}
176
177rust_do_install () {
178 mkdir -p ${D}${bindir}
179 cp build/${HOST_SYS}/stage2/bin/* ${D}${bindir}
180
181 mkdir -p ${D}${libdir}/rustlib
182 cp -pRd build/${HOST_SYS}/stage2/lib/* ${D}${libdir}
183 # Remove absolute symlink so bitbake doesn't complain
184 rm -f ${D}${libdir}/rustlib/src/rust
185}
186
187rust_install_targets() {
188 # Install our custom target.json files
189 local td="${D}${libdir}/rustlib/"
190 install -d "$td"
191 for tgt in "${WORKDIR}/targets/"* ; do
192 install -m 0644 "$tgt" "$td"
193 done
194}
195
196
197do_install () {
198 rust_do_install
199 rust_install_targets
200}
201# ex: sts=4 et sw=4 ts=8