authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-16 19:55:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-16 19:55:49-07:00
logafa5d1ea1223b8eab475c4186faed4385ac95da0
tree7cc4aaf850316ef15bc10bca0c610a67be274bbd
parent194e93a5824309d2eb8bf90b50b9ad4262f1f7ce

os: fix ability to compile for windows


1 files changed, 39 insertions(+), 3 deletions(-)

src/os.cpp+39-3
...@@ -34,7 +34,6 @@...@@ -34,7 +34,6 @@
34#define ZIG_OS_POSIX34#define ZIG_OS_POSIX
3535
36#include <unistd.h>36#include <unistd.h>
37#include <errno.h>
38#include <sys/types.h>37#include <sys/types.h>
39#include <sys/stat.h>38#include <sys/stat.h>
40#include <sys/wait.h>39#include <sys/wait.h>
...@@ -43,7 +42,11 @@...@@ -43,7 +42,11 @@
4342
44#endif43#endif
4544
45#include <stdlib.h>
46#include <errno.h>
47
4648
49#if defined(ZIG_OS_POSIX)
47static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, int *return_code) {50static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, int *return_code) {
48 pid_t pid = fork();51 pid_t pid = fork();
49 if (pid == -1)52 if (pid == -1)
...@@ -63,6 +66,7 @@ static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args,...@@ -63,6 +66,7 @@ static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args,
63 waitpid(pid, return_code, 0);66 waitpid(pid, return_code, 0);
64 }67 }
65}68}
69#endif
6670
67#if defined(ZIG_OS_WINDOWS)71#if defined(ZIG_OS_WINDOWS)
68static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, int *return_code) {72static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, int *return_code) {
...@@ -106,6 +110,9 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {...@@ -106,6 +110,9 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
106}110}
107111
108int os_path_real(Buf *rel_path, Buf *out_abs_path) {112int os_path_real(Buf *rel_path, Buf *out_abs_path) {
113#if defined(ZIG_OS_WINDOWS)
114 zig_panic("TODO os_path_real for windows");
115#elif defined(ZIG_OS_POSIX)
109 buf_resize(out_abs_path, PATH_MAX + 1);116 buf_resize(out_abs_path, PATH_MAX + 1);
110 char *result = realpath(buf_ptr(rel_path), buf_ptr(out_abs_path));117 char *result = realpath(buf_ptr(rel_path), buf_ptr(out_abs_path));
111 if (!result) {118 if (!result) {
...@@ -122,6 +129,9 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path) {...@@ -122,6 +129,9 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path) {
122 }129 }
123 buf_resize(out_abs_path, strlen(buf_ptr(out_abs_path)));130 buf_resize(out_abs_path, strlen(buf_ptr(out_abs_path)));
124 return ErrorNone;131 return ErrorNone;
132#else
133#error "missing os_path_real implementation"
134#endif
125}135}
126136
127int os_fetch_file(FILE *f, Buf *out_buf) {137int os_fetch_file(FILE *f, Buf *out_buf) {
...@@ -199,6 +209,14 @@ static void os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -199,6 +209,14 @@ static void os_exec_process_posix(const char *exe, ZigList<const char *> &args,
199}209}
200#endif210#endif
201211
212#if defined(ZIG_OS_WINDOWS)
213static void os_exec_process_windows(const char *exe, ZigList<const char *> &args,
214 int *return_code, Buf *out_stderr, Buf *out_stdout)
215{
216 zig_panic("TODO implement os_exec_process_windows");
217}
218#endif
219
202void os_exec_process(const char *exe, ZigList<const char *> &args,220void os_exec_process(const char *exe, ZigList<const char *> &args,
203 int *return_code, Buf *out_stderr, Buf *out_stdout)221 int *return_code, Buf *out_stderr, Buf *out_stdout)
204{222{
...@@ -217,7 +235,7 @@ void os_write_file(Buf *full_path, Buf *contents) {...@@ -217,7 +235,7 @@ void os_write_file(Buf *full_path, Buf *contents) {
217 zig_panic("open failed");235 zig_panic("open failed");
218 }236 }
219 size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f);237 size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f);
220 if (amt_written != buf_len(contents))238 if (amt_written != (size_t)buf_len(contents))
221 zig_panic("write failed: %s", strerror(errno));239 zig_panic("write failed: %s", strerror(errno));
222 if (fclose(f))240 if (fclose(f))
223 zig_panic("close failed");241 zig_panic("close failed");
...@@ -276,7 +294,8 @@ bool os_stderr_tty(void) {...@@ -276,7 +294,8 @@ bool os_stderr_tty(void) {
276#endif294#endif
277}295}
278296
279int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {297#if defined(ZIG_OS_POSIX)
298static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
280 buf_resize(out_tmp_path, 0);299 buf_resize(out_tmp_path, 0);
281 buf_appendf(out_tmp_path, "/tmp/XXXXXX%s", buf_ptr(suffix));300 buf_appendf(out_tmp_path, "/tmp/XXXXXX%s", buf_ptr(suffix));
282301
...@@ -298,6 +317,23 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {...@@ -298,6 +317,23 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
298317
299 return 0;318 return 0;
300}319}
320#endif
321
322#if defined(ZIG_OS_WINDOWS)
323static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
324 zig_panic("TODO implement os_buf_to_tmp_file_windows");
325}
326#endif
327
328int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
329#if defined(ZIG_OS_WINDOWS)
330 return os_buf_to_tmp_file_windows(contents, suffix, out_tmp_path);
331#elif defined(ZIG_OS_POSIX)
332 return os_buf_to_tmp_file_posix(contents, suffix, out_tmp_path);
333#else
334#error "missing os_buf_to_tmp_file implementation"
335#endif
336}
301337
302int os_delete_file(Buf *path) {338int os_delete_file(Buf *path) {
303 if (remove(buf_ptr(path))) {339 if (remove(buf_ptr(path))) {