Commit 26718a619c

Andrew Kelley <superjoe30@gmail.com>
2016-05-11 23:44:10
recognize ar program and pass --gc-sections to ld
See #54
1 parent 6b7ffd4
src/all_types.hpp
@@ -1214,6 +1214,7 @@ struct CodeGen {
     Buf *libc_include_dir;
     Buf *dynamic_linker;
     Buf *linker_path;
+    Buf *ar_path;
     Buf triple_str;
     bool is_release_build;
     bool is_test_build;
src/codegen.cpp
@@ -85,6 +85,7 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {
         g->libc_static_lib_dir = buf_create_from_str("");
         g->libc_include_dir = buf_create_from_str("");
         g->linker_path = buf_create_from_str("");
+        g->ar_path = buf_create_from_str("");
         g->darwin_linker_version = buf_create_from_str("");
     } else {
         // native compilation, we can rely on the configuration stuff
@@ -96,6 +97,7 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {
         g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
         g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
         g->linker_path = buf_create_from_str(ZIG_LD_PATH);
+        g->ar_path = buf_create_from_str(ZIG_AR_PATH);
         g->darwin_linker_version = buf_create_from_str(ZIG_HOST_LINK_VERSION);
 
         if (g->zig_target.os == ZigLLVM_Darwin ||
@@ -171,6 +173,10 @@ void codegen_set_linker_path(CodeGen *g, Buf *linker_path) {
     g->linker_path = linker_path;
 }
 
+void codegen_set_ar_path(CodeGen *g, Buf *ar_path) {
+    g->ar_path = ar_path;
+}
+
 void codegen_add_lib_dir(CodeGen *g, const char *dir) {
     g->lib_dirs.append(dir);
 }
src/codegen.hpp
@@ -32,6 +32,7 @@ void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
 void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
 void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);
 void codegen_set_linker_path(CodeGen *g, Buf *linker_path);
+void codegen_set_ar_path(CodeGen *g, Buf *ar_path);
 void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);
 void codegen_set_windows_unicode(CodeGen *g, bool municode);
 void codegen_add_lib_dir(CodeGen *codegen, const char *dir);
src/config.h.in
@@ -12,6 +12,7 @@
 #define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR@"
 #define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR@"
 #define ZIG_LD_PATH "@ZIG_LD_PATH@"
+#define ZIG_AR_PATH "@ZIG_AR_PATH@"
 #define ZIG_DYNAMIC_LINKER "@ZIG_DYNAMIC_LINKER@"
 #define ZIG_HOST_LINK_VERSION "@ZIG_HOST_LINK_VERSION@"
 
src/link.cpp
@@ -152,6 +152,7 @@ static void construct_linker_job_linux(LinkJob *lj) {
         find_libc_lib_path(g);
     }
 
+    lj->args.append("--gc-sections");
 
     lj->args.append("-m");
     lj->args.append(getLDMOption(&g->zig_target));
src/main.cpp
@@ -37,6 +37,7 @@ static int usage(const char *arg0) {
         "  --libc-include-dir [path]    directory where libc stdlib.h resides\n"
         "  --dynamic-linker [path]      set the path to ld.so\n"
         "  --ld-path [path]             set the path to the linker\n"
+        "  --ar-path [path]             set the path to ar\n"
         "  -isystem [dir]               add additional search path for other .h files\n"
         "  -dirafter [dir]              same as -isystem but do it last\n"
         "  --library-path [dir]         add a directory to the library search path\n"
@@ -118,6 +119,7 @@ int main(int argc, char **argv) {
     const char *libc_include_dir = nullptr;
     const char *dynamic_linker = nullptr;
     const char *linker_path = nullptr;
+    const char *ar_path = nullptr;
     ZigList<const char *> clang_argv = {0};
     ZigList<const char *> lib_dirs = {0};
     ZigList<const char *> link_libs = {0};
@@ -196,6 +198,8 @@ int main(int argc, char **argv) {
                     dynamic_linker = argv[i];
                 } else if (strcmp(arg, "--ld-path") == 0) {
                     linker_path = argv[i];
+                } else if (strcmp(arg, "--ar-path") == 0) {
+                    ar_path = argv[i];
                 } else if (strcmp(arg, "-isystem") == 0) {
                     clang_argv.append("-isystem");
                     clang_argv.append(argv[i]);
@@ -356,6 +360,8 @@ int main(int argc, char **argv) {
                 codegen_set_dynamic_linker(g, buf_create_from_str(dynamic_linker));
             if (linker_path)
                 codegen_set_linker_path(g, buf_create_from_str(linker_path));
+            if (ar_path)
+                codegen_set_ar_path(g, buf_create_from_str(ar_path));
             codegen_set_verbose(g, verbose);
             codegen_set_errmsg_color(g, color);
 
std/empty.zig
std/index.zig
@@ -8,7 +8,13 @@ pub const net = @import("net.zig");
 pub const list = @import("list.zig");
 pub const hash_map = @import("hash_map.zig");
 pub const mem = @import("mem.zig");
+pub const linux = switch(@compile_var("os")) {
+    linux => @import("linux.zig"),
+    else => null_import,
+};
 
 pub fn assert(b: bool) {
     if (!b) unreachable{}
 }
+
+const null_import = @import("empty.zig");
std/io.zig
@@ -119,10 +119,10 @@ pub struct OutStream {
     }
 
     pub fn flush(os: &OutStream) -> %void {
-        const amt_written = linux.write(os.fd, &os.buffer[0], os.index);
-        os.index = 0;
-        if (amt_written < 0) {
-            return switch (-amt_written) {
+        const write_ret = linux.write(os.fd, &os.buffer[0], os.index);
+        const write_err = linux.get_errno(write_ret);
+        if (write_err > 0) {
+            return switch (write_err) {
                 errno.EINVAL => unreachable{},
                 errno.EDQUOT => error.DiskQuota,
                 errno.EFBIG  => error.FileTooBig,
@@ -134,6 +134,7 @@ pub struct OutStream {
                 else         => error.Unexpected,
             }
         }
+        os.index = 0;
     }
 
     pub fn close(os: &OutStream) -> %void {
CMakeLists.txt
@@ -18,6 +18,7 @@ set(ZIG_LIBC_LIB_DIR "" CACHE STRING "Default native target libc directory where
 set(ZIG_LIBC_STATIC_LIB_DIR "" CACHE STRING "Default native target libc directory where crtbeginT.o can be found")
 set(ZIG_LIBC_INCLUDE_DIR "/usr/include" CACHE STRING "Default native target libc include directory")
 set(ZIG_LD_PATH "ld" CACHE STRING "Path to ld for the native target")
+set(ZIG_AR_PATH "ar" CACHE STRING "Path to ar for the native target")
 set(ZIG_DYNAMIC_LINKER "" CACHE STRING "Override dynamic linker for native target")
 
 option(ZIG_TEST_COVERAGE "Build Zig with test coverage instrumentation" OFF)
@@ -217,6 +218,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DE
 install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")
 install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")
 install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")
+install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")
 
 add_executable(run_tests ${TEST_SOURCES})
 target_link_libraries(run_tests)