Subversion Repositories Kolibri OS

Rev

Rev 6429 | Rev 8154 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  21. #ifdef ONE_SOURCE
  22. #include "libtcc.c"
  23. #else
  24. #include "tcc.h"
  25. #endif
  26.  
  27. static void print_paths(const char *msg, char **paths, int nb_paths)
  28. {
  29.     int i;
  30.     printf("%s:\n%s", msg, nb_paths ? "" : "  -\n");
  31.     for(i = 0; i < nb_paths; i++)
  32.         printf("  %s\n", paths[i]);
  33. }
  34.  
  35. static void display_info(TCCState *s, int what)
  36. {
  37.     switch (what) {
  38.     case 0:
  39.         printf("tcc version %s ("
  40. #ifdef TCC_TARGET_I386
  41.         "i386"
  42. #elif defined TCC_TARGET_X86_64
  43.         "x86-64"
  44. #elif defined TCC_TARGET_C67
  45.         "C67"
  46. #elif defined TCC_TARGET_ARM
  47.         "ARM"
  48. # ifdef TCC_ARM_HARDFLOAT
  49.         " Hard Float"
  50. # endif
  51. #elif defined TCC_TARGET_ARM64
  52.         "AArch64"
  53. # ifdef TCC_ARM_HARDFLOAT
  54.         " Hard Float"
  55. # endif
  56. #endif
  57. #ifdef TCC_TARGET_PE
  58.         " Windows"
  59. #elif defined TCC_TARGET_MEOS
  60.                 " KolibriOS"
  61. #else
  62.         " Linux"
  63. #endif
  64.         ")\n", TCC_VERSION);
  65.         break;
  66.     case 1:
  67.         printf("install: %s\n", s->tcc_lib_path);
  68.         /* print_paths("programs", NULL, 0); */
  69.         print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
  70.         print_paths("libraries", s->library_paths, s->nb_library_paths);
  71. #ifndef TCC_TARGET_PE
  72.         print_paths("crt", s->crt_paths, s->nb_crt_paths);
  73.         printf("elfinterp:\n  %s\n",  DEFAULT_ELFINTERP(s));
  74. #endif
  75.         break;
  76.     }
  77. }
  78.  
  79. static void help(void)
  80. {
  81.     printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
  82.            "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
  83.            "       tcc [options...] -run infile [arguments...]\n"
  84.            "General options:\n"
  85.            "  -c          compile only - generate an object file\n"
  86.            "  -o outfile  set output filename\n"
  87.            "  -run        run compiled source\n"
  88.            "  -fflag      set or reset (with 'no-' prefix) 'flag' (see man page)\n"
  89.            "  -Wwarning   set or reset (with 'no-' prefix) 'warning' (see man page)\n"
  90.            "  -w          disable all warnings\n"
  91.            "  -v          show version\n"
  92.            "  -vv         show included files (as sole argument: show search paths)\n"
  93.            "  -dumpversion\n"
  94.            "  -bench      show compilation statistics\n"
  95.            "  -xc -xa     specify type of the next infile\n"
  96.            "  -           use stdin pipe as infile\n"
  97.            "  @listfile   read line separated arguments from 'listfile'\n"
  98.            "Preprocessor options:\n"
  99.            "  -Idir       add include path 'dir'\n"
  100.            "  -Dsym[=val] define 'sym' with value 'val'\n"
  101.            "  -Usym       undefine 'sym'\n"
  102.            "  -E          preprocess only\n"
  103.            "    -P[1]       no/alternative output of #line directives\n"
  104.            "    -d{D|M}     dump defines\n"
  105.            "    -C          keep comments\n"
  106.            "Linker options:\n"
  107.            "  -Ldir       add library path 'dir'\n"
  108.            "  -llib       link with dynamic or static library 'lib'\n"
  109.            "  -pthread    link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
  110.            "  -r          generate (relocatable) object file\n"
  111.            "  -rdynamic   export all global symbols to dynamic linker\n"
  112.            "  -shared     generate a shared library\n"
  113.            "  -soname     set name for shared library to be used at runtime\n"
  114.            "  -static     static linking\n"
  115.            "  -Wl,-opt[=val]  set linker option (see manual)\n"
  116.            "  -stack=size set PE/KOS32 stack size\n"
  117.            "Debugger options:\n"
  118.            "  -g          generate runtime debug info\n"
  119. #ifdef CONFIG_TCC_BCHECK
  120.            "  -b          compile with built-in memory and bounds checker (implies -g)\n"
  121. #endif
  122. #ifdef CONFIG_TCC_BACKTRACE
  123.            "  -bt N       show N callers in stack traces\n"
  124. #endif
  125.            "Misc options:\n"
  126.            "  -nostdinc   do not use standard system include paths\n"
  127.            "  -nostdlib   do not link with standard crt and libraries\n"
  128.            "  -Bdir       use 'dir' as tcc internal library and include path\n"
  129.            "  -MD         generate target dependencies for make\n"
  130.            "  -MF depfile put generated dependencies here\n"
  131.            );
  132. }
  133.  
  134. /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
  135. #if (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) && !defined(TCC_TARGET_MEOS)
  136. #ifdef _WIN32
  137. #include <process.h>
  138. static int execvp_win32(const char *prog, char **argv)
  139. {
  140.     int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
  141.     if (-1 == ret)
  142.         return ret;
  143.     cwait(&ret, ret, WAIT_CHILD);
  144.     exit(ret);
  145. }
  146. #define execvp execvp_win32
  147. #endif
  148. static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
  149. {
  150.     char child_path[4096], *child_name; const char *target;
  151.     switch (atoi(optarg)) {
  152. #ifdef TCC_TARGET_I386
  153.         case 32: break;
  154.         case 64: target = "x86_64";
  155. #else
  156.         case 64: break;
  157.         case 32: target = "i386";
  158. #endif
  159.             pstrcpy(child_path, sizeof child_path - 40, argv[0]);
  160.             child_name = tcc_basename(child_path);
  161.             strcpy(child_name, target);
  162. #ifdef TCC_TARGET_PE
  163.             strcat(child_name, "-win32");
  164. #elif defined TCC_TARGET_MEOS
  165.             strcat(child_name, "-kos32");
  166. #endif
  167.             strcat(child_name, "-tcc");
  168.             if (strcmp(argv[0], child_path)) {
  169.                 if (s->verbose > 0)
  170.                     printf("tcc: using '%s'\n", child_name), fflush(stdout);
  171.                 execvp(argv[0] = child_path, argv);
  172.             }
  173.             tcc_error("'%s' not found", child_name);
  174.         case 0: /* ignore -march etc. */
  175.             break;
  176.         default:
  177.             tcc_warning("unsupported option \"-m%s\"", optarg);
  178.     }
  179. }
  180. #else
  181. #define exec_other_tcc(s, argv, optarg)
  182. #endif
  183.  
  184. static void gen_makedeps(TCCState *s, const char *target, const char *filename)
  185. {
  186.     FILE *depout;
  187.     char buf[1024], *ext;
  188.     int i;
  189.  
  190.     if (!filename) {
  191.         /* compute filename automatically
  192.          * dir/file.o -> dir/file.d             */
  193.         pstrcpy(buf, sizeof(buf), target);
  194.         ext = tcc_fileextension(buf);
  195.         pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
  196.         filename = buf;
  197.     }
  198.  
  199.     if (s->verbose)
  200.         printf("<- %s\n", filename);
  201.  
  202.     /* XXX return err codes instead of error() ? */
  203.     depout = fopen(filename, "w");
  204.     if (!depout)
  205.         tcc_error("could not open '%s'", filename);
  206.  
  207.     fprintf(depout, "%s : \\\n", target);
  208.     for (i=0; i<s->nb_target_deps; ++i)
  209.         fprintf(depout, " %s \\\n", s->target_deps[i]);
  210.     fprintf(depout, "\n");
  211.     fclose(depout);
  212. }
  213.  
  214. static char *default_outputfile(TCCState *s, const char *first_file)
  215. {
  216.     char buf[1024];
  217.     char *ext;
  218.     const char *name = "a";
  219.  
  220.     if (first_file && strcmp(first_file, "-"))
  221.         name = tcc_basename(first_file);
  222.     pstrcpy(buf, sizeof(buf), name);
  223.     ext = tcc_fileextension(buf);
  224. #ifdef TCC_TARGET_PE
  225.     if (s->output_type == TCC_OUTPUT_DLL)
  226.         strcpy(ext, ".dll");
  227.     else
  228.     if (s->output_type == TCC_OUTPUT_EXE)
  229.         strcpy(ext, ".exe");
  230.     else
  231. #endif
  232.     if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
  233.           (s->output_type == TCC_OUTPUT_PREPROCESS) )
  234.         && *ext)
  235.         strcpy(ext, ".o");
  236.     else
  237.         strcpy(buf, "a.out");
  238.  
  239.     return tcc_strdup(buf);
  240. }
  241.  
  242. static int64_t getclock_us(void)
  243. {
  244. #ifdef _WIN32
  245.     LARGE_INTEGER frequency, t1;
  246.     QueryPerformanceFrequency(&frequency);
  247.     QueryPerformanceCounter(&t1);
  248.     return t1.QuadPart * 1000000LL / frequency.QuadPart;
  249. #else
  250.     struct timeval tv;
  251.     gettimeofday(&tv, NULL);
  252.     return tv.tv_sec * 1000000LL + tv.tv_usec;
  253. #endif
  254. }
  255.  
  256. int main(int argc, char **argv)
  257. {
  258.     TCCState *s;
  259.     int ret, optind, i;
  260.     int64_t start_time = 0;
  261.  
  262.     s = tcc_new();
  263.  
  264.     optind = tcc_parse_args(s, argc - 1, argv + 1);
  265.  
  266.     if (s->do_bench)
  267.         start_time = getclock_us();
  268.  
  269.     tcc_set_environment(s);
  270.  
  271.     if (optind == 0) {
  272.         help();
  273.         return 1;
  274.     }
  275.  
  276.     if (s->option_m)
  277.         exec_other_tcc(s, argv, s->option_m);
  278.  
  279.     if (s->verbose)
  280.         display_info(s, 0);
  281.  
  282.     if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
  283.         tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
  284.         display_info(s, 1);
  285.         return 0;
  286.     }
  287.  
  288.     if (s->verbose && optind == 1)
  289.         return 0;
  290.  
  291.     if (s->nb_files == 0)
  292.         tcc_error("no input files\n");
  293.  
  294.     /* check -c consistency : only single file handled. XXX: checks file type */
  295.     if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
  296.         if (s->nb_libraries != 0)
  297.             tcc_error("cannot specify libraries with -c");
  298.         /* accepts only a single input file */
  299.         if ((s->nb_files != 1) && s->outfile) {
  300.             tcc_error("cannot specify multiple files with -c and -o");
  301.         }
  302.     }
  303.  
  304.     tcc_set_output_type(s, s->output_type);
  305.  
  306.     /* compile or add each files or library */
  307.     for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
  308.         int filetype = *(unsigned char *)s->files[i];
  309.         const char *filename = s->files[i] + 1;
  310.         if (filename[0] == '-' && filename[1] == 'l') {
  311.             if (tcc_add_library(s, filename + 2) < 0) {
  312.                 /* don't fail on -lm as it's harmless to skip math lib */
  313.                 if (strcmp(filename + 2, "m")) {
  314.                     tcc_error_noabort("cannot find library 'lib%s'", filename + 2);
  315.                     ret = 1;
  316.                 }
  317.             }
  318.         } else {
  319.             if (1 == s->verbose)
  320.                 printf("-> %s\n", filename);
  321.             if (!s->outfile)
  322.                 s->outfile = default_outputfile(s, filename);
  323.             if (tcc_add_file(s, filename, filetype) < 0)
  324.                 ret = 1;
  325.             else
  326.             if (s->output_type == TCC_OUTPUT_OBJ) {
  327.                 ret = !!tcc_output_file(s, s->outfile);
  328.                 if (s->gen_deps && !ret)
  329.                     gen_makedeps(s, s->outfile, s->deps_outfile);
  330.                 if (!ret) {
  331.                     if ((i+1) < s->nb_files) {
  332.                         tcc_delete(s);
  333.                         s = tcc_new();
  334.                         tcc_parse_args(s, argc - 1, argv + 1);
  335.                         tcc_set_environment(s);
  336.                         if (s->output_type != TCC_OUTPUT_OBJ)
  337.                             tcc_error("internal error");
  338.                         tcc_set_output_type(s, s->output_type);
  339.                     }
  340.                 }
  341.             }
  342.         }
  343.     }
  344.  
  345.     if (0 == ret) {
  346.         if (s->output_type == TCC_OUTPUT_MEMORY) {
  347. #ifdef TCC_IS_NATIVE
  348.             ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
  349. #else
  350.             tcc_error_noabort("-run is not available in a cross compiler");
  351.             ret = 1;
  352. #endif
  353.         } else
  354.         if (s->output_type == TCC_OUTPUT_EXE ||
  355.             s->output_type == TCC_OUTPUT_DLL)
  356.         {
  357.             ret = !!tcc_output_file(s, s->outfile);
  358.             if (s->gen_deps && !ret)
  359.                 gen_makedeps(s, s->outfile, s->deps_outfile);
  360.         }
  361.     }
  362.  
  363.     if (s->do_bench)
  364.         tcc_print_stats(s, getclock_us() - start_time);
  365.  
  366.     tcc_delete(s);
  367.     return ret;
  368. }
  369.