-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtinycc.c
206 lines (179 loc) · 4.02 KB
/
tinycc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "tinycc.h"
int _main(int argc0, char **argv0);
#ifdef TCC_TARGET_PE
#if defined __GNUC__ || defined __MINGW32__ || defined __MINGW64__
int main(int argc0, char **argv0)
{
_main(argc0, argv0);
}
#else
#include <tchar.h>
#define __UNKNOWN_APP 0
#define __CONSOLE_APP 1
#define __GUI_APP 2
extern void __set_app_type(int);
typedef struct
{
int newmode;
} _startupinfo;
extern int __cdecl __getmainargs(int *pargc, _TCHAR ***pargv, _TCHAR ***penv, int globb, _startupinfo*);
int _start()
{
_startupinfo start_info = {0};
__set_app_type(__GUI_APP);
//assume no unicode.
__getmainargs( &__argc, &__targv, &_tenviron, 0, &start_info);
_main(__argc, __targv);
return 1;
}
#endif //!__GNUC__
#else //!TCC_TARGET_PE
int main(int argc0, char **argv0)
{
_main(argc0, argv0);
}
#endif
int _main(int argc0, char **argv0)
{
TCCState *s, *s1;
int ret, opt, n = 0, t = 0, done;
unsigned start_time = 0;
const char *first_file;
int argc;
char **argv;
FILE *ppfp = stdout;
redo:
argc = argc0, argv = argv0;
s = s1 = tcc_new();
#ifdef TCC_TARGET_PE
tcc_add_include_path(s, "./include/windows/winapi");
tcc_add_include_path(s, "./include/windows/");
#endif
opt = tcc_parse_args(s, &argc, &argv, 1);
if (n == 0)
{
if (opt == OPT_HELP)
return fputs(help, stdout), 0;
if (opt == OPT_HELP2)
return fputs(help2, stdout), 0;
if (opt == OPT_M32 || opt == OPT_M64)
tcc_tool_cross(s, argv, opt); /* never returns */
if (s->verbose)
printf(version);
if (opt == OPT_AR)
return tcc_tool_ar(s, argc, argv);
#ifdef TCC_TARGET_PE
if (opt == OPT_IMPDEF)
return tcc_tool_impdef(s, argc, argv);
#endif
if (opt == OPT_V)
return 0;
if (opt == OPT_PRINT_DIRS)
{
/* initialize search dirs */
set_environment(s);
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
print_search_dirs(s);
return 0;
}
if (s->nb_files == 0)
tcc_error("no input files\n");
if (s->output_type == TCC_OUTPUT_PREPROCESS)
{
if (s->outfile && 0!=strcmp("-",s->outfile))
{
ppfp = fopen(s->outfile, "w");
if (!ppfp)
tcc_error("could not write '%s'", s->outfile);
}
}
else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r)
{
if (s->nb_libraries)
tcc_error("cannot specify libraries with -c");
if (s->nb_files > 1 && s->outfile)
tcc_error("cannot specify output file with -c many files");
}
else
{
if (s->option_pthread)
tcc_set_options(s, "-lpthread");
}
if (s->do_bench)
start_time = getclock_ms();
}
set_environment(s);
if (s->output_type == 0)
s->output_type = TCC_OUTPUT_EXE;
tcc_set_output_type(s, s->output_type);
s->ppfp = ppfp;
if ((s->output_type == TCC_OUTPUT_MEMORY
|| s->output_type == TCC_OUTPUT_PREPROCESS)
&& (s->dflag & 16)) /* -dt option */
{
if (t)
s->dflag |= 32;
s->run_test = ++t;
if (n)
--n;
}
/* compile or add each files or library */
first_file = NULL, ret = 0;
do
{
struct filespec *f = s->files[n];
s->filetype = f->type;
if (f->type & AFF_TYPE_LIB)
{
if (tcc_add_library_err(s, f->name) < 0)
ret = 1;
}
else
{
if (1 == s->verbose)
printf("-> %s\n", f->name);
if (!first_file)
first_file = f->name;
if (tcc_add_file(s, f->name) < 0)
ret = 1;
}
done = ret || ++n >= s->nb_files;
}
while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
if (s->run_test)
{
t = 0;
}
else if (s->output_type == TCC_OUTPUT_PREPROCESS)
{
;
}
else if (0 == ret)
{
if (s->output_type == TCC_OUTPUT_MEMORY)
{
#ifdef TCC_IS_NATIVE
ret = tcc_run(s, argc, argv);
#endif
}
else
{
if (!s->outfile)
s->outfile = default_outputfile(s, first_file);
if (tcc_output_file(s, s->outfile))
ret = 1;
else if (s->gen_deps)
gen_makedeps(s, s->outfile, s->deps_outfile);
}
}
if (s->do_bench && done && !(t | ret))
tcc_print_stats(s, getclock_ms() - start_time);
tcc_delete(s);
if (!done)
goto redo; /* compile more files with -c */
if (t)
goto redo; /* run more tests with -dt -run */
if (ppfp && ppfp != stdout)
fclose(ppfp);
return ret;
}