authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-27 21:51:34-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-05-27 21:51:34-04:00
logf924fbddcfb615014aa7527686c1651c6e4d7846
treeebd48dee465b3a301aeea64695023e89d0b1f7a6
parent2c0280ba085893984007706fb40c7b291f43074d
parent99ee0608f74268248a2dc684cb9e6e9b03e3a9f5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2552 from Sahnvour/issue-2543

gen-h: do not output visibility macros when the build is static

3 files changed, 29 insertions(+), 22 deletions(-)

src/codegen.cpp+18-11
...@@ -9085,8 +9085,11 @@ static void gen_h_file(CodeGen *g) {...@@ -9085,8 +9085,11 @@ static void gen_h_file(CodeGen *g) {
9085 if (!out_h)9085 if (!out_h)
9086 zig_panic("unable to open %s: %s\n", buf_ptr(out_h_path), strerror(errno));9086 zig_panic("unable to open %s: %s\n", buf_ptr(out_h_path), strerror(errno));
90879087
9088 Buf *export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));9088 Buf *export_macro = nullptr;
9089 buf_upcase(export_macro);9089 if (g->is_dynamic) {
9090 export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
9091 buf_upcase(export_macro);
9092 }
90909093
9091 Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)));9094 Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)));
9092 buf_upcase(extern_c_macro);9095 buf_upcase(extern_c_macro);
...@@ -9111,10 +9114,11 @@ static void gen_h_file(CodeGen *g) {...@@ -9111,10 +9114,11 @@ static void gen_h_file(CodeGen *g) {
9111 FnExport *fn_export = &fn_table_entry->export_list.items[0];9114 FnExport *fn_export = &fn_table_entry->export_list.items[0];
9112 symbol_name = &fn_export->name;9115 symbol_name = &fn_export->name;
9113 }9116 }
9117
9114 buf_appendf(&h_buf, "%s %s %s(",9118 buf_appendf(&h_buf, "%s %s %s(",
9115 buf_ptr(export_macro),9119 buf_ptr(g->is_dynamic ? export_macro : extern_c_macro),
9116 buf_ptr(&return_type_c),9120 buf_ptr(&return_type_c),
9117 buf_ptr(symbol_name));9121 buf_ptr(symbol_name));
91189122
9119 Buf param_type_c = BUF_INIT;9123 Buf param_type_c = BUF_INIT;
9120 if (fn_type_id->param_count > 0) {9124 if (fn_type_id->param_count > 0) {
...@@ -9164,13 +9168,16 @@ static void gen_h_file(CodeGen *g) {...@@ -9164,13 +9168,16 @@ static void gen_h_file(CodeGen *g) {
9164 fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro));9168 fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro));
9165 fprintf(out_h, "#endif\n");9169 fprintf(out_h, "#endif\n");
9166 fprintf(out_h, "\n");9170 fprintf(out_h, "\n");
9167 fprintf(out_h, "#if defined(_WIN32)\n");9171
9168 fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));9172 if (g->is_dynamic) {
9169 fprintf(out_h, "#else\n");9173 fprintf(out_h, "#if defined(_WIN32)\n");
9170 fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",9174 fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
9175 fprintf(out_h, "#else\n");
9176 fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
9171 buf_ptr(export_macro), buf_ptr(extern_c_macro));9177 buf_ptr(export_macro), buf_ptr(extern_c_macro));
9172 fprintf(out_h, "#endif\n");9178 fprintf(out_h, "#endif\n");
9173 fprintf(out_h, "\n");9179 fprintf(out_h, "\n");
9180 }
91749181
9175 for (size_t type_i = 0; type_i < gen_h->types_to_declare.length; type_i += 1) {9182 for (size_t type_i = 0; type_i < gen_h->types_to_declare.length; type_i += 1) {
9176 ZigType *type_entry = gen_h->types_to_declare.at(type_i);9183 ZigType *type_entry = gen_h->types_to_declare.at(type_i);
test/gen_h.zig+8-8
...@@ -11,7 +11,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -11,7 +11,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
11 \\ C = 211 \\ C = 2
12 \\};12 \\};
13 \\13 \\
14 \\TEST_EXPORT void entry(enum Foo foo);14 \\TEST_EXTERN_C void entry(enum Foo foo);
15 \\15 \\
16 );16 );
1717
...@@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
35 \\ uint64_t F;35 \\ uint64_t F;
36 \\};36 \\};
37 \\37 \\
38 \\TEST_EXPORT void entry(struct Foo foo);38 \\TEST_EXTERN_C void entry(struct Foo foo);
39 \\39 \\
40 );40 );
4141
...@@ -70,7 +70,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -70,7 +70,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
70 \\ struct Big D;70 \\ struct Big D;
71 \\};71 \\};
72 \\72 \\
73 \\TEST_EXPORT void entry(union Foo foo);73 \\TEST_EXTERN_C void entry(union Foo foo);
74 \\74 \\
75 );75 );
7676
...@@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
81 ,81 ,
82 \\struct Foo;82 \\struct Foo;
83 \\83 \\
84 \\TEST_EXPORT void entry(struct Foo * foo);84 \\TEST_EXTERN_C void entry(struct Foo * foo);
85 );85 );
8686
87 cases.add("array field-type",87 cases.add("array field-type",
...@@ -96,7 +96,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -96,7 +96,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
96 \\ uint32_t * B[4];96 \\ uint32_t * B[4];
97 \\};97 \\};
98 \\98 \\
99 \\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]);99 \\TEST_EXTERN_C void entry(struct Foo foo, uint8_t bar[]);
100 \\100 \\
101 );101 );
102102
...@@ -110,7 +110,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -110,7 +110,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
110 \\}110 \\}
111 ,111 ,
112 \\struct S;112 \\struct S;
113 \\TEST_EXPORT uint8_t a(struct S * s);113 \\TEST_EXTERN_C uint8_t a(struct S * s);
114 \\114 \\
115 );115 );
116116
...@@ -125,7 +125,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -125,7 +125,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
125 \\}125 \\}
126 ,126 ,
127 \\union U;127 \\union U;
128 \\TEST_EXPORT uint8_t a(union U * s);128 \\TEST_EXTERN_C uint8_t a(union U * s);
129 \\129 \\
130 );130 );
131131
...@@ -140,7 +140,7 @@ pub fn addCases(cases: *tests.GenHContext) void {...@@ -140,7 +140,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
140 \\}140 \\}
141 ,141 ,
142 \\enum E;142 \\enum E;
143 \\TEST_EXPORT uint8_t a(enum E * s);143 \\TEST_EXTERN_C uint8_t a(enum E * s);
144 \\144 \\
145 );145 );
146}146}
test/tests.zig+3-3
...@@ -341,7 +341,7 @@ pub const CompareOutputContext = struct {...@@ -341,7 +341,7 @@ pub const CompareOutputContext = struct {
341 \\341 \\
342 \\========= Expected this output: =========342 \\========= Expected this output: =========
343 \\{}343 \\{}
344 \\================================================344 \\========= But found: ====================
345 \\{}345 \\{}
346 \\346 \\
347 , self.expected_output, stdout.toSliceConst());347 , self.expected_output, stdout.toSliceConst());
...@@ -1036,7 +1036,7 @@ pub const TranslateCContext = struct {...@@ -1036,7 +1036,7 @@ pub const TranslateCContext = struct {
1036 \\1036 \\
1037 \\========= Expected this output: ================1037 \\========= Expected this output: ================
1038 \\{}1038 \\{}
1039 \\================================================1039 \\========= But found: ===========================
1040 \\{}1040 \\{}
1041 \\1041 \\
1042 , expected_line, stdout);1042 , expected_line, stdout);
...@@ -1211,7 +1211,7 @@ pub const GenHContext = struct {...@@ -1211,7 +1211,7 @@ pub const GenHContext = struct {
1211 \\1211 \\
1212 \\========= Expected this output: ================1212 \\========= Expected this output: ================
1213 \\{}1213 \\{}
1214 \\================================================1214 \\========= But found: ===========================
1215 \\{}1215 \\{}
1216 \\1216 \\
1217 , expected_line, actual_h);1217 , expected_line, actual_h);