authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-14 02:44:31-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-14 02:47:16-04:00
logf1761632dae8951d21e62cb13e14bd4b4b0ed8a8
tree8ae6966a01439c347bd69f87e40a9fc5e4f62476
parentcf9b21c09fc641b4697e06981ac5114a8d3d09ab

darwin compat fixups

- delete commented out code - delete redundant check for missing mmacosx-version-min/maxdir - add TODO comment in std library - rename 'os' to 'self' in io.zig - `openSelfExe` aborts on darwin instead of compile error - only allow warnings on the one parseh test that has `#include <stdint.h>`.

5 files changed, 73 insertions(+), 65 deletions(-)

src/codegen.cpp-7
......@@ -43,13 +43,6 @@ static void init_darwin_native(CodeGen *g) {
4343 } else if (ios_target) {
4444 g->mios_version_min = buf_create_from_str(ios_target);
4545 }
46
47 // we should check for the command line option to throw an error if not specified
48 //
49
50 /* else {
51 zig_panic("unable to determine -mmacosx-version-min or -mios-version-min");
52 } */
5346}
5447
5548static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) {
src/main.cpp-10
......@@ -389,16 +389,6 @@ int main(int argc, char **argv) {
389389 return EXIT_FAILURE;
390390 }
391391
392 if((g->zig_target.os == ZigLLVM_Darwin ||
393 g->zig_target.os == ZigLLVM_MacOSX ||
394 g->zig_target.os == ZigLLVM_IOS) &&
395 (!mmacosx_version_min &&
396 !mios_version_min &&
397 !g->mmacosx_version_min &&
398 !g->mios_version_min) && target) {
399 zig_panic("unable to determine -mmacosx-version-min or -mios-version-min");
400 }
401
402392 if (mmacosx_version_min) {
403393 codegen_set_mmacosx_version_min(g, buf_create_from_str(mmacosx_version_min));
404394 }
std/darwin.zig+3
......@@ -101,6 +101,9 @@ pub fn getrandom(buf: &u8, count: usize) -> usize {
101101}
102102
103103pub fn raise(sig: i32) -> i32 {
104 // TODO investigate whether we need to block signals before calling kill
105 // like we do in the linux version of raise
106
104107 //var set: sigset_t = undefined;
105108 //blockAppSignals(&set);
106109 const pid = i32(arch.syscall0(arch.SYS_getpid));
std/io.zig+29-24
......@@ -9,6 +9,7 @@ const math = @import("math.zig");
99const endian = @import("endian.zig");
1010const debug = @import("debug.zig");
1111const assert = debug.assert;
12const os = @import("os.zig");
1213
1314pub const stdin_fileno = 0;
1415pub const stdout_fileno = 1;
......@@ -72,23 +73,23 @@ pub struct OutStream {
7273 buffer: [buffer_size]u8,
7374 index: usize,
7475
75 pub fn writeByte(os: &OutStream, b: u8) -> %void {
76 if (os.buffer.len == os.index) %return os.flush();
77 os.buffer[os.index] = b;
78 os.index += 1;
76 pub fn writeByte(self: &OutStream, b: u8) -> %void {
77 if (self.buffer.len == self.index) %return self.flush();
78 self.buffer[self.index] = b;
79 self.index += 1;
7980 }
8081
81 pub fn write(os: &OutStream, bytes: []const u8) -> %usize {
82 pub fn write(self: &OutStream, bytes: []const u8) -> %usize {
8283 var src_bytes_left = bytes.len;
8384 var src_index: @typeOf(bytes.len) = 0;
84 const dest_space_left = os.buffer.len - os.index;
85 const dest_space_left = self.buffer.len - self.index;
8586
8687 while (src_bytes_left > 0) {
8788 const copy_amt = math.min(dest_space_left, src_bytes_left);
88 @memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt);
89 os.index += copy_amt;
90 if (os.index == os.buffer.len) {
91 %return os.flush();
89 @memcpy(&self.buffer[self.index], &bytes[src_index], copy_amt);
90 self.index += copy_amt;
91 if (self.index == self.buffer.len) {
92 %return self.flush();
9293 }
9394 src_bytes_left -= copy_amt;
9495 }
......@@ -97,25 +98,25 @@ pub struct OutStream {
9798
9899 /// Prints a byte buffer, flushes the buffer, then returns the number of
99100 /// bytes printed. The "f" is for "flush".
100 pub fn printf(os: &OutStream, str: []const u8) -> %usize {
101 const byte_count = %return os.write(str);
102 %return os.flush();
101 pub fn printf(self: &OutStream, str: []const u8) -> %usize {
102 const byte_count = %return self.write(str);
103 %return self.flush();
103104 return byte_count;
104105 }
105106
106 pub fn printInt(os: &OutStream, inline T: type, x: T) -> %usize {
107 pub fn printInt(self: &OutStream, inline T: type, x: T) -> %usize {
107108 // TODO replace max_u64_base10_digits with math.log10(math.pow(2, @sizeOf(T)))
108 if (os.index + max_u64_base10_digits >= os.buffer.len) {
109 %return os.flush();
109 if (self.index + max_u64_base10_digits >= self.buffer.len) {
110 %return self.flush();
110111 }
111 const amt_printed = bufPrintInt(T, os.buffer[os.index...], x);
112 os.index += amt_printed;
112 const amt_printed = bufPrintInt(T, self.buffer[self.index...], x);
113 self.index += amt_printed;
113114 return amt_printed;
114115 }
115116
116 pub fn flush(os: &OutStream) -> %void {
117 pub fn flush(self: &OutStream) -> %void {
117118 while (true) {
118 const write_ret = system.write(os.fd, &os.buffer[0], os.index);
119 const write_ret = system.write(self.fd, &self.buffer[0], self.index);
119120 const write_err = system.getErrno(write_ret);
120121 if (write_err > 0) {
121122 return switch (write_err) {
......@@ -130,14 +131,14 @@ pub struct OutStream {
130131 else => error.Unexpected,
131132 }
132133 }
133 os.index = 0;
134 self.index = 0;
134135 return;
135136 }
136137 }
137138
138 pub fn close(os: &OutStream) -> %void {
139 pub fn close(self: &OutStream) -> %void {
139140 while (true) {
140 const close_ret = system.close(os.fd);
141 const close_ret = system.close(self.fd);
141142 const close_err = system.getErrno(close_ret);
142143 if (close_err > 0) {
143144 return switch (close_err) {
......@@ -438,9 +439,13 @@ fn parseU64DigitTooBig() {
438439
439440pub fn openSelfExe(stream: &InStream) -> %void {
440441 switch (@compileVar("os")) {
441 linux,darwin => {
442 linux => {
442443 %return stream.open("/proc/self/exe");
443444 },
445 darwin => {
446 %%stderr.printf("TODO: openSelfExe on Darwin\n");
447 os.abort();
448 },
444449 else => @compileError("unsupported os"),
445450 }
446451}
test/run_tests.cpp+41-24
......@@ -18,6 +18,11 @@ struct TestSourceFile {
1818 const char *source_code;
1919};
2020
21enum AllowWarnings {
22 AllowWarningsNo,
23 AllowWarningsYes,
24};
25
2126struct TestCase {
2227 const char *case_name;
2328 const char *output;
......@@ -29,6 +34,7 @@ struct TestCase {
2934 bool is_self_hosted;
3035 bool is_release_mode;
3136 bool is_debug_safety;
37 AllowWarnings allow_warnings;
3238};
3339
3440static ZigList<TestCase*> test_cases = {0};
......@@ -173,13 +179,16 @@ static void add_debug_safety_case(const char *case_name, const char *source) {
173179 }
174180}
175181
176static TestCase *add_parseh_case(const char *case_name, const char *source, int count, ...) {
182static TestCase *add_parseh_case(const char *case_name, AllowWarnings allow_warnings,
183 const char *source, int count, ...)
184{
177185 va_list ap;
178186 va_start(ap, count);
179187
180188 TestCase *test_case = allocate<TestCase>(1);
181189 test_case->case_name = case_name;
182190 test_case->is_parseh = true;
191 test_case->allow_warnings = allow_warnings;
183192
184193 test_case->source_files.resize(1);
185194 test_case->source_files.at(0).relative_path = tmp_h_path;
......@@ -1635,7 +1644,7 @@ fn unsigned_cast(x: i32) -> u32 {
16351644//////////////////////////////////////////////////////////////////////////////
16361645
16371646static void add_parseh_test_cases(void) {
1638 add_parseh_case("simple data types", R"SOURCE(
1647 add_parseh_case("simple data types", AllowWarningsYes, R"SOURCE(
16391648#include <stdint.h>
16401649int foo(char a, unsigned char b, signed char c);
16411650int foo(char a, unsigned char b, signed char c); // test a duplicate prototype
......@@ -1646,11 +1655,11 @@ void baz(int8_t a, int16_t b, int32_t c, int64_t d);
16461655 "pub extern fn bar(a: u8, b: u16, c: u32, d: u64);",
16471656 "pub extern fn baz(a: i8, b: i16, c: i32, d: i64);");
16481657
1649 add_parseh_case("noreturn attribute", R"SOURCE(
1658 add_parseh_case("noreturn attribute", AllowWarningsNo, R"SOURCE(
16501659void foo(void) __attribute__((noreturn));
16511660 )SOURCE", 1, R"OUTPUT(pub extern fn foo() -> unreachable;)OUTPUT");
16521661
1653 add_parseh_case("enums", R"SOURCE(
1662 add_parseh_case("enums", AllowWarningsNo, R"SOURCE(
16541663enum Foo {
16551664 FooA,
16561665 FooB,
......@@ -1665,11 +1674,11 @@ pub const FooB = enum_Foo.B;
16651674pub const Foo1 = enum_Foo.@"1";)",
16661675 R"(pub const Foo = enum_Foo;)");
16671676
1668 add_parseh_case("restrict -> noalias", R"SOURCE(
1677 add_parseh_case("restrict -> noalias", AllowWarningsNo, R"SOURCE(
16691678void foo(void *restrict bar, void *restrict);
16701679 )SOURCE", 1, R"OUTPUT(pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
16711680
1672 add_parseh_case("simple struct", R"SOURCE(
1681 add_parseh_case("simple struct", AllowWarningsNo, R"SOURCE(
16731682struct Foo {
16741683 int x;
16751684 char *y;
......@@ -1680,7 +1689,7 @@ struct Foo {
16801689 y: ?&u8,
16811690})OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
16821691
1683 add_parseh_case("qualified struct and enum", R"SOURCE(
1692 add_parseh_case("qualified struct and enum", AllowWarningsNo, R"SOURCE(
16841693struct Foo {
16851694 int x;
16861695 int y;
......@@ -1703,12 +1712,13 @@ pub const BarB = enum_Bar.B;)OUTPUT",
17031712 R"OUTPUT(pub const Foo = struct_Foo;
17041713pub const Bar = enum_Bar;)OUTPUT");
17051714
1706 add_parseh_case("constant size array", R"SOURCE(
1715 add_parseh_case("constant size array", AllowWarningsNo, R"SOURCE(
17071716void func(int array[20]);
17081717 )SOURCE", 1, "pub extern fn func(array: ?&c_int);");
17091718
17101719
1711 add_parseh_case("self referential struct with function pointer", R"SOURCE(
1720 add_parseh_case("self referential struct with function pointer",
1721 AllowWarningsNo, R"SOURCE(
17121722struct Foo {
17131723 void (*derp)(struct Foo *foo);
17141724};
......@@ -1717,7 +1727,7 @@ struct Foo {
17171727})OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
17181728
17191729
1720 add_parseh_case("struct prototype used in func", R"SOURCE(
1730 add_parseh_case("struct prototype used in func", AllowWarningsNo, R"SOURCE(
17211731struct Foo;
17221732struct Foo *some_func(struct Foo *foo, int x);
17231733 )SOURCE", 2, R"OUTPUT(pub type struct_Foo = u8;
......@@ -1725,17 +1735,19 @@ pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT",
17251735 R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
17261736
17271737
1728 add_parseh_case("#define a char literal", R"SOURCE(
1738 add_parseh_case("#define a char literal", AllowWarningsNo, R"SOURCE(
17291739#define A_CHAR 'a'
17301740 )SOURCE", 1, R"OUTPUT(pub const A_CHAR = 'a';)OUTPUT");
17311741
17321742
1733 add_parseh_case("#define an unsigned integer literal", R"SOURCE(
1743 add_parseh_case("#define an unsigned integer literal", AllowWarningsNo,
1744 R"SOURCE(
17341745#define CHANNEL_COUNT 24
17351746 )SOURCE", 1, R"OUTPUT(pub const CHANNEL_COUNT = 24;)OUTPUT");
17361747
17371748
1738 add_parseh_case("#define referencing another #define", R"SOURCE(
1749 add_parseh_case("#define referencing another #define", AllowWarningsNo,
1750 R"SOURCE(
17391751#define THING2 THING1
17401752#define THING1 1234
17411753 )SOURCE", 2,
......@@ -1743,7 +1755,7 @@ pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT",
17431755 "pub const THING2 = THING1;");
17441756
17451757
1746 add_parseh_case("variables", R"SOURCE(
1758 add_parseh_case("variables", AllowWarningsNo, R"SOURCE(
17471759extern int extern_var;
17481760static const int int_var = 13;
17491761 )SOURCE", 2,
......@@ -1751,7 +1763,7 @@ static const int int_var = 13;
17511763 "pub const int_var: c_int = 13;");
17521764
17531765
1754 add_parseh_case("circular struct definitions", R"SOURCE(
1766 add_parseh_case("circular struct definitions", AllowWarningsNo, R"SOURCE(
17551767struct Bar;
17561768
17571769struct Foo {
......@@ -1770,14 +1782,15 @@ struct Bar {
17701782})SOURCE");
17711783
17721784
1773 add_parseh_case("typedef void", R"SOURCE(
1785 add_parseh_case("typedef void", AllowWarningsNo, R"SOURCE(
17741786typedef void Foo;
17751787Foo fun(Foo *a);
17761788 )SOURCE", 2,
17771789 "pub const Foo = c_void;",
17781790 "pub extern fn fun(a: ?&c_void);");
17791791
1780 add_parseh_case("generate inline func for #define global extern fn", R"SOURCE(
1792 add_parseh_case("generate inline func for #define global extern fn", AllowWarningsNo,
1793 R"SOURCE(
17811794extern void (*fn_ptr)(void);
17821795#define foo fn_ptr
17831796 )SOURCE", 2,
......@@ -1787,19 +1800,19 @@ extern void (*fn_ptr)(void);
17871800})SOURCE");
17881801
17891802
1790 add_parseh_case("#define string", R"SOURCE(
1803 add_parseh_case("#define string", AllowWarningsNo, R"SOURCE(
17911804#define foo "a string"
17921805 )SOURCE", 1, "pub const foo = c\"a string\";");
17931806
1794 add_parseh_case("__cdecl doesn't mess up function pointers", R"SOURCE(
1807 add_parseh_case("__cdecl doesn't mess up function pointers", AllowWarningsNo, R"SOURCE(
17951808void foo(void (__cdecl *fn_ptr)(void));
17961809 )SOURCE", 1, "pub extern fn foo(fn_ptr: ?extern fn());");
17971810
1798 add_parseh_case("comment after integer literal", R"SOURCE(
1811 add_parseh_case("comment after integer literal", AllowWarningsNo, R"SOURCE(
17991812#define SDL_INIT_VIDEO 0x00000020 /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
18001813 )SOURCE", 1, "pub const SDL_INIT_VIDEO = 32;");
18011814
1802 add_parseh_case("zig keywords in C code", R"SOURCE(
1815 add_parseh_case("zig keywords in C code", AllowWarningsNo, R"SOURCE(
18031816struct type {
18041817 int defer;
18051818};
......@@ -1807,7 +1820,7 @@ struct type {
18071820 @"defer": c_int,
18081821})", R"(pub const @"type" = struct_type;)");
18091822
1810 add_parseh_case("macro defines string literal with octal", R"SOURCE(
1823 add_parseh_case("macro defines string literal with octal", AllowWarningsNo, R"SOURCE(
18111824#define FOO "aoeu\023 derp"
18121825#define FOO2 "aoeu\0234 derp"
18131826#define FOO_CHAR '\077'
......@@ -1925,10 +1938,14 @@ static void run_test(TestCase *test_case) {
19251938
19261939 if (test_case->is_parseh) {
19271940 if (buf_len(&zig_stderr) > 0) {
1928 printf("\n!!!!! parseh emitted warnings:\n");
1941 printf("\nparseh emitted warnings:\n");
1942 printf("------------------------------\n");
19291943 print_compiler_invocation(test_case);
19301944 printf("%s\n", buf_ptr(&zig_stderr));
1931// exit(1);
1945 printf("------------------------------\n");
1946 if (test_case->allow_warnings == AllowWarningsNo) {
1947 exit(1);
1948 }
19321949 }
19331950
19341951 for (int i = 0; i < test_case->compile_errors.length; i += 1) {