-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.jou
92 lines (75 loc) · 3.01 KB
/
run.jou
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import "stdlib/str.jou"
import "stdlib/mem.jou"
import "stdlib/io.jou"
import "stdlib/process.jou"
import "../config.jou"
import "./command_line_args.jou"
import "./paths.jou"
@public
def run_linker(objpaths: byte**, exepath: byte*) -> None:
jou_exe = find_current_executable()
instdir = dirname(jou_exe)
if command_line_args.linker_flags != NULL:
linker_flags = malloc(strlen(command_line_args.linker_flags) + 50)
assert linker_flags != NULL
strcpy(linker_flags, "-lm ")
strcat(linker_flags, command_line_args.linker_flags)
else:
linker_flags = strdup("-lm")
assert linker_flags != NULL
size = 10L
for i = 0; objpaths[i] != NULL; i++:
size += strlen(objpaths[i]) + 10
quoted_object_files: byte* = malloc(size)
assert quoted_object_files != NULL
quoted_object_files[0] = '\0'
for i = 0; objpaths[i] != NULL; i++:
if i != 0:
strcat(quoted_object_files, " ")
strcat(quoted_object_files, "\"")
strcat(quoted_object_files, objpaths[i]) # TODO: escape properly?
strcat(quoted_object_files, "\"")
size = strlen(instdir) + strlen(quoted_object_files) + strlen(exepath) + strlen(linker_flags) + 100
if get_jou_clang_path() != NULL:
size += strlen(get_jou_clang_path())
command: byte* = malloc(size)
if WINDOWS:
# Assume mingw with clang has been downloaded with windows_setup.sh.
# Could also use clang, but gcc has less dependencies so we can make the Windows zips smaller.
# Windows quoting is weird. The outermost quotes get stripped here.
snprintf(command, size, "\"\"%s\\mingw64\\bin\\gcc.exe\" %s -o \"%s\" %s\"", instdir, quoted_object_files, exepath, linker_flags)
else:
# Assume clang is installed and use it to link. Could use lld, but clang is needed anyway.
# instdir is not used in this case.
snprintf(command, size, "'%s' %s -o '%s' %s", get_jou_clang_path(), quoted_object_files, exepath, linker_flags)
free(quoted_object_files)
free(jou_exe)
free(linker_flags)
if command_line_args.verbosity >= 2:
printf("Running linker: %s\n", command)
elif command_line_args.verbosity >= 1:
printf("Running linker\n")
if system(command) != 0:
exit(1)
free(command)
@public
def run_exe(exepath: byte*, valgrind: bool) -> int:
command = malloc(strlen(exepath) + 1000)
if WINDOWS:
sprintf(command, "\"%s\"", exepath)
while strstr(command, "/") != NULL:
*strstr(command, "/") = '\\'
else:
if valgrind:
sprintf(command, "valgrind -q --leak-check=full --show-leak-kinds=all --error-exitcode=1 '%s'", exepath)
else:
sprintf(command, "'%s'", exepath)
# Make sure that everything else shows up before the user's prints.
fflush(stdout)
fflush(stderr)
ret = system(command)
free(command)
if ret == 0:
return 0 # success
else:
return 1 # TODO: extract actual error code / return value