Skip to content

Darwin c_import SDK discovery still shells out to xcrun #349

Closed

Description

@ehartford

Summary

Replace the unconditional xcrun --show-sdk-path shell-out used for Darwin SDK discovery in the c_import bridge with a With-owned target-SDK configuration/discovery path. Target SDKs are legitimate target inputs; discovering them by spawning a host tool on every compile is an implementation gap that breaks hermetic builds and cross-compilation.

Spec

§16.1 c_import (Target C headers are inputs / Cross-compilation):

  • "Platform libc headers, operating system SDKs, vendor headers, and package headers are part of the target environment being imported. They may be supplied by the host platform SDK, by package metadata such as with get c.*, by with.toml, or by build target include paths."
  • "They are target inputs, not a dependency on an arbitrary host LLVM/Clang installation."
  • "Any remaining shell-out to host tools for SDK discovery or macro/preprocessor helper paths is an implementation gap, not a language requirement."

Current state

get_sdk_path in src/compiler/ClangBridge.w:650-668: on macOS, the first call builds argv xcrun --show-sdk-path (ClangBridge.w:660-661), runs it via capture_command_stdout (ClangBridge.w:620, backed by with_exec_argv_capture) with a 30s timeout, and caches the first output line in sdk_path_buf. Every libclang session passes the result as -isysroot: ClangBridge.w:1170 (main parse), 2185 (macro collection), 2447, 2531, plus the cc helper paths via append_cc_common_args (ClangBridge.w:637; see #348).

Contrast: the Clang resource dir is already self-contained — get_clang_resource_dir (ClangBridge.w:677-689) uses only the embedded resources plus the WITH_CLANG_RESOURCE_DIR override, with an explicit comment that probing external LLVM is forbidden. SDK discovery never got the same treatment.

Failure mode today: no xcrun (non-Xcode host, sandboxed CI) → empty sdk_path_buf → libclang parses without -isysroot → 'stdio.h' file not found with no hint that the target SDK input is missing.

Implementation plan

  1. Add a configuration-first resolution order in get_sdk_path (or a new resolve_target_sdk_path it delegates to):
    1. WITH_SDKROOT env override (and honor the conventional SDKROOT if set — Xcode populates it, no process spawn needed);
    2. with.toml key (e.g. [c_import] sdk_path = "..." in src/compiler/ProjectConfig.w — add to project_config_wants_key/project_config_apply_entry next to include_paths, ProjectConfig.w:148-149/254-255) and/or build-target input plumbed the same way g_cimport_include_paths is;
    3. well-known filesystem probe on Darwin: check /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk and /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk for existence (stat via runtime fs calls — file checks, not process spawns).
  2. Delete the xcrun argv construction (ClangBridge.w:656-662) and the now-unused temp-file plumbing for it. Do not keep xcrun even as a last resort — that re-introduces the silent host-tool dependency this issue exists to remove.
  3. Loud diagnostic when required but unresolved: when a c_import parse fails AND no SDK path was resolved on Darwin, the c_import failure diagnostic must say so, e.g. c_import: no macOS SDK found for target headers; set SDKROOT/WITH_SDKROOT, configure [c_import] sdk_path in with.toml, or install Command Line Tools — naming the missing target input, not a host tool. Do not fail eagerly when the header parses without an SDK (pure local-header imports must keep working with no SDK at all).
  4. Do not probe system LLVM/Clang resources as a workaround (AGENTS.md Self-Contained Toolchain; get_clang_resource_dir comment is the precedent).
  5. Pure compiler-source change (ClangBridge.w + ProjectConfig.w); no rt/Link.w/seed coupling. Normal with build / :fixpoint gates apply. Note ClangBridge is compiled into the compiler; the installed seed keeps its baked-in behavior until a new seed is installed (expected, not a hazard).

Test plan

  • test/behavior/behav_c_import_sdk_header.w: use c_import("stdio.h") + call a modeled value function — proves system-header import still works after the change (macOS CI has CLT SDK at the well-known path).
  • No-xcrun regression: internals/driver test that runs the compiler with PATH emptied of xcrun and SDKROOT pointing at the CLT SDK; c_import("stdio.h") must still succeed (proves resolution no longer spawns xcrun).
  • test/compile_errors/err_c_import_missing_sdk.w (driver-level, env WITH_SDKROOT=/nonexistent with probes disabled or pointing nowhere): //! expect-error: containing "no macOS SDK" / "target SDK" guidance text.
  • Local-header import with no SDK resolvable (env-sanitized): local .h via include path must import fine — proves SDK is optional for non-system headers.

Acceptance criteria

  • No xcrun invocation remains in src/compiler/ClangBridge.w.
  • SDK resolution order is: explicit env (WITH_SDKROOT/SDKROOT) → with.toml/build-target config → well-known path stat-probes; no process spawn in any branch.
  • Missing-SDK failures name the missing target SDK/sysroot input and the three remedies; missing host tools are never mentioned because none are used.
  • System-header c_import continues to work on a normal macOS host; local-header c_import works with no SDK at all.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Traceability

Per spec §16.1 (v7.0 rework): SDK discovery shell-outs are implementation gaps, not language requirements.

Activity

  1. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Implemented in 3256541.

    ClangBridge.get_sdk_path no longer shells out to xcrun. Resolution is now spawn-free:

    1. WITH_SDKROOT, then the Xcode-populated SDKROOT env var
    2. with.toml [c_import] sdk_path (new ProjectConfig key, plumbed via ci_set_sdk_path alongside include_paths)
    3. well-known Command-Line-Tools / Xcode SDK paths (existence stat, not a process spawn)

    The xcrun argv + temp-file plumbing is deleted. A c_import header-parse failure with no resolvable SDK on macOS now appends a directional hint naming the missing target SDK input and the three remedies (never a host tool, since none is used). Local-header imports still work with no SDK at all.

    Acceptance criteria

    • No xcrun invocation remains in src/compiler/ClangBridge.w (verified: grep xcrun → none)
    • Resolution order: explicit env → with.toml config → well-known stat-probes; no process spawn in any branch
    • Missing-SDK failures name the target SDK/sysroot input and the remedies
    • System-header c_import works on a normal macOS host (stdio.h → SEEK_SET/SEEK_END); local-header works with no SDK
    • build / fixpoint / tests pass (behavior 691, native-compile-error 603, native-spec 173, fixpoint holds)

    Note: the cc -E macro-helper shell-outs in the same file share append_cc_common_args and are tracked separately in #348.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions