From cf9b21c09fc641b4697e06981ac5114a8d3d09ab Mon Sep 17 00:00:00 2001 From: alter Date: Mon, 12 Sep 2016 01:01:06 -0300 Subject: MacOSX compatibility - Implemented some syscall for MacOSX - tested on : El Capitan 10.11 x86_64 - make self hosted test run on macosx - modified run_test so it does not fail when parseh throws warnings (most of them are related to buildin types from gcc that arent defined in header files and unions) - making -mmacosx-version-min and -mios-version-min works like gcc (command line paramers have precedence over enviroment variables) --- src/codegen.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index ddf06b7eef..e2dd435598 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -42,9 +42,14 @@ static void init_darwin_native(CodeGen *g) { g->mmacosx_version_min = buf_create_from_str(osx_target); } else if (ios_target) { g->mios_version_min = buf_create_from_str(ios_target); - } else { - zig_panic("unable to determine -mmacosx-version-min or -mios-version-min"); } + + // we should check for the command line option to throw an error if not specified + // + + /* else { + zig_panic("unable to determine -mmacosx-version-min or -mios-version-min"); + } */ } static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) { -- cgit v1.2.3 From f1761632dae8951d21e62cb13e14bd4b4b0ed8a8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 14 Sep 2016 02:44:31 -0400 Subject: 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 `. --- src/codegen.cpp | 7 ------ src/main.cpp | 10 --------- std/darwin.zig | 3 +++ std/io.zig | 53 ++++++++++++++++++++++++-------------------- test/run_tests.cpp | 65 ++++++++++++++++++++++++++++++++++-------------------- 5 files changed, 73 insertions(+), 65 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index e2dd435598..827836925c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -43,13 +43,6 @@ static void init_darwin_native(CodeGen *g) { } else if (ios_target) { g->mios_version_min = buf_create_from_str(ios_target); } - - // we should check for the command line option to throw an error if not specified - // - - /* else { - zig_panic("unable to determine -mmacosx-version-min or -mios-version-min"); - } */ } static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) { diff --git a/src/main.cpp b/src/main.cpp index 23fcaafa99..e55350a274 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -389,16 +389,6 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - if((g->zig_target.os == ZigLLVM_Darwin || - g->zig_target.os == ZigLLVM_MacOSX || - g->zig_target.os == ZigLLVM_IOS) && - (!mmacosx_version_min && - !mios_version_min && - !g->mmacosx_version_min && - !g->mios_version_min) && target) { - zig_panic("unable to determine -mmacosx-version-min or -mios-version-min"); - } - if (mmacosx_version_min) { codegen_set_mmacosx_version_min(g, buf_create_from_str(mmacosx_version_min)); } diff --git a/std/darwin.zig b/std/darwin.zig index 99eca52065..0bf8b62f58 100644 --- a/std/darwin.zig +++ b/std/darwin.zig @@ -101,6 +101,9 @@ pub fn getrandom(buf: &u8, count: usize) -> usize { } pub fn raise(sig: i32) -> i32 { + // TODO investigate whether we need to block signals before calling kill + // like we do in the linux version of raise + //var set: sigset_t = undefined; //blockAppSignals(&set); const pid = i32(arch.syscall0(arch.SYS_getpid)); diff --git a/std/io.zig b/std/io.zig index ba63eac418..9c30233cb4 100644 --- a/std/io.zig +++ b/std/io.zig @@ -9,6 +9,7 @@ const math = @import("math.zig"); const endian = @import("endian.zig"); const debug = @import("debug.zig"); const assert = debug.assert; +const os = @import("os.zig"); pub const stdin_fileno = 0; pub const stdout_fileno = 1; @@ -72,23 +73,23 @@ pub struct OutStream { buffer: [buffer_size]u8, index: usize, - pub fn writeByte(os: &OutStream, b: u8) -> %void { - if (os.buffer.len == os.index) %return os.flush(); - os.buffer[os.index] = b; - os.index += 1; + pub fn writeByte(self: &OutStream, b: u8) -> %void { + if (self.buffer.len == self.index) %return self.flush(); + self.buffer[self.index] = b; + self.index += 1; } - pub fn write(os: &OutStream, bytes: []const u8) -> %usize { + pub fn write(self: &OutStream, bytes: []const u8) -> %usize { var src_bytes_left = bytes.len; var src_index: @typeOf(bytes.len) = 0; - const dest_space_left = os.buffer.len - os.index; + const dest_space_left = self.buffer.len - self.index; while (src_bytes_left > 0) { const copy_amt = math.min(dest_space_left, src_bytes_left); - @memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt); - os.index += copy_amt; - if (os.index == os.buffer.len) { - %return os.flush(); + @memcpy(&self.buffer[self.index], &bytes[src_index], copy_amt); + self.index += copy_amt; + if (self.index == self.buffer.len) { + %return self.flush(); } src_bytes_left -= copy_amt; } @@ -97,25 +98,25 @@ pub struct OutStream { /// Prints a byte buffer, flushes the buffer, then returns the number of /// bytes printed. The "f" is for "flush". - pub fn printf(os: &OutStream, str: []const u8) -> %usize { - const byte_count = %return os.write(str); - %return os.flush(); + pub fn printf(self: &OutStream, str: []const u8) -> %usize { + const byte_count = %return self.write(str); + %return self.flush(); return byte_count; } - pub fn printInt(os: &OutStream, inline T: type, x: T) -> %usize { + pub fn printInt(self: &OutStream, inline T: type, x: T) -> %usize { // TODO replace max_u64_base10_digits with math.log10(math.pow(2, @sizeOf(T))) - if (os.index + max_u64_base10_digits >= os.buffer.len) { - %return os.flush(); + if (self.index + max_u64_base10_digits >= self.buffer.len) { + %return self.flush(); } - const amt_printed = bufPrintInt(T, os.buffer[os.index...], x); - os.index += amt_printed; + const amt_printed = bufPrintInt(T, self.buffer[self.index...], x); + self.index += amt_printed; return amt_printed; } - pub fn flush(os: &OutStream) -> %void { + pub fn flush(self: &OutStream) -> %void { while (true) { - const write_ret = system.write(os.fd, &os.buffer[0], os.index); + const write_ret = system.write(self.fd, &self.buffer[0], self.index); const write_err = system.getErrno(write_ret); if (write_err > 0) { return switch (write_err) { @@ -130,14 +131,14 @@ pub struct OutStream { else => error.Unexpected, } } - os.index = 0; + self.index = 0; return; } } - pub fn close(os: &OutStream) -> %void { + pub fn close(self: &OutStream) -> %void { while (true) { - const close_ret = system.close(os.fd); + const close_ret = system.close(self.fd); const close_err = system.getErrno(close_ret); if (close_err > 0) { return switch (close_err) { @@ -438,9 +439,13 @@ fn parseU64DigitTooBig() { pub fn openSelfExe(stream: &InStream) -> %void { switch (@compileVar("os")) { - linux,darwin => { + linux => { %return stream.open("/proc/self/exe"); }, + darwin => { + %%stderr.printf("TODO: openSelfExe on Darwin\n"); + os.abort(); + }, else => @compileError("unsupported os"), } } diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 6bb8b7c3a1..a0fa1c7ad4 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -18,6 +18,11 @@ struct TestSourceFile { const char *source_code; }; +enum AllowWarnings { + AllowWarningsNo, + AllowWarningsYes, +}; + struct TestCase { const char *case_name; const char *output; @@ -29,6 +34,7 @@ struct TestCase { bool is_self_hosted; bool is_release_mode; bool is_debug_safety; + AllowWarnings allow_warnings; }; static ZigList test_cases = {0}; @@ -173,13 +179,16 @@ static void add_debug_safety_case(const char *case_name, const char *source) { } } -static TestCase *add_parseh_case(const char *case_name, const char *source, int count, ...) { +static TestCase *add_parseh_case(const char *case_name, AllowWarnings allow_warnings, + const char *source, int count, ...) +{ va_list ap; va_start(ap, count); TestCase *test_case = allocate(1); test_case->case_name = case_name; test_case->is_parseh = true; + test_case->allow_warnings = allow_warnings; test_case->source_files.resize(1); test_case->source_files.at(0).relative_path = tmp_h_path; @@ -1635,7 +1644,7 @@ fn unsigned_cast(x: i32) -> u32 { ////////////////////////////////////////////////////////////////////////////// static void add_parseh_test_cases(void) { - add_parseh_case("simple data types", R"SOURCE( + add_parseh_case("simple data types", AllowWarningsYes, R"SOURCE( #include int foo(char a, unsigned char b, signed char c); int 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); "pub extern fn bar(a: u8, b: u16, c: u32, d: u64);", "pub extern fn baz(a: i8, b: i16, c: i32, d: i64);"); - add_parseh_case("noreturn attribute", R"SOURCE( + add_parseh_case("noreturn attribute", AllowWarningsNo, R"SOURCE( void foo(void) __attribute__((noreturn)); )SOURCE", 1, R"OUTPUT(pub extern fn foo() -> unreachable;)OUTPUT"); - add_parseh_case("enums", R"SOURCE( + add_parseh_case("enums", AllowWarningsNo, R"SOURCE( enum Foo { FooA, FooB, @@ -1665,11 +1674,11 @@ pub const FooB = enum_Foo.B; pub const Foo1 = enum_Foo.@"1";)", R"(pub const Foo = enum_Foo;)"); - add_parseh_case("restrict -> noalias", R"SOURCE( + add_parseh_case("restrict -> noalias", AllowWarningsNo, R"SOURCE( void foo(void *restrict bar, void *restrict); )SOURCE", 1, R"OUTPUT(pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT"); - add_parseh_case("simple struct", R"SOURCE( + add_parseh_case("simple struct", AllowWarningsNo, R"SOURCE( struct Foo { int x; char *y; @@ -1680,7 +1689,7 @@ struct Foo { y: ?&u8, })OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT"); - add_parseh_case("qualified struct and enum", R"SOURCE( + add_parseh_case("qualified struct and enum", AllowWarningsNo, R"SOURCE( struct Foo { int x; int y; @@ -1703,12 +1712,13 @@ pub const BarB = enum_Bar.B;)OUTPUT", R"OUTPUT(pub const Foo = struct_Foo; pub const Bar = enum_Bar;)OUTPUT"); - add_parseh_case("constant size array", R"SOURCE( + add_parseh_case("constant size array", AllowWarningsNo, R"SOURCE( void func(int array[20]); )SOURCE", 1, "pub extern fn func(array: ?&c_int);"); - add_parseh_case("self referential struct with function pointer", R"SOURCE( + add_parseh_case("self referential struct with function pointer", + AllowWarningsNo, R"SOURCE( struct Foo { void (*derp)(struct Foo *foo); }; @@ -1717,7 +1727,7 @@ struct Foo { })OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT"); - add_parseh_case("struct prototype used in func", R"SOURCE( + add_parseh_case("struct prototype used in func", AllowWarningsNo, R"SOURCE( struct Foo; struct Foo *some_func(struct Foo *foo, int x); )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", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT"); - add_parseh_case("#define a char literal", R"SOURCE( + add_parseh_case("#define a char literal", AllowWarningsNo, R"SOURCE( #define A_CHAR 'a' )SOURCE", 1, R"OUTPUT(pub const A_CHAR = 'a';)OUTPUT"); - add_parseh_case("#define an unsigned integer literal", R"SOURCE( + add_parseh_case("#define an unsigned integer literal", AllowWarningsNo, + R"SOURCE( #define CHANNEL_COUNT 24 )SOURCE", 1, R"OUTPUT(pub const CHANNEL_COUNT = 24;)OUTPUT"); - add_parseh_case("#define referencing another #define", R"SOURCE( + add_parseh_case("#define referencing another #define", AllowWarningsNo, + R"SOURCE( #define THING2 THING1 #define THING1 1234 )SOURCE", 2, @@ -1743,7 +1755,7 @@ pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT", "pub const THING2 = THING1;"); - add_parseh_case("variables", R"SOURCE( + add_parseh_case("variables", AllowWarningsNo, R"SOURCE( extern int extern_var; static const int int_var = 13; )SOURCE", 2, @@ -1751,7 +1763,7 @@ static const int int_var = 13; "pub const int_var: c_int = 13;"); - add_parseh_case("circular struct definitions", R"SOURCE( + add_parseh_case("circular struct definitions", AllowWarningsNo, R"SOURCE( struct Bar; struct Foo { @@ -1770,14 +1782,15 @@ struct Bar { })SOURCE"); - add_parseh_case("typedef void", R"SOURCE( + add_parseh_case("typedef void", AllowWarningsNo, R"SOURCE( typedef void Foo; Foo fun(Foo *a); )SOURCE", 2, "pub const Foo = c_void;", "pub extern fn fun(a: ?&c_void);"); - add_parseh_case("generate inline func for #define global extern fn", R"SOURCE( + add_parseh_case("generate inline func for #define global extern fn", AllowWarningsNo, + R"SOURCE( extern void (*fn_ptr)(void); #define foo fn_ptr )SOURCE", 2, @@ -1787,19 +1800,19 @@ extern void (*fn_ptr)(void); })SOURCE"); - add_parseh_case("#define string", R"SOURCE( + add_parseh_case("#define string", AllowWarningsNo, R"SOURCE( #define foo "a string" )SOURCE", 1, "pub const foo = c\"a string\";"); - add_parseh_case("__cdecl doesn't mess up function pointers", R"SOURCE( + add_parseh_case("__cdecl doesn't mess up function pointers", AllowWarningsNo, R"SOURCE( void foo(void (__cdecl *fn_ptr)(void)); )SOURCE", 1, "pub extern fn foo(fn_ptr: ?extern fn());"); - add_parseh_case("comment after integer literal", R"SOURCE( + add_parseh_case("comment after integer literal", AllowWarningsNo, R"SOURCE( #define SDL_INIT_VIDEO 0x00000020 /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */ )SOURCE", 1, "pub const SDL_INIT_VIDEO = 32;"); - add_parseh_case("zig keywords in C code", R"SOURCE( + add_parseh_case("zig keywords in C code", AllowWarningsNo, R"SOURCE( struct type { int defer; }; @@ -1807,7 +1820,7 @@ struct type { @"defer": c_int, })", R"(pub const @"type" = struct_type;)"); - add_parseh_case("macro defines string literal with octal", R"SOURCE( + add_parseh_case("macro defines string literal with octal", AllowWarningsNo, R"SOURCE( #define FOO "aoeu\023 derp" #define FOO2 "aoeu\0234 derp" #define FOO_CHAR '\077' @@ -1925,10 +1938,14 @@ static void run_test(TestCase *test_case) { if (test_case->is_parseh) { if (buf_len(&zig_stderr) > 0) { - printf("\n!!!!! parseh emitted warnings:\n"); + printf("\nparseh emitted warnings:\n"); + printf("------------------------------\n"); print_compiler_invocation(test_case); printf("%s\n", buf_ptr(&zig_stderr)); -// exit(1); + printf("------------------------------\n"); + if (test_case->allow_warnings == AllowWarningsNo) { + exit(1); + } } for (int i = 0; i < test_case->compile_errors.length; i += 1) { -- cgit v1.2.3