summaryrefslogtreecommitdiff
path: root/components/rust/tools/build_support.py
blob: f535b198eeec8f800a1954e1433ae28b03225420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import os
import subprocess


def _parse_cflags(cflags: str):
    info = {
        "march": None,
        "mabi": None,
        "rv_bits": None,  # 32 or 64
        "has_f": False,
        "has_d": False,
    }

    if not cflags:
        return info

    parts = cflags.split()
    for flag in parts:
        if flag.startswith("-march="):
            info["march"] = flag.split("=", 1)[1]
            if "rv32" in info["march"]:
                info["rv_bits"] = 32
            elif "rv64" in info["march"]:
                info["rv_bits"] = 64
            # crude feature detection
            m = info["march"]
            if m:
                info["has_f"] = ("f" in m)
                info["has_d"] = ("d" in m)
        elif flag.startswith("-mabi="):
            info["mabi"] = flag.split("=", 1)[1]
            if info["mabi"] in ("ilp32d", "ilp32f", "lp64d", "lp64f"):
                # floating-point ABI implies FPU availability
                info["has_f"] = True
                info["has_d"] = info["mabi"].endswith("d")

    return info


def detect_rust_target(has, rtconfig):
    """
    Decide the Rust target triple based on RT-Thread Kconfig and rtconfig.*.
    `has` is a callable: has("SYMBOL") -> bool
    """
    # ARM Cortex-M
    if has("ARCH_ARM"):
        # FPU hints from flags/macros
        cflags = getattr(rtconfig, "CFLAGS", "")
        hard_float = "-mfloat-abi=hard" in cflags or has("ARCH_ARM_FPU") or has("ARCH_FPU_VFP")

        if has("ARCH_ARM_CORTEX_M0") or has("ARCH_ARM_CORTEX_M0PLUS"):
            return "thumbv6m-none-eabi"
        if has("ARCH_ARM_CORTEX_M3"):
            return "thumbv7m-none-eabi"
        if has("ARCH_ARM_CORTEX_M4") or has("ARCH_ARM_CORTEX_M7"):
            return "thumbv7em-none-eabihf" if hard_float else "thumbv7em-none-eabi"
        if has("ARCH_ARM_CORTEX_M23"):
            return "thumbv8m.base-none-eabi"
        if has("ARCH_ARM_CORTEX_M33"):
            # v8m.main
            return "thumbv8m.main-none-eabihf" if hard_float else "thumbv8m.main-none-eabi"
        if has("ARCH_ARM_CORTEX_A"):
            return "armv7a-none-eabi"

    # AArch64
    if has("ARCH_AARCH64") or has("ARCH_ARMV8") or has("ARCH_ARM64"):
        if has("ARCH_CPU_FLOAT_ABI_SOFT"):
            return "aarch64-unknown-none-softfloat"
        return "aarch64-unknown-none"
    
    # RISC-V
    if has("ARCH_RISCV32") or has("ARCH_RISCV64"):
        cflags = getattr(rtconfig, "CFLAGS", "")
        info = _parse_cflags(cflags)

        # fallback to Kconfig hint if march not present
        rv_bits = info["rv_bits"] or (32 if has("ARCH_RISCV32") else 64)

        # ABI must carry f/d to actually use hard-float calling convention
        abi = info["mabi"] or ""
        abi_has_fp = abi.endswith("f") or abi.endswith("d")

        if rv_bits == 32:
            # Only pick *f* target when ABI uses hard-float; otherwise use soft-float even if core has F/D
            return "riscv32imafc-unknown-none-elf" if abi_has_fp else "riscv32imac-unknown-none-elf"
        else:
            # rv64: prefer gc (includes fd) only when ABI uses hard-float
            return "riscv64gc-unknown-none-elf" if abi_has_fp else "riscv64imac-unknown-none-elf"

    # Fallback by ARCH string or CFLAGS
    arch = getattr(rtconfig, "ARCH", None)
    if arch:
        arch_l = arch.lower()
        if "aarch64" in arch_l:
            return "aarch64-unknown-none"
        if "arm" == arch_l or "armv7" in arch_l:
            return "armv7a-none-eabi"
        if "riscv32" in arch_l:
            return "riscv32imac-unknown-none-elf"
        if "riscv64" in arch_l:
            return "riscv64imac-unknown-none-elf"
        if "risc-v" in arch_l:
            info = _parse_cflags(getattr(rtconfig, "CFLAGS", ""))
            abi = info["mabi"] or ""
            abi_has_fp = abi.endswith("f") or abi.endswith("d")
            if info["rv_bits"] == 32:
                return "riscv32imafc-unknown-none-elf" if abi_has_fp else "riscv32imac-unknown-none-elf"
            if info["rv_bits"] == 64:
                return "riscv64gc-unknown-none-elf" if abi_has_fp else "riscv64imac-unknown-none-elf"
            # Many BSPs use "risc-v" token; assume 64-bit for virt64 when CFLAGS do not specify width
            return "riscv64imac-unknown-none-elf"

    # Parse CFLAGS for hints
    cflags = getattr(rtconfig, "CFLAGS", "")
    if "-mcpu=cortex-m3" in cflags:
        return "thumbv7m-none-eabi"
    if "-mcpu=cortex-m4" in cflags or "-mcpu=cortex-m7" in cflags:
        if "-mfpu=" in cflags and "-mfloat-abi=hard" in cflags:
            return "thumbv7em-none-eabihf"
        return "thumbv7em-none-eabi"
    if "-march=rv32" in cflags:
        info = _parse_cflags(cflags)
        abi = info["mabi"] or ""
        abi_has_fp = abi.endswith("f") or abi.endswith("d")
        return "riscv32imafc-unknown-none-elf" if abi_has_fp else "riscv32imac-unknown-none-elf"
    if "-march=rv64" in cflags:
        info = _parse_cflags(cflags)
        abi = info["mabi"] or ""
        abi_has_fp = abi.endswith("f") or abi.endswith("d")
        if abi_has_fp:
            return "riscv64gc-unknown-none-elf"
        return "riscv64imac-unknown-none-elf"

    return None


def uses_short_wchar_abi(rtconfig, target: str):
    if not isinstance(target, str) or not target:
        return False
    if not (target.startswith("thumb") or target.startswith("arm")):
        return False

    cflags = getattr(rtconfig, "CFLAGS", "")
    return "-fshort-wchar" in cflags.split()


def make_cargo_build_std_args(rtconfig, target: str):
    if uses_short_wchar_abi(rtconfig, target):
        return ["-Z", "build-std=core,alloc"]
    return []


def make_rustflags(rtconfig, target: str):
    if not isinstance(target, str) or not target:
        arch = getattr(rtconfig, "ARCH", None)
        cflags = getattr(rtconfig, "CFLAGS", "")
        raise ValueError(
            "Unsupported Rust target: unable to detect a Rust target "
            f"for ARCH={arch!r}, CFLAGS={cflags!r}"
        )

    rustflags = [
        "-C", "opt-level=z",
        "-C", "panic=abort",
        "-C", "relocation-model=static",
    ]

    if "riscv" in target:
        rustflags += [
            "-C", "code-model=medium",
            "-C", "link-dead-code",
        ]
        # propagate march/mabi for consistency (use link-arg for staticlib builds – harmless)
        cflags = getattr(rtconfig, "CFLAGS", "")
        for flag in cflags.split():
            if flag.startswith("-march=") or flag.startswith("-mabi="):
                rustflags += ["-C", f"link-arg={flag}"]

    if "thumb" in target or "aarch64" in target:
        rustflags += ["-C", "link-arg=-nostartfiles"]

    if uses_short_wchar_abi(rtconfig, target):
        rustflags += ["-Zllvm_module_flag=wchar_size:u32:2:error"]

    return " ".join(rustflags)


def collect_features(has):
    cargo_toml_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "..", "core", "Cargo.toml")
    )
    if not os.path.isfile(cargo_toml_path):
        raise RuntimeError(f"Rust core Cargo.toml not found: {cargo_toml_path}")

    try:
        import toml
    except ImportError as e:
        raise RuntimeError("Missing toml module required to parse Rust core Cargo.toml") from e

    try:
        with open(cargo_toml_path, "r") as f:
            cargo_data = toml.load(f)
    except OSError as e:
        raise RuntimeError(f"Failed to read Rust core Cargo.toml {cargo_toml_path}: {e}") from e
    except Exception as e:
        raise RuntimeError(f"Failed to parse Rust core Cargo.toml {cargo_toml_path}: {e}") from e

    declared_features = cargo_data.get("features")
    if not isinstance(declared_features, dict):
        raise RuntimeError("Invalid Rust core Cargo.toml: [features] must be a table")

    package = cargo_data.get("package")
    metadata = package.get("metadata") if isinstance(package, dict) else None
    rt_thread = metadata.get("rt-thread") if isinstance(metadata, dict) else None
    feature_mappings = rt_thread.get("features") if isinstance(rt_thread, dict) else None
    if not isinstance(feature_mappings, dict):
        raise RuntimeError(
            "Invalid Rust core feature metadata: package.metadata.rt-thread.features must be a table"
        )

    for feature in feature_mappings:
        if feature not in declared_features:
            raise RuntimeError(
                f"Rust core feature metadata references undeclared Cargo feature: {feature}"
            )

    feats = []
    for feature in declared_features:
        if feature == "default" or feature not in feature_mappings:
            continue

        mapping = feature_mappings[feature]
        if not isinstance(mapping, dict):
            raise RuntimeError(f"Invalid Rust core feature metadata for '{feature}': expected a table")
        if set(mapping) != {"all"}:
            raise RuntimeError(
                f"Invalid Rust core feature metadata for '{feature}': only 'all' is supported"
            )

        symbols = mapping["all"]
        if not isinstance(symbols, list):
            raise RuntimeError(f"Invalid Rust core feature metadata for '{feature}': 'all' must be a list")
        if not symbols:
            raise RuntimeError(f"Invalid Rust core feature metadata for '{feature}': 'all' must not be empty")
        if any(not isinstance(symbol, str) or not symbol for symbol in symbols):
            raise RuntimeError(
                f"Invalid Rust core feature metadata for '{feature}': 'all' must contain only non-empty strings"
            )

        if all(has(symbol) for symbol in symbols):
            feats.append(feature)
    return feats


def verify_rust_toolchain():
    try:
        r1 = subprocess.run(["rustc", "--version"], capture_output=True, text=True)
        r2 = subprocess.run(["cargo", "--version"], capture_output=True, text=True)
        return r1.returncode == 0 and r2.returncode == 0
    except Exception:
        return False


def parse_installed_rust_targets(output):
    return {line.strip() for line in output.splitlines() if line.strip()}


def get_staticlib_artifact_name(rust_dir):
    cargo_toml_path = os.path.join(rust_dir, "Cargo.toml")
    lib_name = "rt_rust"

    try:
        import toml
        with open(cargo_toml_path, "r") as f:
            cargo_data = toml.load(f)

        package_name = cargo_data.get("package", {}).get("name")
        lib_name = cargo_data.get("lib", {}).get("name") or package_name or lib_name
    except Exception as e:
        print(f"Warning: Failed to parse Rust static library metadata from {cargo_toml_path}: {e}")

    if not isinstance(lib_name, str) or not lib_name:
        lib_name = "rt_rust"

    lib_name = lib_name.replace("-", "_")
    return f"lib{lib_name}.a"


def get_staticlib_link_name(rust_dir):
    cargo_toml_path = os.path.join(rust_dir, "Cargo.toml")
    lib_name = "rt_rust"

    try:
        import toml
        with open(cargo_toml_path, "r") as f:
            cargo_data = toml.load(f)

        package_name = cargo_data.get("package", {}).get("name")
        lib_name = cargo_data.get("lib", {}).get("name") or package_name or lib_name
    except Exception as e:
        print(f"Warning: Failed to parse Rust static library metadata from {cargo_toml_path}: {e}")

    if not isinstance(lib_name, str) or not lib_name:
        lib_name = "rt_rust"

    return lib_name.replace("-", "_")


def ensure_rust_target_installed(target: str):
    if not isinstance(target, str) or not target:
        print("Invalid Rust target: expected a non-empty string")
        return False

    try:
        result = subprocess.run(["rustup", "target", "list", "--installed"], capture_output=True, text=True)
        installed_targets = parse_installed_rust_targets(result.stdout)
        if result.returncode == 0 and target in installed_targets:
            return True
        print(f"Rust target '{target}' is not installed.")
        print(f"Please install it with: rustup target add {target}")
    except Exception:
        print("Warning: Failed to check rustup target list (rustup missing?)")
    return False


def cargo_build_staticlib(rust_dir: str, target: str, features, debug: bool, rustflags: str = None, build_root: str = None, cargo_extra_args=None):
    if build_root is None:
        build_root = os.path.join((os.path.abspath(os.path.join(rust_dir, os.pardir, os.pardir))), "build", "rust")
    else:
        build_root = os.path.abspath(os.fspath(build_root))
    target_dir = os.path.join(build_root, "target")
    os.makedirs(build_root, exist_ok=True)

    env = os.environ.copy()
    if rustflags:
        prev = env.get("RUSTFLAGS", "").strip()
        env["RUSTFLAGS"] = (prev + " " + rustflags).strip() if prev else rustflags
    env["CARGO_TARGET_DIR"] = target_dir

    cmd = ["cargo", "build", "--target", target, "--manifest-path", os.path.join(rust_dir, "Cargo.toml")]
    if not debug:
        cmd.insert(2, "--release")
    if cargo_extra_args:
        cmd[2:2] = cargo_extra_args
    if features:
        cmd += ["--no-default-features", "--features", ",".join(features)]

    print("Building Rust component (cargo)…")
    try:
        res = subprocess.run(cmd, cwd=rust_dir, env=env, capture_output=True, text=True)
    except FileNotFoundError:
        print("Error: cargo executable not found. Please install Rust/Cargo and ensure it is in PATH.")
        return None
    if res.returncode != 0:
        print("Warning: Rust build failed")
        if res.stderr:
            print(res.stderr)
        return None

    mode = "debug" if debug else "release"
    artifact_name = get_staticlib_artifact_name(rust_dir)
    lib_path = os.path.join(target_dir, target, mode, artifact_name)
    if os.path.isfile(lib_path) and os.path.getsize(lib_path) > 0:
        print("Rust component built successfully")
        return lib_path
    print(f"Warning: Rust static library artifact not found, is not a file, or is empty: {lib_path}")
    return None


def clean_rust_build(bsp_root: str, artifact_type: str = "rust"):
    """Return the build directory path for SCons Clean operation"""
    build_dir = os.path.join(bsp_root, "build", artifact_type)
    return build_dir