From 01becffe2c5118a984c7a34d802cc90c9f6413b0 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 22 Oct 2014 22:20:25 -0400 Subject: [PATCH] Implemented object compilation mode --- modules/libcds | 2 +- source/sclpl/codegen.c | 2 +- source/sclpl/main.c | 43 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/modules/libcds b/modules/libcds index 20a2ea4..6f230a5 160000 --- a/modules/libcds +++ b/modules/libcds @@ -1 +1 @@ -Subproject commit 20a2ea4fb0e0154f41032c945b25f66069a45a37 +Subproject commit 6f230a518de102ec91186f35ffff401546fd4878 diff --git a/source/sclpl/codegen.c b/source/sclpl/codegen.c index 9fe250e..8488828 100644 --- a/source/sclpl/codegen.c +++ b/source/sclpl/codegen.c @@ -113,7 +113,7 @@ static void print_string(FILE* file, const char* str) { /*****************************************************************************/ static void emit_header(FILE* file) { - fputs("#include \n\n", file); + fputs("#include \"sclpl.h\"\n\n", file); } static void emit_fn_signature(FILE* file, char* name, tree_t* fnval) { diff --git a/source/sclpl/main.c b/source/sclpl/main.c index fdb7232..930d793 100644 --- a/source/sclpl/main.c +++ b/source/sclpl/main.c @@ -125,7 +125,8 @@ bool file_exists(const char* name) { list_t* input_files(void) { list_t* infiles = list_new(); - const char** files = opts_arguments(); + const char** fvec = opts_arguments(); + const char** files = fvec; while (NULL != files[0]) { if (!file_exists(files[0])) { mem_release(infiles); @@ -136,9 +137,30 @@ list_t* input_files(void) { files++; } mem_release(list_pop_front(infiles)); + free(fvec); return infiles; } +/* Command Building + *****************************************************************************/ +char Object_Cmd[] = "clang -c -I. -o %s %s"; +char Program_Cmd[] = "clang -o %s %s"; +char StaticLib_Cmd[] = "ar rcs %s %s"; +char SharedLib_Cmd[] = "clang -shared %s"; + +str_t* str_join(char* joinstr, vec_t* strs) { + str_t* ret = str_new(""); + str_t* jstr = str_new(joinstr); + for (size_t idx = 0; idx < vec_size(strs); idx++) { + str_t* str = (str_t*)vec_at(strs, idx); + if (str_size(ret) > 0) + mem_swap((void**)&ret, str_concat(ret, jstr)); + mem_swap((void**)&ret, str_concat(ret, str)); + } + mem_release(jstr); + return ret; +} + /* Utility Functions *****************************************************************************/ typedef enum { @@ -165,7 +187,7 @@ str_t* get_extension(file_type_t ftype) { str_t* get_filename(file_type_t ftype, str_t* infile) { str_t* ext_ind = str_new("."); size_t index = str_rfind(infile, ext_ind); - str_t* rawname = str_substr(infile, 0, str_size(infile)-index); + str_t* rawname = str_substr(infile, 0, index); str_t* ext = get_extension(ftype); str_t* fname = str_concat(rawname, ext); mem_release(ext_ind); @@ -212,7 +234,18 @@ str_t* translate_file(str_t* in) { } str_t* compile_file(str_t* in) { - str_t* ofname = get_filename(OBJECT, in); + str_t* ofname = get_filename(OBJECT, in); + vec_t* parts = vec_new(3, str_new("clang -c -o"), mem_retain(ofname), mem_retain(in)); + str_t* command = str_join(" ", parts); + if (opts_is_set(NULL, "verbose")) + puts(str_cstr(command)); + if (0 != system(str_cstr(command))) { + remove(str_cstr(ofname)); + mem_swap((void**)&ofname, NULL); + } + remove(str_cstr(in)); + mem_release(parts); + mem_release(command); return ofname; } @@ -297,7 +330,7 @@ static int emit_object(void) { } else if (1 == nfiles) { str_t* fname = list_front(files)->contents; str_t* csrc = translate_file(fname); - str_t* obj = compile_file(fname); + str_t* obj = compile_file(csrc); mem_release(csrc); mem_release(obj); } else { @@ -330,8 +363,6 @@ static int emit_program(void) { int main(int argc, char **argv) { opts_parse( Options_Config, argc, argv ); - mem_release(input_files()); - if (!opts_is_set(NULL,"mode")) { print_usage(); } else if(opts_equal(NULL, "mode", "repl")) { -- 2.49.0