authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 13:51:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 13:51:36-07:00
logfefbee166d68d17dd0d5904fc52f72397fb51092
treee667c4d2d3ff2af639468c6ebaf750ae26a74cea
parent925c805d4b2431b1c3140ba9c9dd7fc81de8a7d7

hello world example working


5 files changed, 65 insertions(+), 3 deletions(-)

CMakeLists.txt+1
...@@ -31,6 +31,7 @@ set(ZIG_SOURCES...@@ -31,6 +31,7 @@ set(ZIG_SOURCES
31 "${CMAKE_SOURCE_DIR}/src/util.cpp"31 "${CMAKE_SOURCE_DIR}/src/util.cpp"
32 "${CMAKE_SOURCE_DIR}/src/codegen.cpp"32 "${CMAKE_SOURCE_DIR}/src/codegen.cpp"
33 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"33 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
34 "${CMAKE_SOURCE_DIR}/src/os.cpp"
34)35)
3536
36set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")37set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
src/codegen.cpp+13-1
...@@ -8,6 +8,7 @@...@@ -8,6 +8,7 @@
8#include "codegen.hpp"8#include "codegen.hpp"
9#include "hash_map.hpp"9#include "hash_map.hpp"
10#include "zig_llvm.hpp"10#include "zig_llvm.hpp"
11#include "os.hpp"
1112
12#include <stdio.h>13#include <stdio.h>
1314
...@@ -422,7 +423,18 @@ void code_gen_link(CodeGen *g, bool is_static, const char *out_file) {...@@ -422,7 +423,18 @@ void code_gen_link(CodeGen *g, bool is_static, const char *out_file) {
422 LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(target_ref, native_triple,423 LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(target_ref, native_triple,
423 native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault);424 native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault);
424425
425 if (LLVMTargetMachineEmitToFile(target_machine, g->mod, strdup(out_file), LLVMObjectFile, &err_msg)) {426 Buf out_file_o = {0};
427 buf_init_from_str(&out_file_o, out_file);
428 buf_append_str(&out_file_o, ".o");
429
430 if (LLVMTargetMachineEmitToFile(target_machine, g->mod, buf_ptr(&out_file_o), LLVMObjectFile, &err_msg)) {
426 zig_panic("unable to write object file: %s", err_msg);431 zig_panic("unable to write object file: %s", err_msg);
427 }432 }
433
434 ZigList<const char *> args = {0};
435 args.append("-o");
436 args.append(out_file);
437 args.append((const char *)buf_ptr(&out_file_o));
438 args.append("-lc");
439 os_spawn_process("ld", args, false);
428}440}
src/os.cpp created+33
...@@ -0,0 +1,33 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include "os.hpp"
9#include "util.hpp"
10
11#include <unistd.h>
12#include <errno.h>
13
14void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detached) {
15 pid_t pid = fork();
16 if (pid == -1)
17 zig_panic("fork failed");
18 if (pid != 0)
19 return;
20 if (detached) {
21 if (setsid() == -1)
22 zig_panic("process detach failed");
23 }
24
25 const char **argv = allocate<const char *>(args.length + 2);
26 argv[0] = exe;
27 argv[args.length + 1] = nullptr;
28 for (int i = 0; i < args.length; i += 1) {
29 argv[i + 1] = args.at(i);
30 }
31 execvp(exe, const_cast<char * const *>(argv));
32 zig_panic("execvp failed: %s", strerror(errno));
33}
src/os.hpp created+16
...@@ -0,0 +1,16 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_OS_HPP
9#define ZIG_OS_HPP
10
11#include "list.hpp"
12#include "buffer.hpp"
13
14void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detached);
15
16#endif
test/add.zig+2-2
...@@ -1,3 +1,3 @@...@@ -1,3 +1,3 @@
1pub fn add(a: int, b: int) -> int {1export fn add(a: i32, b: i32) -> i32 {
2 a + b2 return a + b;
3}3}