Subversion Repositories Kolibri OS

Rev

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