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 @@
66# CLANG_INCLUDE_DIRS
77# 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
1114macro(FIND_AND_ADD_CLANG_LIB _libname_)
1215 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)
1420 if(CLANG_${_prettylibname_}_LIB)
1521 set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})
1622 endif()
src/os.cpp+105-32
......@@ -9,6 +9,30 @@
99#include "util.hpp"
1010#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
1236#include <unistd.h>
1337#include <errno.h>
1438#include <sys/types.h>
......@@ -17,7 +41,10 @@
1741#include <fcntl.h>
1842#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) {
2148 pid_t pid = fork();
2249 if (pid == -1)
2350 zig_panic("fork failed");
......@@ -37,24 +64,20 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_
3764 }
3865}
3966
40static int read_all_fd_stream(int fd, Buf *out_buf) {
41 static const ssize_t buf_size = 0x2000;
42 buf_resize(out_buf, buf_size);
43 ssize_t actual_buf_len = 0;
44 for (;;) {
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 }
67#if defined(ZIG_OS_WINDOWS)
68static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, int *return_code) {
69 zig_panic("TODO os_spawn_process_windows");
70}
71#endif
5472
55 buf_resize(out_buf, actual_buf_len + buf_size);
56 }
57 zig_unreachable();
73void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_code) {
74#if defined(ZIG_OS_WINDOWS)
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
5881}
5982
6083void 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) {
101124 return ErrorNone;
102125}
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,
105151 int *return_code, Buf *out_stderr, Buf *out_stdout)
106152{
107153 int stdin_pipe[2];
......@@ -146,27 +192,37 @@ void os_exec_process(const char *exe, ZigList<const char *> &args,
146192
147193 waitpid(pid, return_code, 0);
148194
149 read_all_fd_stream(stdout_pipe[0], out_stdout);
150 read_all_fd_stream(stderr_pipe[0], out_stderr);
195 os_fetch_file(fdopen(stdout_pipe[0], "rb"), out_stdout);
196 os_fetch_file(fdopen(stderr_pipe[0], "rb"), out_stderr);
151197
152198 }
153199}
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
155214void os_write_file(Buf *full_path, Buf *contents) {
156 int fd;
157 if ((fd = open(buf_ptr(full_path), O_CREAT|O_CLOEXEC|O_WRONLY|O_TRUNC, S_IRWXU)) == -1)
215 FILE *f = fopen(buf_ptr(full_path), "wb");
216 if (!f) {
158217 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);
160220 if (amt_written != buf_len(contents))
161221 zig_panic("write failed: %s", strerror(errno));
162 if (close(fd) == -1)
222 if (fclose(f))
163223 zig_panic("close failed");
164224}
165225
166int os_fetch_file(FILE *f, Buf *out_contents) {
167 return read_all_fd_stream(fileno(f), out_contents);
168}
169
170226int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
171227 FILE *f = fopen(buf_ptr(full_path), "rb");
172228 if (!f) {
......@@ -192,6 +248,9 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
192248}
193249
194250int 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)
195254 int err = ERANGE;
196255 buf_resize(out_cwd, 512);
197256 while (err == ERANGE) {
......@@ -202,10 +261,19 @@ int os_get_cwd(Buf *out_cwd) {
202261 zig_panic("unable to get cwd: %s", strerror(err));
203262
204263 return 0;
264#else
265#error "missing os_get_cwd implementation"
266#endif
205267}
206268
207269bool 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
209277}
210278
211279int 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) {
217285 return ErrorFileSystem;
218286 }
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);
221294 if (amt_written != buf_len(contents))
222295 zig_panic("write failed: %s", strerror(errno));
223 if (close(fd) == -1)
296 if (fclose(f))
224297 zig_panic("close failed");
225298
226299 return 0;