Subversion Repositories Kolibri OS

Rev

Rev 8157 | Rev 9305 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
145 halyavin 1
/*
2
 *  TCC - Tiny C Compiler
6441 siemargl 3
 *
145 halyavin 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
 
6429 siemargl 21
#ifdef ONE_SOURCE
22
#include "libtcc.c"
145 halyavin 23
#else
6429 siemargl 24
#include "tcc.h"
145 halyavin 25
#endif
609 andrew_pro 26
 
6429 siemargl 27
static void print_paths(const char *msg, char **paths, int nb_paths)
28
{
145 halyavin 29
    int i;
6429 siemargl 30
    printf("%s:\n%s", msg, nb_paths ? "" : "  -\n");
31
    for(i = 0; i < nb_paths; i++)
32
        printf("  %s\n", paths[i]);
145 halyavin 33
}
609 andrew_pro 34
 
6429 siemargl 35
static void display_info(TCCState *s, int what)
145 halyavin 36
{
6429 siemargl 37
    switch (what) {
38
    case 0:
39
        printf("tcc version %s ("
145 halyavin 40
#ifdef TCC_TARGET_I386
6429 siemargl 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
145 halyavin 56
#endif
6429 siemargl 57
#ifdef TCC_TARGET_PE
58
        " Windows"
59
#elif defined TCC_TARGET_MEOS
60
		" KolibriOS"
145 halyavin 61
#else
6429 siemargl 62
        " Linux"
145 halyavin 63
#endif
6429 siemargl 64
        ")\n", TCC_VERSION);
145 halyavin 65
        break;
6429 siemargl 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));
145 halyavin 74
#endif
75
        break;
76
    }
77
}
78
 
6429 siemargl 79
static void help(void)
145 halyavin 80
{
6429 siemargl 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"
6441 siemargl 116
           "  -stack=size set PE/KOS32 stack size\n"
6429 siemargl 117
           "Debugger options:\n"
118
           "  -g          generate runtime debug info\n"
145 halyavin 119
#ifdef CONFIG_TCC_BCHECK
6429 siemargl 120
           "  -b          compile with built-in memory and bounds checker (implies -g)\n"
145 halyavin 121
#endif
6429 siemargl 122
#ifdef CONFIG_TCC_BACKTRACE
123
           "  -bt N       show N callers in stack traces\n"
145 halyavin 124
#endif
6429 siemargl 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"
9284 Coldy 131
#if defined(TCC_TARGET_MEOS) && !defined (TCC_TARGET_KX)
8157 Boppan 132
           "For KolibriOS only:\n"
133
           "  -nobss      do not emit BSS section into file\n"
9284 Coldy 134
#endif
6429 siemargl 135
           );
145 halyavin 136
}
137
 
6429 siemargl 138
/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
139
#if (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) && !defined(TCC_TARGET_MEOS)
140
#ifdef _WIN32
141
#include 
142
static int execvp_win32(const char *prog, char **argv)
145 halyavin 143
{
6429 siemargl 144
    int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
145
    if (-1 == ret)
146
        return ret;
147
    cwait(&ret, ret, WAIT_CHILD);
148
    exit(ret);
145 halyavin 149
}
6429 siemargl 150
#define execvp execvp_win32
145 halyavin 151
#endif
6429 siemargl 152
static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
145 halyavin 153
{
6429 siemargl 154
    char child_path[4096], *child_name; const char *target;
155
    switch (atoi(optarg)) {
145 halyavin 156
#ifdef TCC_TARGET_I386
6429 siemargl 157
        case 32: break;
158
        case 64: target = "x86_64";
145 halyavin 159
#else
6429 siemargl 160
        case 64: break;
161
        case 32: target = "i386";
145 halyavin 162
#endif
6429 siemargl 163
            pstrcpy(child_path, sizeof child_path - 40, argv[0]);
164
            child_name = tcc_basename(child_path);
165
            strcpy(child_name, target);
166
#ifdef TCC_TARGET_PE
167
            strcat(child_name, "-win32");
168
#elif defined TCC_TARGET_MEOS
169
            strcat(child_name, "-kos32");
145 halyavin 170
#endif
6429 siemargl 171
            strcat(child_name, "-tcc");
172
            if (strcmp(argv[0], child_path)) {
173
                if (s->verbose > 0)
174
                    printf("tcc: using '%s'\n", child_name), fflush(stdout);
175
                execvp(argv[0] = child_path, argv);
145 halyavin 176
            }
6429 siemargl 177
            tcc_error("'%s' not found", child_name);
178
        case 0: /* ignore -march etc. */
145 halyavin 179
            break;
180
        default:
6429 siemargl 181
            tcc_warning("unsupported option \"-m%s\"", optarg);
145 halyavin 182
    }
183
}
184
#else
6429 siemargl 185
#define exec_other_tcc(s, argv, optarg)
145 halyavin 186
#endif
187
 
6429 siemargl 188
static void gen_makedeps(TCCState *s, const char *target, const char *filename)
145 halyavin 189
{
6429 siemargl 190
    FILE *depout;
191
    char buf[1024], *ext;
145 halyavin 192
    int i;
193
 
6429 siemargl 194
    if (!filename) {
195
        /* compute filename automatically
196
         * dir/file.o -> dir/file.d             */
197
        pstrcpy(buf, sizeof(buf), target);
198
        ext = tcc_fileextension(buf);
199
        pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
200
        filename = buf;
145 halyavin 201
    }
202
 
6429 siemargl 203
    if (s->verbose)
204
        printf("<- %s\n", filename);
145 halyavin 205
 
6429 siemargl 206
    /* XXX return err codes instead of error() ? */
207
    depout = fopen(filename, "w");
208
    if (!depout)
209
        tcc_error("could not open '%s'", filename);
145 halyavin 210
 
6429 siemargl 211
    fprintf(depout, "%s : \\\n", target);
212
    for (i=0; inb_target_deps; ++i)
213
        fprintf(depout, " %s \\\n", s->target_deps[i]);
214
    fprintf(depout, "\n");
215
    fclose(depout);
145 halyavin 216
}
217
 
6429 siemargl 218
static char *default_outputfile(TCCState *s, const char *first_file)
145 halyavin 219
{
6429 siemargl 220
    char buf[1024];
221
    char *ext;
222
    const char *name = "a";
145 halyavin 223
 
6429 siemargl 224
    if (first_file && strcmp(first_file, "-"))
225
        name = tcc_basename(first_file);
226
    pstrcpy(buf, sizeof(buf), name);
227
    ext = tcc_fileextension(buf);
145 halyavin 228
#ifdef TCC_TARGET_PE
6429 siemargl 229
    if (s->output_type == TCC_OUTPUT_DLL)
230
        strcpy(ext, ".dll");
231
    else
232
    if (s->output_type == TCC_OUTPUT_EXE)
233
        strcpy(ext, ".exe");
234
    else
145 halyavin 235
#endif
6429 siemargl 236
    if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
237
          (s->output_type == TCC_OUTPUT_PREPROCESS) )
238
        && *ext)
239
        strcpy(ext, ".o");
145 halyavin 240
    else
6429 siemargl 241
        strcpy(buf, "a.out");
145 halyavin 242
 
6429 siemargl 243
    return tcc_strdup(buf);
145 halyavin 244
}
245
 
246
static int64_t getclock_us(void)
247
{
6429 siemargl 248
#ifdef _WIN32
249
    LARGE_INTEGER frequency, t1;
250
    QueryPerformanceFrequency(&frequency);
251
    QueryPerformanceCounter(&t1);
252
    return t1.QuadPart * 1000000LL / frequency.QuadPart;
145 halyavin 253
#else
254
    struct timeval tv;
255
    gettimeofday(&tv, NULL);
256
    return tv.tv_sec * 1000000LL + tv.tv_usec;
257
#endif
258
}
259
 
6429 siemargl 260
int main(int argc, char **argv)
145 halyavin 261
{
6429 siemargl 262
    TCCState *s;
263
    int ret, optind, i;
264
    int64_t start_time = 0;
145 halyavin 265
 
6429 siemargl 266
    s = tcc_new();
145 halyavin 267
 
6429 siemargl 268
    optind = tcc_parse_args(s, argc - 1, argv + 1);
145 halyavin 269
 
6429 siemargl 270
    if (s->do_bench)
271
        start_time = getclock_us();
145 halyavin 272
 
6429 siemargl 273
    tcc_set_environment(s);
145 halyavin 274
 
6429 siemargl 275
    if (optind == 0) {
276
        help();
277
        return 1;
145 halyavin 278
    }
279
 
6429 siemargl 280
    if (s->option_m)
281
        exec_other_tcc(s, argv, s->option_m);
145 halyavin 282
 
6429 siemargl 283
    if (s->verbose)
284
        display_info(s, 0);
145 halyavin 285
 
6429 siemargl 286
    if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
287
        tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
288
        display_info(s, 1);
289
        return 0;
145 halyavin 290
    }
291
 
6429 siemargl 292
    if (s->verbose && optind == 1)
145 halyavin 293
        return 0;
294
 
6429 siemargl 295
    if (s->nb_files == 0)
296
        tcc_error("no input files\n");
145 halyavin 297
 
6429 siemargl 298
    /* check -c consistency : only single file handled. XXX: checks file type */
299
    if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
300
        if (s->nb_libraries != 0)
301
            tcc_error("cannot specify libraries with -c");
145 halyavin 302
        /* accepts only a single input file */
6429 siemargl 303
        if ((s->nb_files != 1) && s->outfile) {
304
            tcc_error("cannot specify multiple files with -c and -o");
145 halyavin 305
        }
306
    }
307
 
6429 siemargl 308
    tcc_set_output_type(s, s->output_type);
145 halyavin 309
 
310
    /* compile or add each files or library */
6429 siemargl 311
    for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
312
        int filetype = *(unsigned char *)s->files[i];
313
        const char *filename = s->files[i] + 1;
314
        if (filename[0] == '-' && filename[1] == 'l') {
315
            if (tcc_add_library(s, filename + 2) < 0) {
316
                /* don't fail on -lm as it's harmless to skip math lib */
317
                if (strcmp(filename + 2, "m")) {
318
                    tcc_error_noabort("cannot find library 'lib%s'", filename + 2);
319
                    ret = 1;
320
                }
321
            }
145 halyavin 322
        } else {
6429 siemargl 323
            if (1 == s->verbose)
324
                printf("-> %s\n", filename);
325
            if (!s->outfile)
326
                s->outfile = default_outputfile(s, filename);
327
            if (tcc_add_file(s, filename, filetype) < 0)
145 halyavin 328
                ret = 1;
6429 siemargl 329
            else
330
            if (s->output_type == TCC_OUTPUT_OBJ) {
331
                ret = !!tcc_output_file(s, s->outfile);
332
                if (s->gen_deps && !ret)
333
                    gen_makedeps(s, s->outfile, s->deps_outfile);
334
                if (!ret) {
335
                    if ((i+1) < s->nb_files) {
336
                        tcc_delete(s);
337
                        s = tcc_new();
338
                        tcc_parse_args(s, argc - 1, argv + 1);
339
                        tcc_set_environment(s);
340
                        if (s->output_type != TCC_OUTPUT_OBJ)
341
                            tcc_error("internal error");
342
                        tcc_set_output_type(s, s->output_type);
343
                    }
344
                }
145 halyavin 345
            }
346
        }
347
    }
348
 
6429 siemargl 349
    if (0 == ret) {
350
        if (s->output_type == TCC_OUTPUT_MEMORY) {
351
#ifdef TCC_IS_NATIVE
352
            ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
609 andrew_pro 353
#else
6429 siemargl 354
            tcc_error_noabort("-run is not available in a cross compiler");
355
            ret = 1;
145 halyavin 356
#endif
6429 siemargl 357
        } else
358
        if (s->output_type == TCC_OUTPUT_EXE ||
359
            s->output_type == TCC_OUTPUT_DLL)
360
        {
361
            ret = !!tcc_output_file(s, s->outfile);
362
            if (s->gen_deps && !ret)
363
                gen_makedeps(s, s->outfile, s->deps_outfile);
364
        }
145 halyavin 365
    }
366
 
6429 siemargl 367
    if (s->do_bench)
368
        tcc_print_stats(s, getclock_us() - start_time);
369
 
370
    tcc_delete(s);
145 halyavin 371
    return ret;
372
}