authorgravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-05-25 14:17:59+02:00
committergravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-05-25 14:17:59+02:00
log99ee0608f74268248a2dc684cb9e6e9b03e3a9f5
tree3f734d2b26f131b903dc5e293ff0faee7bc07a42
parentc89b5222337d6adc14c83b8fa20a139196c4a38b

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


2 files changed, 26 insertions(+), 19 deletions(-)

src/codegen.cpp+18-11
......@@ -9075,8 +9075,11 @@ static void gen_h_file(CodeGen *g) {
90759075 if (!out_h)
90769076 zig_panic("unable to open %s: %s\n", buf_ptr(out_h_path), strerror(errno));
90779077
9078 Buf *export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
9079 buf_upcase(export_macro);
9078 Buf *export_macro = nullptr;
9079 if (g->is_dynamic) {
9080 export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
9081 buf_upcase(export_macro);
9082 }
90809083
90819084 Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)));
90829085 buf_upcase(extern_c_macro);
......@@ -9101,10 +9104,11 @@ static void gen_h_file(CodeGen *g) {
91019104 FnExport *fn_export = &fn_table_entry->export_list.items[0];
91029105 symbol_name = &fn_export->name;
91039106 }
9107
91049108 buf_appendf(&h_buf, "%s %s %s(",
9105 buf_ptr(export_macro),
9106 buf_ptr(&return_type_c),
9107 buf_ptr(symbol_name));
9109 buf_ptr(g->is_dynamic ? export_macro : extern_c_macro),
9110 buf_ptr(&return_type_c),
9111 buf_ptr(symbol_name));
91089112
91099113 Buf param_type_c = BUF_INIT;
91109114 if (fn_type_id->param_count > 0) {
......@@ -9154,13 +9158,16 @@ static void gen_h_file(CodeGen *g) {
91549158 fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro));
91559159 fprintf(out_h, "#endif\n");
91569160 fprintf(out_h, "\n");
9157 fprintf(out_h, "#if defined(_WIN32)\n");
9158 fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
9159 fprintf(out_h, "#else\n");
9160 fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
9161
9162 if (g->is_dynamic) {
9163 fprintf(out_h, "#if defined(_WIN32)\n");
9164 fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
9165 fprintf(out_h, "#else\n");
9166 fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
91619167 buf_ptr(export_macro), buf_ptr(extern_c_macro));
9162 fprintf(out_h, "#endif\n");
9163 fprintf(out_h, "\n");
9168 fprintf(out_h, "#endif\n");
9169 fprintf(out_h, "\n");
9170 }
91649171
91659172 for (size_t type_i = 0; type_i < gen_h->types_to_declare.length; type_i += 1) {
91669173 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}