tcc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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|n] specify type of the next infile\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. " -image-base= -Ttext= set base address of executable\n"
  118. " -section-alignment= set section alignment in executable\n"
  119. #ifdef TCC_TARGET_PE
  120. " -file-alignment= set PE file alignment\n"
  121. " -stack= set PE stack reserve\n"
  122. " -large-address-aware set related PE option\n"
  123. " -subsystem=[console/windows] set PE subsystem\n"
  124. " -oformat=[pe-* binary] set executable output format\n"
  125. "Predefined macros:\n"
  126. " tcc -E -dM - < nul\n"
  127. #else
  128. " -rpath= set dynamic library search path\n"
  129. " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
  130. " -soname= set DT_SONAME elf tag\n"
  131. " -Bsymbolic set DT_SYMBOLIC elf tag\n"
  132. " -oformat=[elf32/64-* binary] set executable output format\n"
  133. " -init= -fini= -as-needed -O (ignored)\n"
  134. "Predefined macros:\n"
  135. " tcc -E -dM - < /dev/null\n"
  136. #endif
  137. "See also the manual for more details.\n"
  138. ;
  139. static const char version[] =
  140. "tcc version "TCC_VERSION" ("
  141. #ifdef TCC_TARGET_I386
  142. "i386"
  143. #elif defined TCC_TARGET_X86_64
  144. "x86_64"
  145. #elif defined TCC_TARGET_C67
  146. "C67"
  147. #elif defined TCC_TARGET_ARM
  148. "ARM"
  149. #elif defined TCC_TARGET_ARM64
  150. "AArch64"
  151. #endif
  152. #ifdef TCC_ARM_HARDFLOAT
  153. " Hard Float"
  154. #endif
  155. #ifdef TCC_TARGET_PE
  156. " Windows"
  157. #elif defined(TCC_TARGET_MACHO)
  158. " Darwin"
  159. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  160. " FreeBSD"
  161. #else
  162. " Linux"
  163. #endif
  164. ")\n"
  165. ;
  166. static void print_dirs(const char *msg, char **paths, int nb_paths)
  167. {
  168. int i;
  169. printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
  170. for(i = 0; i < nb_paths; i++)
  171. printf(" %s\n", paths[i]);
  172. }
  173. static void print_search_dirs(TCCState *s)
  174. {
  175. printf("install: %s\n", s->tcc_lib_path);
  176. /* print_dirs("programs", NULL, 0); */
  177. print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
  178. print_dirs("libraries", s->library_paths, s->nb_library_paths);
  179. #ifndef TCC_TARGET_PE
  180. print_dirs("crt", s->crt_paths, s->nb_crt_paths);
  181. printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
  182. printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
  183. #endif
  184. }
  185. static void set_environment(TCCState *s)
  186. {
  187. char * path;
  188. path = getenv("C_INCLUDE_PATH");
  189. if(path != NULL) {
  190. tcc_add_sysinclude_path(s, path);
  191. }
  192. path = getenv("CPATH");
  193. if(path != NULL) {
  194. tcc_add_include_path(s, path);
  195. }
  196. path = getenv("LIBRARY_PATH");
  197. if(path != NULL) {
  198. tcc_add_library_path(s, path);
  199. }
  200. }
  201. static char *default_outputfile(TCCState *s, const char *first_file)
  202. {
  203. char buf[1024];
  204. char *ext;
  205. const char *name = "a";
  206. if (first_file && strcmp(first_file, "-"))
  207. name = tcc_basename(first_file);
  208. snprintf(buf, sizeof(buf), "%s", name);
  209. ext = tcc_fileextension(buf);
  210. #ifdef TCC_TARGET_PE
  211. if (s->output_type == TCC_OUTPUT_DLL)
  212. strcpy(ext, ".dll");
  213. else
  214. if (s->output_type == TCC_OUTPUT_EXE)
  215. strcpy(ext, ".exe");
  216. else
  217. #endif
  218. if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
  219. strcpy(ext, ".o");
  220. else
  221. strcpy(buf, "a.out");
  222. return tcc_strdup(buf);
  223. }
  224. static unsigned getclock_ms(void)
  225. {
  226. #ifdef _WIN32
  227. return GetTickCount();
  228. #else
  229. struct timeval tv;
  230. gettimeofday(&tv, NULL);
  231. return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
  232. #endif
  233. }
  234. int main(int argc0, char **argv0)
  235. {
  236. TCCState *s;
  237. int ret, opt, n = 0, t = 0;
  238. unsigned start_time = 0;
  239. const char *first_file;
  240. int argc; char **argv;
  241. FILE *ppfp = stdout;
  242. redo:
  243. argc = argc0, argv = argv0;
  244. s = tcc_new();
  245. opt = tcc_parse_args(s, &argc, &argv, 1);
  246. if ((n | t) == 0) {
  247. if (opt == OPT_HELP)
  248. return printf(help), 1;
  249. if (opt == OPT_HELP2)
  250. return printf(help2), 1;
  251. if (opt == OPT_M32 || opt == OPT_M64)
  252. tcc_tool_cross(s, argv, opt); /* never returns */
  253. if (s->verbose)
  254. printf(version);
  255. if (opt == OPT_AR)
  256. return tcc_tool_ar(s, argc, argv);
  257. #ifdef TCC_TARGET_PE
  258. if (opt == OPT_IMPDEF)
  259. return tcc_tool_impdef(s, argc, argv);
  260. #endif
  261. if (opt == OPT_V)
  262. return 0;
  263. if (opt == OPT_PRINT_DIRS) {
  264. /* initialize search dirs */
  265. set_environment(s);
  266. tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
  267. print_search_dirs(s);
  268. return 0;
  269. }
  270. n = s->nb_files;
  271. if (n == 0)
  272. tcc_error("no input files\n");
  273. if (s->output_type == TCC_OUTPUT_PREPROCESS) {
  274. if (s->outfile) {
  275. ppfp = fopen(s->outfile, "w");
  276. if (!ppfp)
  277. tcc_error("could not write '%s'", s->outfile);
  278. }
  279. } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
  280. if (s->nb_libraries)
  281. tcc_error("cannot specify libraries with -c");
  282. if (n > 1 && s->outfile)
  283. tcc_error("cannot specify output file with -c many files");
  284. } else {
  285. if (s->option_pthread)
  286. tcc_set_options(s, "-lpthread");
  287. }
  288. if (s->do_bench)
  289. start_time = getclock_ms();
  290. }
  291. set_environment(s);
  292. if (s->output_type == 0)
  293. s->output_type = TCC_OUTPUT_EXE;
  294. tcc_set_output_type(s, s->output_type);
  295. s->ppfp = ppfp;
  296. if ((s->output_type == TCC_OUTPUT_MEMORY
  297. || s->output_type == TCC_OUTPUT_PREPROCESS) && (s->dflag & 16))
  298. s->dflag |= t ? 32 : 0, s->run_test = ++t, n = s->nb_files;
  299. /* compile or add each files or library */
  300. for (first_file = NULL, ret = 0;;) {
  301. struct filespec *f = s->files[s->nb_files - n];
  302. s->filetype = f->type;
  303. s->alacarte_link = f->alacarte;
  304. if (f->type == AFF_TYPE_LIB) {
  305. if (tcc_add_library_err(s, f->name) < 0)
  306. ret = 1;
  307. } else {
  308. if (1 == s->verbose)
  309. printf("-> %s\n", f->name);
  310. if (!first_file)
  311. first_file = f->name;
  312. if (tcc_add_file(s, f->name) < 0)
  313. ret = 1;
  314. }
  315. s->filetype = 0;
  316. s->alacarte_link = 1;
  317. if (--n == 0 || ret
  318. || (s->output_type == TCC_OUTPUT_OBJ && !s->option_r))
  319. break;
  320. }
  321. if (s->run_test) {
  322. t = 0;
  323. } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
  324. ;
  325. } else if (0 == ret) {
  326. if (s->output_type == TCC_OUTPUT_MEMORY) {
  327. #ifdef TCC_IS_NATIVE
  328. ret = tcc_run(s, argc, argv);
  329. #endif
  330. } else {
  331. if (!s->outfile)
  332. s->outfile = default_outputfile(s, first_file);
  333. if (tcc_output_file(s, s->outfile))
  334. ret = 1;
  335. else if (s->gen_deps)
  336. gen_makedeps(s, s->outfile, s->deps_outfile);
  337. }
  338. }
  339. if (s->do_bench && (n | t | ret) == 0)
  340. tcc_print_stats(s, getclock_ms() - start_time);
  341. tcc_delete(s);
  342. if (ret == 0 && n)
  343. goto redo; /* compile more files with -c */
  344. if (t)
  345. goto redo; /* run more tests with -dt -run */
  346. if (ppfp && ppfp != stdout)
  347. fclose(ppfp);
  348. return ret;
  349. }