| 1 | #!/usr/bin/env bash |
| 2 | # Guard the OpenHarmony target dependency graph. |
| 3 | # |
| 4 | # This check intentionally does not require an OpenHarmony SDK or sysroot. It |
| 5 | # only asks Cargo to resolve the codewhale-tui dependency graph for the OHOS |
| 6 | # target and fails if crates known to break or be unsupported on OHOS re-enter |
| 7 | # that graph. It also proves the OHOS target activates the rquickjs-sys |
| 8 | # `bindgen` feature for codewhale-workflow-js, which is the only reason the |
| 9 | # crate compiles for a target with no pre-generated QuickJS bindings. |
| 10 | set -euo pipefail |
| 11 | |
| 12 | cd "$(dirname "$0")/../.." |
| 13 | |
| 14 | target="${1:-aarch64-unknown-linux-ohos}" |
| 15 | package="${CODEWHALE_OHOS_DEP_PACKAGE:-codewhale-tui}" |
| 16 | workflow_js_package="${CODEWHALE_OHOS_WORKFLOW_JS_PACKAGE:-codewhale-workflow-js}" |
| 17 | |
| 18 | require_literal() { |
| 19 | local file="$1" |
| 20 | local literal="$2" |
| 21 | local description="$3" |
| 22 | |
| 23 | if ! grep -qF -- "${literal}" "${file}"; then |
| 24 | echo "::error::OHOS Windows linker contract lost ${description}: ${file}" >&2 |
| 25 | return 1 |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | # Cargo invokes its configured linker directly. On Windows that entry point |
| 30 | # must be the repository launcher, not the SDK's bare clang.exe, so the final |
| 31 | # Rust link receives the OHOS target, sysroot, and musl flags as reliably as C, |
| 32 | # C++, and bindgen invocations do. This is a static, no-SDK contract check; the |
| 33 | # launcher delegates to the PowerShell compiler wrapper that validates the SDK |
| 34 | # and preserves Cargo's linker arguments and exit status. The launcher must |
| 35 | # also re-quote each argument it forwards: cmd's %* expansion strips the quotes |
| 36 | # rustc puts around arguments containing spaces, so a spaced SDK path (for |
| 37 | # example the default "D:\DevEco Studio\..." install) would otherwise arrive at |
| 38 | # clang with --sysroot split on the space. |
| 39 | require_literal \ |
| 40 | scripts/ohos/ohos-env.ps1 \ |
| 41 | '$linker = [System.IO.Path]::Combine($PSScriptRoot, "ohos-clang.cmd")' \ |
| 42 | "the repository-local linker launcher" |
| 43 | require_literal \ |
| 44 | scripts/ohos/ohos-env.ps1 \ |
| 45 | '$env:CARGO_TARGET_AARCH64_UNKNOWN_LINUX_OHOS_LINKER = $linker' \ |
| 46 | "Cargo's target-specific linker assignment" |
| 47 | require_literal \ |
| 48 | scripts/ohos/ohos-env.ps1 \ |
| 49 | '$env:OHOS_NATIVE_SDK = $sdk' \ |
| 50 | "the absolute SDK path inherited by Cargo" |
| 51 | require_literal \ |
| 52 | scripts/ohos/ohos-clang.cmd \ |
| 53 | 'ohos-clang.ps1' \ |
| 54 | "the cmd-to-PowerShell delegation" |
| 55 | require_literal \ |
| 56 | scripts/ohos/ohos-clang.cmd \ |
| 57 | '-File "%OHOS_LINKER_SCRIPT%" %ARGS%' \ |
| 58 | "linker argument forwarding" |
| 59 | require_literal \ |
| 60 | scripts/ohos/ohos-clang.cmd \ |
| 61 | 'set "ARGS=%ARGS% "%~1""' \ |
| 62 | "argument re-quoting for space-containing paths" |
| 63 | require_literal \ |
| 64 | scripts/ohos/ohos-clang.cmd \ |
| 65 | 'exit /b %ERRORLEVEL%' \ |
| 66 | "PowerShell exit-status propagation" |
| 67 | require_literal \ |
| 68 | scripts/ohos/ohos-clang.ps1 \ |
| 69 | '& $clang -target aarch64-linux-ohos "--sysroot=$sysroot" -D__MUSL__ @args' \ |
| 70 | "the target/sysroot/musl linker flags" |
| 71 | require_literal \ |
| 72 | scripts/ohos/ohos-clang.ps1 \ |
| 73 | 'exit $LASTEXITCODE' \ |
| 74 | "native linker exit-status propagation" |
| 75 | |
| 76 | direct_clang_status=0 |
| 77 | grep -qF -- '$env:CARGO_TARGET_AARCH64_UNKNOWN_LINUX_OHOS_LINKER = $clang' scripts/ohos/ohos-env.ps1 || direct_clang_status=$? |
| 78 | if ((direct_clang_status == 0)); then |
| 79 | echo "::error::OHOS Windows Cargo linker points directly at clang.exe instead of the target-aware wrapper." >&2 |
| 80 | exit 1 |
| 81 | elif ((direct_clang_status > 1)); then |
| 82 | echo "::error::Failed to inspect scripts/ohos/ohos-env.ps1 for direct clang linker usage (grep exit ${direct_clang_status})." >&2 |
| 83 | exit "${direct_clang_status}" |
| 84 | fi |
| 85 | |
| 86 | echo "OHOS Windows linker wrapper contract OK." |
| 87 | |
| 88 | grep_matches() { |
| 89 | local input="$1" |
| 90 | shift |
| 91 | local -a pipe_status=() |
| 92 | local rc_printf |
| 93 | local rc_grep |
| 94 | |
| 95 | set +e |
| 96 | printf '%s\n' "${input}" | grep "$@" |
| 97 | pipe_status=("${PIPESTATUS[@]}") |
| 98 | set -e |
| 99 | |
| 100 | rc_printf="${pipe_status[0]:-2}" |
| 101 | rc_grep="${pipe_status[1]:-2}" |
| 102 | if ((rc_grep > 1)); then |
| 103 | return "${rc_grep}" |
| 104 | fi |
| 105 | if ((rc_printf != 0)); then |
| 106 | echo "::error::Failed to pipe dependency graph to grep (printf exit ${rc_printf})." >&2 |
| 107 | return 2 |
| 108 | fi |
| 109 | return "${rc_grep}" |
| 110 | } |
| 111 | |
| 112 | cargo_tree_with_retry() { |
| 113 | local package="$1" |
| 114 | shift |
| 115 | local attempt |
| 116 | local max_attempts="${CODEWHALE_OHOS_DEP_RETRIES:-3}" |
| 117 | local delay_seconds="${CODEWHALE_OHOS_DEP_RETRY_DELAY_SECONDS:-10}" |
| 118 | local tmp_root="${TMPDIR:-/tmp}" |
| 119 | local err_file="" |
| 120 | local output |
| 121 | local status |
| 122 | |
| 123 | if ! [[ "${max_attempts}" =~ ^[0-9]+$ ]] || ((max_attempts < 1)); then |
| 124 | echo "CODEWHALE_OHOS_DEP_RETRIES must be an integer greater than or equal to 1." >&2 |
| 125 | return 1 |
| 126 | fi |
| 127 | |
| 128 | tmp_root="${tmp_root%/}" |
| 129 | tmp_root="${tmp_root:-/}" |
| 130 | if ! err_file="$(mktemp "${tmp_root}/check-ohos-deps.XXXXXX")"; then |
| 131 | echo "::error::Failed to create temporary file in ${tmp_root}; set TMPDIR to a writable directory with free space." >&2 |
| 132 | return 1 |
| 133 | fi |
| 134 | trap "rm -f -- $(printf '%q' "${err_file}")" EXIT |
| 135 | if ! printf 'probe\n' >"${err_file}" || ! : >"${err_file}"; then |
| 136 | echo "::error::Temporary directory ${tmp_root} is not writable or out of space." >&2 |
| 137 | return 1 |
| 138 | fi |
| 139 | |
| 140 | for ((attempt = 1; attempt <= max_attempts; attempt++)); do |
| 141 | if output="$( |
| 142 | cargo tree \ |
| 143 | --locked \ |
| 144 | --package "${package}" \ |
| 145 | --target "${target}" \ |
| 146 | --prefix none \ |
| 147 | "$@" \ |
| 148 | 2>"${err_file}" |
| 149 | )"; then |
| 150 | if [[ -n "${output}" ]]; then |
| 151 | rm -f "${err_file}" |
| 152 | printf '%s\n' "${output}" |
| 153 | return 0 |
| 154 | fi |
| 155 | echo "::error::cargo tree returned empty output for ${package} on ${target}." >&2 |
| 156 | status=1 |
| 157 | else |
| 158 | status=$? |
| 159 | fi |
| 160 | |
| 161 | cat "${err_file}" >&2 |
| 162 | if ((attempt >= max_attempts)); then |
| 163 | rm -f "${err_file}" |
| 164 | return "${status}" |
| 165 | fi |
| 166 | echo "cargo tree for OHOS dependency graph failed (attempt ${attempt}/${max_attempts}); retrying in ${delay_seconds}s..." >&2 |
| 167 | sleep "${delay_seconds}" |
| 168 | done |
| 169 | } |
| 170 | |
| 171 | tree="$(cargo_tree_with_retry "${package}" --all-features --no-dedupe)" |
| 172 | |
| 173 | if [[ -z "${tree}" ]]; then |
| 174 | echo "::error::OHOS dependency graph for ${package} on ${target} was empty." >&2 |
| 175 | exit 1 |
| 176 | fi |
| 177 | |
| 178 | if [[ "${tree}" != "${package} v"* ]]; then |
| 179 | echo "::error::OHOS dependency graph for ${package} on ${target} did not contain root package ${package}." >&2 |
| 180 | exit 1 |
| 181 | fi |
| 182 | |
| 183 | disallowed="" |
| 184 | disallowed_status=0 |
| 185 | disallowed="$( |
| 186 | grep_matches "${tree}" -E '^(nix v0\.(28|29)\.|portable-pty v|starlark v|arboard v|keyring v)' |
| 187 | )" || disallowed_status=$? |
| 188 | |
| 189 | if ((disallowed_status > 1)); then |
| 190 | echo "::error::Failed to inspect OHOS dependency graph for ${package} on ${target} (exit ${disallowed_status})." >&2 |
| 191 | exit "${disallowed_status}" |
| 192 | fi |
| 193 | |
| 194 | if ((disallowed_status == 0)) || [[ -n "${disallowed}" ]]; then |
| 195 | { |
| 196 | echo "::error::OHOS target graph for ${package} includes unsupported dependencies:" |
| 197 | echo "${disallowed}" |
| 198 | echo |
| 199 | echo "The OpenHarmony port avoids the rustyline/starlark/portable-pty/nix chain" |
| 200 | echo "by target-gating those crates away from target_env=ohos. Keep this graph" |
| 201 | echo "clean unless a real OHOS-compatible dependency update lands." |
| 202 | } >&2 |
| 203 | exit 1 |
| 204 | fi |
| 205 | |
| 206 | echo "OHOS dependency graph OK for ${package} on ${target}." |
| 207 | |
| 208 | # codewhale-workflow-js only compiles for OHOS because its |
| 209 | # `cfg(target_env = "ohos")` dependency gate activates rquickjs's `bindgen` |
| 210 | # feature, which forwards to rquickjs-sys so QuickJS bindings are generated at |
| 211 | # build time. Resolve the feature graph for the OHOS target (pure metadata, no |
| 212 | # SDK or target toolchain needed) and fail loudly if either feature edge |
| 213 | # disappears — for example because the target gate was dropped or an rquickjs |
| 214 | # upgrade renamed the feature. |
| 215 | workflow_js_features="$(cargo_tree_with_retry "${workflow_js_package}" --edges features)" |
| 216 | |
| 217 | if [[ -z "${workflow_js_features}" ]]; then |
| 218 | echo "::error::OHOS feature graph for ${workflow_js_package} on ${target} was empty." >&2 |
| 219 | exit 1 |
| 220 | fi |
| 221 | |
| 222 | rquickjs_status=0 |
| 223 | grep_matches "${workflow_js_features}" -F -- 'rquickjs feature "bindgen"' >/dev/null || rquickjs_status=$? |
| 224 | if ((rquickjs_status > 1)); then |
| 225 | echo "::error::Failed to inspect OHOS feature graph for ${workflow_js_package} on ${target} (exit ${rquickjs_status})." >&2 |
| 226 | exit "${rquickjs_status}" |
| 227 | fi |
| 228 | |
| 229 | rquickjs_sys_status=0 |
| 230 | grep_matches "${workflow_js_features}" -F -- 'rquickjs-sys feature "bindgen"' >/dev/null || rquickjs_sys_status=$? |
| 231 | if ((rquickjs_sys_status > 1)); then |
| 232 | echo "::error::Failed to inspect OHOS feature graph for ${workflow_js_package} on ${target} (exit ${rquickjs_sys_status})." >&2 |
| 233 | exit "${rquickjs_sys_status}" |
| 234 | fi |
| 235 | |
| 236 | if ((rquickjs_status != 0)) || ((rquickjs_sys_status != 0)); then |
| 237 | { |
| 238 | echo "::error::OHOS target graph for ${workflow_js_package} lost the rquickjs bindgen feature edges:" |
| 239 | grep_matches "${workflow_js_features}" -E '^(rquickjs|rquickjs-sys|rquickjs-core|bindgen)( v| feature)' || true |
| 240 | echo |
| 241 | echo "crates/workflow-js/Cargo.toml must keep activating rquickjs's \`bindgen\`" |
| 242 | echo "feature under cfg(target_env = \"ohos\"); rquickjs ships no pre-generated" |
| 243 | echo "QuickJS bindings for OpenHarmony, so without that edge the crate cannot" |
| 244 | echo "compile for ${target}." |
| 245 | } >&2 |
| 246 | exit 1 |
| 247 | fi |
| 248 | |
| 249 | echo "OHOS rquickjs-sys bindgen feature edge OK for ${workflow_js_package} on ${target}." |
| 250 |