about summary refs log tree commit diff
path: root/boot/catboot.c
diff options
context:
space:
mode:
Diffstat (limited to 'boot/catboot.c')
-rw-r--r--boot/catboot.c63
1 files changed, 47 insertions, 16 deletions
diff --git a/boot/catboot.c b/boot/catboot.c
index 8412ffc..a0b4ca1 100644
--- a/boot/catboot.c
+++ b/boot/catboot.c
@@ -13,11 +13,13 @@
  * this compiler version will be permanently retired.
  * (although that's still very far away!! have fun! :3)
  *
- * Copyright (c) 2025, Mel G. <mel@rnrd.eu>
+ * Copyright (c) 2025-2026, Mel G. <mel@rnrd.eu>
  *
  * SPDX-License-Identifier: MPL-2.0
  */
 
+#define _POSIX_C_SOURCE 200809L
+
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -106,7 +108,10 @@ debug_parse_pass(struct Source_File source_file)
 
     struct Tree tree;
     int parser_result = parser_do_your_thing(&parser, &tree);
-    if (parser_result != 0) fprintf(stderr, "parser finished with errors\n");
+    if (parser_result != 0) {
+        log_error("parser finished with errors\n");
+        return parser_result;
+    }
 
     tree_printer(&tree);
 
@@ -125,7 +130,7 @@ debug_transpile_pass(struct Source_File source_file)
     struct Tree tree;
     int parser_result = parser_do_your_thing(&parser, &tree);
     if (parser_result != 0) {
-        fprintf(stderr, "parser finished with errors\n");
+        log_error("parser finished with errors\n");
         return parser_result;
     }
 
@@ -218,31 +223,57 @@ default_command(struct Command_Arguments* arguments)
 
     struct Tree tree;
     if (parser_do_your_thing(&parser, &tree) != 0) {
-        fprintf(stderr, "parser finished with errors\n");
+        log_error("parser finished with errors\n");
+
+        result = COMMAND_FAIL;
         goto end;
     }
 
-    FILE* output_file = nil;
-    if (arguments->output) {
-        output_file = fopen(arguments->output, "w");
-        if (!output_file) {
-            fprintf(stderr, "could not open output file: %s\n", arguments->output);
-            result = COMMAND_FAIL;
-            goto end;
-        }
-    } else {
-        output_file = stdout;
+    ascii build_directory_template[] = "/tmp/catboot-build-XXXXXX"; // TODO: /tmp/catboot/build-XXXXXX
+
+    ascii* build_directory_path = mkdtemp(build_directory_template);
+    struct String build_path = string_from_c_string(build_directory_path);
+    struct String source_path = string_append_c_str(build_path, "/input.c");
+
+    FILE* output_file = fopen(string_c_str(source_path), "w");
+    if (!output_file) {
+        log_error("could not open temporary output file: %s\n", string_c_str(source_path));
+
+        result = COMMAND_FAIL;
+        goto end;
     }
 
     struct Transpiler transpiler;
     transpiler_new(&transpiler, output_file);
 
     if (transpiler_catskill_to_c(&transpiler, &tree) != 0) {
-        fprintf(stderr, "transpiler finished with errors\n");
+        log_error("transpiler finished with errors\n");
+        fclose(output_file);
+
         result = COMMAND_FAIL;
+        goto end;
     }
+    fclose(output_file);
 
-    if (output_file != stdout) fclose(output_file);
+    struct Build_Result build_result = build_executable(build_path);
+    if (!build_result.success) {
+        log_error("backend failure with exit code %ld\n", build_result.compiler_exit_code);
+        log_error("compiler output:\n%.*s\n", (int)build_result.compiler_log.length, build_result.compiler_log.data);
+
+        result = COMMAND_FAIL;
+        goto end;
+    }
+
+    // copy the output executable to the desired location
+    const ascii* output_filename = arguments->output ? arguments->output : "a.out";
+
+    // simple copy command for now
+    ascii copy_command[2048];
+    snprintf(copy_command, sizeof(copy_command), "cp %s %s", string_c_str(build_result.output_path), output_filename);
+    if (system(copy_command) != 0) {
+        log_error("failed to copy executable to %s\n", output_filename);
+        result = COMMAND_FAIL;
+    }
 
 end:
     return result;