Commit 99ee0608f7

Sahnvour <sahnvour@pm.me>
2019-05-25 14:17:59
gen-h: do not output visibility macros when the build is static
1 parent c89b522
Changed files (2)
src/codegen.cpp
@@ -9075,8 +9075,11 @@ static void gen_h_file(CodeGen *g) {
     if (!out_h)
         zig_panic("unable to open %s: %s\n", buf_ptr(out_h_path), strerror(errno));
 
-    Buf *export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
-    buf_upcase(export_macro);
+    Buf *export_macro = nullptr;
+    if (g->is_dynamic) {
+        export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
+        buf_upcase(export_macro);
+    }
 
     Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)));
     buf_upcase(extern_c_macro);
@@ -9101,10 +9104,11 @@ static void gen_h_file(CodeGen *g) {
             FnExport *fn_export = &fn_table_entry->export_list.items[0];
             symbol_name = &fn_export->name;
         }
+
         buf_appendf(&h_buf, "%s %s %s(",
-                buf_ptr(export_macro),
-                buf_ptr(&return_type_c),
-                buf_ptr(symbol_name));
+            buf_ptr(g->is_dynamic ? export_macro : extern_c_macro),
+            buf_ptr(&return_type_c),
+            buf_ptr(symbol_name));
 
         Buf param_type_c = BUF_INIT;
         if (fn_type_id->param_count > 0) {
@@ -9154,13 +9158,16 @@ static void gen_h_file(CodeGen *g) {
     fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro));
     fprintf(out_h, "#endif\n");
     fprintf(out_h, "\n");
-    fprintf(out_h, "#if defined(_WIN32)\n");
-    fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
-    fprintf(out_h, "#else\n");
-    fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
+
+    if (g->is_dynamic) {
+        fprintf(out_h, "#if defined(_WIN32)\n");
+        fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
+        fprintf(out_h, "#else\n");
+        fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
             buf_ptr(export_macro), buf_ptr(extern_c_macro));
-    fprintf(out_h, "#endif\n");
-    fprintf(out_h, "\n");
+        fprintf(out_h, "#endif\n");
+        fprintf(out_h, "\n");
+    }
 
     for (size_t type_i = 0; type_i < gen_h->types_to_declare.length; type_i += 1) {
         ZigType *type_entry = gen_h->types_to_declare.at(type_i);
test/gen_h.zig
@@ -11,7 +11,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\    C = 2
         \\};
         \\
-        \\TEST_EXPORT void entry(enum Foo foo);
+        \\TEST_EXTERN_C void entry(enum Foo foo);
         \\
     );
 
@@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\    uint64_t F;
         \\};
         \\
-        \\TEST_EXPORT void entry(struct Foo foo);
+        \\TEST_EXTERN_C void entry(struct Foo foo);
         \\
     );
 
@@ -70,7 +70,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\    struct Big D;
         \\};
         \\
-        \\TEST_EXPORT void entry(union Foo foo);
+        \\TEST_EXTERN_C void entry(union Foo foo);
         \\
     );
 
@@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
     ,
         \\struct Foo;
         \\
-        \\TEST_EXPORT void entry(struct Foo * foo);
+        \\TEST_EXTERN_C void entry(struct Foo * foo);
     );
 
     cases.add("array field-type",
@@ -96,7 +96,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\    uint32_t * B[4];
         \\};
         \\
-        \\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]);
+        \\TEST_EXTERN_C void entry(struct Foo foo, uint8_t bar[]);
         \\
     );
 
@@ -110,7 +110,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\}
     ,
         \\struct S;
-        \\TEST_EXPORT uint8_t a(struct S * s);
+        \\TEST_EXTERN_C uint8_t a(struct S * s);
         \\
     );
 
@@ -125,7 +125,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\}
     ,
         \\union U;
-        \\TEST_EXPORT uint8_t a(union U * s);
+        \\TEST_EXTERN_C uint8_t a(union U * s);
         \\
     );
 
@@ -140,7 +140,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
         \\}
     ,
         \\enum E;
-        \\TEST_EXPORT uint8_t a(enum E * s);
+        \\TEST_EXTERN_C uint8_t a(enum E * s);
         \\
     );
 }