From d165e87340c3e802f2d0c6354934c5529386fa84 Mon Sep 17 00:00:00 2001 From: grischka Date: Sat, 18 Apr 2009 13:17:27 +0200 Subject: [PATCH] libtcc: new api tcc_set_lib_path --- Makefile | 2 +- libtcc.h | 3 +++ libtcc_test.c | 16 +++++++++++----- tcc.c | 19 ++++++++++++------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index c3ad055..1f76ba8 100644 --- a/Makefile +++ b/Makefile @@ -305,7 +305,7 @@ libtcc_test$(EXESUF): libtcc_test.c libtcc.a $(CC) $(CFLAGS) -o $@ $< libtcc.a $(LIBS) libtest: libtcc_test - ./libtcc_test + ./libtcc_test lib_path=. # targets for development diff --git a/libtcc.h b/libtcc.h index e7c7ae1..f82a1d0 100644 --- a/libtcc.h +++ b/libtcc.h @@ -92,6 +92,9 @@ int tcc_relocate(TCCState *s1, void *ptr); /* return symbol value or NULL if not found */ void *tcc_get_symbol(TCCState *s, const char *name); +/* set CONFIG_TCCDIR at runtime */ +void tcc_set_lib_path(TCCState *s, const char *path); + #ifdef __cplusplus } #endif diff --git a/libtcc_test.c b/libtcc_test.c index d257a72..a602fb0 100644 --- a/libtcc_test.c +++ b/libtcc_test.c @@ -5,6 +5,7 @@ */ #include #include +#include #include "libtcc.h" @@ -14,7 +15,7 @@ int add(int a, int b) return a + b; } -char my_program[] = +char my_program[] = "int fib(int n)\n" "{\n" " if (n <= 2)\n" @@ -37,22 +38,27 @@ int main(int argc, char **argv) int (*func)(int); void *mem; int size; - + s = tcc_new(); if (!s) { fprintf(stderr, "Could not create tcc state\n"); exit(1); } - /* MUST BE CALLED before any compilation or file loading */ + /* if tcclib.h and libtcc1.a are not installed, where can we find them */ + if (argc == 2 && !memcmp(argv[1], "lib_path=",9)) + tcc_set_lib_path(s, argv[1]+9); + + /* MUST BE CALLED before any compilation */ tcc_set_output_type(s, TCC_OUTPUT_MEMORY); - tcc_compile_string(s, my_program); + if (tcc_compile_string(s, my_program) == -1) + return 1; /* as a test, we add a symbol that the compiled program can use. You may also open a dll with tcc_add_dll() and use symbols from that */ tcc_add_symbol(s, "add", add); - + /* get needed size of the code */ size = tcc_relocate(s, NULL); if (size == -1) diff --git a/tcc.c b/tcc.c index fba0fd6..ae382c2 100644 --- a/tcc.c +++ b/tcc.c @@ -1122,7 +1122,7 @@ char *normalize_slashes(char *path) return path; } -char *w32_tcc_lib_path(void) +void tcc_set_lib_path_w32(TCCState *s) { /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */ @@ -1134,7 +1134,7 @@ char *w32_tcc_lib_path(void) else if (p > path) p--; *p = 0; - return strdup(path); + tcc_set_lib_path(s, path); } #endif @@ -10967,6 +10967,12 @@ int tcc_set_flag(TCCState *s, const char *flag_name, int value) flag_name, value); } +/* set CONFIG_TCCDIR at runtime */ +void tcc_set_lib_path(TCCState *s, const char *path) +{ + tcc_lib_path = tcc_strdup(path); +} + #if !defined(LIBTCC) static int64_t getclock_us(void) @@ -11217,7 +11223,7 @@ int parse_args(TCCState *s, int argc, char **argv) break; case TCC_OPTION_B: /* set tcc utilities path (mainly for tcc development) */ - tcc_lib_path = optarg; + tcc_set_lib_path(s, optarg); break; case TCC_OPTION_l: dynarray_add((void ***)&files, &nb_files, r); @@ -11350,11 +11356,10 @@ int main(int argc, char **argv) char objfilename[1024]; int64_t start_time = 0; -#ifdef _WIN32 - tcc_lib_path = w32_tcc_lib_path(); -#endif - s = tcc_new(); +#ifdef _WIN32 + tcc_set_lib_path_w32(s); +#endif output_type = TCC_OUTPUT_EXE; outfile = NULL; multiple_files = 1;