1
Fork 0

defs: fix import paths

Empirically these need to come from most specfic to least specific.

The error message is as follows:

    In file included from test/c/main.c:1:
    In file included from external/zig_sdk/lib/libcxx/include/stdio.h:107:
    In file included from external/zig_sdk/lib/libc/include/generic-glibc/stdio.h:38:
    external/zig_sdk/lib/libc/include/generic-glibc/bits/types.h:139:3: error:
    # error
      ^
    external/zig_sdk/lib/libc/include/generic-glibc/bits/types.h:145:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __DEV_T_TYPE __dev_t;        /* Type of device numbers.  */

Dissected `generic-glibc/bits/types.h:#error`:

    #if __WORDSIZE == 32
    <...>
    # define __STD_TYPE		__extension__ typedef
    #elif __WORDSIZE == 64
    <...>
    # define __STD_TYPE		typedef
    #else
    # error
    #endif

So we do not have the `__WORDSIZE`. Where does that come from? Probably from a
directory that has an `x86_64` in it. How does that get included? Let's start
with `lib/libcxx/include/stdio.h`:

	16 #include_next <stdio.h>

Now previously our `c++` command line looked like this:
    external/zig_sdk/tools/c++ \
    <...>
    -Iexternal/zig_sdk/lib/include \
    -Iexternal/zig_sdk/lib/libcxx/include \
    -Iexternal/zig_sdk/lib/libcxxabi/include \
    -Iexternal/zig_sdk/lib/libunwind/include \
    -Iexternal/zig_sdk/lib/libc/include/generic-glibc \
    -Iexternal/zig_sdk/lib/libc/include/any-linux-any \
    -Iexternal/zig_sdk/lib/libc/include/x86_64-linux-gnu \
    -Iexternal/zig_sdk/lib/libc/include/x86_64-linux-any \
    -Iexternal/zig_sdk/lib/libc/include/x86-linux-any \
    -Iexternal/zig_sdk/glibc-hacks \
    <...>

So the next place it will find `stdio.h` is in `generic-glibc`, which already
uses the `__WORDSIZE`. If we make the "next" include to be the arch-specific
one instead of the generic-glibc, things start compiling again.

Fix the same fo musl.
nix
Motiejus Jakštys 2022-09-26 10:14:08 +03:00
parent ab59c18d60
commit 0302fb630a
3 changed files with 39 additions and 23 deletions

View File

@ -1,6 +1,13 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_user_netrc", "use_netrc")
load("@bazel-zig-cc//toolchain/private:defs.bzl", "DEFAULT_INCLUDE_DIRECTORIES", "target_structs", "zig_tool_path")
load("@bazel-zig-cc//toolchain/private:defs.bzl", "target_structs", "zig_tool_path")
# Directories that `zig c++` includes behind the scenes.
_DEFAULT_INCLUDE_DIRECTORIES = [
"libcxx/include",
"libcxxabi/include",
"libunwind/include",
]
_fcntl_map = """
GLIBC_2.2.5 {
@ -86,12 +93,14 @@ _ZIG_TOOLS = [
"wasm-ld", # WebAssembly
]
# TODO: ZIG_LIB_DIR equivalent in powershell?
_ZIG_TOOL_WRAPPER_WINDOWS_CACHE_KNOWN = """@echo off
set ZIG_LOCAL_CACHE_DIR={cache_prefix}\\bazel-zig-cc
set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
"{zig}" "{zig_tool}" %*
"""
# TODO: ZIG_LIB_DIR equivalent in powershell?
_ZIG_TOOL_WRAPPER_WINDOWS_CACHE_GUESS = """@echo off
if exist "%TMP%\\*" goto :usertmp
set ZIG_LOCAL_CACHE_DIR=C:\\Temp\\bazel-zig-cc
@ -104,6 +113,12 @@ set ZIG_GLOBAL_CACHE_DIR=%ZIG_LOCAL_CACHE_DIR%
"""
_ZIG_TOOL_WRAPPER_CACHE_KNOWN = """#!/bin/sh
set -e
if [ -d external/zig_sdk/lib ]; then
export ZIG_LIB_DIR=external/zig_sdk/lib
else
export ZIG_LIB_DIR="$(dirname "$0")/../lib"
fi
export ZIG_LOCAL_CACHE_DIR="{cache_prefix}/bazel-zig-cc"
export ZIG_GLOBAL_CACHE_DIR="{cache_prefix}/bazel-zig-cc"
exec "{zig}" "{zig_tool}" "$@"
@ -111,6 +126,11 @@ exec "{zig}" "{zig_tool}" "$@"
_ZIG_TOOL_WRAPPER_CACHE_GUESS = """#!/bin/sh
set -e
if [ -d external/zig_sdk/lib ]; then
export ZIG_LIB_DIR=external/zig_sdk/lib
else
export ZIG_LIB_DIR="$(dirname "$0")/../lib"
fi
if [ -n "$TMPDIR" ]; then
_cache_prefix=$TMPDIR
elif [ -n "$HOME" ]; then
@ -258,7 +278,7 @@ def declare_files(os, zig_include_root):
lazy_filegroups = {}
for target_config in target_structs():
for d in DEFAULT_INCLUDE_DIRECTORIES + target_config.includes:
for d in _DEFAULT_INCLUDE_DIRECTORIES + target_config.includes:
d = zig_include_root + d
if d not in lazy_filegroups:
lazy_filegroups[d] = filegroup(name = d, srcs = native.glob([d + "/**"]))

View File

@ -1,4 +1,4 @@
load(":defs.bzl", "DEFAULT_INCLUDE_DIRECTORIES", "target_structs", "zig_tool_path")
load(":defs.bzl", "target_structs", "zig_tool_path")
load("@bazel-zig-cc//toolchain:zig_toolchain.bzl", "zig_cc_toolchain_config")
DEFAULT_TOOL_PATHS = {
@ -17,9 +17,6 @@ def declare_cc_toolchains(os, absolute_path, zig_include_root):
zigtarget = target_config.zigtarget
cxx_builtin_include_directories = []
for d in DEFAULT_INCLUDE_DIRECTORIES + target_config.includes:
d = zig_include_root + d
cxx_builtin_include_directories.append(absolute_path + "/" + d)
for d in getattr(target_config, "toplevel_include", []):
cxx_builtin_include_directories.append(absolute_path + "/" + d)
@ -53,7 +50,7 @@ def declare_cc_toolchains(os, absolute_path, zig_include_root):
compiler = "clang",
abi_version = "unknown",
abi_libc_version = "unknown",
# visibility = ["//visibility:private"],
visibility = ["//visibility:private"],
)
native.cc_toolchain(
@ -68,5 +65,5 @@ def declare_cc_toolchains(os, absolute_path, zig_include_root):
objcopy_files = "@zig_sdk//:empty",
strip_files = "@zig_sdk//:empty",
supports_param_files = 0,
# visibility = ["//visibility:private"],
visibility = ["//visibility:private"],
)

View File

@ -1,9 +1,3 @@
DEFAULT_INCLUDE_DIRECTORIES = [
"include",
"libcxx/include",
"libcxxabi/include",
]
_ZIG_TOOL_PATH = "tools/{zig_tool}"
# Zig supports even older glibcs than defined below, but we have tested only
@ -101,12 +95,14 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version):
gotarget = "linux_{}_{}".format(gocpu, glibc_suffix),
zigtarget = "{}-linux-{}".format(zigcpu, glibc_suffix),
includes = [
"libunwind/include",
"libc/include/generic-glibc",
"libc/include/{}-linux-gnu".format(zigcpu),
"libc/include/generic-glibc",
] +
# x86_64-linux-any is x86_64-linux and x86-linux combined.
(["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []) +
(["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [
"libc/include/any-linux-any",
"libc/include/{}-linux-gnu".format(zigcpu),
"libc/include/{}-linux-any".format(zigcpu),
] + (["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []),
],
toplevel_include = ["glibc-hacks"] if fcntl_hack else [],
compiler_extra_includes = ["glibc-hacks/glibchack-fcntl.h"] if fcntl_hack else [],
linker_version_scripts = ["glibc-hacks/fcntl.map"] if fcntl_hack else [],
@ -127,11 +123,14 @@ def _target_linux_musl(gocpu, zigcpu):
gotarget = "linux_{}_musl".format(gocpu),
zigtarget = "{}-linux-musl".format(zigcpu),
includes = [
"libc/include/generic-musl",
"libc/include/{}-linux-musl".format(zigcpu),
"libc/include/generic-musl",
] +
# x86_64-linux-any is x86_64-linux and x86-linux combined.
(["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []) +
(["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [
"libc/include/any-linux-any",
"libc/include/{}-linux-musl".format(zigcpu),
"libc/include/{}-linux-any".format(zigcpu),
] + (["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []),
],
linkopts = [],
dynamic_library_linkopts = [],
copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"],