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