World - Change default compiler from gcc-5 to gcc-8
[dragonfly.git] / usr.bin / objformat / objformat.c
CommitLineData
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
5cc5a38c 44#define BINUTILSVER_DEFAULT "binutils227"
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 61enum cmd_type { OBJFORMAT, COMPILER, BINUTILS, LINKER };
91b13002
JS
62
63struct command {
64 const char *cmd;
04d63062 65 enum cmd_type type;
91b13002
JS
66};
67
68static 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},
93 {"incremental-dump", BINUTILS},
94 {"objformat", OBJFORMAT},
95 {"", -1}
91b13002
JS
96};
97
984263bc
MD
98int
99main(int argc, char **argv)
100{
b69d2d5e
JM
101 char ld_def[] = LINKER_DEFAULT;
102 char ld_alt[] = LINKER_ALT;
645f509d 103 struct command *cmds;
984263bc
MD
104 char objformat[32];
105 char *path, *chunk;
106 char *cmd, *newcmd = NULL;
b69d2d5e 107 char *ldcmd = ld_def;
ee3181ae
SW
108 const char *objformat_path;
109 const char *ccver;
110 const char *buver;
0784934a 111 const char *ldver;
5228fe46 112 const char *env_value = NULL;
91b13002
JS
113 const char *base_path = NULL;
114 int use_objformat = 0;
984263bc
MD
115
116 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
117 errx(1, "Invalid object format");
118
4806ab2e 119 /*
67ba35da 120 * Get the last path element of the program name being executed
4806ab2e 121 */
984263bc
MD
122 cmd = strrchr(argv[0], '/');
123 if (cmd != NULL)
124 cmd++;
125 else
126 cmd = argv[0];
127
04d63062 128 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
91b13002 129 if (strcmp(cmd, cmds->cmd) == 0)
645f509d
MD
130 break;
131 }
5228fe46 132
645f509d
MD
133 if (cmds) {
134 switch (cmds->type) {
135 case COMPILER:
2350540c
JM
136 ccver = getenv("CCVER");
137 if ((ccver == NULL) || ccver[0] == 0)
138 ccver = CCVER_DEFAULT;
17030342 139 base_path = "/usr/libexec";
645f509d 140 use_objformat = 0;
5228fe46 141 env_value = ccver;
645f509d 142 break;
04d63062 143 case BINUTILS:
2350540c
JM
144 buver = getenv("BINUTILSVER");
145 if (buver == NULL)
146 buver = BINUTILSVER_DEFAULT;
04d63062 147 base_path = "/usr/libexec";
645f509d 148 use_objformat = 1;
5228fe46 149 env_value = buver;
91b13002 150 break;
0784934a 151 case LINKER:
2350540c
JM
152 buver = getenv("BINUTILSVER");
153 if (buver == NULL)
154 buver = BINUTILSVER_DEFAULT;
f5e09b04 155 ldver = getenv("LDVER");
b69d2d5e
JM
156 if ((ldver != NULL) && (strcmp(ldver, ld_alt) == 0))
157 ldcmd = ld_alt;
0784934a
JM
158 base_path = "/usr/libexec";
159 use_objformat = 1;
160 env_value = buver;
161 cmd = ldcmd;
162 break;
645f509d
MD
163 case OBJFORMAT:
164 break;
05e72d3a
SW
165 default:
166 errx(1, "unknown command type");
167 break;
91b13002 168 }
645f509d 169 }
4806ab2e
MD
170
171 /*
172 * The objformat command itself doesn't need another exec
173 */
91b13002 174 if (cmds->type == OBJFORMAT) {
984263bc
MD
175 if (argc != 1) {
176 fprintf(stderr, "Usage: objformat\n");
177 exit(1);
178 }
91b13002 179
984263bc
MD
180 printf("%s\n", objformat);
181 exit(0);
182 }
183
4806ab2e 184 /*
fce5f52e 185 * make buildworld glue and CCVER overrides.
4806ab2e 186 */
2806e6a6
JM
187 /*
188 * CCVER=clang check temporary; only for base clang import work
189 */
190 if (cmds && cmds->type == COMPILER &&
191 strcmp (env_value, "clang") == 0) {
192 objformat_path = "";
193 } else {
194 objformat_path = getenv("OBJFORMAT_PATH");
195 if (objformat_path == NULL)
196 objformat_path = OBJFORMAT_PATH_DEFAULT;
197 }
91b13002 198
a1478cac 199again:
984263bc
MD
200 path = strdup(objformat_path);
201
6fb88001
SS
202 if (setenv("OBJFORMAT", objformat, 1) == -1)
203 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
984263bc 204
4806ab2e
MD
205 /*
206 * objformat_path could be sequence of colon-separated paths.
207 */
984263bc
MD
208 while ((chunk = strsep(&path, ":")) != NULL) {
209 if (newcmd != NULL) {
210 free(newcmd);
211 newcmd = NULL;
212 }
645f509d 213 if (use_objformat) {
b5cd2d99
SW
214 asprintf(&newcmd, "%s%s/%s/%s/%s",
215 chunk, base_path, env_value, objformat, cmd);
645f509d 216 } else {
a1478cac
SS
217 asprintf(&newcmd, "%s%s/%s/%s",
218 chunk, base_path, env_value, cmd);
645f509d 219 }
984263bc 220 if (newcmd == NULL)
4806ab2e 221 err(1, "cannot allocate memory");
984263bc
MD
222
223 argv[0] = newcmd;
224 execv(newcmd, argv);
225 }
a1478cac
SS
226
227 /*
228 * Fallback: if we're searching for a compiler, but didn't
229 * find any, try again using the custom compiler driver.
230 */
231 if (cmds && cmds->type == COMPILER &&
232 strcmp(env_value, "custom") != 0) {
233 env_value = "custom";
234 goto again;
235 }
236
645f509d 237 if (use_objformat) {
91b13002
JS
238 err(1, "in path [%s]%s/%s/%s/%s",
239 objformat_path, base_path, env_value, objformat, cmd);
645f509d 240 } else {
91b13002
JS
241 err(1, "in path [%s]%s/%s/%s",
242 objformat_path, base_path, env_value, cmd);
645f509d 243 }
984263bc 244}