support -I to set paths of .h file
authoryu.dongliang <18588496441@163.com>
Thu, 23 Jul 2026 15:22:12 +0000 (23:22 +0800)
committeryu.dongliang <18588496441@163.com>
Thu, 23 Jul 2026 15:22:12 +0000 (23:22 +0800)
lex/scf_macro.c
parse/main.c
parse/scf_dfa_include.c
parse/scf_parse.c
parse/scf_parse.h
util/scf_string.c
util/scf_string.h

index ee3b80e0ada4271fe730268648dbd6696d702f05..8afdd567fc5c41e717c0387ab5436faf6c8dc41f 100644 (file)
@@ -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;
index a4bc58906dc7af3baef8a9780303b4fb2feb60f9..32371fa9e50016f05bf46cb207798292c63e7729 100644 (file)
@@ -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;
 }
index 5acebc701cc6ac37b720b2f427db9942a4eacb33..071b05a0c6a9be7da70e4703f0588a6e52293e7b 100644 (file)
@@ -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;
index 69cc4efb83125bc2e82016dce91710ea52ceb630..e15f6f8ca69759acd1d9077fcb0e51ad14415f07 100644 (file)
@@ -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;
 
index d56ba39225a2466a6cb940587db8a1eec51a3fce..a1dc31b749311823981aaa5fc9c359db74e34c44 100644 (file)
@@ -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);
index bd33b0f0e486590e84946175cf360cf925e7ca48..24cfc8dde293c2639fefa0450dbe6c2c10586f97 100644 (file)
@@ -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[])
 {
index 9e433a44c9a6f15a48d3f9934cca7bb701b5cda3..19d4be94e218e72b9037a7db61b92479c36fff34 100644 (file)
@@ -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