annotate lwcc/driver/main.c @ 288:fc76f1a0dc49 ccdev

Initial version of lwcc compiler driver and Makefile infrastructure Implement an initial version of the lwcc compiler front end and arrange for the Makefile to understand how to compile it.
author William Astle <lost@l-w.ca>
date Sun, 08 Sep 2013 15:53:06 -0600
parents
children 83f682ed4d65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/driver/main.c
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
3
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
5
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
7
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
12
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
17
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
21
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <errno.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <signal.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <stdarg.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <stdio.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
26 #include <stdlib.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include <string.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <sys/types.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include <sys/wait.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
30 #include <unistd.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
31
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
32 #include <lw_alloc.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
33 #include <lw_string.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
34 #include <lw_stringlist.h>
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
35
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
36 #define VERSTRING "lwcc from " PACKAGE_STRING
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
37
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
38 /* list of compilation phases */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
39 enum phase_t {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
40 PHASE_DEFAULT = 0,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
41 PHASE_PREPROCESS,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
42 PHASE_COMPILE,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
43 PHASE_ASSEMBLE,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
44 PHASE_LINK
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
45 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
46
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
47 /* these are the names of various programs the compiler calls */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
48 const char *linker_program_name = "lwlink";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
49 const char *compiler_program_name = "lwcc1";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
50 const char *assembler_program_name = "lwasm";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
51 const char *preprocessor_program_name = "lwcpp";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
52
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
53 /* this will be set to the directory where temporary files get created */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
54 const char *temp_directory = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
55
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
56 /* these are for book keeping if we get interrupted - the volatile and atomic
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
57 types are needed because they are accessed in a signal handler */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
58 static volatile sig_atomic_t sigterm_received = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
59 static volatile sig_atomic_t child_pid = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
60
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
61 /* path specified with --sysroot */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
62 const char *sysroot = "";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
63 /* path specified with -isysroot */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
64 const char *isysroot = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
65
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
66 /* record which phase to stop after for -c, -E, and -S */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
67 /* default is to stop after PHASE_LINK */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
68 static int stop_after = PHASE_DEFAULT;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
69
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
70 int nostdinc = 0; // set if -nostdinc is specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
71 int nostartfiles = 0; // set if -nostartfiles is specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
72 int nostdlib = 0; // set if -nostdlib is specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
73 int verbose_mode = 0; // set to number of --verbose arguments
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
74 int save_temps = 0; // set if -save-temps is specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
75 int debug_mode = 0; // set if -g specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
76 int pic_mode = 0; // set to 1 if -fpic, 2 if -fPIC; last one specified wins
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
77 const char *output_file; // set to the value of the -o option (output file)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
78
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
79 /* compiler base directory - from -B */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
80 const char *basedir = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
81
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
82 /* used to ensure a unique temporary file at every stage */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
83 static int file_counter = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
84
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
85 /* these are various string lists used to keep track of things, mostly
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
86 command line arguments. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
87
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
88 lw_stringlist_t input_files; // input files from command line
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
89 lw_stringlist_t runtime_dirs; // directories to search for runtime files
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
90 lw_stringlist_t lib_dirs; // directories to search for library files
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
91 lw_stringlist_t program_dirs; // directories to search for compiler program components
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
92 lw_stringlist_t preproc_args; // recorded arguments to pass through to the preprocessor
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
93 lw_stringlist_t include_dirs; // include paths specified with -I
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
94 lw_stringlist_t includes; // include paths specified with -include
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
95 lw_stringlist_t user_sysincdirs; // include paths specified with -isystem
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
96 lw_stringlist_t asm_args; // recorded arguments to pass through to the assembler
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
97 lw_stringlist_t linker_args; // recorded arguments to pass through to the linker
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
98 lw_stringlist_t sysincdirs; // the standard system include directories
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
99 lw_stringlist_t tempfiles; // a list of temporary files created which need to be cleaned up
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
100 lw_stringlist_t compiler_args; // recorded arguments to pass through to the compiler
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
101
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
102 /* forward delcarations */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
103 static void parse_command_line(int, char **);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
104
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
105 /* signal handler for SIGTERM - all it does is record the fact that
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
106 SIGTERM happened and propagate the signal to whatever child process
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
107 might currently be running */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
108 static void exit_on_signal(int sig)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
109 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
110 sigterm_received = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
111 if (child_pid)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
112 kill(child_pid, SIGTERM);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
113 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
114
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
115 /* utility function to carp about an error condition and bail */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
116 void do_error(const char *f, ...)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
117 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
118 va_list arg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
119 va_start(arg, f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
120 fprintf(stderr, "ERROR: ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
121 vfprintf(stderr, f, arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
122 putc('\n', stderr);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
123 va_end(arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
124 exit(1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
125 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
126
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
127 /* utility function to carp about some condition; do not bail */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
128 void do_warning(const char *f, ...)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
129 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
130 va_list arg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
131 va_start(arg, f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
132 fprintf(stderr, "WARNING: ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
133 vfprintf(stderr, f, arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
134 putc('\n', stderr);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
135 va_end(arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
136 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
137
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
138 /* utility function to print out an array of strings - stops at the first
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
139 NULL string pointer. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
140 static void print_array(char **arr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
141 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
142 int c = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
143 while (*arr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
144 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
145 if (c)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
146 printf(" ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
147 printf("%s", *arr);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
148 arr++;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
149 c = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
150 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
151 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
152
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
153 /* expand any search path entries to reflect the sysroot and
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
154 isysroot settings. Note that it does NOT apply to the compiler
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
155 program search path */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
156 static void expand_sysroot(void)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
157 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
158 /* list of path lists to process for replacements of = */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
159 lw_stringlist_t *lists[] = { &runtime_dirs, &sysincdirs, &include_dirs, &user_sysincdirs, &lib_dirs, NULL };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
160 /* list of replacement strings for = in the same order */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
161 const char *sysroots[] = { sysroot, isysroot, isysroot, isysroot, sysroot, NULL };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
162 size_t i, sysroot_len, value_len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
163 char *path;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
164 lw_stringlist_t newlist;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
165 lw_stringlist_t working;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
166 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
167
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
168 /* for each list, run through entry by entry, do any needed replacement
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
169 and add the entry to a new list. Then replace the old list with the
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
170 new one. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
171 for (i = 0; lists[i] != NULL; i++)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
172 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
173 working = *lists[i];
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
174 newlist = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
175
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
176 lw_stringlist_reset(working);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
177 for (s = lw_stringlist_current(working); s; s = lw_stringlist_next(working))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
178 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
179 if (s[0] == '=')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
180 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
181 sysroot_len = strlen(sysroots[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
182 value_len = strlen(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
183 /* note that the skipped = will make up for the trailing NUL */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
184 path = lw_alloc(sysroot_len + value_len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
185 memcpy(path, sysroots[i], sysroot_len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
186 /* the +1 here will copy the trailing NUL */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
187 memcpy(path + sysroot_len, s + 1, value_len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
188 lw_stringlist_addstring(newlist, path);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
189 lw_free(path);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
190 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
191 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
192 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
193 lw_stringlist_addstring(newlist, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
194 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
195 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
196 lw_stringlist_destroy(working);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
197 *lists[i] = newlist;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
198 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
199 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
200
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
201 /* look for file fn in path list p which is okay for access mode mode.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
202 Return a string allocated by lw_alloc. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
203 static char *find_file(const char *fn, lw_stringlist_t p, int mode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
204 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
205 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
206 char *f;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
207 size_t lf, lp;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
208 int need_slash;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
209
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
210 lf = strlen(fn);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
211 lw_stringlist_reset(p);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
212 for (s = lw_stringlist_current(p); s; s = lw_stringlist_next(p))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
213 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
214 lp = strlen(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
215 need_slash = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
216 if (lp && s[lp - 1] == '/')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
217 need_slash = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
218 f = lw_alloc(lp + lf + need_slash + 1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
219 memcpy(f, s, lp);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
220 if (need_slash)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
221 f[lp] = '/';
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
222 /* +1 gets the NUL */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
223 memcpy(f + lp + need_slash, fn, lf + 1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
224 if (access(f, mode) == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
225 return f;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
226 lw_free(f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
227 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
228 /* if not found anywhere, try the bare filename - it might work */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
229 return lw_strdup(fn);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
230 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
231
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
232 /* take a string list which contains an argv and execute the specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
233 program */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
234 static int execute_program(lw_stringlist_t args)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
235 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
236 int argc;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
237 char **argv;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
238 int result;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
239 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
240
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
241 argc = lw_stringlist_nstrings(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
242 argv = lw_alloc(sizeof(char *) * (argc + 1));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
243 lw_stringlist_reset(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
244 for (result = 0, s = lw_stringlist_current(args); s; s = lw_stringlist_next(args))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
245 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
246 argv[result] = s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
247 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
248 argv[result] = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
249
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
250 if (verbose_mode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
251 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
252 printf("Executing ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
253 print_array(argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
254 printf("\n");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
255 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
256
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
257 /* bail now if a signal happened */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
258 if (sigterm_received)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
259 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
260 lw_free(argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
261 return 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
262 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
263
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
264 /* make sure stdio has flushed everything so that output from the
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
265 child process doesn't get intermingled */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
266 fflush(NULL);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
267
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
268 /* now make the child process */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
269 child_pid = fork();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
270 if (child_pid == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
271 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
272 /* child process */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
273 /* try executing program */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
274 execvp(argv[0], argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
275 /* only way to get here is if execvp() failed so carp about it and exit */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
276 fprintf(stderr, "Exec of %s failed: %s", argv[0], strerror(errno));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
277 /* exit with failure but don't call any atexit(), etc., functions */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
278 _exit(127);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
279 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
280 else if (child_pid == -1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
281 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
282 /* failure to make child process */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
283 do_error("Failed to execute program %s: %s", argv[0], strerror(errno));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
284 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
285 /* clean up argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
286 lw_free(argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
287
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
288 /* parent process - wait for child to exit */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
289 while (waitpid(child_pid, &result, 0) == -1 && errno == EINTR)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
290 /* do nothing */;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
291 /* fetch actual return status */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
292 result = WEXITSTATUS(result);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
293 if (result)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
294 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
295 /* carp about non-zero return status */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
296 do_error("%s terminated with status %d", argv[0], result);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
297 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
298 /* return nonzero if signalled to exit */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
299 return sigterm_received;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
300 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
301
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
302 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
303 construct an output file name as follows:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
304
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
305 1. if it is the last phase of compilation and an output file name is
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
306 specified, use that if not specified
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
307 2. if it is the last phase or we are saving temporary files, any suffix
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
308 on f is removed and replaced with nsuffix
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
309 3. otherwise, a temporary file is created. If necessary, a temporary
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
310 directory is created to hold the temporary file. The name of the temporary
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
311 file is recorded in the tempfiles string list for later cleanup. The name
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
312 of the temporary directory is recorded in temp_directory for later cleanup.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
313 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
314 static char *output_name(const char *f, const char *nsuffix, int last)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
315 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
316 const char *osuffix;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
317 char *name;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
318 size_t lf, ls, len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
319 int counter_len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
320
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
321 /* get a new file counter */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
322 file_counter++;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
323
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
324 /* if the output was specified, use it */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
325 if (last && output_file)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
326 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
327 return lw_strdup(output_file);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
328 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
329
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
330 /* find the start of the old suffix */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
331 osuffix = strrchr(f, '.');
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
332 if (osuffix != NULL && strchr(osuffix, '/') != NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
333 osuffix = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
334 if (osuffix == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
335 osuffix = f + strlen(f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
336
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
337 ls = strlen(nsuffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
338
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
339 /* if this is the last stage or we're saving temps, use a name derived
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
340 from the original file name by replacing the suffix with nsuffix */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
341 if (save_temps || last)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
342 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
343 lf = osuffix - f;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
344 name = lw_alloc(lf + ls + 1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
345 memcpy(name, f, lf);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
346 /* note that the +1 will copy the trailing NUL */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
347 memcpy(name + lf, nsuffix, ls + 1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
348 return name;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
349 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
350
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
351 /* finally, use a temporary file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
352 if (temp_directory == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
353 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
354 /* if we haven't already made a temporary directory, do so */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
355 const char *dirtempl;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
356 char *path;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
357 size_t dirtempl_len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
358 int need_slash;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
359
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
360 /* look for a TMPFIR environment variable and use that if present
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
361 but use /tmp as a fallback */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
362 dirtempl = getenv("TMPDIR");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
363 if (dirtempl == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
364 dirtempl = "/tmp";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
365 dirtempl_len = strlen(dirtempl);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
366 /* work out if we need to add a slash on the end of the directory */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
367 if (dirtempl_len && dirtempl[dirtempl_len - 1] == '/')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
368 need_slash = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
369 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
370 need_slash = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
371 /* make a string of the form <tempdir>/lwcc-XXXXXX */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
372 path = lw_alloc(dirtempl_len + need_slash + 11 + 1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
373 memcpy(path, dirtempl, dirtempl_len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
374 if (need_slash)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
375 path[dirtempl_len] = '/';
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
376 memcpy(path + dirtempl_len + need_slash, "lwcc-XXXXXX", 12);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
377 /* now make a temporary directory */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
378 if (mkdtemp(path) == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
379 do_error("mkdtemp failed: %s", strerror(errno));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
380 /* record the temporary directory name */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
381 temp_directory = path;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
382 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
383 /* now create a file name in the temporary directory. The strategy here
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
384 uses a counter that is passed along and is guaranteed to be unique for
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
385 every file requested. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
386 lf = strlen(temp_directory);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
387 /* this gets the length of the counter as a string but doesn't actually
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
388 allocate anything so we can make a string long enough */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
389 counter_len = snprintf(NULL, 0, "%d", file_counter);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
390 if (counter_len < 1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
391 do_error("snprintf failure: %s", strerror(errno));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
392 len = lf + 1 + (size_t)counter_len + ls + 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
393 name = lw_alloc(len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
394 /* it should be impossible for ths snprintf call to fail */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
395 snprintf(name, len, "%s/%d%s", temp_directory, file_counter, nsuffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
396
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
397 /* record the temporary file name for later */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
398 lw_stringlist_addstring(tempfiles, name);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
399 return name;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
400 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
401
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
402 /* this calls the actual compiler, passing the contents of compiler_args
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
403 as arguments. It also adds the input file and output file. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
404 static int compile_file(const char *file, char *input, char **output, const char *suffix)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
405 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
406 lw_stringlist_t args;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
407 char *out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
408 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
409 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
410
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
411 args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
412
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
413 /* find the compiler executable and make that argv[0] */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
414 s = find_file(compiler_program_name, program_dirs, X_OK);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
415 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
416 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
417
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
418 /* add all the saved compiler arguments to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
419 lw_stringlist_reset(compiler_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
420 for (s = lw_stringlist_current(compiler_args); s; s = lw_stringlist_next(compiler_args))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
421 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
422 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
423 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
424 /* work out the output file name and add that to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
425 out = output_name(file, suffix, stop_after == PHASE_COMPILE);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
426 lw_stringlist_addstring(args, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
427 lw_stringlist_addstring(args, out);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
428 /* add the input file to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
429 lw_stringlist_addstring(args, input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
430 /* if the input file name and the output file name pointers are the same
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
431 free the input one */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
432 if (*output == input)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
433 lw_free(input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
434 /* tell the caller what the output name is */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
435 *output = out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
436 /* actually run the compiler */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
437 retval = execute_program(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
438
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
439 lw_stringlist_destroy(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
440 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
441 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
442
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
443 /* this calls the actual assembler, passing the contents of asm_args
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
444 as arguments. It also adds the input file and output file. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
445 static int assemble_file(const char *file, char *input, char **output, const char *suffix)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
446 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
447 lw_stringlist_t args;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
448 char *out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
449 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
450 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
451
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
452 args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
453
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
454 /* find the assembler binary and add that as argv[0] */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
455 s = find_file(assembler_program_name, program_dirs, X_OK);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
456 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
457 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
458
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
459 /* add asm_args to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
460 lw_stringlist_reset(asm_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
461 for (s = lw_stringlist_current(asm_args); s; s = lw_stringlist_next(asm_args))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
462 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
463 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
464 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
465 /* get an output file name and add that to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
466 out = output_name(file, ".o", stop_after == PHASE_ASSEMBLE);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
467 lw_stringlist_addstring(args, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
468 lw_stringlist_addstring(args, out);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
469 /* finally, add the input file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
470 lw_stringlist_addstring(args, input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
471 /* clean up input file name if same as output pointer */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
472 if (*output == input)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
473 lw_free(input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
474 /* tell caller what file we made */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
475 *output = out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
476 /* actually run the assembler */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
477 retval = execute_program(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
478
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
479 lw_stringlist_destroy(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
480 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
481 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
482
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
483 /* run the preprocessor. Pass along preproc_args and appropriate options
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
484 for all the include directories */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
485 static int preprocess_file(const char *file, char *input, char **output, const char *suffix)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
486 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
487 lw_stringlist_t args;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
488 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
489 char *out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
490 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
491
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
492 args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
493
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
494 /* find the linker binary and make that argv[0] */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
495 s = find_file(preprocessor_program_name, program_dirs, X_OK);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
496 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
497 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
498
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
499 /* add preproc_args to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
500 lw_stringlist_reset(preproc_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
501 for (s = lw_stringlist_current(preproc_args); s; s = lw_stringlist_next(preproc_args))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
502 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
503 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
504 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
505
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
506 /* add the include files specified by -i */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
507 lw_stringlist_reset(includes);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
508 for (s = lw_stringlist_current(includes); s; s = lw_stringlist_next(includes))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
509 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
510 lw_stringlist_addstring(args, "-i");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
511 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
512 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
513
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
514 /* add the include directories specified by -I */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
515 lw_stringlist_reset(include_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
516 for (s = lw_stringlist_current(include_dirs); s; s = lw_stringlist_next(include_dirs))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
517 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
518 lw_stringlist_addstring(args, "-I");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
519 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
520 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
521
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
522 /* add the user specified system include directories (-isystem) */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
523 lw_stringlist_reset(user_sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
524 for (s = lw_stringlist_current(user_sysincdirs); s; s = lw_stringlist_next(user_sysincdirs))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
525 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
526 lw_stringlist_addstring(args, "-S");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
527 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
528 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
529
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
530 /* and, if not -nostdinc, the standard system include directories */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
531 if (!nostdinc)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
532 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
533 lw_stringlist_reset(sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
534 for (s = lw_stringlist_current(sysincdirs); s; s = lw_stringlist_next(sysincdirs))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
535 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
536 lw_stringlist_addstring(args, "-S");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
537 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
538 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
539 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
540
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
541 /* if we stop after preprocessing, output to stdout if no output file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
542 if (stop_after == PHASE_PREPROCESS && output_file == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
543 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
544 out = lw_strdup("-");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
545 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
546 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
547 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
548 /* otherwise, make an output file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
549 out = output_name(file, suffix, stop_after == PHASE_PREPROCESS);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
550 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
551 /* if not stdout, add the output file to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
552 if (strcmp(out, "-") != 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
553 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
554 lw_stringlist_addstring(args, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
555 lw_stringlist_addstring(args, out);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
556 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
557 /* add the input file name to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
558 lw_stringlist_addstring(args, input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
559
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
560 /* if input and output pointers are same, clean up input */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
561 if (*output == input)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
562 lw_free(input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
563 /* tell caller what our output file is */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
564 *output = out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
565 /* finally, actually run the preprocessor */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
566 retval = execute_program(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
567
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
568 lw_stringlist_destroy(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
569 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
570 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
571
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
572 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
573 handle an input file through the various stages of compilation. If any
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
574 stage decides to handle an input file, that fact is recorded. If control
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
575 reaches the end of the function without a file being handled, that
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
576 fact is mentioned to the user. Unknown files are passed to the linker
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
577 if nothing handles them and linking is to be done. It's possible the linker
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
578 will actually know what to do with them.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
579 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
580 static int handle_input_file(const char *f)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
581 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
582 const char *suffix;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
583 char *src;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
584 int handled, retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
585
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
586 /* note: this needs to handle -x but for now, assume c for stdin */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
587 if (strcmp(f, "-") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
588 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
589 suffix = ".c";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
590 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
591 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
592 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
593 /* work out the suffix on the file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
594 suffix = strrchr(f, '.');
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
595 if (suffix != NULL && strchr(suffix, '/') != NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
596 suffix = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
597 if (suffix == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
598 suffix = "";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
599 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
600
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
601 /* make a copy of the file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
602 src = lw_strdup(f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
603
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
604 /* preprocess if appropriate */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
605 if (strcmp(suffix, ".c") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
606 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
607 /* preprocessed c input source goes to .i */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
608 suffix = ".i";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
609 retval = preprocess_file(f, src, &src, suffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
610 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
611 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
612 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
613 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
614 else if (strcmp(suffix, ".S") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
615 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
616 /* preprocessed asm source goes to .s */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
617 suffix = ".s";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
618 retval = preprocess_file(f, src, &src, suffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
619 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
620 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
621 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
622 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
623 /* if we're only preprocessing, bail */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
624 if (stop_after == PHASE_PREPROCESS)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
625 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
626
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
627 /* now on to compile if appropriate */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
628 if (strcmp(suffix, ".i") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
629 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
630 /* preprocessed c source goes to .s after compiling */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
631 suffix = ".s";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
632 retval = compile_file(f, src, &src, suffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
633 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
634 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
635 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
636 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
637 /* bail if we're only compiling, not assembling */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
638 if (stop_after == PHASE_COMPILE)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
639 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
640
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
641 /* assemble if appropriate */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
642 if (strcmp(suffix, ".s") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
643 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
644 /* assembler output is an object file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
645 suffix = ".o";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
646 retval = assemble_file(f, src, &src, suffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
647 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
648 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
649 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
650 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
651 /* bail if we're not linking */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
652 if (stop_after == PHASE_ASSEMBLE)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
653 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
654
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
655 /* if we get here with a .o unhandled, pretend it is handled */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
656 if (strcmp(suffix, ".o") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
657 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
658
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
659 /* add the final file name to the linker args */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
660 lw_stringlist_addstring(linker_args, src);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
661 done:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
662 if (!handled && !retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
663 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
664 /* carp about unhandled files if there is no error */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
665 if (stop_after == PHASE_LINK)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
666 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
667 do_warning("unknown suffix %s; passing file down to linker", suffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
668 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
669 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
670 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
671 do_warning("unknown suffix %s; skipped", suffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
672 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
673 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
674 /* clean up the file name */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
675 lw_free(src);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
676
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
677 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
678 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
679
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
680 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
681 This actually runs the linker. Along the way, all the files the linker
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
682 is supposed to handle will have been added to linker_args.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
683 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
684 static int handle_linking(void)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
685 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
686 lw_stringlist_t linker_flags;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
687 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
688 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
689
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
690 linker_flags = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
691
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
692 /* find the linker binary and make that argv[0] */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
693 s = find_file(linker_program_name, program_dirs, X_OK);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
694 lw_stringlist_addstring(linker_flags, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
695 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
696
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
697 /* tell the linker about the output file name, if specified */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
698 if (output_file)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
699 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
700 lw_stringlist_addstring(linker_flags, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
701 lw_stringlist_addstring(linker_flags, (char *)output_file);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
702 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
703
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
704 /* add the standard library options if not -nostdlib */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
705 if (!nostdlib)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
706 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
707 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
708
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
709 /* add the standard startup files if not -nostartfiles */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
710 if (!nostartfiles)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
711 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
712 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
713
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
714 /* pass along the various input files, etc., to the linker */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
715 lw_stringlist_reset(linker_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
716 for (s = lw_stringlist_current(linker_args); s; s = lw_stringlist_next(linker_args))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
717 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
718 lw_stringlist_addstring(linker_flags, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
719 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
720
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
721 /* actually run the linker */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
722 retval = execute_program(linker_flags);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
723
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
724 lw_stringlist_destroy(linker_flags);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
725 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
726 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
727
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
728 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
729 Do various setup tasks, process the command line, handle the input files,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
730 and clean up.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
731 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
732 int main(int argc, char **argv)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
733 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
734 char *ap;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
735 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
736
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
737 input_files = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
738 runtime_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
739 lib_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
740 program_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
741 preproc_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
742 include_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
743 user_sysincdirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
744 asm_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
745 linker_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
746 sysincdirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
747 includes = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
748 tempfiles = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
749 compiler_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
750
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
751 parse_command_line(argc, argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
752 if (stop_after == PHASE_DEFAULT)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
753 stop_after = PHASE_LINK;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
754
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
755 if (verbose_mode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
756 printf("%s\n", VERSTRING);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
757
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
758 if (isysroot == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
759 isysroot = sysroot;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
760 expand_sysroot();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
761
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
762 if (stop_after != PHASE_LINK && output_file && lw_stringlist_nstrings(input_files) > 1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
763 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
764 do_error("-o cannot be specified with multiple inputs unless linking");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
765 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
766
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
767 // default to stdout for preprocessing
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
768 if (stop_after == PHASE_PREPROCESS && output_file == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
769 output_file = "-";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
770
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
771 if (lw_stringlist_nstrings(input_files) == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
772 do_error("No input files specified");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
773
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
774 /* handle -B here */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
775
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
776 retval = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
777 /* make sure we exit if interrupted */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
778 signal(SIGTERM, exit_on_signal);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
779
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
780 /* handle input files */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
781 lw_stringlist_reset(input_files);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
782 for (ap = lw_stringlist_current(input_files); ap; ap = lw_stringlist_next(input_files))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
783 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
784 if (handle_input_file(ap))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
785 retval = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
786 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
787
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
788 if (!retval && stop_after >= PHASE_LINK)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
789 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
790 retval = handle_linking();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
791 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
792
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
793 /* if a signal nixed us, mention the fact */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
794 if (sigterm_received)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
795 do_warning("Terminating on signal");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
796
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
797 /* clean up temporary files */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
798 if (!save_temps)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
799 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
800 lw_stringlist_reset(tempfiles);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
801 for (ap = lw_stringlist_current(tempfiles); ap; ap = lw_stringlist_next(tempfiles))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
802 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
803 if (unlink(ap) == -1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
804 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
805 do_warning("Removal of %s failed: %s", ap, strerror(errno));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
806 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
807 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
808 if (temp_directory)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
809 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
810 if (rmdir(temp_directory) == -1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
811 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
812 do_warning("Removal of temporary directory %s failed: %s", temp_directory, strerror(errno));
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
813 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
814 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
815 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
816
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
817 /* be polite and clean up all the string lists */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
818 lw_stringlist_destroy(input_files);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
819 lw_stringlist_destroy(runtime_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
820 lw_stringlist_destroy(lib_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
821 lw_stringlist_destroy(program_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
822 lw_stringlist_destroy(preproc_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
823 lw_stringlist_destroy(include_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
824 lw_stringlist_destroy(user_sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
825 lw_stringlist_destroy(asm_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
826 lw_stringlist_destroy(linker_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
827 lw_stringlist_destroy(sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
828 lw_stringlist_destroy(includes);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
829 lw_stringlist_destroy(tempfiles);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
830 lw_stringlist_destroy(compiler_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
831
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
832 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
833 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
834
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
835 struct option_e
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
836 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
837 char *optbase; // base name of option, with -
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
838 int needarg; // nonzero if option needs argument
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
839 int noextra; // nonzero if there must not be anything after optbase
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
840 int optcode; // option code (passed to fn)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
841 void *optptr; // pointer for opt (passed to fn)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
842 int (*fn)(char *, char *, int, void *); // function to handle argument, NULL to ignore it
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
843 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
844
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
845 enum CMD_MISC {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
846 CMD_MISC_VERSION,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
847 CMD_MISC_OPTIMIZE,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
848 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
849
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
850 enum OPT_ARG {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
851 OPT_ARG_OPT = 0, // argument is optional
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
852 OPT_ARG_SEP = 1, // argument may be separate
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
853 OPT_ARG_INC = 2, // argument must not be separate
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
854 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
855
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
856 /* set an integer at *optptr to optcode */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
857 static int cmdline_set_int(char *opt, char *optarg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
858 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
859 *((int *)optptr) = optcode;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
860 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
861 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
862
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
863 /* set a string at *optptr to optarg */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
864 static int cmdline_set_string(char *opt, char *optarg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
865 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
866 char **s = (char **)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
867 *s = optarg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
868
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
869 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
870 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
871
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
872 /* set a string at *optptr to optarg */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
873 static int cmdline_set_stringifnull(char *opt, char *optarg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
874 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
875 char **s = (char **)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
876
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
877 if (*s)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
878 do_error("Multiple %.*s options specified", optcode ? optcode : strlen(opt), opt);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
879 *s = optarg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
880
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
881 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
882 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
883
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
884 /* split arg on commas and add the results to string list *optptr */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
885 static int cmdline_argsplit(char *opt, char *arg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
886 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
887 lw_stringlist_t l = *(lw_stringlist_t *)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
888 char *next;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
889
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
890 for (; arg != NULL; arg = next)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
891 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
892 next = strchr(arg, ',');
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
893 if (next != NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
894 *next++ = '\0';
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
895 lw_stringlist_addstring(l, arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
896 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
897 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
898 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
899
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
900 /* add opt to string list *optptr */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
901 static int cmdline_arglist(char *opt, char *arg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
902 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
903 lw_stringlist_t l = *(lw_stringlist_t *)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
904
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
905 lw_stringlist_addstring(l, opt);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
906 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
907 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
908
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
909 /* add optarg to string list *optptr */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
910 static int cmdline_optarglist(char *opt, char *optarg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
911 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
912 lw_stringlist_t l = *(lw_stringlist_t *)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
913
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
914 lw_stringlist_addstring(l, optarg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
915 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
916 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
917
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
918 static int cmdline_misc(char *opt, char *optarg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
919 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
920 switch (optcode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
921 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
922 case CMD_MISC_VERSION:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
923 printf("%s\n", VERSTRING);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
924 exit(0);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
925
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
926 case CMD_MISC_OPTIMIZE:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
927 if (!optarg)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
928 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
929 switch (*optarg)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
930 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
931 case '0':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
932 case '1':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
933 case '2':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
934 case '3':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
935 case 's':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
936 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
937 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
938 return -1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
939
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
940 default:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
941 return -1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
942 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
943 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
944 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
945
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
946 static int cmdline_set_intifzero(char *opt, char *optarg, int optcode, void *optptr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
947 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
948 int *iv = (int *)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
949
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
950 if (*iv && *iv != optcode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
951 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
952 do_error("conflicting compiler option specified: %s", opt);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
953 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
954 *iv = optcode;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
955 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
956 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
957
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
958 struct option_e optionlist[] =
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
959 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
960 { "--version", OPT_ARG_OPT, 1, CMD_MISC_VERSION, NULL, cmdline_misc },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
961 { "--sysroot=", OPT_ARG_INC, 0, 0, &sysroot, cmdline_set_string },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
962 { "-B", OPT_ARG_INC, 0, 0, &basedir, cmdline_set_string },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
963 { "-C", OPT_ARG_OPT, 1, 0, &preproc_args, cmdline_arglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
964 { "-c", OPT_ARG_OPT, 1, PHASE_COMPILE, &stop_after, cmdline_set_intifzero },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
965 { "-D", OPT_ARG_INC, 0, 0, &preproc_args, cmdline_arglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
966 { "-E", OPT_ARG_OPT, 1, PHASE_PREPROCESS, &stop_after, cmdline_set_intifzero },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
967 { "-fPIC", OPT_ARG_OPT, 1, 2, &pic_mode, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
968 { "-fpic", OPT_ARG_OPT, 1, 1, &pic_mode, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
969 { "-g", OPT_ARG_OPT, 1, 1, &debug_mode, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
970 { "-I", OPT_ARG_SEP, 0, 0, &include_dirs, cmdline_optarglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
971 { "-include", OPT_ARG_SEP, 1, 0, &includes, cmdline_optarglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
972 { "-isysroot", OPT_ARG_SEP, 1, 0, &isysroot, cmdline_set_string },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
973 { "-isystem", OPT_ARG_SEP, 1, 0, &user_sysincdirs, cmdline_optarglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
974 { "-M", OPT_ARG_OPT, 1, 0, &preproc_args, cmdline_arglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
975 { "-nostartfiles", OPT_ARG_OPT, 1, 1, &nostartfiles, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
976 { "-nostdinc", OPT_ARG_OPT, 1, 1, &nostdinc, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
977 { "-nostdlib", OPT_ARG_OPT, 1, 1, &nostdlib, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
978 { "-O", OPT_ARG_OPT, 0, CMD_MISC_OPTIMIZE, NULL, cmdline_misc },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
979 { "-o", OPT_ARG_SEP, 0, 2, &output_file, cmdline_set_stringifnull },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
980 { "-S", OPT_ARG_OPT, 1, PHASE_ASSEMBLE, &stop_after, cmdline_set_intifzero },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
981 { "-save-temps", OPT_ARG_OPT, 1, 1, &save_temps, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
982 { "-trigraphs", OPT_ARG_OPT, 1, 0, &preproc_args, cmdline_arglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
983 { "-U", OPT_ARG_INC, 0, 0, &preproc_args, cmdline_arglist },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
984 { "-v", OPT_ARG_OPT, 1, 1, &verbose_mode, cmdline_set_int },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
985 { "-Wp,", OPT_ARG_INC, 0, 0, &preproc_args, cmdline_argsplit },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
986 { "-Wa,", OPT_ARG_INC, 0, 0, &asm_args, cmdline_argsplit },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
987 { "-Wl,", OPT_ARG_INC, 0, 0, &linker_args, cmdline_argsplit },
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
988 { "-W", OPT_ARG_INC, 0, 0, NULL, NULL }, /* warning options */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
989 { "-x", OPT_ARG_SEP, 1, 0, NULL, NULL }, /* language options */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
990 { NULL, 0, 0 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
991 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
992
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
993 static void parse_command_line(int argc, char **argv)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
994 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
995 int i, j, olen, ilen;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
996 char *optarg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
997
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
998 for (i = 1; i < argc; i++)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
999 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1000 if (argv[i][0] != '-' || argv[i][1] == '\0')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1001 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1002 /* we have a non-option argument */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1003 lw_stringlist_addstring(input_files, argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1004 continue;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1005 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1006 olen = strlen(argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1007 for (j = 0; optionlist[j].optbase; j++)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1008 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1009 ilen = strlen(optionlist[j].optbase);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1010 /* if length of optbase is longer than argv[i], it can't match */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1011 if (ilen > olen)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1012 continue;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1013 /* does the base match? */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1014 if (strncmp(optionlist[j].optbase, argv[i], ilen) == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1015 break;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1016 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1017 if (optionlist[j].optbase == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1018 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1019 do_error("Unsupported option %s", argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1020 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1021 /* is the option supposed to be exact? */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1022 if (optionlist[j].noextra && argv[i][ilen] != '\0')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1023 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1024 do_error("Unsupported option %s", argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1025 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1026 /* is there an argument? */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1027 optarg = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1028 if (argv[i][ilen])
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1029 optarg = argv[i] + ilen;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1030 if (!optarg && optionlist[j].needarg == 1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1031 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1032 if (i == argc)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1033 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1034 do_error("Option %s requires an argument", argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1035 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1036 optarg = argv[++i];
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1037 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1038 if (!optarg && optionlist[j].needarg == 2)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1039 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1040 do_error("Option %s requires an argument", argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1041 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1042 /* handle the option */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1043 if (optionlist[j].fn)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1044 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1045 if ((*(optionlist[j].fn))(argv[i], optarg, optionlist[j].optcode, optionlist[j].optptr) != 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1046 do_error("Unsupported option %s %s", argv[i], optarg ? optarg : "");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1047 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1048 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1049 }