Commit | Line | Data |
---|---|---|
984263bc | 1 | /*- |
f29fb17d | 2 | * Copyright (c) 2004, The DragonFly Project. All rights reserved. |
984263bc MD |
3 | * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au> |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
25 | * SUCH DAMAGE. | |
26 | * | |
27 | * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $ | |
28 | */ | |
29 | ||
04d63062 SW |
30 | #include <sys/param.h> |
31 | ||
984263bc MD |
32 | #include <err.h> |
33 | #include <objformat.h> | |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | #include <unistd.h> | |
38 | ||
990bfa79 | 39 | #ifndef CCVER_DEFAULT |
2a89766a | 40 | #define CCVER_DEFAULT "gcc80" |
990bfa79 | 41 | #endif |
a1ea2019 | 42 | |
990bfa79 | 43 | #ifndef BINUTILSVER_DEFAULT |
628f4db5 | 44 | #define BINUTILSVER_DEFAULT "binutils234" |
990bfa79 MD |
45 | #endif |
46 | ||
b69d2d5e | 47 | #define LINKER_BFD "ld.bfd" |
0784934a | 48 | #define LINKER_GOLD "ld.gold" |
b69d2d5e JM |
49 | #define LINKER_DEFAULT LINKER_GOLD |
50 | #define LINKER_ALT LINKER_BFD | |
0784934a | 51 | |
3c0aa72e SS |
52 | #ifndef OBJFORMAT_PATH_DEFAULT |
53 | #define OBJFORMAT_PATH_DEFAULT "" | |
54 | #endif | |
55 | ||
bf4591b3 SG |
56 | /* Macro for array size */ |
57 | #ifndef NELEM | |
58 | #define NELEM(ary) (sizeof(ary) / sizeof((ary)[0])) | |
59 | #endif | |
60 | ||
0784934a | 61 | enum cmd_type { OBJFORMAT, COMPILER, BINUTILS, LINKER }; |
91b13002 JS |
62 | |
63 | struct command { | |
64 | const char *cmd; | |
04d63062 | 65 | enum cmd_type type; |
91b13002 JS |
66 | }; |
67 | ||
68 | static struct command commands[] = { | |
2350540c JM |
69 | {"CC", COMPILER}, |
70 | {"c++", COMPILER}, | |
71 | {"cc", COMPILER}, | |
72 | {"cpp", COMPILER}, | |
73 | {"g++", COMPILER}, | |
74 | {"gcc", COMPILER}, | |
75 | {"gcov", COMPILER}, | |
76 | {"ld", LINKER}, | |
77 | {"addr2line", BINUTILS}, | |
78 | {"ar", BINUTILS}, | |
79 | {"as", BINUTILS}, | |
80 | {"c++filt", BINUTILS}, | |
81 | {"elfedit", BINUTILS}, | |
82 | {"gprof", BINUTILS}, | |
875016c4 JM |
83 | {"ld.bfd", BINUTILS}, |
84 | {"ld.gold", BINUTILS}, | |
2350540c JM |
85 | {"nm", BINUTILS}, |
86 | {"objcopy", BINUTILS}, | |
87 | {"objdump", BINUTILS}, | |
88 | {"ranlib", BINUTILS}, | |
89 | {"readelf", BINUTILS}, | |
90 | {"size", BINUTILS}, | |
91 | {"strings", BINUTILS}, | |
92 | {"strip", BINUTILS}, | |
2350540c JM |
93 | {"objformat", OBJFORMAT}, |
94 | {"", -1} | |
91b13002 JS |
95 | }; |
96 | ||
984263bc MD |
97 | int |
98 | main(int argc, char **argv) | |
99 | { | |
b69d2d5e JM |
100 | char ld_def[] = LINKER_DEFAULT; |
101 | char ld_alt[] = LINKER_ALT; | |
645f509d | 102 | struct command *cmds; |
984263bc MD |
103 | char objformat[32]; |
104 | char *path, *chunk; | |
105 | char *cmd, *newcmd = NULL; | |
b69d2d5e | 106 | char *ldcmd = ld_def; |
ee3181ae SW |
107 | const char *objformat_path; |
108 | const char *ccver; | |
109 | const char *buver; | |
0784934a | 110 | const char *ldver; |
5228fe46 | 111 | const char *env_value = NULL; |
91b13002 JS |
112 | const char *base_path = NULL; |
113 | int use_objformat = 0; | |
984263bc MD |
114 | |
115 | if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) | |
116 | errx(1, "Invalid object format"); | |
117 | ||
4806ab2e | 118 | /* |
67ba35da | 119 | * Get the last path element of the program name being executed |
4806ab2e | 120 | */ |
984263bc MD |
121 | cmd = strrchr(argv[0], '/'); |
122 | if (cmd != NULL) | |
123 | cmd++; | |
124 | else | |
125 | cmd = argv[0]; | |
126 | ||
04d63062 | 127 | for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) { |
91b13002 | 128 | if (strcmp(cmd, cmds->cmd) == 0) |
645f509d MD |
129 | break; |
130 | } | |
5228fe46 | 131 | |
645f509d MD |
132 | if (cmds) { |
133 | switch (cmds->type) { | |
134 | case COMPILER: | |
2350540c JM |
135 | ccver = getenv("CCVER"); |
136 | if ((ccver == NULL) || ccver[0] == 0) | |
137 | ccver = CCVER_DEFAULT; | |
17030342 | 138 | base_path = "/usr/libexec"; |
645f509d | 139 | use_objformat = 0; |
5228fe46 | 140 | env_value = ccver; |
645f509d | 141 | break; |
04d63062 | 142 | case BINUTILS: |
2350540c JM |
143 | buver = getenv("BINUTILSVER"); |
144 | if (buver == NULL) | |
145 | buver = BINUTILSVER_DEFAULT; | |
04d63062 | 146 | base_path = "/usr/libexec"; |
645f509d | 147 | use_objformat = 1; |
5228fe46 | 148 | env_value = buver; |
91b13002 | 149 | break; |
0784934a | 150 | case LINKER: |
2350540c JM |
151 | buver = getenv("BINUTILSVER"); |
152 | if (buver == NULL) | |
153 | buver = BINUTILSVER_DEFAULT; | |
f5e09b04 | 154 | ldver = getenv("LDVER"); |
b69d2d5e JM |
155 | if ((ldver != NULL) && (strcmp(ldver, ld_alt) == 0)) |
156 | ldcmd = ld_alt; | |
0784934a JM |
157 | base_path = "/usr/libexec"; |
158 | use_objformat = 1; | |
159 | env_value = buver; | |
160 | cmd = ldcmd; | |
161 | break; | |
645f509d MD |
162 | case OBJFORMAT: |
163 | break; | |
05e72d3a SW |
164 | default: |
165 | errx(1, "unknown command type"); | |
166 | break; | |
91b13002 | 167 | } |
645f509d | 168 | } |
4806ab2e MD |
169 | |
170 | /* | |
171 | * The objformat command itself doesn't need another exec | |
172 | */ | |
91b13002 | 173 | if (cmds->type == OBJFORMAT) { |
984263bc MD |
174 | if (argc != 1) { |
175 | fprintf(stderr, "Usage: objformat\n"); | |
176 | exit(1); | |
177 | } | |
91b13002 | 178 | |
984263bc MD |
179 | printf("%s\n", objformat); |
180 | exit(0); | |
181 | } | |
182 | ||
4806ab2e | 183 | /* |
fce5f52e | 184 | * make buildworld glue and CCVER overrides. |
4806ab2e | 185 | */ |
a3e064e4 SW |
186 | objformat_path = getenv("OBJFORMAT_PATH"); |
187 | if (objformat_path == NULL) | |
188 | objformat_path = OBJFORMAT_PATH_DEFAULT; | |
91b13002 | 189 | |
a1478cac | 190 | again: |
984263bc MD |
191 | path = strdup(objformat_path); |
192 | ||
6fb88001 SS |
193 | if (setenv("OBJFORMAT", objformat, 1) == -1) |
194 | err(1, "setenv: cannot set OBJFORMAT=%s", objformat); | |
984263bc | 195 | |
4806ab2e MD |
196 | /* |
197 | * objformat_path could be sequence of colon-separated paths. | |
198 | */ | |
984263bc MD |
199 | while ((chunk = strsep(&path, ":")) != NULL) { |
200 | if (newcmd != NULL) { | |
201 | free(newcmd); | |
202 | newcmd = NULL; | |
203 | } | |
645f509d | 204 | if (use_objformat) { |
b5cd2d99 SW |
205 | asprintf(&newcmd, "%s%s/%s/%s/%s", |
206 | chunk, base_path, env_value, objformat, cmd); | |
645f509d | 207 | } else { |
a1478cac SS |
208 | asprintf(&newcmd, "%s%s/%s/%s", |
209 | chunk, base_path, env_value, cmd); | |
645f509d | 210 | } |
984263bc | 211 | if (newcmd == NULL) |
4806ab2e | 212 | err(1, "cannot allocate memory"); |
984263bc MD |
213 | |
214 | argv[0] = newcmd; | |
215 | execv(newcmd, argv); | |
216 | } | |
a1478cac SS |
217 | |
218 | /* | |
219 | * Fallback: if we're searching for a compiler, but didn't | |
220 | * find any, try again using the custom compiler driver. | |
221 | */ | |
222 | if (cmds && cmds->type == COMPILER && | |
223 | strcmp(env_value, "custom") != 0) { | |
224 | env_value = "custom"; | |
225 | goto again; | |
226 | } | |
227 | ||
645f509d | 228 | if (use_objformat) { |
91b13002 JS |
229 | err(1, "in path [%s]%s/%s/%s/%s", |
230 | objformat_path, base_path, env_value, objformat, cmd); | |
645f509d | 231 | } else { |
91b13002 JS |
232 | err(1, "in path [%s]%s/%s/%s", |
233 | objformat_path, base_path, env_value, cmd); | |
645f509d | 234 | } |
984263bc | 235 | } |