Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | ## |
| 8 | ## Purpose: |
| 9 | ## This class is to support building with cargo. It |
| 10 | ## must be different than cargo.bbclass because Rust |
| 11 | ## now builds with Cargo but cannot use cargo.bbclass |
| 12 | ## due to dependencies and assumptions in cargo.bbclass |
| 13 | ## that Rust & Cargo are already installed. So this |
| 14 | ## is used by cargo.bbclass and Rust |
| 15 | ## |
| 16 | |
| 17 | # add crate fetch support |
| 18 | inherit rust-common |
| 19 | |
| 20 | # Where we download our registry and dependencies to |
| 21 | export CARGO_HOME = "${WORKDIR}/cargo_home" |
| 22 | |
| 23 | # The pkg-config-rs library used by cargo build scripts disables itself when |
| 24 | # cross compiling unless this is defined. We set up pkg-config appropriately |
| 25 | # for cross compilation, so tell it we know better than it. |
| 26 | export PKG_CONFIG_ALLOW_CROSS = "1" |
| 27 | |
| 28 | # Don't instruct cargo to use crates downloaded by bitbake. Some rust packages, |
| 29 | # for example the rust compiler itself, come with their own vendored sources. |
| 30 | # Specifying two [source.crates-io] will not work. |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 31 | CARGO_DISABLE_BITBAKE_VENDORING ??= "0" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 32 | |
| 33 | # Used by libstd-rs to point to the vendor dir included in rustc src |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 34 | CARGO_VENDORING_DIRECTORY ??= "${CARGO_HOME}/bitbake" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 35 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 36 | # The directory of the Cargo.toml relative to the root directory, per default |
| 37 | # assume there's a Cargo.toml directly in the root directory |
| 38 | CARGO_SRC_DIR ??= "" |
| 39 | |
| 40 | # The actual path to the Cargo.toml |
| 41 | CARGO_MANIFEST_PATH ??= "${S}/${CARGO_SRC_DIR}/Cargo.toml" |
| 42 | |
| 43 | # Path to Cargo.lock |
| 44 | CARGO_LOCK_PATH ??= "${@ os.path.join(os.path.dirname(d.getVar('CARGO_MANIFEST_PATH', True)), 'Cargo.lock')}" |
| 45 | |
| 46 | CARGO_RUST_TARGET_CCLD ??= "${RUST_TARGET_CCLD}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 47 | cargo_common_do_configure () { |
| 48 | mkdir -p ${CARGO_HOME}/bitbake |
| 49 | |
| 50 | cat <<- EOF > ${CARGO_HOME}/config |
| 51 | # EXTRA_OECARGO_PATHS |
| 52 | paths = [ |
| 53 | $(for p in ${EXTRA_OECARGO_PATHS}; do echo \"$p\",; done) |
| 54 | ] |
| 55 | EOF |
| 56 | |
| 57 | cat <<- EOF >> ${CARGO_HOME}/config |
| 58 | |
| 59 | # Local mirror vendored by bitbake |
| 60 | [source.bitbake] |
| 61 | directory = "${CARGO_VENDORING_DIRECTORY}" |
| 62 | EOF |
| 63 | |
| 64 | if [ ${CARGO_DISABLE_BITBAKE_VENDORING} = "0" ]; then |
| 65 | cat <<- EOF >> ${CARGO_HOME}/config |
| 66 | |
| 67 | [source.crates-io] |
| 68 | replace-with = "bitbake" |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 69 | local-registry = "/nonexistent" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 70 | EOF |
| 71 | fi |
| 72 | |
| 73 | cat <<- EOF >> ${CARGO_HOME}/config |
| 74 | |
| 75 | [http] |
| 76 | # Multiplexing can't be enabled because http2 can't be enabled |
| 77 | # in curl-native without dependency loops |
| 78 | multiplexing = false |
| 79 | |
| 80 | # Ignore the hard coded and incorrect path to certificates |
| 81 | cainfo = "${STAGING_ETCDIR_NATIVE}/ssl/certs/ca-certificates.crt" |
| 82 | |
| 83 | EOF |
| 84 | |
| 85 | cat <<- EOF >> ${CARGO_HOME}/config |
| 86 | |
| 87 | # HOST_SYS |
| 88 | [target.${RUST_HOST_SYS}] |
| 89 | linker = "${CARGO_RUST_TARGET_CCLD}" |
| 90 | EOF |
| 91 | |
| 92 | if [ "${RUST_HOST_SYS}" != "${RUST_BUILD_SYS}" ]; then |
| 93 | cat <<- EOF >> ${CARGO_HOME}/config |
| 94 | |
| 95 | # BUILD_SYS |
| 96 | [target.${RUST_BUILD_SYS}] |
| 97 | linker = "${RUST_BUILD_CCLD}" |
| 98 | EOF |
| 99 | fi |
| 100 | |
| 101 | if [ "${RUST_TARGET_SYS}" != "${RUST_BUILD_SYS}" -a "${RUST_TARGET_SYS}" != "${RUST_HOST_SYS}" ]; then |
| 102 | cat <<- EOF >> ${CARGO_HOME}/config |
| 103 | |
| 104 | # TARGET_SYS |
| 105 | [target.${RUST_TARGET_SYS}] |
| 106 | linker = "${RUST_TARGET_CCLD}" |
| 107 | EOF |
| 108 | fi |
| 109 | |
| 110 | # Put build output in build directory preferred by bitbake instead of |
| 111 | # inside source directory unless they are the same |
| 112 | if [ "${B}" != "${S}" ]; then |
| 113 | cat <<- EOF >> ${CARGO_HOME}/config |
| 114 | |
| 115 | [build] |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 116 | # Use out of tree build destination to avoid polluting the source tree |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 117 | target-dir = "${B}/target" |
| 118 | EOF |
| 119 | fi |
| 120 | |
| 121 | cat <<- EOF >> ${CARGO_HOME}/config |
| 122 | |
| 123 | [term] |
| 124 | progress.when = 'always' |
| 125 | progress.width = 80 |
| 126 | EOF |
| 127 | } |
| 128 | |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 129 | python cargo_common_do_patch_paths() { |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 130 | import shutil |
| 131 | |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 132 | cargo_config = os.path.join(d.getVar("CARGO_HOME"), "config") |
| 133 | if not os.path.exists(cargo_config): |
| 134 | return |
| 135 | |
| 136 | src_uri = (d.getVar('SRC_URI') or "").split() |
| 137 | if len(src_uri) == 0: |
| 138 | return |
| 139 | |
| 140 | patches = dict() |
| 141 | workdir = d.getVar('WORKDIR') |
| 142 | fetcher = bb.fetch2.Fetch(src_uri, d) |
| 143 | for url in fetcher.urls: |
| 144 | ud = fetcher.ud[url] |
| 145 | if ud.type == 'git': |
| 146 | name = ud.parm.get('name') |
| 147 | destsuffix = ud.parm.get('destsuffix') |
| 148 | if name is not None and destsuffix is not None: |
| 149 | if ud.user: |
| 150 | repo = '%s://%s@%s%s' % (ud.proto, ud.user, ud.host, ud.path) |
| 151 | else: |
| 152 | repo = '%s://%s%s' % (ud.proto, ud.host, ud.path) |
| 153 | path = '%s = { path = "%s" }' % (name, os.path.join(workdir, destsuffix)) |
| 154 | patches.setdefault(repo, []).append(path) |
| 155 | |
| 156 | with open(cargo_config, "a+") as config: |
| 157 | for k, v in patches.items(): |
| 158 | print('\n[patch."%s"]' % k, file=config) |
| 159 | for name in v: |
| 160 | print(name, file=config) |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 161 | |
| 162 | if not patches: |
| 163 | return |
| 164 | |
| 165 | # Cargo.lock file is needed for to be sure that artifacts |
| 166 | # downloaded by the fetch steps are those expected by the |
| 167 | # project and that the possible patches are correctly applied. |
| 168 | # Moreover since we do not want any modification |
| 169 | # of this file (for reproducibility purpose), we prevent it by |
| 170 | # using --frozen flag (in CARGO_BUILD_FLAGS) and raise a clear error |
| 171 | # here is better than letting cargo tell (in case the file is missing) |
| 172 | # "Cargo.lock should be modified but --frozen was given" |
| 173 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 174 | lockfile = d.getVar("CARGO_LOCK_PATH", True) |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 175 | if not os.path.exists(lockfile): |
| 176 | bb.fatal(f"{lockfile} file doesn't exist") |
| 177 | |
| 178 | # There are patched files and so Cargo.lock should be modified but we use |
| 179 | # --frozen so let's handle that modifications here. |
| 180 | # |
| 181 | # Note that a "better" (more elegant ?) would have been to use cargo update for |
| 182 | # patched packages: |
| 183 | # cargo update --offline -p package_1 -p package_2 |
| 184 | # But this is not possible since it requires that cargo local git db |
| 185 | # to be populated and this is not the case as we fetch git repo ourself. |
| 186 | |
| 187 | lockfile_orig = lockfile + ".orig" |
| 188 | if not os.path.exists(lockfile_orig): |
| 189 | shutil.copy(lockfile, lockfile_orig) |
| 190 | |
| 191 | newlines = [] |
| 192 | with open(lockfile_orig, "r") as f: |
| 193 | for line in f.readlines(): |
| 194 | if not line.startswith("source = \"git"): |
| 195 | newlines.append(line) |
| 196 | |
| 197 | with open(lockfile, "w") as f: |
| 198 | f.writelines(newlines) |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 199 | } |
| 200 | do_configure[postfuncs] += "cargo_common_do_patch_paths" |
| 201 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 202 | do_compile:prepend () { |
| 203 | oe_cargo_fix_env |
| 204 | } |
| 205 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 206 | oe_cargo_fix_env () { |
| 207 | export CC="${RUST_TARGET_CC}" |
| 208 | export CXX="${RUST_TARGET_CXX}" |
| 209 | export CFLAGS="${CFLAGS}" |
| 210 | export CXXFLAGS="${CXXFLAGS}" |
| 211 | export AR="${AR}" |
| 212 | export TARGET_CC="${RUST_TARGET_CC}" |
| 213 | export TARGET_CXX="${RUST_TARGET_CXX}" |
| 214 | export TARGET_CFLAGS="${CFLAGS}" |
| 215 | export TARGET_CXXFLAGS="${CXXFLAGS}" |
| 216 | export TARGET_AR="${AR}" |
| 217 | export HOST_CC="${RUST_BUILD_CC}" |
| 218 | export HOST_CXX="${RUST_BUILD_CXX}" |
| 219 | export HOST_CFLAGS="${BUILD_CFLAGS}" |
| 220 | export HOST_CXXFLAGS="${BUILD_CXXFLAGS}" |
| 221 | export HOST_AR="${BUILD_AR}" |
| 222 | } |
| 223 | |
| 224 | EXTRA_OECARGO_PATHS ??= "" |
| 225 | |
| 226 | EXPORT_FUNCTIONS do_configure |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 227 | |
| 228 | # The culprit for this setting is the libc crate, |
| 229 | # which as of Jun 2023 calls directly into 32 bit time functions in glibc, |
| 230 | # bypassing all of glibc provisions to choose the right Y2038-safe functions. As |
| 231 | # rust components statically link with that crate, pretty much everything |
| 232 | # is affected, and so there's no point trying to have recipe-specific |
| 233 | # INSANE_SKIP entries. |
| 234 | # |
| 235 | # Upstream ticket and PR: |
| 236 | # https://github.com/rust-lang/libc/issues/3223 |
| 237 | # https://github.com/rust-lang/libc/pull/3175 |
| 238 | INSANE_SKIP:append = " 32bit-time" |