Unified · Split
| @@ -305,7 +305,7 @@ libtcc_test$(EXESUF): libtcc_test.c libtcc.a · 304 unmodified lines | |||
| 305 | $(CC) $(CFLAGS) -o $@ $< libtcc.a $(LIBS) | 305 | $(CC) $(CFLAGS) -o $@ $< libtcc.a $(LIBS) |
| 306 | 306 | ||
| 307 | libtest: libtcc_test | 307 | libtest: libtcc_test |
| 308 | ./libtcc_test | 308 | ./libtcc_test lib_path=. |
| 309 | 309 | ||
| 310 | # targets for development | 310 | # targets for development |
| 311 | 311 | ||
| @@ -92,6 +92,9 @@ int tcc_relocate(TCCState *s1, void *ptr); · 91 unmodified lines | |||
| 92 | /* return symbol value or NULL if not found */ | 92 | /* return symbol value or NULL if not found */ |
| 93 | void *tcc_get_symbol(TCCState *s, const char *name); | 93 | void *tcc_get_symbol(TCCState *s, const char *name); |
| 94 | 94 | ||
| 95 | /* set CONFIG_TCCDIR at runtime */ | ||
| 96 | void tcc_set_lib_path(TCCState *s, const char *path); | ||
| 97 | |||
| 95 | #ifdef __cplusplus | 98 | #ifdef __cplusplus |
| 96 | } | 99 | } |
| 97 | #endif | 100 | #endif |
| @@ -5,6 +5,7 @@ · 4 unmodified lines | |||
| 5 | */ | 5 | */ |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> | 7 | #include <stdio.h> |
| 8 | #include <string.h> | ||
| 8 | 9 | ||
| 9 | #include "libtcc.h" | 10 | #include "libtcc.h" |
| 10 | 11 | ||
| @@ -14,7 +15,7 @@ int add(int a, int b) · 3 unmodified lines | |||
| 14 | return a + b; | 15 | return a + b; |
| 15 | } | 16 | } |
| 16 | 17 | ||
| 17 | char my_program[] = | 18 | char my_program[] = |
| 18 | "int fib(int n)\n" | 19 | "int fib(int n)\n" |
| 19 | "{\n" | 20 | "{\n" |
| 20 | " if (n <= 2)\n" | 21 | " if (n <= 2)\n" |
| @@ -37,22 +38,27 @@ int main(int argc, char **argv) · 16 unmodified lines | |||
| 37 | int (*func)(int); | 38 | int (*func)(int); |
| 38 | void *mem; | 39 | void *mem; |
| 39 | int size; | 40 | int size; |
| 40 | 41 | ||
| 41 | s = tcc_new(); | 42 | s = tcc_new(); |
| 42 | if (!s) { | 43 | if (!s) { |
| 43 | fprintf(stderr, "Could not create tcc state\n"); | 44 | fprintf(stderr, "Could not create tcc state\n"); |
| 44 | exit(1); | 45 | exit(1); |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 47 | /* MUST BE CALLED before any compilation or file loading */ | 48 | /* if tcclib.h and libtcc1.a are not installed, where can we find them */ |
| 49 | if (argc == 2 && !memcmp(argv[1], "lib_path=",9)) | ||
| 50 | tcc_set_lib_path(s, argv[1]+9); | ||
| 51 | |||
| 52 | /* MUST BE CALLED before any compilation */ | ||
| 48 | tcc_set_output_type(s, TCC_OUTPUT_MEMORY); | 53 | tcc_set_output_type(s, TCC_OUTPUT_MEMORY); |
| 49 | 54 | ||
| 50 | tcc_compile_string(s, my_program); | 55 | if (tcc_compile_string(s, my_program) == -1) |
| 56 | return 1; | ||
| 51 | 57 | ||
| 52 | /* as a test, we add a symbol that the compiled program can use. | 58 | /* as a test, we add a symbol that the compiled program can use. |
| 53 | You may also open a dll with tcc_add_dll() and use symbols from that */ | 59 | You may also open a dll with tcc_add_dll() and use symbols from that */ |
| 54 | tcc_add_symbol(s, "add", add); | 60 | tcc_add_symbol(s, "add", add); |
| 55 | 61 | ||
| 56 | /* get needed size of the code */ | 62 | /* get needed size of the code */ |
| 57 | size = tcc_relocate(s, NULL); | 63 | size = tcc_relocate(s, NULL); |
| 58 | if (size == -1) | 64 | if (size == -1) |
| @@ -1122,7 +1122,7 @@ char *normalize_slashes(char *path) · 1121 unmodified lines | |||
| 1122 | return path; | 1122 | return path; |
| 1123 | } | 1123 | } |
| 1124 | 1124 | ||
| 1125 | char *w32_tcc_lib_path(void) | 1125 | void tcc_set_lib_path_w32(TCCState *s) |
| 1126 | { | 1126 | { |
| 1127 | /* on win32, we suppose the lib and includes are at the location | 1127 | /* on win32, we suppose the lib and includes are at the location |
| 1128 | of 'tcc.exe' */ | 1128 | of 'tcc.exe' */ |
| @@ -1134,7 +1134,7 @@ char *w32_tcc_lib_path(void) · 5 unmodified lines | |||
| 1134 | else if (p > path) | 1134 | else if (p > path) |
| 1135 | p--; | 1135 | p--; |
| 1136 | *p = 0; | 1136 | *p = 0; |
| 1137 | return strdup(path); | 1137 | tcc_set_lib_path(s, path); |
| 1138 | } | 1138 | } |
| 1139 | #endif | 1139 | #endif |
| 1140 | 1140 | ||
| @@ -10967,6 +10967,12 @@ int tcc_set_flag(TCCState *s, const char *flag_name, int value) · 9826 unmodified lines | |||
| 10967 | flag_name, value); | 10967 | flag_name, value); |
| 10968 | } | 10968 | } |
| 10969 | 10969 | ||
| 10970 | /* set CONFIG_TCCDIR at runtime */ | ||
| 10971 | void tcc_set_lib_path(TCCState *s, const char *path) | ||
| 10972 | { | ||
| 10973 | tcc_lib_path = tcc_strdup(path); | ||
| 10974 | } | ||
| 10975 | |||
| 10970 | #if !defined(LIBTCC) | 10976 | #if !defined(LIBTCC) |
| 10971 | 10977 | ||
| 10972 | static int64_t getclock_us(void) | 10978 | static int64_t getclock_us(void) |
| @@ -11217,7 +11223,7 @@ int parse_args(TCCState *s, int argc, char **argv) · 244 unmodified lines | |||
| 11217 | break; | 11223 | break; |
| 11218 | case TCC_OPTION_B: | 11224 | case TCC_OPTION_B: |
| 11219 | /* set tcc utilities path (mainly for tcc development) */ | 11225 | /* set tcc utilities path (mainly for tcc development) */ |
| 11220 | tcc_lib_path = optarg; | 11226 | tcc_set_lib_path(s, optarg); |
| 11221 | break; | 11227 | break; |
| 11222 | case TCC_OPTION_l: | 11228 | case TCC_OPTION_l: |
| 11223 | dynarray_add((void ***)&files, &nb_files, r); | 11229 | dynarray_add((void ***)&files, &nb_files, r); |
| @@ -11350,11 +11356,10 @@ int main(int argc, char **argv) · 126 unmodified lines | |||
| 11350 | char objfilename[1024]; | 11356 | char objfilename[1024]; |
| 11351 | int64_t start_time = 0; | 11357 | int64_t start_time = 0; |
| 11352 | 11358 | ||
| 11359 | s = tcc_new(); | ||
| 11353 | #ifdef _WIN32 | 11360 | #ifdef _WIN32 |
| 11354 | tcc_lib_path = w32_tcc_lib_path(); | 11361 | tcc_set_lib_path_w32(s); |
| 11355 | #endif | 11362 | #endif |
| 11356 | |||
| 11357 | s = tcc_new(); | ||
| 11358 | output_type = TCC_OUTPUT_EXE; | 11363 | output_type = TCC_OUTPUT_EXE; |
| 11359 | outfile = NULL; | 11364 | outfile = NULL; |
| 11360 | multiple_files = 1; | 11365 | multiple_files = 1; |