Skip to content

zig c++ -MD -MF produces non-hermetic dependencies #12980

Closed

Description

@motiejus

Summary

The .d file produced by zig c++ -MD depends on the absolute path
of zig sdk. This is bad for reproducibility and hermeticity.

Steps to Reproduce

Copy over the zig lib to the current directory and try to instruct zig c++ to
use that. It will still point to the absolute paths:

$ cp -r /path/to/zig/lib .
$ cat <<EOF > main.cc
#include <float.h>
int main() { return 0; }
EOF
$ zig c++ -MD -isystem lib/libcxx/include -isystem lib/include -isystem lib/libc/include/generic-glibc main.cc
$ cat main.d
/home/motiejus/.cache/zig/tmp/c845ca506ebdf8dc-main.o: main.cc \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libcxx/include/float.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libcxx/include/__config \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/generic-glibc/features.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/generic-glibc/features-time64.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/x86_64-linux-gnu/bits/wordsize.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/x86_64-linux-gnu/bits/timesize.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/generic-glibc/stdc-predef.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/generic-glibc/sys/cdefs.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/x86_64-linux-gnu/bits/long-double.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/x86_64-linux-gnu/gnu/stubs.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/libc/include/x86_64-linux-gnu/gnu/stubs-64.h \
  /code/zig-linux-x86_64-0.10.0-dev.3838+77f31ebbb/lib/include/float.h \
  lib/libcxx/include/float.h

Expected result

Something akin to clang, where the resulting header files are declared relatively:

$ clang-15 -MD -isystem lib/libcxx/include -isystem lib/include -isystem lib/libc/include/generic-glibc main.cc
$ cat main.d
main.o: main.cc lib/libcxx/include/float.h lib/libcxx/include/__config \
  lib/libc/include/generic-glibc/features.h \
  lib/libc/include/generic-glibc/features-time64.h \
  lib/libc/include/generic-glibc/bits/wordsize.h \
  lib/libc/include/generic-glibc/sgidefs.h \
  lib/libc/include/generic-glibc/bits/timesize.h \
  lib/libc/include/generic-glibc/stdc-predef.h \
  lib/libc/include/generic-glibc/sys/cdefs.h \
  lib/libc/include/generic-glibc/bits/long-double.h \
  lib/libc/include/generic-glibc/gnu/stubs.h lib/include/float.h \
  /usr/lib/llvm-15/lib/clang/15.0.1/include/float.h

(The last line is incorrect; my clang-foo is not good enough to make all of them relative; but hopefully it conveys the idea.)

Impact

Bazel:

  • Is very prescriptive about where the compilation dependencies are. Everything
    that is used for compilation needs to be managed via Bazel.
  • Has decent remote cache support, but that requires all paths to be relative,
    otherwise the Bazel's cache keys will be different, which busts our remote
    cache.

Currently the resulting .d file contains absolute paths, which is undesirable.

Discussion

zig c++ adds include paths for libcxxabi, libcxx and libunwind:

zig/src/libcxx.zig

Lines 127 to 128 in 9cd521a

const cxxabi_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", "include" });
const cxx_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "include" });

zig/src/libcxx.zig

Lines 190 to 194 in 9cd521a

try cflags.append("-I");
try cflags.append(cxx_include_path);
try cflags.append("-I");
try cflags.append(cxxabi_include_path);

zig/src/Compilation.zig

Lines 4105 to 4110 in 9cd521a

const libunwind_include_path = try std.fs.path.join(arena, &[_][]const u8{
comp.zig_lib_directory.path.?, "libunwind", "include",
});
try argv.append("-isystem");
try argv.append(libunwind_include_path);

The zig_lib_directory path is hardcoded. If only we could specify it
ourselves, that would be great.

-stdlib++-isystem may be a step towards that direction; however, it leaves
paths for libcxxabi and libunwind without a way to configure it.

Activity

  1. added
    bug
    on Sep 26, 2022
  2. motiejus commented on Sep 26, 2022

    @motiejus
    ContributorAuthor

    An environment variable ala ZIG_LIB_DIR would do the trick here (optional argument is not an option, because zig cc/c++ only accept what underlying clang accepts). But I am not sure if's the desired approach.

  3. motiejus commented on Sep 27, 2022

    @motiejus
    ContributorAuthor

    🤦 ZIG_LIB_DIR is a thing with zig c++ too.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions