Added missing file.
[tinycc/k1w1.git] / tcc.c
blobd6e25838fc59a5c7441c2757e211020cb3d553cd
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
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.
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.
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
21 #include "tcc.h"
23 void help(void)
25 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
26 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
27 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
28 " [-static] [infile1 infile2...] [-run infile args...]\n"
29 "\n"
30 "General options:\n"
31 " -v display current version, increase verbosity\n"
32 " -c compile only - generate an object file\n"
33 " -o outfile set output filename\n"
34 " -Bdir set tcc internal library path\n"
35 " -bench output compilation statistics\n"
36 " -run run compiled source\n"
37 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
38 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
39 " -w disable all warnings\n"
40 "Preprocessor options:\n"
41 " -E preprocess only\n"
42 " -Idir add include path 'dir'\n"
43 " -Dsym[=val] define 'sym' with value 'val'\n"
44 " -Usym undefine 'sym'\n"
45 "Linker options:\n"
46 " -Ldir add library path 'dir'\n"
47 " -llib link with dynamic or static library 'lib'\n"
48 " -shared generate a shared library\n"
49 " -soname set name for shared library to be used at runtime\n"
50 " -static static linking\n"
51 " -rdynamic export all global symbols to dynamic linker\n"
52 " -r generate (relocatable) object file\n"
53 "Debugger options:\n"
54 " -g generate runtime debug info\n"
55 #ifdef CONFIG_TCC_BCHECK
56 " -b compile with built-in memory and bounds checker (implies -g)\n"
57 #endif
58 #ifdef CONFIG_TCC_BACKTRACE
59 " -bt N show N callers in stack traces\n"
60 #endif
64 int main(int argc, char **argv)
66 TCCConfig config;
67 int i;
68 TCCState *s;
69 int nb_objfiles, ret, optind;
70 char objfilename[1024];
71 int64_t start_time = 0;
73 s = tcc_new();
74 init_tcc_config(&config);
76 ret = 0;
78 optind = parse_args(&config, s, argc - 1, argv + 1);
79 if (config.print_search_dirs) {
80 /* enough for Linux kernel */
81 printf("install: %s/\n", s->tcc_lib_path);
82 return 0;
84 if (optind == 0 || config.nb_files == 0) {
85 if (optind && s->verbose)
86 return 0;
87 help();
88 return 1;
91 nb_objfiles = config.nb_files - config.nb_libraries;
93 /* if outfile provided without other options, we output an
94 executable */
95 if (config.outfile && config.output_type == TCC_OUTPUT_MEMORY)
96 config.output_type = TCC_OUTPUT_EXE;
98 /* check -c consistency : only single file handled. XXX: checks file type */
99 if (config.output_type == TCC_OUTPUT_OBJ && !config.reloc_output) {
100 /* accepts only a single input file */
101 if (nb_objfiles != 1)
102 error("cannot specify multiple files with -c");
103 if (config.nb_libraries != 0)
104 error("cannot specify libraries with -c");
108 if (config.output_type == TCC_OUTPUT_PREPROCESS) {
109 if (!config.outfile) {
110 s->outfile = stdout;
111 } else {
112 s->outfile = fopen(config.outfile, "w");
113 if (!s->outfile)
114 error("could not open '%s", config.outfile);
116 } else if (config.output_type != TCC_OUTPUT_MEMORY) {
117 if (!config.outfile) {
118 /* compute default outfile name */
119 char *ext;
120 const char *name =
121 strcmp(config.files[0], "-") == 0 ? "a" : tcc_basename(config.files[0]);
122 pstrcpy(objfilename, sizeof(objfilename), name);
123 ext = tcc_fileextension(objfilename);
124 #ifdef TCC_TARGET_PE
125 if (config.output_type == TCC_OUTPUT_DLL)
126 strcpy(ext, ".dll");
127 else
128 if (config.output_type == TCC_OUTPUT_EXE)
129 strcpy(ext, ".exe");
130 else
131 #endif
132 if (config.output_type == TCC_OUTPUT_OBJ && !config.reloc_output && *ext)
133 strcpy(ext, ".o");
134 else
135 pstrcpy(objfilename, sizeof(objfilename), "a.out");
136 config.outfile = objfilename;
140 if (config.do_bench) {
141 start_time = getclock_us();
144 tcc_set_output_type(s, config.output_type);
146 /* compile or add each files or library */
147 for(i = 0; i < config.nb_files && ret == 0; i++) {
148 const char *filename;
150 filename = config.files[i];
151 if (filename[0] == '-' && filename[1]) {
152 if (tcc_add_library(s, filename + 2) < 0) {
153 error_noabort("cannot find %s", filename);
154 ret = 1;
156 } else {
157 if (1 == s->verbose)
158 printf("-> %s\n", filename);
159 if (tcc_add_file(s, filename) < 0)
160 ret = 1;
164 /* free all files */
165 tcc_free(config.files);
167 if (0 == ret) {
168 if (config.do_bench)
169 tcc_print_stats(s, getclock_us() - start_time);
171 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
172 if (config.outfile)
173 fclose(s->outfile);
174 } else if (s->output_type == TCC_OUTPUT_MEMORY)
175 ret = tcc_run(s, argc - optind, argv + optind);
176 else
177 ret = tcc_output_file(s, config.outfile) ? 1 : 0;
180 tcc_delete(s);
182 #ifdef MEM_DEBUG
183 if (do_bench) {
184 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
186 #endif
187 return ret;