authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-16 19:42:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-16 19:42:46-07:00
log194e93a5824309d2eb8bf90b50b9ad4262f1f7ce
treeb1066eb5c15f879f1d16d520e3913b8499e0c501
parent0e92b440431bb9dd181ff4c0dc4da3b599a6a66c

add windows os code


2 files changed, 113 insertions(+), 34 deletions(-)

cmake/Findclang.cmake+8-2
...@@ -6,11 +6,17 @@...@@ -6,11 +6,17 @@
6# CLANG_INCLUDE_DIRS6# CLANG_INCLUDE_DIRS
7# CLANG_LIBRARIES7# CLANG_LIBRARIES
88
9find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h PATHS /usr/lib/llvm-3.7/include)9find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
10 PATHS
11 /usr/lib/llvm-3.7/include
12 /mingw64/include)
1013
11macro(FIND_AND_ADD_CLANG_LIB _libname_)14macro(FIND_AND_ADD_CLANG_LIB _libname_)
12 string(TOUPPER ${_libname_} _prettylibname_)15 string(TOUPPER ${_libname_} _prettylibname_)
13 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} PATHS /usr/lib/llvm-3.7/lib)16 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
17 PATHS
18 /usr/lib/llvm-3.7/lib
19 /mingw64/lib)
14 if(CLANG_${_prettylibname_}_LIB)20 if(CLANG_${_prettylibname_}_LIB)
15 set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})21 set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})
16 endif()22 endif()
src/os.cpp+105-32
...@@ -9,6 +9,30 @@...@@ -9,6 +9,30 @@
9#include "util.hpp"9#include "util.hpp"
10#include "error.hpp"10#include "error.hpp"
1111
12#if defined(_WIN32)
13#define ZIG_OS_WINDOWS
14
15#if !defined(NOMINMAX)
16#define NOMINMAX
17#endif
18
19#if !defined(VC_EXTRALEAN)
20#define VC_EXTRALEAN
21#endif
22
23#if !defined(WIN32_LEAN_AND_MEAN)
24#define WIN32_LEAN_AND_MEAN
25#endif
26
27#if !defined(UNICODE)
28#define UNICODE
29#endif
30
31#include <windows.h>
32#include <io.h>
33#else
34#define ZIG_OS_POSIX
35
12#include <unistd.h>36#include <unistd.h>
13#include <errno.h>37#include <errno.h>
14#include <sys/types.h>38#include <sys/types.h>
...@@ -17,7 +41,10 @@...@@ -17,7 +41,10 @@
17#include <fcntl.h>41#include <fcntl.h>
18#include <limits.h>42#include <limits.h>
1943
20void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_code) {44#endif
45
46
47static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, int *return_code) {
21 pid_t pid = fork();48 pid_t pid = fork();
22 if (pid == -1)49 if (pid == -1)
23 zig_panic("fork failed");50 zig_panic("fork failed");
...@@ -37,24 +64,20 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_...@@ -37,24 +64,20 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_
37 }64 }
38}65}
3966
40static int read_all_fd_stream(int fd, Buf *out_buf) {67#if defined(ZIG_OS_WINDOWS)
41 static const ssize_t buf_size = 0x2000;68static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, int *return_code) {
42 buf_resize(out_buf, buf_size);69 zig_panic("TODO os_spawn_process_windows");
43 ssize_t actual_buf_len = 0;70}
44 for (;;) {71#endif
45 ssize_t amt_read = read(fd, buf_ptr(out_buf) + actual_buf_len, buf_size);
46 if (amt_read < 0) {
47 return ErrorFileSystem;
48 }
49 actual_buf_len += amt_read;
50 if (amt_read == 0) {
51 buf_resize(out_buf, actual_buf_len);
52 return 0;
53 }
5472
55 buf_resize(out_buf, actual_buf_len + buf_size);73void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_code) {
56 }74#if defined(ZIG_OS_WINDOWS)
57 zig_unreachable();75 os_spawn_process_windows(exe, args, return_code);
76#elif defined(ZIG_OS_POSIX)
77 os_spawn_process_posix(exe, args, return_code);
78#else
79#error "missing os_spawn_process implementation"
80#endif
58}81}
5982
60void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) {83void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) {
...@@ -101,7 +124,30 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path) {...@@ -101,7 +124,30 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path) {
101 return ErrorNone;124 return ErrorNone;
102}125}
103126
104void os_exec_process(const char *exe, ZigList<const char *> &args,127int os_fetch_file(FILE *f, Buf *out_buf) {
128 static const ssize_t buf_size = 0x2000;
129 buf_resize(out_buf, buf_size);
130 ssize_t actual_buf_len = 0;
131 for (;;) {
132 size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);
133 actual_buf_len += amt_read;
134 if (amt_read != buf_size) {
135 if (feof(f)) {
136 buf_resize(out_buf, actual_buf_len);
137 return 0;
138 } else {
139 return ErrorFileSystem;
140 }
141 }
142
143 buf_resize(out_buf, actual_buf_len + buf_size);
144 }
145 zig_unreachable();
146}
147
148
149#if defined(ZIG_OS_POSIX)
150static void os_exec_process_posix(const char *exe, ZigList<const char *> &args,
105 int *return_code, Buf *out_stderr, Buf *out_stdout)151 int *return_code, Buf *out_stderr, Buf *out_stdout)
106{152{
107 int stdin_pipe[2];153 int stdin_pipe[2];
...@@ -146,27 +192,37 @@ void os_exec_process(const char *exe, ZigList<const char *> &args,...@@ -146,27 +192,37 @@ void os_exec_process(const char *exe, ZigList<const char *> &args,
146192
147 waitpid(pid, return_code, 0);193 waitpid(pid, return_code, 0);
148194
149 read_all_fd_stream(stdout_pipe[0], out_stdout);195 os_fetch_file(fdopen(stdout_pipe[0], "rb"), out_stdout);
150 read_all_fd_stream(stderr_pipe[0], out_stderr);196 os_fetch_file(fdopen(stderr_pipe[0], "rb"), out_stderr);
151197
152 }198 }
153}199}
200#endif
201
202void os_exec_process(const char *exe, ZigList<const char *> &args,
203 int *return_code, Buf *out_stderr, Buf *out_stdout)
204{
205#if defined(ZIG_OS_WINDOWS)
206 return os_exec_process_windows(exe, args, return_code, out_stderr, out_stdout);
207#elif defined(ZIG_OS_POSIX)
208 return os_exec_process_posix(exe, args, return_code, out_stderr, out_stdout);
209#else
210#error "missing os_exec_process implementation"
211#endif
212}
154213
155void os_write_file(Buf *full_path, Buf *contents) {214void os_write_file(Buf *full_path, Buf *contents) {
156 int fd;215 FILE *f = fopen(buf_ptr(full_path), "wb");
157 if ((fd = open(buf_ptr(full_path), O_CREAT|O_CLOEXEC|O_WRONLY|O_TRUNC, S_IRWXU)) == -1)216 if (!f) {
158 zig_panic("open failed");217 zig_panic("open failed");
159 ssize_t amt_written = write(fd, buf_ptr(contents), buf_len(contents));218 }
219 size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f);
160 if (amt_written != buf_len(contents))220 if (amt_written != buf_len(contents))
161 zig_panic("write failed: %s", strerror(errno));221 zig_panic("write failed: %s", strerror(errno));
162 if (close(fd) == -1)222 if (fclose(f))
163 zig_panic("close failed");223 zig_panic("close failed");
164}224}
165225
166int os_fetch_file(FILE *f, Buf *out_contents) {
167 return read_all_fd_stream(fileno(f), out_contents);
168}
169
170int os_fetch_file_path(Buf *full_path, Buf *out_contents) {226int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
171 FILE *f = fopen(buf_ptr(full_path), "rb");227 FILE *f = fopen(buf_ptr(full_path), "rb");
172 if (!f) {228 if (!f) {
...@@ -192,6 +248,9 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents) {...@@ -192,6 +248,9 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
192}248}
193249
194int os_get_cwd(Buf *out_cwd) {250int os_get_cwd(Buf *out_cwd) {
251#if defined(ZIG_OS_WINDOWS)
252 zig_panic("TODO os_get_cwd for windows");
253#elif defined(ZIG_OS_POSIX)
195 int err = ERANGE;254 int err = ERANGE;
196 buf_resize(out_cwd, 512);255 buf_resize(out_cwd, 512);
197 while (err == ERANGE) {256 while (err == ERANGE) {
...@@ -202,10 +261,19 @@ int os_get_cwd(Buf *out_cwd) {...@@ -202,10 +261,19 @@ int os_get_cwd(Buf *out_cwd) {
202 zig_panic("unable to get cwd: %s", strerror(err));261 zig_panic("unable to get cwd: %s", strerror(err));
203262
204 return 0;263 return 0;
264#else
265#error "missing os_get_cwd implementation"
266#endif
205}267}
206268
207bool os_stderr_tty(void) {269bool os_stderr_tty(void) {
208 return isatty(STDERR_FILENO);270#if defined(ZIG_OS_WINDOWS)
271 return _isatty(STDERR_FILENO) != 0;
272#elif defined(ZIG_OS_POSIX)
273 return isatty(STDERR_FILENO) != 0;
274#else
275#error "missing os_stderr_tty implementation"
276#endif
209}277}
210278
211int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {279int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
...@@ -217,10 +285,15 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {...@@ -217,10 +285,15 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
217 return ErrorFileSystem;285 return ErrorFileSystem;
218 }286 }
219287
220 ssize_t amt_written = write(fd, buf_ptr(contents), buf_len(contents));288 FILE *f = fdopen(fd, "wb");
289 if (!f) {
290 zig_panic("fdopen failed");
291 }
292
293 size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f);
221 if (amt_written != buf_len(contents))294 if (amt_written != buf_len(contents))
222 zig_panic("write failed: %s", strerror(errno));295 zig_panic("write failed: %s", strerror(errno));
223 if (close(fd) == -1)296 if (fclose(f))
224 zig_panic("close failed");297 zig_panic("close failed");
225298
226 return 0;299 return 0;