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>`.
This commit is contained in:
Andrew Kelley 2016-09-14 02:44:31 -04:00
parent cf9b21c09f
commit f1761632da
5 changed files with 73 additions and 65 deletions

View File

@ -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) {

View File

@ -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));
}

View File

@ -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));

View File

@ -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"),
}
}

View File

@ -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<TestCase*> 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<TestCase>(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 <stdint.h>
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) {