Skip to content

when CC environment variable is set to zig and zig cannot provide libc, error: unable to create compilation: UnableToSpawnCCompiler #8960

Closed

Description

@andrewrk

To reproduce the issue, pick a system where Zig cannot provide libc, such as FreeBSD (until #2876 is solved) and override the CC environment variable to be Zig.

When trying to link libc, you will get some output like this:

[ 12%] Building C object CMakeFiles/z.dir/compress.o
Zig attempted to find the path to native system libc headers by executing this command:
/home/andrewrk/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -target x86_64-native -mcpu=x86_64_v2 -E -Wp,-v -xc /dev/null
error: unable to create compilation: UnableToSpawnCCompiler
Zig attempted to find the path to native system libc headers by executing this command:
/home/andrewrk/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -target x86_64-native -mcpu=x86_64_v2 -E -Wp,-v -xc /dev/null
error: unable to create compilation: UnableToSpawnCCompiler

However we can make this work, by detecting when the CC environment variable is set to Zig inside libc_installation.zig, and invoking cc rather than the zig compiler from CC, in order to find the native libc installation paths.

const cc_exe = std.os.getenvZ("CC") orelse default_cc_exe;

const cc_exe = std.os.getenvZ("CC") orelse default_cc_exe;

Another bug, which needs to be fixed in order to solve this one, is that Zig does not pick part the CC parameters and separate them by spaces, as is widely supported by systems that recognize the CC environment variable.

Until this is solved, the workaround for this problem is to execute zig libc before overriding the environment variable and save the output somewhere. Then use it in the CLI when overriding CC, like this:

zig libc >~/tmp/libc.txt
export ZIG_LIBC=$HOME/tmp/libc.txt
export CC="zig cc ..."

Activity

  1. added
    bug
    zig cc
    on Jun 1, 2021
  2. added this to the 0.8.0 milestone on Jun 1, 2021
  3. added
    contributor friendly
    on Jun 1, 2021
  4. mikdusan commented on Jun 3, 2021

    @mikdusan
    Member

    By utilizing the toolchain idea from #8973, during creation of the toolchain dir:

    • record native libc information to $TOOLCHAIN_BIN/../lib/libc.txt
    • upon using a toolchain driver (wrapper), it shall pass --libc $TOOLCHAIN_BIN/../lib/libc.txt to main zig

    In the case of zig-bootstrap, #8973 is used to generate a toolchain dir at a folder inside the zig-bootstrap artifact with a relative dir to find zig.exe artifact.

    edit: many ways to encode libc.txt kvpairs; it could just be embedded in the wrapper exe and no need for libc.txt

  5. modified the milestones: 0.8.0, 0.8.1 on Jun 4, 2021
  6. added a commit that references this issue on Jun 8, 2021
    3dc3308
  7. added 2 commits that reference this issue on Jun 9, 2021
    d128ec3
    c230258
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

    bugcontributor friendlyzig cc

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions