authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-11 14:44:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-11 14:44:10-07:00
log26718a619c572602c21c83b1588e50723447709a
tree6c28906895aaf119e7d344c58b5b0f2c2380fb7a
parent6b7ffd4cbe1006013cda4a36c9659c4822ae1b53

recognize ar program and pass --gc-sections to ld

See #54

10 files changed, 29 insertions(+), 4 deletions(-)

CMakeLists.txt+2
...@@ -18,6 +18,7 @@ set(ZIG_LIBC_LIB_DIR "" CACHE STRING "Default native target libc directory where...@@ -18,6 +18,7 @@ set(ZIG_LIBC_LIB_DIR "" CACHE STRING "Default native target libc directory where
18set(ZIG_LIBC_STATIC_LIB_DIR "" CACHE STRING "Default native target libc directory where crtbeginT.o can be found")18set(ZIG_LIBC_STATIC_LIB_DIR "" CACHE STRING "Default native target libc directory where crtbeginT.o can be found")
19set(ZIG_LIBC_INCLUDE_DIR "/usr/include" CACHE STRING "Default native target libc include directory")19set(ZIG_LIBC_INCLUDE_DIR "/usr/include" CACHE STRING "Default native target libc include directory")
20set(ZIG_LD_PATH "ld" CACHE STRING "Path to ld for the native target")20set(ZIG_LD_PATH "ld" CACHE STRING "Path to ld for the native target")
21set(ZIG_AR_PATH "ar" CACHE STRING "Path to ar for the native target")
21set(ZIG_DYNAMIC_LINKER "" CACHE STRING "Override dynamic linker for native target")22set(ZIG_DYNAMIC_LINKER "" CACHE STRING "Override dynamic linker for native target")
2223
23option(ZIG_TEST_COVERAGE "Build Zig with test coverage instrumentation" OFF)24option(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...@@ -217,6 +218,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DE
217install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")218install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")
218install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")219install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")
219install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")220install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")
221install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")
220222
221add_executable(run_tests ${TEST_SOURCES})223add_executable(run_tests ${TEST_SOURCES})
222target_link_libraries(run_tests)224target_link_libraries(run_tests)
src/all_types.hpp+1
...@@ -1214,6 +1214,7 @@ struct CodeGen {...@@ -1214,6 +1214,7 @@ struct CodeGen {
1214 Buf *libc_include_dir;1214 Buf *libc_include_dir;
1215 Buf *dynamic_linker;1215 Buf *dynamic_linker;
1216 Buf *linker_path;1216 Buf *linker_path;
1217 Buf *ar_path;
1217 Buf triple_str;1218 Buf triple_str;
1218 bool is_release_build;1219 bool is_release_build;
1219 bool is_test_build;1220 bool is_test_build;
src/codegen.cpp+6
...@@ -85,6 +85,7 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {...@@ -85,6 +85,7 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {
85 g->libc_static_lib_dir = buf_create_from_str("");85 g->libc_static_lib_dir = buf_create_from_str("");
86 g->libc_include_dir = buf_create_from_str("");86 g->libc_include_dir = buf_create_from_str("");
87 g->linker_path = buf_create_from_str("");87 g->linker_path = buf_create_from_str("");
88 g->ar_path = buf_create_from_str("");
88 g->darwin_linker_version = buf_create_from_str("");89 g->darwin_linker_version = buf_create_from_str("");
89 } else {90 } else {
90 // native compilation, we can rely on the configuration stuff91 // native compilation, we can rely on the configuration stuff
...@@ -96,6 +97,7 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {...@@ -96,6 +97,7 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {
96 g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);97 g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
97 g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);98 g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
98 g->linker_path = buf_create_from_str(ZIG_LD_PATH);99 g->linker_path = buf_create_from_str(ZIG_LD_PATH);
100 g->ar_path = buf_create_from_str(ZIG_AR_PATH);
99 g->darwin_linker_version = buf_create_from_str(ZIG_HOST_LINK_VERSION);101 g->darwin_linker_version = buf_create_from_str(ZIG_HOST_LINK_VERSION);
100102
101 if (g->zig_target.os == ZigLLVM_Darwin ||103 if (g->zig_target.os == ZigLLVM_Darwin ||
...@@ -171,6 +173,10 @@ void codegen_set_linker_path(CodeGen *g, Buf *linker_path) {...@@ -171,6 +173,10 @@ void codegen_set_linker_path(CodeGen *g, Buf *linker_path) {
171 g->linker_path = linker_path;173 g->linker_path = linker_path;
172}174}
173175
176void codegen_set_ar_path(CodeGen *g, Buf *ar_path) {
177 g->ar_path = ar_path;
178}
179
174void codegen_add_lib_dir(CodeGen *g, const char *dir) {180void codegen_add_lib_dir(CodeGen *g, const char *dir) {
175 g->lib_dirs.append(dir);181 g->lib_dirs.append(dir);
176}182}
src/codegen.hpp+1
...@@ -32,6 +32,7 @@ void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);...@@ -32,6 +32,7 @@ void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
32void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);32void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
33void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);33void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);
34void codegen_set_linker_path(CodeGen *g, Buf *linker_path);34void codegen_set_linker_path(CodeGen *g, Buf *linker_path);
35void codegen_set_ar_path(CodeGen *g, Buf *ar_path);
35void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);36void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);
36void codegen_set_windows_unicode(CodeGen *g, bool municode);37void codegen_set_windows_unicode(CodeGen *g, bool municode);
37void codegen_add_lib_dir(CodeGen *codegen, const char *dir);38void codegen_add_lib_dir(CodeGen *codegen, const char *dir);
src/config.h.in+1
...@@ -12,6 +12,7 @@...@@ -12,6 +12,7 @@
12#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR@"12#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR@"
13#define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR@"13#define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR@"
14#define ZIG_LD_PATH "@ZIG_LD_PATH@"14#define ZIG_LD_PATH "@ZIG_LD_PATH@"
15#define ZIG_AR_PATH "@ZIG_AR_PATH@"
15#define ZIG_DYNAMIC_LINKER "@ZIG_DYNAMIC_LINKER@"16#define ZIG_DYNAMIC_LINKER "@ZIG_DYNAMIC_LINKER@"
16#define ZIG_HOST_LINK_VERSION "@ZIG_HOST_LINK_VERSION@"17#define ZIG_HOST_LINK_VERSION "@ZIG_HOST_LINK_VERSION@"
1718
src/link.cpp+1
...@@ -152,6 +152,7 @@ static void construct_linker_job_linux(LinkJob *lj) {...@@ -152,6 +152,7 @@ static void construct_linker_job_linux(LinkJob *lj) {
152 find_libc_lib_path(g);152 find_libc_lib_path(g);
153 }153 }
154154
155 lj->args.append("--gc-sections");
155156
156 lj->args.append("-m");157 lj->args.append("-m");
157 lj->args.append(getLDMOption(&g->zig_target));158 lj->args.append(getLDMOption(&g->zig_target));
src/main.cpp+6
...@@ -37,6 +37,7 @@ static int usage(const char *arg0) {...@@ -37,6 +37,7 @@ static int usage(const char *arg0) {
37 " --libc-include-dir [path] directory where libc stdlib.h resides\n"37 " --libc-include-dir [path] directory where libc stdlib.h resides\n"
38 " --dynamic-linker [path] set the path to ld.so\n"38 " --dynamic-linker [path] set the path to ld.so\n"
39 " --ld-path [path] set the path to the linker\n"39 " --ld-path [path] set the path to the linker\n"
40 " --ar-path [path] set the path to ar\n"
40 " -isystem [dir] add additional search path for other .h files\n"41 " -isystem [dir] add additional search path for other .h files\n"
41 " -dirafter [dir] same as -isystem but do it last\n"42 " -dirafter [dir] same as -isystem but do it last\n"
42 " --library-path [dir] add a directory to the library search path\n"43 " --library-path [dir] add a directory to the library search path\n"
...@@ -118,6 +119,7 @@ int main(int argc, char **argv) {...@@ -118,6 +119,7 @@ int main(int argc, char **argv) {
118 const char *libc_include_dir = nullptr;119 const char *libc_include_dir = nullptr;
119 const char *dynamic_linker = nullptr;120 const char *dynamic_linker = nullptr;
120 const char *linker_path = nullptr;121 const char *linker_path = nullptr;
122 const char *ar_path = nullptr;
121 ZigList<const char *> clang_argv = {0};123 ZigList<const char *> clang_argv = {0};
122 ZigList<const char *> lib_dirs = {0};124 ZigList<const char *> lib_dirs = {0};
123 ZigList<const char *> link_libs = {0};125 ZigList<const char *> link_libs = {0};
...@@ -196,6 +198,8 @@ int main(int argc, char **argv) {...@@ -196,6 +198,8 @@ int main(int argc, char **argv) {
196 dynamic_linker = argv[i];198 dynamic_linker = argv[i];
197 } else if (strcmp(arg, "--ld-path") == 0) {199 } else if (strcmp(arg, "--ld-path") == 0) {
198 linker_path = argv[i];200 linker_path = argv[i];
201 } else if (strcmp(arg, "--ar-path") == 0) {
202 ar_path = argv[i];
199 } else if (strcmp(arg, "-isystem") == 0) {203 } else if (strcmp(arg, "-isystem") == 0) {
200 clang_argv.append("-isystem");204 clang_argv.append("-isystem");
201 clang_argv.append(argv[i]);205 clang_argv.append(argv[i]);
...@@ -356,6 +360,8 @@ int main(int argc, char **argv) {...@@ -356,6 +360,8 @@ int main(int argc, char **argv) {
356 codegen_set_dynamic_linker(g, buf_create_from_str(dynamic_linker));360 codegen_set_dynamic_linker(g, buf_create_from_str(dynamic_linker));
357 if (linker_path)361 if (linker_path)
358 codegen_set_linker_path(g, buf_create_from_str(linker_path));362 codegen_set_linker_path(g, buf_create_from_str(linker_path));
363 if (ar_path)
364 codegen_set_ar_path(g, buf_create_from_str(ar_path));
359 codegen_set_verbose(g, verbose);365 codegen_set_verbose(g, verbose);
360 codegen_set_errmsg_color(g, color);366 codegen_set_errmsg_color(g, color);
361367
std/empty.zig created
std/index.zig+6
...@@ -8,7 +8,13 @@ pub const net = @import("net.zig");...@@ -8,7 +8,13 @@ pub const net = @import("net.zig");
8pub const list = @import("list.zig");8pub const list = @import("list.zig");
9pub const hash_map = @import("hash_map.zig");9pub const hash_map = @import("hash_map.zig");
10pub const mem = @import("mem.zig");10pub const mem = @import("mem.zig");
11pub const linux = switch(@compile_var("os")) {
12 linux => @import("linux.zig"),
13 else => null_import,
14};
1115
12pub fn assert(b: bool) {16pub fn assert(b: bool) {
13 if (!b) unreachable{}17 if (!b) unreachable{}
14}18}
19
20const null_import = @import("empty.zig");
std/io.zig+5-4
...@@ -119,10 +119,10 @@ pub struct OutStream {...@@ -119,10 +119,10 @@ pub struct OutStream {
119 }119 }
120120
121 pub fn flush(os: &OutStream) -> %void {121 pub fn flush(os: &OutStream) -> %void {
122 const amt_written = linux.write(os.fd, &os.buffer[0], os.index);122 const write_ret = linux.write(os.fd, &os.buffer[0], os.index);
123 os.index = 0;123 const write_err = linux.get_errno(write_ret);
124 if (amt_written < 0) {124 if (write_err > 0) {
125 return switch (-amt_written) {125 return switch (write_err) {
126 errno.EINVAL => unreachable{},126 errno.EINVAL => unreachable{},
127 errno.EDQUOT => error.DiskQuota,127 errno.EDQUOT => error.DiskQuota,
128 errno.EFBIG => error.FileTooBig,128 errno.EFBIG => error.FileTooBig,
...@@ -134,6 +134,7 @@ pub struct OutStream {...@@ -134,6 +134,7 @@ pub struct OutStream {
134 else => error.Unexpected,134 else => error.Unexpected,
135 }135 }
136 }136 }
137 os.index = 0;
137 }138 }
138139
139 pub fn close(os: &OutStream) -> %void {140 pub fn close(os: &OutStream) -> %void {