From 7343af3c1b31c88c7edddfd6260a4096df4ad42c Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Tue, 5 Dec 2023 02:57:47 -0500 Subject: [PATCH] Add option to add c flags through ocen CLI --- bootstrap/stage0.c | 12 ++++++++++++ compiler/main.oc | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/bootstrap/stage0.c b/bootstrap/stage0.c index 0022bc7..1e686b6 100644 --- a/bootstrap/stage0.c +++ b/bootstrap/stage0.c @@ -1404,6 +1404,7 @@ bool debug = false; u32 error_level = ((u32)2); char *docs_path = NULL; bool include_stdlib = true; +std_vector_Vector__5 *extra_c_flags; /* function declarations */ FILE *std_File_open(char *path, char *mode); i32 std_File_write(FILE *this, void *buf, u32 size); @@ -11769,6 +11770,7 @@ void usage(i32 code) { printf(" -d Emit debug information (default: false)""\n"); printf(" -l path Directory to search for libraries (can be used multiple times)""\n"); printf(" --docs path Output documentation JSON (default: none)""\n"); + printf(" --cflags flags Additional C flags (can be used multiple times)""\n"); printf(" (Default: working directory)""\n"); printf("--------------------------------------------------------""\n"); exit(code); @@ -11793,6 +11795,13 @@ void save_and_compile_code(ast_program_Program *program, char *code) { strcat(cmdbuf, flag); } } + for (std_vector_Iterator__5 __iter = std_vector_Vector__5_iter(extra_c_flags); std_vector_Iterator__5_has_value(&__iter); std_vector_Iterator__5_next(&__iter)) { + char *flag = std_vector_Iterator__5_cur(&__iter); + { + strcat(cmdbuf, " "); + strcat(cmdbuf, flag); + } + } if (debug) { strcat(cmdbuf, " -ggdb3"); } @@ -11807,6 +11816,7 @@ void save_and_compile_code(ast_program_Program *program, char *code) { } void parse_args(i32 argc, char **argv, ast_program_Program *program) { + extra_c_flags=std_vector_Vector__5_new(((u32)16)); for (u32 i = ((u32)1); (i < ((u32)argc)); i+=((u32)1)) { { char *__match_str = argv[i]; @@ -11839,6 +11849,8 @@ void parse_args(i32 argc, char **argv, ast_program_Program *program) { program->check_doc_links=true; } else if (!strcmp(__match_str, "--no-stdlib")) { include_stdlib=false; + } else if (!strcmp(__match_str, "--cflags") || !strcmp(__match_str, "-cf")) { + std_vector_Vector__5_push(extra_c_flags, argv[++i]); } else { if (argv[i][((u32)0)]=='-') { printf("Unknown option: %s""\n", argv[i]); diff --git a/compiler/main.oc b/compiler/main.oc index 9b7124d..4625b56 100644 --- a/compiler/main.oc +++ b/compiler/main.oc @@ -1,6 +1,7 @@ //* Entry point for the compiler import std::libc::{ exit, calloc, system } +import std::vector::Vector import .ast::program::Program import .parser::Parser import .passes::{ run_typecheck_passes, run_codegen_passes } @@ -22,6 +23,7 @@ def usage(code: i32) { println(" -d Emit debug information (default: false)") println(" -l path Directory to search for libraries (can be used multiple times)") println(" --docs path Output documentation JSON (default: none)") + println(" --cflags flags Additional C flags (can be used multiple times)") println(" (Default: working directory)") println("--------------------------------------------------------") exit(code) @@ -36,6 +38,7 @@ let debug: bool = false let error_level: u32 = 2 let docs_path: str = null let include_stdlib: bool = true +let extra_c_flags: &Vector def save_and_compile_code(program: &Program, code: str) { if not c_path? { @@ -54,6 +57,10 @@ def save_and_compile_code(program: &Program, code: str) { cmdbuf.concat(" ") cmdbuf.concat(flag) } + for flag : extra_c_flags.iter() { + cmdbuf.concat(" ") + cmdbuf.concat(flag) + } if debug { cmdbuf.concat(" -ggdb3") } @@ -69,6 +76,8 @@ def save_and_compile_code(program: &Program, code: str) { } def parse_args(argc: i32, argv: &str, program: &Program) { + extra_c_flags = Vector::new() + for let i = 1; i < argc as u32; i += 1 { match argv[i] { "-h" => usage(code: 0) @@ -96,6 +105,7 @@ def parse_args(argc: i32, argv: &str, program: &Program) { program.check_doc_links = true } "--no-stdlib" => include_stdlib = false + "--cflags" | "-cf" => extra_c_flags.push(argv[++i]) else => { if argv[i][0] == '-' { println("Unknown option: %s", argv[i])