tcc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * TCC - Tiny C Compiler
  3. *
  4. * Copyright (c) 2001-2004 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "tcc.h"
  21. #if ONE_SOURCE
  22. # include "libtcc.c"
  23. #endif
  24. #include "tcctools.c"
  25. static const char help[] =
  26. "Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
  27. "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
  28. " tcc [options...] -run infile [arguments...]\n"
  29. "General options:\n"
  30. " -c compile only - generate an object file\n"
  31. " -o outfile set output filename\n"
  32. " -run run compiled source\n"
  33. " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
  34. " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
  35. " -w disable all warnings\n"
  36. " -v -vv show version, show search paths or loaded files\n"
  37. " -h -hh show this, show more help\n"
  38. " -bench show compilation statistics\n"
  39. " - use stdin pipe as infile\n"
  40. " @listfile read arguments from listfile\n"
  41. "Preprocessor options:\n"
  42. " -Idir add include path 'dir'\n"
  43. " -Dsym[=val] define 'sym' with value 'val'\n"
  44. " -Usym undefine 'sym'\n"
  45. " -E preprocess only\n"
  46. "Linker options:\n"
  47. " -Ldir add library path 'dir'\n"
  48. " -llib link with dynamic or static library 'lib'\n"
  49. " -r generate (relocatable) object file\n"
  50. " -shared generate a shared library/dll\n"
  51. " -rdynamic export all global symbols to dynamic linker\n"
  52. " -soname set name for shared library to be used at runtime\n"
  53. " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
  54. "Debugger options:\n"
  55. " -g generate runtime debug info\n"
  56. #ifdef CONFIG_TCC_BCHECK
  57. " -b compile with built-in memory and bounds checker (implies -g)\n"
  58. #endif
  59. #ifdef CONFIG_TCC_BACKTRACE
  60. " -bt N show N callers in stack traces\n"
  61. #endif
  62. "Misc. options:\n"
  63. " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
  64. " -nostdinc do not use standard system include paths\n"
  65. " -nostdlib do not link with standard crt and libraries\n"
  66. " -Bdir set tcc's private include/library dir\n"
  67. " -MD generate dependency file for make\n"
  68. " -MF file specify dependency file name\n"
  69. " -m32/64 defer to i386/x86_64 cross compiler\n"
  70. "Tools:\n"
  71. " create library : tcc -ar [rcsv] lib.a files\n"
  72. #ifdef TCC_TARGET_PE
  73. " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
  74. #endif
  75. ;
  76. static const char help2[] =
  77. "Tiny C Compiler "TCC_VERSION" - More Options\n"
  78. "Special options:\n"
  79. " -P -P1 with -E: no/alternative #line output\n"
  80. " -dD -dM with -E: output #define directives\n"
  81. " -pthread same as -D_REENTRANT and -lpthread\n"
  82. " -On same as -D__OPTIMIZE__ for n > 0\n"
  83. " -Wp,-opt same as -opt\n"
  84. " -include file include 'file' above each input file\n"
  85. " -isystem dir add 'dir' to system include path\n"
  86. " -static link to static libraries (not recommended)\n"
  87. " -dumpversion print version\n"
  88. " -print-search-dirs print search paths\n"
  89. " -dt with -run/-E: auto-define 'test_...' macros\n"
  90. "Ignored options:\n"
  91. " --param -pedantic -pipe -s -std -traditional\n"
  92. "-W... warnings:\n"
  93. " all turn on some (*) warnings\n"
  94. " error stop after first warning\n"
  95. " unsupported warn about ignored options, pragmas, etc.\n"
  96. " write-strings strings are const\n"
  97. " implicit-function-declaration warn for missing prototype (*)\n"
  98. "-f[no-]... flags:\n"
  99. " unsigned-char default char is unsigned\n"
  100. " signed-char default char is signed\n"
  101. " common use common section instead of bss\n"
  102. " leading-underscore decorate extern symbols\n"
  103. " ms-extensions allow anonymous struct in struct\n"
  104. " dollars-in-identifiers allow '$' in C symbols\n"
  105. "-m... target specific options:\n"
  106. " ms-bitfields use MSVC bitfield layout\n"
  107. #ifdef TCC_TARGET_ARM
  108. " float-abi hard/softfp on arm\n"
  109. #endif
  110. #ifdef TCC_TARGET_X86_64
  111. " no-sse disable floats on x86_64\n"
  112. #endif
  113. "-Wl,... linker options:\n"
  114. " -nostdlib do not link with standard crt/libs\n"
  115. " -[no-]whole-archive load lib(s) fully/only as needed\n"
  116. " -export-all-symbols same as -rdynamic\n"
  117. " -export-dynamic same as -rdynamic\n"
  118. " -image-base= -Ttext= set base address of executable\n"
  119. " -section-alignment= set section alignment in executable\n"
  120. #ifdef TCC_TARGET_PE
  121. " -file-alignment= set PE file alignment\n"
  122. " -stack= set PE stack reserve\n"
  123. " -large-address-aware set related PE option\n"
  124. " -subsystem=[console/windows] set PE subsystem\n"
  125. " -oformat=[pe-* binary] set executable output format\n"
  126. "Predefined macros:\n"
  127. " tcc -E -dM - < nul\n"
  128. #else
  129. " -rpath= set dynamic library search path\n"
  130. " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
  131. " -soname= set DT_SONAME elf tag\n"
  132. " -Bsymbolic set DT_SYMBOLIC elf tag\n"
  133. " -oformat=[elf32/64-* binary] set executable output format\n"
  134. " -init= -fini= -as-needed -O (ignored)\n"
  135. "Predefined macros:\n"
  136. " tcc -E -dM - < /dev/null\n"
  137. #endif
  138. "See also the manual for more details.\n"
  139. ;
  140. static const char version[] =
  141. "tcc version "TCC_VERSION" ("
  142. #ifdef TCC_TARGET_I386
  143. "i386"
  144. #elif defined TCC_TARGET_X86_64
  145. "x86_64"
  146. #elif defined TCC_TARGET_C67
  147. "C67"
  148. #elif defined TCC_TARGET_ARM
  149. "ARM"
  150. #elif defined TCC_TARGET_ARM64
  151. "AArch64"
  152. #endif
  153. #ifdef TCC_ARM_HARDFLOAT
  154. " Hard Float"
  155. #endif
  156. #ifdef TCC_TARGET_PE
  157. " Windows"
  158. #elif defined(TCC_TARGET_MACHO)
  159. " Darwin"
  160. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  161. " FreeBSD"
  162. #else
  163. " Linux"
  164. #endif
  165. ")\n"
  166. ;
  167. static void print_dirs(const char *msg, char **paths, int nb_paths)
  168. {
  169. int i;
  170. printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
  171. for(i = 0; i < nb_paths; i++)
  172. printf(" %s\n", paths[i]);
  173. }
  174. static void print_search_dirs(TCCState *s)
  175. {
  176. printf("install: %s\n", s->tcc_lib_path);
  177. /* print_dirs("programs", NULL, 0); */
  178. print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
  179. print_dirs("libraries", s->library_paths, s->nb_library_paths);
  180. #ifdef TCC_TARGET_PE
  181. printf("libtcc1:\n %s/lib/"TCC_LIBTCC1"\n", s->tcc_lib_path);
  182. #else
  183. printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
  184. print_dirs("crt", s->crt_paths, s->nb_crt_paths);
  185. printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
  186. #endif
  187. }
  188. static void set_environment(TCCState *s)
  189. {
  190. char * path;
  191. path = getenv("C_INCLUDE_PATH");
  192. if(path != NULL) {
  193. tcc_add_sysinclude_path(s, path);
  194. }
  195. path = getenv("CPATH");
  196. if(path != NULL) {
  197. tcc_add_include_path(s, path);
  198. }
  199. path = getenv("LIBRARY_PATH");
  200. if(path != NULL) {
  201. tcc_add_library_path(s, path);
  202. }
  203. }
  204. static char *default_outputfile(TCCState *s, const char *first_file)
  205. {
  206. char buf[1024];
  207. char *ext;
  208. const char *name = "a";
  209. if (first_file && strcmp(first_file, "-"))
  210. name = tcc_basename(first_file);
  211. snprintf(buf, sizeof(buf), "%s", name);
  212. ext = tcc_fileextension(buf);
  213. #ifdef TCC_TARGET_PE
  214. if (s->output_type == TCC_OUTPUT_DLL)
  215. strcpy(ext, ".dll");
  216. else
  217. if (s->output_type == TCC_OUTPUT_EXE)
  218. strcpy(ext, ".exe");
  219. else
  220. #endif
  221. if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
  222. strcpy(ext, ".o");
  223. else
  224. strcpy(buf, "a.out");
  225. return tcc_strdup(buf);
  226. }
  227. static unsigned getclock_ms(void)
  228. {
  229. #ifdef _WIN32
  230. return GetTickCount();
  231. #else
  232. struct timeval tv;
  233. gettimeofday(&tv, NULL);
  234. return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
  235. #endif
  236. }
  237. int main(int argc0, char **argv0)
  238. {
  239. TCCState *s;
  240. int ret, opt, n = 0, t = 0;
  241. unsigned start_time = 0;
  242. const char *first_file;
  243. int argc; char **argv;
  244. FILE *ppfp = stdout;
  245. redo:
  246. argc = argc0, argv = argv0;
  247. s = tcc_new();
  248. opt = tcc_parse_args(s, &argc, &argv, 1);
  249. if ((n | t) == 0) {
  250. if (opt == OPT_HELP)
  251. return printf(help), 1;
  252. if (opt == OPT_HELP2)
  253. return printf(help2), 1;
  254. if (opt == OPT_M32 || opt == OPT_M64)
  255. tcc_tool_cross(s, argv, opt); /* never returns */
  256. if (s->verbose)
  257. printf(version);
  258. if (opt == OPT_AR)
  259. return tcc_tool_ar(s, argc, argv);
  260. #ifdef TCC_TARGET_PE
  261. if (opt == OPT_IMPDEF)
  262. return tcc_tool_impdef(s, argc, argv);
  263. #endif
  264. if (opt == OPT_V)
  265. return 0;
  266. if (opt == OPT_PRINT_DIRS) {
  267. /* initialize search dirs */
  268. set_environment(s);
  269. tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
  270. print_search_dirs(s);
  271. return 0;
  272. }
  273. n = s->nb_files;
  274. if (n == 0)
  275. tcc_error("no input files\n");
  276. if (s->output_type == TCC_OUTPUT_PREPROCESS) {
  277. if (s->outfile) {
  278. ppfp = fopen(s->outfile, "w");
  279. if (!ppfp)
  280. tcc_error("could not write '%s'", s->outfile);
  281. }
  282. } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
  283. if (s->nb_libraries)
  284. tcc_error("cannot specify libraries with -c");
  285. if (n > 1 && s->outfile)
  286. tcc_error("cannot specify output file with -c many files");
  287. } else {
  288. if (s->option_pthread) {
  289. tcc_set_options(s, "-lpthread");
  290. n = s->nb_files;
  291. }
  292. }
  293. if (s->do_bench)
  294. start_time = getclock_ms();
  295. }
  296. set_environment(s);
  297. if (s->output_type == 0)
  298. s->output_type = TCC_OUTPUT_EXE;
  299. tcc_set_output_type(s, s->output_type);
  300. s->ppfp = ppfp;
  301. if ((s->output_type == TCC_OUTPUT_MEMORY
  302. || s->output_type == TCC_OUTPUT_PREPROCESS) && (s->dflag & 16))
  303. s->dflag |= t ? 32 : 0, s->run_test = ++t, n = s->nb_files;
  304. /* compile or add each files or library */
  305. for (first_file = NULL, ret = 0;;) {
  306. struct filespec *f = s->files[s->nb_files - n];
  307. s->filetype = f->type;
  308. if (f->type & AFF_TYPE_LIB) {
  309. if (tcc_add_library_err(s, f->name) < 0)
  310. ret = 1;
  311. } else {
  312. if (1 == s->verbose)
  313. printf("-> %s\n", f->name);
  314. if (!first_file)
  315. first_file = f->name;
  316. if (tcc_add_file(s, f->name) < 0)
  317. ret = 1;
  318. }
  319. if (--n == 0 || ret
  320. || (s->output_type == TCC_OUTPUT_OBJ && !s->option_r))
  321. break;
  322. }
  323. if (s->run_test) {
  324. t = 0;
  325. } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
  326. ;
  327. } else if (0 == ret) {
  328. if (s->output_type == TCC_OUTPUT_MEMORY) {
  329. #ifdef TCC_IS_NATIVE
  330. ret = tcc_run(s, argc, argv);
  331. #endif
  332. } else {
  333. if (!s->outfile)
  334. s->outfile = default_outputfile(s, first_file);
  335. if (tcc_output_file(s, s->outfile))
  336. ret = 1;
  337. else if (s->gen_deps)
  338. gen_makedeps(s, s->outfile, s->deps_outfile);
  339. }
  340. }
  341. if (s->do_bench && (n | t | ret) == 0)
  342. tcc_print_stats(s, getclock_ms() - start_time);
  343. tcc_delete(s);
  344. if (ret == 0 && n)
  345. goto redo; /* compile more files with -c */
  346. if (t)
  347. goto redo; /* run more tests with -dt -run */
  348. if (ppfp && ppfp != stdout)
  349. fclose(ppfp);
  350. return ret;
  351. }