From adcf55d9299e94f50a9b2af44035550affa49781 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 15 Apr 2025 02:27:56 +0300 Subject: [PATCH 1/3] refactored? --- compiler/run.jou | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/compiler/run.jou b/compiler/run.jou index 24fcde9a0..e36487dd3 100644 --- a/compiler/run.jou +++ b/compiler/run.jou @@ -28,12 +28,10 @@ def quote_paths(paths: List[byte*]) -> byte*: return result -@public -def run_linker(objpaths: List[byte*], exepath: byte*, linker_flags: byte*) -> None: - quoted_object_files = quote_paths(objpaths) - - command: byte* - if WINDOWS: +# On Linux, we prefer LLVM tools (clang and llvm-ar) of the same LLVM version. +# On Windows, the tools are in mingw64/bin folder. +if WINDOWS: + def get_mingw_command(exe_filename: byte*, args: byte*) -> byte*: # Assume mingw with clang has been downloaded with windows_setup.sh. # # During the bootstrapping, the location of mingw is something @@ -49,16 +47,28 @@ def run_linker(objpaths: List[byte*], exepath: byte*, linker_flags: byte*) -> No asprintf(&mingw_dir, "%s\\mingw64", dirname(jou_exe)) free(jou_exe) - # Could also use clang, but gcc has less dependencies so we can make the Windows zips smaller. + result: byte* + # Windows quoting is weird. The outermost quotes get stripped here. - asprintf(&command, "\"\"%s\\bin\\gcc.exe\" %s -o \"%s\" %s\"", mingw_dir, quoted_object_files, exepath, linker_flags) - free(mingw_dir) - else: - # Assume clang is installed and use it to link. Could use lld, but clang is needed anyway. - asprintf(&command, "'%s' %s -o '%s' %s", JOU_CLANG_PATH, quoted_object_files, exepath, linker_flags) + asprintf(&result, "\"\"%s\\bin\\%s\" %s\"", exe_filename, args) + +@public +def run_linker(objpaths: List[byte*], exepath: byte*, linker_flags: byte*) -> None: + quoted_object_files = quote_paths(objpaths) + + args: byte* + asprintf(&args, "%s -o \"%s\" %s", quoted_object_files, exepath, linker_flags) free(quoted_object_files) + command: byte* + if WINDOWS: + # Could also use clang, but gcc has less dependencies so we can make the Windows zips smaller. + command = get_mingw_command("gcc.exe", args) + else: + asprintf(&command, "'%s' %s", JOU_CLANG_PATH, args) + free(args) + if command_line_args.verbosity >= 1: printf("Running linker: %s\n", command) From 35c36ce6c559dbc216dcfa986c1381da8ff3ebb9 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 15 Apr 2025 02:44:40 +0300 Subject: [PATCH 2/3] ar works --- Makefile.posix | 2 ++ compiler/main.jou | 15 ++++++++++++--- compiler/run.jou | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Makefile.posix b/Makefile.posix index 59b77e0e7..26d5ed0d2 100644 --- a/Makefile.posix +++ b/Makefile.posix @@ -28,6 +28,8 @@ config.jou: Makefile.posix echo 'link "$(shell $(LLVM_CONFIG) --ldflags --libs)"' >> config.jou echo '@public' >> config.jou echo 'const JOU_CLANG_PATH: byte* = "$(shell which `$(LLVM_CONFIG) --bindir`/clang || which clang)"' >> config.jou + echo '@public' >> config.jou + echo 'const JOU_AR_PATH: byte* = "$(shell which `$(LLVM_CONFIG) --bindir`/llvm-ar || which ar)"' >> config.jou jou_bootstrap: bootstrap.sh env LLVM_CONFIG=$(LLVM_CONFIG) ./bootstrap.sh diff --git a/compiler/main.jou b/compiler/main.jou index c73769880..1895fc73d 100644 --- a/compiler/main.jou +++ b/compiler/main.jou @@ -492,10 +492,19 @@ def main(argc: int, argv: byte**) -> int: compst.free() free(stdlib) - exepath = decide_exe_path() - run_linker(objpaths, exepath, linker_flags) - free(linker_flags) + exepath: byte* = NULL + if ( + command_line_args.outfile != NULL + and strlen(command_line_args.outfile) > 2 + and ends_with(command_line_args.outfile, ".a") + ): + # Output .a file instead of executable + run_ar(objpaths, command_line_args.outfile) + else: + exepath = decide_exe_path() + run_linker(objpaths, exepath, linker_flags) + free(linker_flags) for p = objpaths.ptr; p < objpaths.end(); p++: free(*p) free(objpaths.ptr) diff --git a/compiler/run.jou b/compiler/run.jou index e36487dd3..1d6dd08ea 100644 --- a/compiler/run.jou +++ b/compiler/run.jou @@ -77,6 +77,30 @@ def run_linker(objpaths: List[byte*], exepath: byte*, linker_flags: byte*) -> No free(command) +@public +def run_ar(objpaths: List[byte*], a_path: byte*) -> None: + assert ends_with(a_path, ".a") + quoted_object_files = quote_paths(objpaths) + + args: byte* + asprintf(&args, "rcs \"%s\" %s", a_path, quoted_object_files) + free(quoted_object_files) + + command: byte* + if WINDOWS: + command = get_mingw_command("ar.exe", args) + else: + asprintf(&command, "'%s' %s", JOU_AR_PATH, args) + free(args) + + if command_line_args.verbosity >= 1: + printf("Running ar: %s\n", command) + + if system(command) != 0: + exit(1) + free(command) + + @public def run_exe(exepath: byte*, valgrind: bool) -> int: command: byte* From 6a6c6aaed1f0eed93ad6f8501cd359710c1cd568 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 15 Apr 2025 02:54:46 +0300 Subject: [PATCH 3/3] fixed? --- compiler/run.jou | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/run.jou b/compiler/run.jou index 1d6dd08ea..53141d6c7 100644 --- a/compiler/run.jou +++ b/compiler/run.jou @@ -47,10 +47,12 @@ if WINDOWS: asprintf(&mingw_dir, "%s\\mingw64", dirname(jou_exe)) free(jou_exe) + # Windows quoting is weird. The outermost quotes get stripped here. result: byte* + asprintf(&result, "\"\"%s\\bin\\%s\" %s\"", mingw_dir, exe_filename, args) - # Windows quoting is weird. The outermost quotes get stripped here. - asprintf(&result, "\"\"%s\\bin\\%s\" %s\"", exe_filename, args) + free(mingw_dir) + return result @public