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

look for libc at runtime on linux #870

Closed
2 tasks
andrewrk opened this issue Mar 30, 2018 · 1 comment · Fixed by #872
Closed
2 tasks

look for libc at runtime on linux #870

andrewrk opened this issue Mar 30, 2018 · 1 comment · Fixed by #872
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-linux
Milestone

Comments

@andrewrk
Copy link
Member

Right now we have a pretty decent runtime libc detection on Windows. This enables us to provide automatically built artifacts from appveyor for every commit to master branch. Users can download the .zip file, unzip, and it just works.

Linux users can sometimes have a hard time obtaining newer zig releases, especially when llvm has recently released and hasn't quite made it to all the package managers.

I've managed to create a static build of zig using alpine linux inside docker (PR coming soon to https://github.com/zig-lang/docker-zig), but the static binary does not know where to find libc, since we currently rely on compile-time configuration of the default libc on linux.

I also noticed that zig does not find its own std lib based on its own exe path like it does on windows:
Unable to find zig lib directory. Reinstall Zig or use --zig-install-prefix.

This issue is to do the following things:

  • fix the logic on linux for zig compiler to find its own libraries by searching up the file system tree based on its own executable path. (This logic is present for windows and seems to be disabled for linux)
  • update zig to detect the system libc at runtime. we can follow the same strategy that we recommend in the README:
-DZIG_LIBC_LIB_DIR=$(dirname $(cc -print-file-name=crt1.o))
-DZIG_LIBC_INCLUDE_DIR=$(echo -n | cc -E -x c - -v 2>&1 | grep -B1 "End of search list." | head -n1 | cut -c 2- | sed "s/ .*//")
-DZIG_LIBC_STATIC_LIB_DIR=$(dirname $(cc -print-file-name=crtbegin.o))

Of course, instead of executing shell scripts, zig will directly run cc and do the text manipulations.

One can always overwrite this detection with command line arguments:

  --libc-include-dir [path]    directory where libc stdlib.h resides
  --libc-lib-dir [path]        directory where libc crt1.o resides
  --libc-static-lib-dir [path] directory where libc crtbegin.o resides
  --msvc-lib-dir [path]        (windows) directory where vcruntime.lib resides

If no c compiler is installed or no runtime libc can be detected, and no libc arguments are given, this is when, once #514 is done, we would use zig's own libc.

Once the above 2 checkbox items are complete, we can have Travis CI (or maybe docker?) build a static linux binary for each commit to master branch, and this will work on every linux distribution.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-linux labels Mar 30, 2018
@andrewrk andrewrk added this to the 0.3.0 milestone Mar 30, 2018
@andrewrk
Copy link
Member Author

(This logic is present for windows and seems to be disabled for linux)

aha :)

int os_self_exe_path(Buf *out_path) {
#if defined(ZIG_OS_WINDOWS)
    buf_resize(out_path, 256);
    for (;;) {
        DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path));
        if (copied_amt <= 0) {
            return ErrorFileNotFound;
        }
        if (copied_amt < buf_len(out_path)) {
            buf_resize(out_path, copied_amt);
            return 0;
        }
        buf_resize(out_path, buf_len(out_path) * 2);
    }

#elif defined(ZIG_OS_DARWIN)
    return ErrorFileNotFound;
#elif defined(ZIG_OS_LINUX)
    return ErrorFileNotFound;
#endif
    return ErrorFileNotFound;
}

andrewrk added a commit that referenced this issue Mar 30, 2018
this removes the following configure options:
 * ZIG_LIBC_LIB_DIR
 * ZIG_LIBC_STATIC_LIB_DIR
 * ZIG_LIBC_INCLUDE_DIR
 * ZIG_DYNAMIC_LINKER
 * ZIG_EACH_LIB_RPATH
 * zig's reliance on CMAKE_INSTALL_PREFIX

these options are still available as command line options, however,
the default will attempt to execute the system's C compiler to
collect system defaults for these values.

closes #870
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-linux
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant