diff --git a/src/cfg/opt.c b/src/cfg/opt.c index 256e2eb..3529ded 100644 --- a/src/cfg/opt.c +++ b/src/cfg/opt.c @@ -102,10 +102,37 @@ TargetConfig* default_target_config() { config->output_directory = strdup("bin"); config->optimization_level = 1; config->root_module = NULL; + config->link_search_paths = g_array_new(FALSE, FALSE, sizeof(char*)); return config; } +const char* get_absolute_link_path(const TargetConfig* config, const char* link_target_name) { + + for (guint i = 0; i < config->link_search_paths->len; i++) { + const char* link_directory_path = g_array_index(config->link_search_paths, char*, i); + + char* path = g_build_filename(link_directory_path, link_target_name, NULL); + char* cwd = g_get_current_dir(); + char* canonical = g_canonicalize_filename(path, cwd); + + const gboolean exists = g_file_test(canonical, G_FILE_TEST_EXISTS); + const gboolean is_dir = g_file_test(canonical, G_FILE_TEST_IS_DIR); + + g_free(path); + g_free(cwd); + + if (exists && !is_dir) { + return canonical; + } + + g_free(canonical); + } + + // file not found + return NULL; +} + TargetConfig* default_target_config_from_args() { DEBUG("generating default target from command line..."); @@ -145,6 +172,36 @@ TargetConfig* default_target_config_from_args() { } } + if (is_option_set("link-paths")) { + const Option* opt = get_option("link-paths"); + + if (opt->value != NULL) { + + const char* start = opt->value; + const char* end = NULL; + while((end = strchr(start, ',')) != NULL) { + + const int len = end - start; + char* link_path = malloc(len + 1); + memcpy(link_path, start, len); + link_path[len] = 0; + + g_array_append_val(config->link_search_paths, link_path); + + start = end; + } + + const int len = strlen(start); + if (len > 0) { + char* link_path = malloc(len + 1); + memcpy(link_path, start, len); + link_path[len] = 0; + + g_array_append_val(config->link_search_paths, link_path); + } + } + } + GArray* files = get_non_options_after("compile"); if (files == NULL) { @@ -172,11 +229,12 @@ void print_help(void) { "Compile non-project file: gsc compile [file]", "Output information: gsc