Subversion Repositories Kolibri OS

Rev

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