-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcc3.c
172 lines (143 loc) · 3.69 KB
/
cc3.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
// SPDX-License-Identifier: GPL-2.0-only
#include "cc3.h"
static int run_cpp(char *in_path)
{
// Create pipe
int pipefd[2];
if (pipe(pipefd) < 0)
return -1;
// Fork child process
pid_t pid = fork();
if (pid < 0) {
close(pipefd[0]);
close(pipefd[1]);
return -1;
}
if (pid) {
// Parent process
close(pipefd[1]);
return pipefd[0];
} else {
// Child process
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[0]);
close(pipefd[1]);
char *args[] = { "cpp", "-U__GNUC__", "-P", in_path, NULL };
execvp(args[0], args);
exit(errno);
}
}
static int run_command(char **args)
{
pid_t pid = fork();
if (pid < 0) // Failed to fork
return -1;
if (pid) { // Parent process
int status;
if (waitpid(pid, &status, 0) < 0) // Failed to waitpid
return -1;
if (!WIFEXITED(status)) // Child didn't exit
return -1;
if ((errno = WEXITSTATUS(status))) // Child reported error
return -1;
return 0;
}
execvp(args[0], args); // Child process
exit(errno); // Exit if execvp fails
}
static int do_compile(int in_fd, char *out_path)
{
int out_fd = open(out_path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (out_fd < 0)
return -1;
cc3_compile(in_fd, out_fd);
close(out_fd);
return 0;
}
static int do_assemble(int in_fd, char *out_path)
{
char asm_path[] = "/tmp/asmXXXXXX";
int asm_fd = mkstemp(asm_path);
if (asm_fd < 0)
return -1;
// Generate assembly
cc3_compile(in_fd, asm_fd);
// Assemble
char *args[] = { "as", "-o", out_path, asm_path, NULL };
int err = run_command(args);
close(asm_fd);
unlink(asm_path);
return err;
}
static int do_link(int in_fd, char *out_path)
{
char obj_path[] = "/tmp/objXXXXXX";
int obj_fd = mkstemp(obj_path);
if (obj_fd < 0)
return -1;
// Run assembler
int err = do_assemble(in_fd, obj_path);
// Run linker
if (err == 0) {
char *args[] = { "cc", "-o", out_path, obj_path, NULL };
err = run_command(args);
}
close(obj_fd);
unlink(obj_path);
return err;
}
int main(int argc, char **argv)
{
int opt;
bool opt_S = false;
bool opt_c = false;
bool opt_r = false;
char *out_path = NULL;
while ((opt = getopt(argc, argv, "Scro:h")) != -1)
switch (opt) {
case 'S': // Generate assembly
opt_S = true;
break;
case 'c': // Generate object file
opt_c = true;
break;
case 'r': // Read raw input
opt_r = true;
break;
case 'o': // Output file
out_path = optarg;
break;
case 'h':
default:
usage:
fprintf(stderr, "Usage: %s [-Sc] -o OUTPUT INPUT\n", argv[0]);
return 1;
}
if (!out_path)
goto usage;
if (optind >= argc)
goto usage;
// Run pre-processor or read raw input
int in_fd;
if (opt_r)
in_fd = open(argv[optind], O_RDONLY);
else
in_fd = run_cpp(argv[optind]);
if (in_fd < 0)
goto err;
// Perform the actions requested by the user
if (opt_S) {
if (do_compile(in_fd, out_path) < 0)
goto err;
} else if (opt_c) {
if (do_assemble(in_fd, out_path) < 0)
goto err;
} else {
if (do_link(in_fd, out_path) < 0)
goto err;
}
return 0;
err:
perror("cc3");
return 1;
}