authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-08 00:50:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-08 00:50:51-07:00
log430d0dfcb24178fd079be5d6988298bffec8beab
tree9212d6e5b43bc590cc5c436f092b3298a3e066b2
parentea3bd585634fa648cce7b597e611e9db35f5f929

support static linking against libc


7 files changed, 127 insertions(+), 56 deletions(-)

README.md+5-4
......@@ -70,13 +70,14 @@ compromises backward compatibility.
7070
7171### Debug / Development Build
7272
73If you have gcc or clang installed, you can find out what `ZIG_LIBC_LIB_DIR` should
74be set to (example below). `ZIG_LIBC_INCLUDE_DIR` likely can be set to `/usr/include`.
73If you have gcc or clang installed, you can find out what `ZIG_LIBC_LIB_DIR`
74and `ZIG_LIBC_STATIC_LIB_DIR` should be set to (example below).
75`ZIG_LIBC_INCLUDE_DIR` likely can be set to `/usr/include`.
7576
7677```
7778mkdir build
7879cd build
79cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd) -DZIG_LIBC_LIB_DIR=$(dirname $(cc -print-file-name=crt1.o)) -DZIG_LIBC_INCLUDE_DIR=/usr/include
80cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd) -DZIG_LIBC_LIB_DIR=$(dirname $(cc -print-file-name=crt1.o)) -DZIG_LIBC_INCLUDE_DIR=/usr/include -DZIG_LIBC_STATIC_LIB_DIR=$(dirname $(cc -print-file-name=crtbeginT.o))
8081make
8182make install
8283./run_tests
......@@ -90,7 +91,7 @@ by the `--libc-lib-dir` and `--libc-include-dir` parameters to the zig binary.
9091```
9192mkdir build
9293cd build
93cmake .. -DCMAKE_BUILD_TYPE=Release -DZIG_LIBC_LIB_DIR=/some/path -DZIG_LIBC_INCLUDE_DIR=/some/path
94cmake .. -DCMAKE_BUILD_TYPE=Release -DZIG_LIBC_LIB_DIR=/some/path -DZIG_LIBC_INCLUDE_DIR=/some/path -DZIG_LIBC_STATIC_INCLUDE_DIR=/some/path
9495make
9596sudo make install
9697```
src/all_types.hpp+1
......@@ -1104,6 +1104,7 @@ struct CodeGen {
11041104 bool have_exported_main;
11051105 bool link_libc;
11061106 Buf *libc_lib_dir;
1107 Buf *libc_static_lib_dir;
11071108 Buf *libc_include_dir;
11081109 bool is_release_build;
11091110 bool is_test_build;
src/analyze.cpp+3
......@@ -5852,6 +5852,9 @@ void find_libc_path(CodeGen *g) {
58525852 if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0) {
58535853 zig_panic("Unable to determine libc lib path. probably need to reconfigure");
58545854 }
5855 if (!g->libc_static_lib_dir || buf_len(g->libc_static_lib_dir) == 0) {
5856 zig_panic("Unable to determine libc static lib path. probably need to reconfigure");
5857 }
58555858 if (!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
58565859 zig_panic("Unable to determine libc include path. probably need to reconfigure");
58575860 }
src/codegen.cpp+92-35
......@@ -35,6 +35,7 @@ CodeGen *codegen_create(Buf *root_source_dir) {
3535 g->error_value_count = 1;
3636
3737 g->libc_lib_dir = buf_create_from_str(ZIG_LIBC_LIB_DIR);
38 g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
3839 g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
3940
4041 return g;
......@@ -81,6 +82,10 @@ void codegen_set_libc_lib_dir(CodeGen *g, Buf *libc_lib_dir) {
8182 g->libc_lib_dir = libc_lib_dir;
8283}
8384
85void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir) {
86 g->libc_static_lib_dir = libc_static_lib_dir;
87}
88
8489void codegen_set_libc_include_dir(CodeGen *g, Buf *libc_include_dir) {
8590 g->libc_include_dir = libc_include_dir;
8691}
......@@ -4010,6 +4015,12 @@ static const char *get_libc_file(CodeGen *g, const char *file) {
40104015 return buf_ptr(out_buf);
40114016}
40124017
4018static const char *get_libc_static_file(CodeGen *g, const char *file) {
4019 Buf *out_buf = buf_alloc();
4020 os_path_join(g->libc_static_lib_dir, buf_create_from_str(file), out_buf);
4021 return buf_ptr(out_buf);
4022}
4023
40134024static Buf *build_o(CodeGen *parent_gen, const char *oname) {
40144025 Buf *source_basename = buf_sprintf("%s.zig", oname);
40154026 Buf *std_dir_path = buf_create_from_str(ZIG_STD_DIR);
......@@ -4096,27 +4107,63 @@ void codegen_link(CodeGen *g, const char *out_file) {
40964107 return;
40974108 }
40984109
4110 bool link_in_crt = (g->link_libc && g->out_type == OutTypeExe);
4111 if (link_in_crt) {
4112 find_libc_path(g);
4113 }
4114
4115
40994116
41004117 // invoke `ld`
41014118 ZigList<const char *> args = {0};
4102 const char *crt1o;
4119
4120 // TODO make this target dependent
4121 args.append("-m");
4122 args.append("elf_x86_64");
4123
41034124 if (g->is_static) {
41044125 args.append("-static");
4105 crt1o = "crt1.o";
4106 } else {
4107 crt1o = "Scrt1.o";
41084126 }
41094127
4110 // TODO don't pass this parameter unless linking with libc
4111 char *ZIG_NATIVE_DYNAMIC_LINKER = getenv("ZIG_NATIVE_DYNAMIC_LINKER");
4112 if (g->is_native_target && ZIG_NATIVE_DYNAMIC_LINKER) {
4113 if (ZIG_NATIVE_DYNAMIC_LINKER[0] != 0) {
4114 args.append("-dynamic-linker");
4115 args.append(ZIG_NATIVE_DYNAMIC_LINKER);
4128 args.append("-o");
4129 args.append(out_file);
4130
4131 if (link_in_crt) {
4132 const char *crt1o;
4133 const char *crtbegino;
4134 if (g->is_static) {
4135 crt1o = "crt1.o";
4136 crtbegino = "crtbeginT.o";
4137 } else {
4138 crt1o = "Scrt1.o";
4139 crtbegino = "crtbegin.o";
41164140 }
4117 } else {
4141 args.append(get_libc_file(g, crt1o));
4142 args.append(get_libc_file(g, "crti.o"));
4143 args.append(get_libc_static_file(g, crtbegino));
4144 }
4145
4146 for (int i = 0; i < g->lib_dirs.length; i += 1) {
4147 const char *lib_dir = g->lib_dirs.at(i);
4148 args.append("-L");
4149 args.append(lib_dir);
4150 }
4151
4152 if (g->link_libc) {
4153 args.append("-L");
4154 args.append(buf_ptr(g->libc_lib_dir));
4155
4156 args.append("-L");
4157 args.append(buf_ptr(g->libc_static_lib_dir));
4158 }
4159
4160 // TODO don't pass this parameter unless linking with libc
4161 if (ZIG_DYNAMIC_LINKER[0] == 0) {
41184162 args.append("-dynamic-linker");
41194163 args.append(buf_ptr(get_dynamic_linker(g->target_machine)));
4164 } else {
4165 args.append("-dynamic-linker");
4166 args.append(ZIG_DYNAMIC_LINKER);
41204167 }
41214168
41224169 if (g->out_type == OutTypeLib) {
......@@ -4129,24 +4176,9 @@ void codegen_link(CodeGen *g, const char *out_file) {
41294176 out_file = buf_ptr(out_lib_so);
41304177 }
41314178
4132 args.append("-o");
4133 args.append(out_file);
4134
4135 bool link_in_crt = (g->link_libc && g->out_type == OutTypeExe);
4136
4137 if (link_in_crt) {
4138 find_libc_path(g);
4139
4140 args.append(get_libc_file(g, crt1o));
4141 args.append(get_libc_file(g, "crti.o"));
4142 }
4143
4179 // .o files
41444180 args.append((const char *)buf_ptr(&out_file_o));
41454181
4146 if (link_in_crt) {
4147 args.append(get_libc_file(g, "crtn.o"));
4148 }
4149
41504182 if (g->is_test_build) {
41514183 const char *test_runner_name = g->link_libc ? "test_runner_libc" : "test_runner_nolibc";
41524184 Buf *test_runner_o_path = build_o(g, test_runner_name);
......@@ -4158,20 +4190,45 @@ void codegen_link(CodeGen *g, const char *out_file) {
41584190 args.append(buf_ptr(builtin_o_path));
41594191 }
41604192
4161 for (int i = 0; i < g->lib_dirs.length; i += 1) {
4162 const char *lib_dir = g->lib_dirs.at(i);
4163 args.append("-L");
4164 args.append(lib_dir);
4165 }
4166
41674193 auto it = g->link_table.entry_iterator();
41684194 for (;;) {
41694195 auto *entry = it.next();
41704196 if (!entry)
41714197 break;
41724198
4173 Buf *arg = buf_sprintf("-l%s", buf_ptr(entry->key));
4174 args.append(buf_ptr(arg));
4199 // we handle libc explicitly, don't do it here
4200 if (!buf_eql_str(entry->key, "c")) {
4201 Buf *arg = buf_sprintf("-l%s", buf_ptr(entry->key));
4202 args.append(buf_ptr(arg));
4203 }
4204 }
4205
4206
4207 // libc dep
4208 if (g->link_libc) {
4209 if (g->is_static) {
4210 args.append("--start-group");
4211 args.append("-lgcc");
4212 args.append("-lgcc_eh");
4213 args.append("-lc");
4214 args.append("--end-group");
4215 } else {
4216 args.append("-lgcc");
4217 args.append("--as-needed");
4218 args.append("-lgcc_s");
4219 args.append("--no-as-needed");
4220 args.append("-lc");
4221 args.append("-lgcc");
4222 args.append("--as-needed");
4223 args.append("-lgcc_s");
4224 args.append("--no-as-needed");
4225 }
4226 }
4227
4228 // crt end
4229 if (link_in_crt) {
4230 args.append(get_libc_static_file(g, "crtend.o"));
4231 args.append(get_libc_file(g, "crtn.o"));
41754232 }
41764233
41774234 if (g->verbose) {
src/codegen.hpp+1
......@@ -26,6 +26,7 @@ void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);
2626void codegen_set_out_type(CodeGen *codegen, OutType out_type);
2727void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
2828void codegen_set_libc_lib_dir(CodeGen *codegen, Buf *libc_lib_dir);
29void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
2930void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
3031void codegen_add_lib_dir(CodeGen *codegen, const char *dir);
3132
src/config.h.in+2
......@@ -10,6 +10,8 @@
1010#define ZIG_STD_DIR "@CMAKE_INSTALL_PREFIX@/@ZIG_STD_DEST@"
1111#define ZIG_LIBC_INCLUDE_DIR "@ZIG_LIBC_INCLUDE_DIR@"
1212#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR@"
13#define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR@"
14#define ZIG_DYNAMIC_LINKER "@ZIG_DYNAMIC_LINKER@"
1315
1416#cmakedefine ZIG_LLVM_OLD_CXX_ABI
1517
src/main.cpp+23-17
......@@ -16,24 +16,25 @@
1616static int usage(const char *arg0) {
1717 fprintf(stderr, "Usage: %s [command] [options]\n"
1818 "Commands:\n"
19 " build create executable, object, or library from target\n"
20 " test create and run a test build\n"
21 " version print version number and exit\n"
22 " parseh convert a c header file to zig extern declarations\n"
19 " build create executable, object, or library from target\n"
20 " test create and run a test build\n"
21 " version print version number and exit\n"
22 " parseh convert a c header file to zig extern declarations\n"
2323 "Options:\n"
24 " --release build with optimizations on and debug protection off\n"
25 " --static output will be statically linked\n"
26 " --strip exclude debug symbols\n"
27 " --export [exe|lib|obj] override output type\n"
28 " --name [name] override output name\n"
29 " --output [file] override destination path\n"
30 " --verbose turn on compiler debug output\n"
31 " --color [auto|off|on] enable or disable colored error messages\n"
32 " --libc-lib-dir [path] set the C compiler data path\n"
33 " --libc-include-dir [path] set the C compiler data path\n"
34 " -isystem [dir] add additional search path for other .h files\n"
35 " -dirafter [dir] same as -isystem but do it last\n"
36 " --library-path [dir] add a directory to the library search path\n"
24 " --release build with optimizations on and debug protection off\n"
25 " --static output will be statically linked\n"
26 " --strip exclude debug symbols\n"
27 " --export [exe|lib|obj] override output type\n"
28 " --name [name] override output name\n"
29 " --output [file] override destination path\n"
30 " --verbose turn on compiler debug output\n"
31 " --color [auto|off|on] enable or disable colored error messages\n"
32 " --libc-lib-dir [path] set the C compiler data path\n"
33 " --libc-static-lib-dir [path] set the C compiler data path\n"
34 " --libc-include-dir [path] set the C compiler data path\n"
35 " -isystem [dir] add additional search path for other .h files\n"
36 " -dirafter [dir] same as -isystem but do it last\n"
37 " --library-path [dir] add a directory to the library search path\n"
3738 , arg0);
3839 return EXIT_FAILURE;
3940}
......@@ -59,6 +60,7 @@ int main(int argc, char **argv) {
5960 bool verbose = false;
6061 ErrColor color = ErrColorAuto;
6162 const char *libc_lib_dir = nullptr;
63 const char *libc_static_lib_dir = nullptr;
6264 const char *libc_include_dir = nullptr;
6365 ZigList<const char *> clang_argv = {0};
6466 ZigList<const char *> lib_dirs = {0};
......@@ -108,6 +110,8 @@ int main(int argc, char **argv) {
108110 out_name = argv[i];
109111 } else if (strcmp(arg, "--libc-lib-dir") == 0) {
110112 libc_lib_dir = argv[i];
113 } else if (strcmp(arg, "--libc-static-lib-dir") == 0) {
114 libc_static_lib_dir = argv[i];
111115 } else if (strcmp(arg, "--libc-include-dir") == 0) {
112116 libc_include_dir = argv[i];
113117 } else if (strcmp(arg, "-isystem") == 0) {
......@@ -202,6 +206,8 @@ int main(int argc, char **argv) {
202206 }
203207 if (libc_lib_dir)
204208 codegen_set_libc_lib_dir(g, buf_create_from_str(libc_lib_dir));
209 if (libc_static_lib_dir)
210 codegen_set_libc_static_lib_dir(g, buf_create_from_str(libc_static_lib_dir));
205211 if (libc_include_dir)
206212 codegen_set_libc_include_dir(g, buf_create_from_str(libc_include_dir));
207213 codegen_set_verbose(g, verbose);