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) {
90859085 if (!out_h)
90869086 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)));
9089 buf_upcase(export_macro);
9088 Buf *export_macro = nullptr;
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
90919094 Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)));
90929095 buf_upcase(extern_c_macro);
......@@ -9111,10 +9114,11 @@ static void gen_h_file(CodeGen *g) {
91119114 FnExport *fn_export = &fn_table_entry->export_list.items[0];
91129115 symbol_name = &fn_export->name;
91139116 }
9117
91149118 buf_appendf(&h_buf, "%s %s %s(",
9115 buf_ptr(export_macro),
9116 buf_ptr(&return_type_c),
9117 buf_ptr(symbol_name));
9119 buf_ptr(g->is_dynamic ? export_macro : extern_c_macro),
9120 buf_ptr(&return_type_c),
9121 buf_ptr(symbol_name));
91189122
91199123 Buf param_type_c = BUF_INIT;
91209124 if (fn_type_id->param_count > 0) {
......@@ -9164,13 +9168,16 @@ static void gen_h_file(CodeGen *g) {
91649168 fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro));
91659169 fprintf(out_h, "#endif\n");
91669170 fprintf(out_h, "\n");
9167 fprintf(out_h, "#if defined(_WIN32)\n");
9168 fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
9169 fprintf(out_h, "#else\n");
9170 fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
9171
9172 if (g->is_dynamic) {
9173 fprintf(out_h, "#if defined(_WIN32)\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",
91719177 buf_ptr(export_macro), buf_ptr(extern_c_macro));
9172 fprintf(out_h, "#endif\n");
9173 fprintf(out_h, "\n");
9178 fprintf(out_h, "#endif\n");
9179 fprintf(out_h, "\n");
9180 }
91749181
91759182 for (size_t type_i = 0; type_i < gen_h->types_to_declare.length; type_i += 1) {
91769183 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 {
1111 \\ C = 2
1212 \\};
1313 \\
14 \\TEST_EXPORT void entry(enum Foo foo);
14 \\TEST_EXTERN_C void entry(enum Foo foo);
1515 \\
1616 );
1717
......@@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
3535 \\ uint64_t F;
3636 \\};
3737 \\
38 \\TEST_EXPORT void entry(struct Foo foo);
38 \\TEST_EXTERN_C void entry(struct Foo foo);
3939 \\
4040 );
4141
......@@ -70,7 +70,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
7070 \\ struct Big D;
7171 \\};
7272 \\
73 \\TEST_EXPORT void entry(union Foo foo);
73 \\TEST_EXTERN_C void entry(union Foo foo);
7474 \\
7575 );
7676
......@@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
8181 ,
8282 \\struct Foo;
8383 \\
84 \\TEST_EXPORT void entry(struct Foo * foo);
84 \\TEST_EXTERN_C void entry(struct Foo * foo);
8585 );
8686
8787 cases.add("array field-type",
......@@ -96,7 +96,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
9696 \\ uint32_t * B[4];
9797 \\};
9898 \\
99 \\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]);
99 \\TEST_EXTERN_C void entry(struct Foo foo, uint8_t bar[]);
100100 \\
101101 );
102102
......@@ -110,7 +110,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
110110 \\}
111111 ,
112112 \\struct S;
113 \\TEST_EXPORT uint8_t a(struct S * s);
113 \\TEST_EXTERN_C uint8_t a(struct S * s);
114114 \\
115115 );
116116
......@@ -125,7 +125,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
125125 \\}
126126 ,
127127 \\union U;
128 \\TEST_EXPORT uint8_t a(union U * s);
128 \\TEST_EXTERN_C uint8_t a(union U * s);
129129 \\
130130 );
131131
......@@ -140,7 +140,7 @@ pub fn addCases(cases: *tests.GenHContext) void {
140140 \\}
141141 ,
142142 \\enum E;
143 \\TEST_EXPORT uint8_t a(enum E * s);
143 \\TEST_EXTERN_C uint8_t a(enum E * s);
144144 \\
145145 );
146146}
test/tests.zig+3-3
......@@ -341,7 +341,7 @@ pub const CompareOutputContext = struct {
341341 \\
342342 \\========= Expected this output: =========
343343 \\{}
344 \\================================================
344 \\========= But found: ====================
345345 \\{}
346346 \\
347347 , self.expected_output, stdout.toSliceConst());
......@@ -1036,7 +1036,7 @@ pub const TranslateCContext = struct {
10361036 \\
10371037 \\========= Expected this output: ================
10381038 \\{}
1039 \\================================================
1039 \\========= But found: ===========================
10401040 \\{}
10411041 \\
10421042 , expected_line, stdout);
......@@ -1211,7 +1211,7 @@ pub const GenHContext = struct {
12111211 \\
12121212 \\========= Expected this output: ================
12131213 \\{}
1214 \\================================================
1214 \\========= But found: ===========================
12151215 \\{}
12161216 \\
12171217 , expected_line, actual_h);