From: yu.dongliang <18588496441@163.com> Date: Thu, 23 Jul 2026 15:22:12 +0000 (+0800) Subject: support -I to set paths of .h file X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=d0ef4e02fdf9e87d5409a86da4b7d8d73e0463e9;p=scf.git support -I to set paths of .h file --- diff --git a/lex/scf_macro.c b/lex/scf_macro.c index ee3b80e..8afdd56 100644 --- a/lex/scf_macro.c +++ b/lex/scf_macro.c @@ -288,8 +288,6 @@ int __parse_macro_define(scf_lex_t* lex, int def_flag) while (1) { if (w) { - scf_logw("w: '%s', file: %s, line: %d\n", w->text->data, w->file->data, w->line); - if (SCF_LEX_WORD_LF == w->type) { scf_lex_word_free(w); w = NULL; diff --git a/parse/main.c b/parse/main.c index a4bc589..32371fa 100644 --- a/parse/main.c +++ b/parse/main.c @@ -40,12 +40,15 @@ static char* __arm32_sofiles[] = void usage(char* path) { - fprintf(stderr, "Usage: %s [-c] [-d] [-t] [-a arch] [-s sysroot] [-o out] src0 [src1]\n\n", path); + fprintf(stderr, "Usage: %s [-c] [-d] [-t] [-a arch] [-s sysroot] [-Iinclude / -I include] [-lsofile / -l sofile] [-Lsopath / -L sopath] [-o out] src0 [src1]\n\n", path); fprintf(stderr, "-c: only compile, not link\n"); fprintf(stderr, "-t: only 3ac code, not compile\n"); fprintf(stderr, "-a: select cpu arch (x64, arm64, naja, or eda), default is x64\n"); fprintf(stderr, "-s: sysroot dir, default is '../lib'\n"); fprintf(stderr, "-d: output dynamic library .so\n"); + fprintf(stderr, "-I: include paths for .h file\n"); + fprintf(stderr, "-l: file name of lib*.so / lib*.a, NOT include prefix 'lib' & post fix '.so' or '.a'\n"); + fprintf(stderr, "-L: file paths for .so / .a file\n"); } int add_sys_files(scf_vector_t* vec, const char* sysroot, const char* arch, char* files[], int n_files) @@ -97,11 +100,25 @@ int main(int argc, char* argv[]) return -EINVAL; } + int ret = 0; + + scf_vector_t* inc_paths = scf_vector_alloc(); + scf_vector_t* lib_paths = scf_vector_alloc(); + scf_vector_t* lib_files = scf_vector_alloc(); + scf_vector_t* afiles = scf_vector_alloc(); scf_vector_t* sofiles = scf_vector_alloc(); scf_vector_t* srcs = scf_vector_alloc(); scf_vector_t* objs = scf_vector_alloc(); + if (!inc_paths + || !lib_paths + || !lib_files + || !afiles || !sofiles || !srcs || !objs) { + ret = -ENOMEM; + goto error; + } + char* sysroot = "../lib"; char* arch = "x64"; char* out = NULL; @@ -134,9 +151,11 @@ int main(int argc, char* argv[]) if ('a' == argv[i][1]) { - if (++i >= argc) { + if (++i >= argc || '-' == argv[i][0]) { usage(argv[0]); - return -EINVAL; + + ret = -EINVAL; + goto error; } arch = argv[i]; @@ -145,9 +164,11 @@ int main(int argc, char* argv[]) if ('s' == argv[i][1]) { - if (++i >= argc) { + if (++i >= argc || '-' == argv[i][0]) { usage(argv[0]); - return -EINVAL; + + ret = -EINVAL; + goto error; } sysroot = argv[i]; @@ -156,17 +177,59 @@ int main(int argc, char* argv[]) if ('o' == argv[i][1]) { - if (++i >= argc) { + if (++i >= argc || '-' == argv[i][0]) { usage(argv[0]); - return -EINVAL; + + ret = -EINVAL; + goto error; } out = argv[i]; continue; } - usage(argv[0]); - return -EINVAL; + scf_vector_t* vec = NULL; + + switch (argv[i][1]) { + case 'I': + vec = inc_paths; + break; + + case 'l': + vec = lib_files; + break; + case 'L': + vec = lib_paths; + break; + + default: + usage(argv[0]); + + ret = -EINVAL; + goto error; + break; + }; + + char* fname = NULL; + + if ('\0' == argv[i][2]) { + + if (++i >= argc || '-' == argv[i][0]) { + usage(argv[0]); + + ret = -EINVAL; + goto error; + } + + fname = argv[i]; + } else + fname = argv[i] + 2; + + ret = scf_vector_add(vec, fname); + if (ret < 0) + goto error; + + continue; } char* fname = argv[i]; @@ -174,12 +237,12 @@ int main(int argc, char* argv[]) if (len < 3) { fprintf(stderr, "file '%s' invalid\n", fname); - return -1; - } - scf_logi("fname: %s\n", fname); + ret = -EINVAL; + goto error; + } - scf_vector_t* vec; + scf_vector_t* vec = NULL; if (!strcmp(fname + len - 2, ".a")) vec = afiles; @@ -194,12 +257,16 @@ int main(int argc, char* argv[]) vec = sofiles; else { fprintf(stderr, "file '%s' invalid\n", fname); - return -1; + + ret = -EINVAL; + goto error; } scf_logi("fname: %s\n", fname); - if (scf_vector_add(vec, fname) < 0) - return -ENOMEM; + + ret = scf_vector_add(vec, fname); + if (ret < 0) + goto error; } printf("\n"); @@ -217,9 +284,10 @@ int main(int argc, char* argv[]) if (srcs->size > 0) { scf_parse_t* parse = NULL; - if (scf_parse_open(&parse) < 0) { + ret = scf_parse_open(&parse, inc_paths); + if (ret < 0) { scf_loge("\n"); - return -1; + goto error; } for (i = 0; i < srcs->size; i++) { @@ -227,64 +295,90 @@ int main(int argc, char* argv[]) assert(file); - if (scf_parse_file(parse, file) < 0) { + ret = scf_parse_file(parse, file); + if (ret < 0) { scf_loge("parse file '%s' failed\n", file); - return -1; + goto error; } } - if (scf_parse_compile(parse, arch, _3ac) < 0) { + ret = scf_parse_compile(parse, arch, _3ac); + if (ret < 0) { scf_loge("\n"); - return -1; + goto error; } - if (scf_parse_to_obj(parse, obj, arch) < 0) { + ret = scf_parse_to_obj(parse, obj, arch); + if (ret < 0) { scf_loge("\n"); - return -1; + goto error; } scf_parse_close(parse); + parse = NULL; } - if (!link) { - printf("%s(),%d, main ok\n", __func__, __LINE__); - return 0; - } - + if (link) { #define MAIN_ADD_FILES(_objs, _sofiles, _arch) \ - do { \ - if (!dyn) { \ - int ret = add_sys_files(objs, sysroot, _arch, _objs, sizeof(_objs) / sizeof(_objs[0])); \ + do { \ + if (!dyn) { \ + ret = add_sys_files(objs, sysroot, _arch, _objs, sizeof(_objs) / sizeof(_objs[0])); \ + if (ret < 0) \ + goto error; \ + } \ + \ + ret = add_sys_files(sofiles, sysroot, _arch, _sofiles, sizeof(_sofiles) / sizeof(_sofiles[0])); \ if (ret < 0) \ - return ret; \ - } \ - \ - int ret = add_sys_files(sofiles, sysroot, _arch, _sofiles, sizeof(_sofiles) / sizeof(_sofiles[0])); \ - if (ret < 0) \ - return ret; \ - } while (0) + goto error; \ + } while (0) - if (!strcmp(arch, "arm64") || !strcmp(arch, "naja")) - MAIN_ADD_FILES(__arm64_objs, __arm64_sofiles, "arm64"); + if (!strcmp(arch, "arm64") || !strcmp(arch, "naja")) + MAIN_ADD_FILES(__arm64_objs, __arm64_sofiles, "arm64"); - else if (!strcmp(arch, "arm32")) - MAIN_ADD_FILES(__arm32_objs, __arm32_sofiles, "arm32"); - else - MAIN_ADD_FILES(__objs, __sofiles, "x64"); + else if (!strcmp(arch, "arm32")) + MAIN_ADD_FILES(__arm32_objs, __arm32_sofiles, "arm32"); + else + MAIN_ADD_FILES(__objs, __sofiles, "x64"); - if (srcs->size > 0) { - if (scf_vector_add(objs, obj) < 0) { - scf_loge("\n"); - return -1; + if (srcs->size > 0) { + ret = scf_vector_add(objs, obj); + if (ret < 0) + goto error; } - } - if (scf_elf_link(objs, afiles, sofiles, sysroot, arch, exec, dyn) < 0) { - scf_loge("\n"); - return -1; + ret = scf_elf_link(objs, afiles, sofiles, sysroot, arch, exec, dyn); + if (ret < 0) { + scf_loge("\n"); + goto error; + } } printf("%s(),%d, main ok\n", __func__, __LINE__); - return 0; + + ret = 0; + +error: + if (inc_paths) + scf_vector_free(inc_paths); + + if (lib_paths) + scf_vector_free(lib_paths); + + if (lib_files) + scf_vector_free(lib_files); + + if (afiles) + scf_vector_free(afiles); + + if (sofiles) + scf_vector_free(sofiles); + + if (srcs) + scf_vector_free(srcs); + + if (objs) + scf_vector_free(objs); + + return ret; } diff --git a/parse/scf_dfa_include.c b/parse/scf_dfa_include.c index 5acebc7..071b05a 100644 --- a/parse/scf_dfa_include.c +++ b/parse/scf_dfa_include.c @@ -13,6 +13,29 @@ static int _include_action_include(scf_dfa_t* dfa, scf_vector_t* words, void* da return SCF_DFA_NEXT_WORD; } +static int _include_path(uint8_t** path, scf_parse_t* parse, const scf_string_t* file) +{ + scf_lex_t* lex = parse->lex; + uint8_t* fpath = NULL; + + int ret = scf_file_path(&fpath, lex->file, file); + if (ret < 0) + return ret; + + if (fpath) { + FILE* fp = fopen(fpath, "r"); + if (fp) { + fclose(fp); + fp = NULL; + + *path = fpath; + return 1; + } + } + + return scf_file_find(path, parse->inc_paths, file); +} + static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; @@ -25,9 +48,13 @@ static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) uint8_t* path = NULL; - int ret = scf_file_path(&path, lex->file, w->data.s); + int ret = _include_path(&path, parse, w->data.s); if (ret < 0) return ret; + else if (0 == ret) { + scf_loge("include '%s' NOT found, at file: %s, line:%d\n", w->data.s->data, w->file->data, w->line); + return SCF_DFA_ERROR; + } if (!path) path = w->data.s->data; diff --git a/parse/scf_parse.c b/parse/scf_parse.c index 69cc4ef..e15f6f8 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -44,7 +44,7 @@ scf_base_type_t base_types[] = {"funcptr", SCF_FUNCTION_PTR, sizeof(void*)}, }; -int scf_parse_open(scf_parse_t** pparse) +int scf_parse_open(scf_parse_t** pparse, const scf_vector_t* inc_paths) { if (!pparse) return -EINVAL; @@ -60,6 +60,7 @@ int scf_parse_open(scf_parse_t** pparse) int i; for (i = 0; i < sizeof(base_types) / sizeof(base_types[0]); i++) { + scf_ast_add_base_type(parse->ast, &base_types[i]); } @@ -80,6 +81,8 @@ int scf_parse_open(scf_parse_t** pparse) if (!parse->debug) goto debug_error; + parse->inc_paths = inc_paths; + *pparse = parse; return 0; diff --git a/parse/scf_parse.h b/parse/scf_parse.h index d56ba39..a1dc31b 100644 --- a/parse/scf_parse.h +++ b/parse/scf_parse.h @@ -31,6 +31,8 @@ struct scf_parse_s scf_vector_t* global_consts; scf_dwarf_t* debug; + + const scf_vector_t* inc_paths; }; typedef struct { @@ -118,7 +120,7 @@ struct dfa_data_s { int scf_parse_dfa_init(scf_parse_t* parse); -int scf_parse_open (scf_parse_t** pparse); +int scf_parse_open (scf_parse_t** pparse, const scf_vector_t* inc_paths); int scf_parse_close(scf_parse_t* parse); int scf_parse_file(scf_parse_t* parse, const char* path); diff --git a/util/scf_string.c b/util/scf_string.c index bd33b0f..24cfc8d 100644 --- a/util/scf_string.c +++ b/util/scf_string.c @@ -431,6 +431,55 @@ int scf_file_path(uint8_t** path, const scf_string_t* base, const scf_string_t* return 0; } +int scf_file_find(uint8_t** path, const scf_vector_t* dirs, const scf_string_t* file) +{ + if (0 == file->len) + return 0; + + FILE* fp = fopen(file->data, "r"); + if (fp) { + fclose(fp); + return 1; + } + + if ('/' == file->data[0] || '~' == file->data[0]) + return 0; + + uint8_t* fpath = NULL; + uint8_t* dir; + int i; + + for (i = 0; i < dirs->size; i++) { + dir = dirs->data[i]; + + scf_string_t base; + + base.len = strlen(dir); + base.data = dir; + base.capacity = -1; + + int ret = scf_file_path(&fpath, &base, file); + if (ret < 0) + return ret; + + if (fpath) { + fp = fopen(fpath, "r"); + if (fp) { + fclose(fp); + fp = NULL; + + *path = fpath; + return 1; + } + + free(fpath); + fpath = NULL; + } + } + + return 0; +} + #if 0 int main(int argc, char* argv[]) { diff --git a/util/scf_string.h b/util/scf_string.h index 9e433a4..19d4be9 100644 --- a/util/scf_string.h +++ b/util/scf_string.h @@ -47,4 +47,5 @@ int scf_string_get_offset(scf_string_t* str, const char* data, size_ int scf_utf8_len(const uint8_t* str, size_t* len); int scf_file_path(uint8_t** path, const scf_string_t* base, const scf_string_t* file); +int scf_file_find(uint8_t** path, const scf_vector_t* dirs, const scf_string_t* file); #endif