| author | |
| committer | |
| log | 99ee0608f74268248a2dc684cb9e6e9b03e3a9f5 |
| tree | 3f734d2b26f131b903dc5e293ff0faee7bc07a42 |
| parent | c89b5222337d6adc14c83b8fa20a139196c4a38b |
2 files changed, 26 insertions(+), 19 deletions(-)
src/codegen.cpp+18-11| ... | ... | @@ -9075,8 +9075,11 @@ static void gen_h_file(CodeGen *g) { |
| 9075 | 9075 | if (!out_h) |
| 9076 | 9076 | zig_panic("unable to open %s: %s\n", buf_ptr(out_h_path), strerror(errno)); |
| 9077 | 9077 | |
| 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 | } | |
| 9080 | 9083 | |
| 9081 | 9084 | Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name))); |
| 9082 | 9085 | buf_upcase(extern_c_macro); |
| ... | ... | @@ -9101,10 +9104,11 @@ static void gen_h_file(CodeGen *g) { |
| 9101 | 9104 | FnExport *fn_export = &fn_table_entry->export_list.items[0]; |
| 9102 | 9105 | symbol_name = &fn_export->name; |
| 9103 | 9106 | } |
| 9107 | ||
| 9104 | 9108 | 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)); | |
| 9108 | 9112 | |
| 9109 | 9113 | Buf param_type_c = BUF_INIT; |
| 9110 | 9114 | if (fn_type_id->param_count > 0) { |
| ... | ... | @@ -9154,13 +9158,16 @@ static void gen_h_file(CodeGen *g) { |
| 9154 | 9158 | fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro)); |
| 9155 | 9159 | fprintf(out_h, "#endif\n"); |
| 9156 | 9160 | 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", | |
| 9161 | 9167 | 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 | } | |
| 9164 | 9171 | |
| 9165 | 9172 | for (size_t type_i = 0; type_i < gen_h->types_to_declare.length; type_i += 1) { |
| 9166 | 9173 | 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 | 11 | \\ 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 | ); |
| 17 | 17 | |
| ... | ... | @@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 35 | 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 | ); |
| 41 | 41 | |
| ... | ... | @@ -70,7 +70,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 70 | 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 | ); |
| 76 | 76 | |
| ... | ... | @@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 81 | 81 | , |
| 82 | 82 | \\struct Foo; |
| 83 | 83 | \\ |
| 84 | \\TEST_EXPORT void entry(struct Foo * foo); | |
| 84 | \\TEST_EXTERN_C void entry(struct Foo * foo); | |
| 85 | 85 | ); |
| 86 | 86 | |
| 87 | 87 | cases.add("array field-type", |
| ... | ... | @@ -96,7 +96,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 96 | 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 | ); |
| 102 | 102 | |
| ... | ... | @@ -110,7 +110,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 110 | 110 | \\} |
| 111 | 111 | , |
| 112 | 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 | ); |
| 116 | 116 | |
| ... | ... | @@ -125,7 +125,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 125 | 125 | \\} |
| 126 | 126 | , |
| 127 | 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 | ); |
| 131 | 131 | |
| ... | ... | @@ -140,7 +140,7 @@ pub fn addCases(cases: *tests.GenHContext) void { |
| 140 | 140 | \\} |
| 141 | 141 | , |
| 142 | 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 | } |