Commit 0ac714f0d6

Andrew Kelley <superjoe30@gmail.com>
2016-02-01 02:32:07
add --library-path cli option
1 parent 41b95cc
src/all_types.hpp
@@ -1096,6 +1096,7 @@ struct CodeGen {
 
     const char **clang_argv;
     int clang_argv_len;
+    ZigList<const char *> lib_dirs;
 };
 
 struct VariableTableEntry {
src/analyze.cpp
@@ -538,7 +538,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId fn_type_id) {
     fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
             gen_param_types, gen_param_index, fn_type_id.is_var_args);
     fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
-    LLVMZigDIFile *di_file = nullptr; // TODO if we get a crash maybe this is the culprit
+    LLVMZigDIFile *di_file = nullptr;
     fn_type->di_type = LLVMZigCreateSubroutineType(g->dbuilder, di_file,
             param_di_types, gen_param_index + 1, 0);
 
src/codegen.cpp
@@ -73,6 +73,10 @@ void codegen_set_libc_path(CodeGen *g, Buf *libc_path) {
     g->libc_path = libc_path;
 }
 
+void codegen_add_lib_dir(CodeGen *g, const char *dir) {
+    g->lib_dirs.append(dir);
+}
+
 static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node);
 static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, TypeTableEntry **out_type_entry);
 static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lvalue);
@@ -3648,6 +3652,12 @@ void codegen_link(CodeGen *g, const char *out_file) {
         args.append(get_libc_file(g, "crtn.o"));
     }
 
+    for (int i = 0; i < g->lib_dirs.length; i += 1) {
+        const char *lib_dir = g->lib_dirs.at(i);
+        args.append("-L");
+        args.append(lib_dir);
+    }
+
     auto it = g->link_table.entry_iterator();
     for (;;) {
         auto *entry = it.next();
@@ -3673,7 +3683,11 @@ void codegen_link(CodeGen *g, const char *out_file) {
 
     if (return_code != 0) {
         fprintf(stderr, "ld failed with return code %d\n", return_code);
-        fprintf(stderr, "%s\n", buf_ptr(&ld_stderr));
+        fprintf(stderr, "ld ");
+        for (int i = 0; i < args.length; i += 1) {
+            fprintf(stderr, "%s ", args.at(i));
+        }
+        fprintf(stderr, "\n%s\n", buf_ptr(&ld_stderr));
         exit(1);
     } else if (buf_len(&ld_stderr)) {
         fprintf(stderr, "%s\n", buf_ptr(&ld_stderr));
src/codegen.hpp
@@ -24,6 +24,7 @@ void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);
 void codegen_set_out_type(CodeGen *codegen, OutType out_type);
 void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
 void codegen_set_libc_path(CodeGen *codegen, Buf *libc_path);
+void codegen_add_lib_dir(CodeGen *codegen, const char *dir);
 
 void codegen_add_root_code(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);
 
src/main.cpp
@@ -31,6 +31,7 @@ static int usage(const char *arg0) {
         "  --libc-path [path]     set the C compiler data path\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"
     , arg0);
     return EXIT_FAILURE;
 }
@@ -56,6 +57,7 @@ int main(int argc, char **argv) {
     ErrColor color = ErrColorAuto;
     const char *libc_path = nullptr;
     ZigList<const char *> clang_argv = {0};
+    ZigList<const char *> lib_dirs = {0};
     int err;
 
     for (int i = 1; i < argc; i += 1) {
@@ -108,6 +110,8 @@ int main(int argc, char **argv) {
                 } else if (strcmp(arg, "-dirafter") == 0) {
                     clang_argv.append("-dirafter");
                     clang_argv.append(argv[i]);
+                } else if (strcmp(arg, "--library-path") == 0) {
+                    lib_dirs.append(argv[i]);
                 } else {
                     return usage(arg0);
                 }
@@ -183,6 +187,10 @@ int main(int argc, char **argv) {
             codegen_set_verbose(g, verbose);
             codegen_set_errmsg_color(g, color);
 
+            for (int i = 0; i < lib_dirs.length; i += 1) {
+                codegen_add_lib_dir(g, lib_dirs.at(i));
+            }
+
             if (cmd == CmdBuild) {
                 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
                 codegen_link(g, out_file);