annotate lwcc/driver-main.c @ 304:d85d173ba120 ccdev

Checkpoint lwcc development - preprocessor is runnable but nonfunctional The preprocessor is currently runnable but doesn't actually do anything useful. This is just a checkpoint.
author William Astle <lost@l-w.ca>
date Tue, 17 Sep 2013 19:33:41 -0600
parents 4b17780f2777
children a38542cf4cc6
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
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
37 #define S(x) S2(x)
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
38 #define S2(x) #x
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
39
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
40 #define BASEDIR S(LWCC_LIBDIR)
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
41
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
42 /* list of compilation phases */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
43 enum phase_t {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
44 PHASE_DEFAULT = 0,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
45 PHASE_PREPROCESS,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
46 PHASE_COMPILE,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
47 PHASE_ASSEMBLE,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
48 PHASE_LINK
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
49 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
50
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
51 /* 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
52 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
53 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
54 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
55 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
56
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
57 /* 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
58 const char *temp_directory = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
59
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
60 /* 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
61 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
62 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
63 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
64
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
65 /* path specified with --sysroot */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
66 const char *sysroot = "";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
67 /* path specified with -isysroot */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
68 const char *isysroot = NULL;
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 /* 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
71 /* 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
72 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
73
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
83 /* compiler base directory - from -B */
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
84 const char *basedir = BASEDIR;
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
85
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
86 /* 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
87 static int file_counter = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
88
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
89 /* 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
90 command line arguments. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
91
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 lw_stringlist_t compiler_args; // recorded arguments to pass through to the compiler
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
105 lw_stringlist_t priv_sysincdirs; // system include directories for lwcc itself
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
106
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
107 /* forward delcarations */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
108 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
109
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
110 /* 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
111 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
112 might currently be running */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
113 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
114 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
115 sigterm_received = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
116 if (child_pid)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
117 kill(child_pid, SIGTERM);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
118 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
119
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
120 /* 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
121 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
122 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
123 va_list arg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
124 va_start(arg, f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
125 fprintf(stderr, "ERROR: ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
126 vfprintf(stderr, f, arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
127 putc('\n', stderr);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
128 va_end(arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
129 exit(1);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
130 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
131
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
132 /* 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
133 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
134 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
135 va_list arg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
136 va_start(arg, f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
137 fprintf(stderr, "WARNING: ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
138 vfprintf(stderr, f, arg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
139 putc('\n', stderr);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
140 va_end(arg);
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
143 /* 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
144 NULL string pointer. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
145 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
146 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
147 int c = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
148 while (*arr)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
149 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
150 if (c)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
151 printf(" ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
152 printf("%s", *arr);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
153 arr++;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
154 c = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
155 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
156 }
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 /* 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
159 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
160 program search path */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
161 static void expand_sysroot(void)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
162 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
163 /* list of path lists to process for replacements of = */
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
164 lw_stringlist_t *lists[] = { &sysincdirs, &include_dirs, &user_sysincdirs, &lib_dirs, NULL };
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
165 /* list of replacement strings for = in the same order */
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
166 const char *sysroots[] = { isysroot, isysroot, isysroot, sysroot, NULL };
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
167 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
168 char *path;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
169 lw_stringlist_t newlist;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
170 lw_stringlist_t working;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
171 char *s;
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 /* 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
174 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
175 new one. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
176 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
177 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
178 working = *lists[i];
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
179 newlist = lw_stringlist_create();
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 lw_stringlist_reset(working);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
182 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
183 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
184 if (s[0] == '=')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
185 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
186 sysroot_len = strlen(sysroots[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
187 value_len = strlen(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
188 /* 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
189 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
190 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
191 /* 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
192 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
193 lw_stringlist_addstring(newlist, path);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
194 lw_free(path);
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 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
197 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
198 lw_stringlist_addstring(newlist, s);
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 lw_stringlist_destroy(working);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
202 *lists[i] = newlist;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
203 }
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
206 /* 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
207 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
208 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
209 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
210 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
211 char *f;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
212 size_t lf, lp;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
213 int need_slash;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
214
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
215 lf = strlen(fn);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
216 lw_stringlist_reset(p);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
217 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
218 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
219 lp = strlen(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
220 need_slash = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
221 if (lp && s[lp - 1] == '/')
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
222 need_slash = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
223 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
224 memcpy(f, s, lp);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
225 if (need_slash)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
226 f[lp] = '/';
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
227 /* +1 gets the NUL */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
228 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
229 if (access(f, mode) == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
230 return f;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
231 lw_free(f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
232 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
233 /* 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
234 return lw_strdup(fn);
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
237 /* 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
238 program */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
239 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
240 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
241 int argc;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
242 char **argv;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
243 int result;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
244 char *s;
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 argc = lw_stringlist_nstrings(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
247 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
248 lw_stringlist_reset(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
249 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
250 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
251 argv[result] = s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
252 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
253 argv[result] = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
254
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
255 if (verbose_mode)
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 printf("Executing ");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
258 print_array(argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
259 printf("\n");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
260 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
261
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
262 /* 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
263 if (sigterm_received)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
264 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
265 lw_free(argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
266 return 1;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
269 /* 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
270 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
271 fflush(NULL);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
272
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
273 /* now make the child process */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
274 child_pid = fork();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
275 if (child_pid == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
276 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
277 /* child process */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
278 /* try executing program */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
279 execvp(argv[0], argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
280 /* 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
281 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
282 /* 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
283 _exit(127);
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 else if (child_pid == -1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
286 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
287 /* failure to make child process */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
288 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
289 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
290 /* clean up argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
291 lw_free(argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
292
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
293 /* 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
294 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
295 /* do nothing */;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
296 /* fetch actual return status */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
297 result = WEXITSTATUS(result);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
298 if (result)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
299 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
300 /* 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
301 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
302 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
303 /* 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
304 return sigterm_received;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
305 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
306
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
307 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
308 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
309
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 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
318 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
319 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
320 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
321 const char *osuffix;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
322 char *name;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
323 size_t lf, ls, len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
324 int counter_len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
325
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
326 /* get a new file counter */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
327 file_counter++;
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 /* 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
330 if (last && output_file)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
331 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
332 return lw_strdup(output_file);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
333 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
334
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
335 /* 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
336 osuffix = strrchr(f, '.');
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
337 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
338 osuffix = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
339 if (osuffix == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
340 osuffix = f + strlen(f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
341
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
342 ls = strlen(nsuffix);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
343
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
344 /* 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
345 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
346 if (save_temps || last)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
347 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
348 lf = osuffix - f;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
349 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
350 memcpy(name, f, lf);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
351 /* 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
352 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
353 return name;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
354 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
355
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
356 /* finally, use a temporary file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
357 if (temp_directory == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
358 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
359 /* 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
360 const char *dirtempl;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
361 char *path;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
362 size_t dirtempl_len;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
363 int need_slash;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
364
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
365 /* 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
366 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
367 dirtempl = getenv("TMPDIR");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
368 if (dirtempl == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
369 dirtempl = "/tmp";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
370 dirtempl_len = strlen(dirtempl);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
371 /* 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
372 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
373 need_slash = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
374 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
375 need_slash = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
376 /* 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
377 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
378 memcpy(path, dirtempl, dirtempl_len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
379 if (need_slash)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
380 path[dirtempl_len] = '/';
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
381 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
382 /* now make a temporary directory */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
383 if (mkdtemp(path) == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
384 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
385 /* record the temporary directory name */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
386 temp_directory = path;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
387 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
388 /* 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
389 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
390 every file requested. */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
391 lf = strlen(temp_directory);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
392 /* 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
393 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
394 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
395 if (counter_len < 1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
396 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
397 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
398 name = lw_alloc(len);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
399 /* 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
400 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
401
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
402 /* 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
403 lw_stringlist_addstring(tempfiles, name);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
404 return name;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
407 /* 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
408 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
409 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
410 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
411 lw_stringlist_t args;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
412 char *out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
413 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
414 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
415
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
416 args = lw_stringlist_create();
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 /* 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
419 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
420 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
421 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
422
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
423 /* 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
424 lw_stringlist_reset(compiler_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
425 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
426 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
427 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
428 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
429 /* 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
430 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
431 lw_stringlist_addstring(args, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
432 lw_stringlist_addstring(args, out);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
433 /* 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
434 lw_stringlist_addstring(args, input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
435 /* 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
436 free the input one */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
437 if (*output == input)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
438 lw_free(input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
439 /* 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
440 *output = out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
441 /* actually run the compiler */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
442 retval = execute_program(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
443
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
444 lw_stringlist_destroy(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
445 return retval;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
448 /* 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
449 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
450 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
451 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
452 lw_stringlist_t args;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
453 char *out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
454 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
455 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
456
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
457 args = lw_stringlist_create();
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 /* 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
460 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
461 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
462 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
463
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
464 /* add asm_args to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
465 lw_stringlist_reset(asm_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
466 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
467 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
468 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
469 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
470 /* 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
471 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
472 lw_stringlist_addstring(args, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
473 lw_stringlist_addstring(args, out);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
474 /* finally, add the input file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
475 lw_stringlist_addstring(args, input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
476 /* 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
477 if (*output == input)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
478 lw_free(input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
479 /* 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
480 *output = out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
481 /* actually run the assembler */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
482 retval = execute_program(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
483
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
484 lw_stringlist_destroy(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
485 return retval;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
488 /* 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
489 for all the include directories */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
490 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
491 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
492 lw_stringlist_t args;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
493 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
494 char *out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
495 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
496
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
497 args = lw_stringlist_create();
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 /* 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
500 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
501 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
502 lw_free(s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
503
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
504 /* add preproc_args to argv */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
505 lw_stringlist_reset(preproc_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
506 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
507 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
508 lw_stringlist_addstring(args, s);
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
511 /* 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
512 lw_stringlist_reset(includes);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
513 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
514 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
515 lw_stringlist_addstring(args, "-i");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
516 lw_stringlist_addstring(args, s);
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
519 /* 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
520 lw_stringlist_reset(include_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
521 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
522 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
523 lw_stringlist_addstring(args, "-I");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
524 lw_stringlist_addstring(args, s);
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
527 /* 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
528 lw_stringlist_reset(user_sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
529 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
530 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
531 lw_stringlist_addstring(args, "-S");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
532 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
533 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
534
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
535 /* 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
536 if (!nostdinc)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
537 {
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
538 lw_stringlist_reset(priv_sysincdirs);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
539 for (s = lw_stringlist_current(priv_sysincdirs); s; s = lw_stringlist_next(priv_sysincdirs))
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
540 {
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
541 lw_stringlist_addstring(args, "-S");
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
542 lw_stringlist_addstring(args, s);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
543 }
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
544 lw_stringlist_reset(sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
545 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
546 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
547 lw_stringlist_addstring(args, "-S");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
548 lw_stringlist_addstring(args, s);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
549 }
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
552 /* 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
553 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
554 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
555 out = lw_strdup("-");
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 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
558 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
559 /* otherwise, make an output file */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
560 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
561 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
562 /* 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
563 if (strcmp(out, "-") != 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
564 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
565 lw_stringlist_addstring(args, "-o");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
566 lw_stringlist_addstring(args, out);
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 /* 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
569 lw_stringlist_addstring(args, input);
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 /* 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
572 if (*output == input)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
573 lw_free(input);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
574 /* 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
575 *output = out;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
576 /* finally, actually run the preprocessor */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
577 retval = execute_program(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
578
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
579 lw_stringlist_destroy(args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
580 return retval;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
583 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
584 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
585 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
586 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
587 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
588 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
589 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
590 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
591 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
592 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
593 const char *suffix;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
594 char *src;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
595 int handled, retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
596
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
597 /* 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
598 if (strcmp(f, "-") == 0)
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 suffix = ".c";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
601 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
602 else
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 /* 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
605 suffix = strrchr(f, '.');
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
606 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
607 suffix = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
608 if (suffix == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
609 suffix = "";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
610 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
611
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
612 /* 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
613 src = lw_strdup(f);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
614
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
615 /* preprocess if appropriate */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
616 if (strcmp(suffix, ".c") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
617 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
618 /* 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
619 suffix = ".i";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
620 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
621 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
622 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
623 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
624 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
625 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
626 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
627 /* 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
628 suffix = ".s";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
629 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
630 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
631 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
632 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
633 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
634 /* 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
635 if (stop_after == PHASE_PREPROCESS)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
636 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
637
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
638 /* 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
639 if (strcmp(suffix, ".i") == 0)
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 /* 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
642 suffix = ".s";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
643 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
644 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
645 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
646 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
647 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
648 /* 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
649 if (stop_after == PHASE_COMPILE)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
650 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
651
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
652 /* assemble if appropriate */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
653 if (strcmp(suffix, ".s") == 0)
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 /* 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
656 suffix = ".o";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
657 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
658 if (retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
659 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
660 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
661 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
662 /* 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
663 if (stop_after == PHASE_ASSEMBLE)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
664 goto done;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
665
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
666 /* 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
667 if (strcmp(suffix, ".o") == 0)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
668 handled = 1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
669
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
670 /* 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
671 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
672 done:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
673 if (!handled && !retval)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
674 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
675 /* 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
676 if (stop_after == PHASE_LINK)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
677 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
678 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
679 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
680 else
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
681 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
682 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
683 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
684 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
685 /* clean up the file name */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
686 lw_free(src);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
687
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
688 return 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
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 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
693 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
694 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
695 static int handle_linking(void)
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 lw_stringlist_t linker_flags;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
698 char *s;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
699 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
700
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
701 linker_flags = lw_stringlist_create();
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 /* 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
704 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
705 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
706 lw_free(s);
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 /* 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
709 if (output_file)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
710 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
711 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
712 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
713 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
714
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
715 /* 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
716 if (!nostdlib)
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 }
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 /* 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
721 if (!nostartfiles)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
722 {
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
725 /* 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
726 lw_stringlist_reset(linker_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
727 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
728 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
729 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
730 }
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 /* actually run the linker */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
733 retval = execute_program(linker_flags);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
734
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
735 lw_stringlist_destroy(linker_flags);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
736 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
737 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
738
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
739 /*
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
740 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
741 and clean up.
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
742 */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
743 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
744 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
745 char *ap;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
746 int retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
747
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
748 input_files = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
749 runtime_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
750 lib_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
751 program_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
752 preproc_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
753 include_dirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
754 user_sysincdirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
755 asm_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
756 linker_args = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
757 sysincdirs = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
758 includes = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
759 tempfiles = lw_stringlist_create();
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
760 compiler_args = lw_stringlist_create();
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
761 priv_sysincdirs = lw_stringlist_create();
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
762
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
763 parse_command_line(argc, argv);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
764 if (stop_after == PHASE_DEFAULT)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
765 stop_after = PHASE_LINK;
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 if (verbose_mode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
768 printf("%s\n", VERSTRING);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
769
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
770 if (isysroot == NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
771 isysroot = sysroot;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
772 expand_sysroot();
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 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
775 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
776 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
777 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
778
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
779 // default to stdout for preprocessing
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
780 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
781 output_file = "-";
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
782
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
783 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
784 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
785
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
786 /* handle -B here */
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
787 ap = lw_alloc(strlen(basedir) + 10);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
788 strcpy(ap, basedir);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
789 strcat(ap, "/bin");
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
790 lw_stringlist_addstring(program_dirs, ap);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
791 strcpy(ap, basedir);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
792 strcat(ap, "/lib");
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
793 lw_stringlist_addstring(runtime_dirs, ap);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
794 strcpy(ap, basedir);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
795 strcat(ap, "/include");
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
796 lw_stringlist_addstring(priv_sysincdirs, ap);
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
797 lw_free(ap);
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
798
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
799 retval = 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
800 /* 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
801 signal(SIGTERM, exit_on_signal);
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 /* handle input files */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
804 lw_stringlist_reset(input_files);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
805 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
806 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
807 if (handle_input_file(ap))
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
808 retval = 1;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
811 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
812 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
813 retval = handle_linking();
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 /* 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
817 if (sigterm_received)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
818 do_warning("Terminating on signal");
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
819
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
820 /* clean up temporary files */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
821 if (!save_temps)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
822 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
823 lw_stringlist_reset(tempfiles);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
824 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
825 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
826 if (unlink(ap) == -1)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
827 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
828 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
829 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
830 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
831 if (temp_directory)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
832 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
833 if (rmdir(temp_directory) == -1)
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 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
836 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
837 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
838 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
839
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
840 /* 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
841 lw_stringlist_destroy(input_files);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
842 lw_stringlist_destroy(runtime_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
843 lw_stringlist_destroy(lib_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
844 lw_stringlist_destroy(program_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
845 lw_stringlist_destroy(preproc_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
846 lw_stringlist_destroy(include_dirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
847 lw_stringlist_destroy(user_sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
848 lw_stringlist_destroy(asm_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
849 lw_stringlist_destroy(linker_args);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
850 lw_stringlist_destroy(sysincdirs);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
851 lw_stringlist_destroy(includes);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
852 lw_stringlist_destroy(tempfiles);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
853 lw_stringlist_destroy(compiler_args);
291
83f682ed4d65 Make lwcc driver understand -B
William Astle <lost@l-w.ca>
parents: 288
diff changeset
854 lw_stringlist_destroy(priv_sysincdirs);
288
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
855 return retval;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
856 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
857
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
858 struct option_e
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
859 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
860 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
861 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
862 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
863 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
864 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
865 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
866 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
867
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
868 enum CMD_MISC {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
869 CMD_MISC_VERSION,
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
870 CMD_MISC_OPTIMIZE,
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
873 enum OPT_ARG {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
874 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
875 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
876 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
877 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
878
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
879 /* 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
880 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
881 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
882 *((int *)optptr) = optcode;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
883 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
884 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
885
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
886 /* 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
887 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
888 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
889 char **s = (char **)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
890 *s = optarg;
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 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
893 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
894
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
895 /* 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
896 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
897 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
898 char **s = (char **)optptr;
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 if (*s)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
901 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
902 *s = optarg;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
903
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
904 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
905 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
906
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
907 /* 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
908 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
909 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
910 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
911 char *next;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
912
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
913 for (; arg != NULL; arg = next)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
914 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
915 next = strchr(arg, ',');
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
916 if (next != NULL)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
917 *next++ = '\0';
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
918 lw_stringlist_addstring(l, arg);
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 return 0;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
923 /* 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
924 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
925 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
926 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
927
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
928 lw_stringlist_addstring(l, opt);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
929 return 0;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
932 /* 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
933 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
934 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
935 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
936
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
937 lw_stringlist_addstring(l, optarg);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
938 return 0;
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
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
941 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
942 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
943 switch (optcode)
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 case CMD_MISC_VERSION:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
946 printf("%s\n", VERSTRING);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
947 exit(0);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
948
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
949 case CMD_MISC_OPTIMIZE:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
950 if (!optarg)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
951 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
952 switch (*optarg)
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 case '0':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
955 case '1':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
956 case '2':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
957 case '3':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
958 case 's':
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
959 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
960 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
961 return -1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
962
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
963 default:
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
964 return -1;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
965 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
966 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
967 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
968
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
969 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
970 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
971 int *iv = (int *)optptr;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
972
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
973 if (*iv && *iv != optcode)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
974 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
975 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
976 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
977 *iv = optcode;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
978 return 0;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
979 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
980
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
981 struct option_e optionlist[] =
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
982 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
983 { "--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
984 { "--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
985 { "-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
986 { "-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
987 { "-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
988 { "-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
989 { "-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
990 { "-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
991 { "-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
992 { "-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
993 { "-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
994 { "-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
995 { "-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
996 { "-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
997 { "-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
998 { "-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
999 { "-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
1000 { "-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
1001 { "-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
1002 { "-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
1003 { "-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
1004 { "-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
1005 { "-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
1006 { "-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
1007 { "-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
1008 { "-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
1009 { "-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
1010 { "-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
1011 { "-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
1012 { "-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
1013 { NULL, 0, 0 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1014 };
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1015
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1016 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
1017 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1018 int i, j, olen, ilen;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1019 char *optarg;
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 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
1022 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1023 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
1024 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1025 /* 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
1026 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
1027 continue;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1028 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1029 olen = strlen(argv[i]);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1030 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
1031 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1032 ilen = strlen(optionlist[j].optbase);
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1033 /* 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
1034 if (ilen > olen)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1035 continue;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1036 /* does the base match? */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1037 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
1038 break;
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 if (optionlist[j].optbase == NULL)
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 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
1043 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1044 /* 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
1045 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
1046 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1047 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
1048 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1049 /* is there an argument? */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1050 optarg = NULL;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1051 if (argv[i][ilen])
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1052 optarg = argv[i] + ilen;
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1053 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
1054 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1055 if (i == argc)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1056 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1057 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
1058 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1059 optarg = argv[++i];
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1060 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1061 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
1062 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1063 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
1064 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1065 /* handle the option */
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1066 if (optionlist[j].fn)
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1067 {
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1068 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
1069 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
1070 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1071 }
fc76f1a0dc49 Initial version of lwcc compiler driver and Makefile infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1072 }