Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
andrewrk opened this issue Jun 1, 2021 · 1 comment · Fixed by #9043
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Jun 1, 2021

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 ..."
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior zig cc Zig as a drop-in C compiler feature labels Jun 1, 2021
@andrewrk andrewrk added this to the 0.8.0 milestone Jun 1, 2021
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label Jun 1, 2021
@mikdusan
Copy link
Member

mikdusan commented Jun 3, 2021

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

@andrewrk andrewrk modified the milestones: 0.8.0, 0.8.1 Jun 4, 2021
andrewrk added a commit that referenced this issue Jun 8, 2021
Zig has detection for when it is accidentally being called recursively
when trying to find the native libc installation. However it was not
working, resulting in a cryptic failure, because zig tried to execute
a command which had spaces in it rather than tokenizing it.

This improves the user experience of `zig cc` for systems that Zig
does not support cross-compiling for.

Closes #8960
andrewrk added a commit that referenced this issue Jun 9, 2021
Zig has detection for when it is accidentally being called recursively
when trying to find the native libc installation. However it was not
working, resulting in a cryptic failure, because zig tried to execute
a command which had spaces in it rather than tokenizing it.

This improves the user experience of `zig cc` for systems that Zig
does not support cross-compiling for.

Closes #8960
andrewrk added a commit that referenced this issue Jun 10, 2021
Zig has detection for when it is accidentally being called recursively
when trying to find the native libc installation. However it was not
working, resulting in a cryptic failure, because zig tried to execute
a command which had spaces in it rather than tokenizing it.

This improves the user experience of `zig cc` for systems that Zig
does not support cross-compiling for.

Closes #8960
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants