Set binutils 2.24 as primary, binutils 2.22 as alternate
[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
6100d1de 40#define CCVER_DEFAULT "gcc47"
990bfa79 41#endif
a1ea2019 42
990bfa79 43#ifndef BINUTILSVER_DEFAULT
e05895f1 44#define BINUTILSVER_DEFAULT "binutils224"
990bfa79
MD
45#endif
46
0784934a
JM
47#define LINKER_DEFAULT "ld.bfd"
48#define LINKER_GOLD "ld.gold"
49
3c0aa72e
SS
50#ifndef OBJFORMAT_PATH_DEFAULT
51#define OBJFORMAT_PATH_DEFAULT ""
52#endif
53
bf4591b3
SG
54/* Macro for array size */
55#ifndef NELEM
56#define NELEM(ary) (sizeof(ary) / sizeof((ary)[0]))
57#endif
58
0784934a 59enum cmd_type { OBJFORMAT, COMPILER, BINUTILS, LINKER };
91b13002
JS
60
61struct command {
62 const char *cmd;
04d63062 63 enum cmd_type type;
91b13002
JS
64};
65
66static struct command commands[] = {
2350540c
JM
67 {"CC", COMPILER},
68 {"c++", COMPILER},
69 {"cc", COMPILER},
70 {"cpp", COMPILER},
71 {"g++", COMPILER},
72 {"gcc", COMPILER},
73 {"gcov", COMPILER},
74 {"ld", LINKER},
75 {"addr2line", BINUTILS},
76 {"ar", BINUTILS},
77 {"as", BINUTILS},
78 {"c++filt", BINUTILS},
79 {"elfedit", BINUTILS},
80 {"gprof", BINUTILS},
81 {"nm", BINUTILS},
82 {"objcopy", BINUTILS},
83 {"objdump", BINUTILS},
84 {"ranlib", BINUTILS},
85 {"readelf", BINUTILS},
86 {"size", BINUTILS},
87 {"strings", BINUTILS},
88 {"strip", BINUTILS},
89 {"incremental-dump", BINUTILS},
90 {"objformat", OBJFORMAT},
91 {"", -1}
91b13002
JS
92};
93
984263bc
MD
94int
95main(int argc, char **argv)
96{
0784934a
JM
97 char ld_orig[] = LINKER_DEFAULT;
98 char ld_gold[] = LINKER_GOLD;
645f509d 99 struct command *cmds;
984263bc
MD
100 char objformat[32];
101 char *path, *chunk;
102 char *cmd, *newcmd = NULL;
0784934a 103 char *ldcmd = ld_orig;
ee3181ae
SW
104 const char *objformat_path;
105 const char *ccver;
106 const char *buver;
0784934a 107 const char *ldver;
5228fe46 108 const char *env_value = NULL;
91b13002
JS
109 const char *base_path = NULL;
110 int use_objformat = 0;
984263bc
MD
111
112 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
113 errx(1, "Invalid object format");
114
4806ab2e 115 /*
67ba35da 116 * Get the last path element of the program name being executed
4806ab2e 117 */
984263bc
MD
118 cmd = strrchr(argv[0], '/');
119 if (cmd != NULL)
120 cmd++;
121 else
122 cmd = argv[0];
123
04d63062 124 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
91b13002 125 if (strcmp(cmd, cmds->cmd) == 0)
645f509d
MD
126 break;
127 }
5228fe46 128
645f509d
MD
129 if (cmds) {
130 switch (cmds->type) {
131 case COMPILER:
2350540c
JM
132 ccver = getenv("CCVER");
133 if ((ccver == NULL) || ccver[0] == 0)
134 ccver = CCVER_DEFAULT;
17030342 135 base_path = "/usr/libexec";
645f509d 136 use_objformat = 0;
5228fe46 137 env_value = ccver;
645f509d 138 break;
04d63062 139 case BINUTILS:
2350540c
JM
140 buver = getenv("BINUTILSVER");
141 if (buver == NULL)
142 buver = BINUTILSVER_DEFAULT;
04d63062 143 base_path = "/usr/libexec";
645f509d 144 use_objformat = 1;
5228fe46 145 env_value = buver;
91b13002 146 break;
0784934a 147 case LINKER:
2350540c
JM
148 buver = getenv("BINUTILSVER");
149 if (buver == NULL)
150 buver = BINUTILSVER_DEFAULT;
f5e09b04 151 ldver = getenv("LDVER");
0784934a
JM
152 if ((ldver != NULL) && (strcmp(ldver, ld_gold) == 0))
153 ldcmd = ld_gold;
154 base_path = "/usr/libexec";
155 use_objformat = 1;
156 env_value = buver;
157 cmd = ldcmd;
158 break;
645f509d
MD
159 case OBJFORMAT:
160 break;
05e72d3a
SW
161 default:
162 errx(1, "unknown command type");
163 break;
91b13002 164 }
645f509d 165 }
4806ab2e
MD
166
167 /*
168 * The objformat command itself doesn't need another exec
169 */
91b13002 170 if (cmds->type == OBJFORMAT) {
984263bc
MD
171 if (argc != 1) {
172 fprintf(stderr, "Usage: objformat\n");
173 exit(1);
174 }
91b13002 175
984263bc
MD
176 printf("%s\n", objformat);
177 exit(0);
178 }
179
4806ab2e 180 /*
fce5f52e 181 * make buildworld glue and CCVER overrides.
4806ab2e 182 */
984263bc
MD
183 objformat_path = getenv("OBJFORMAT_PATH");
184 if (objformat_path == NULL)
3c0aa72e 185 objformat_path = OBJFORMAT_PATH_DEFAULT;
91b13002 186
a1478cac 187again:
984263bc
MD
188 path = strdup(objformat_path);
189
6fb88001
SS
190 if (setenv("OBJFORMAT", objformat, 1) == -1)
191 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
984263bc 192
4806ab2e
MD
193 /*
194 * objformat_path could be sequence of colon-separated paths.
195 */
984263bc
MD
196 while ((chunk = strsep(&path, ":")) != NULL) {
197 if (newcmd != NULL) {
198 free(newcmd);
199 newcmd = NULL;
200 }
645f509d 201 if (use_objformat) {
b5cd2d99
SW
202 asprintf(&newcmd, "%s%s/%s/%s/%s",
203 chunk, base_path, env_value, objformat, cmd);
645f509d 204 } else {
a1478cac
SS
205 asprintf(&newcmd, "%s%s/%s/%s",
206 chunk, base_path, env_value, cmd);
645f509d 207 }
984263bc 208 if (newcmd == NULL)
4806ab2e 209 err(1, "cannot allocate memory");
984263bc
MD
210
211 argv[0] = newcmd;
212 execv(newcmd, argv);
213 }
a1478cac
SS
214
215 /*
216 * Fallback: if we're searching for a compiler, but didn't
217 * find any, try again using the custom compiler driver.
218 */
219 if (cmds && cmds->type == COMPILER &&
220 strcmp(env_value, "custom") != 0) {
221 env_value = "custom";
222 goto again;
223 }
224
645f509d 225 if (use_objformat) {
91b13002
JS
226 err(1, "in path [%s]%s/%s/%s/%s",
227 objformat_path, base_path, env_value, objformat, cmd);
645f509d 228 } else {
91b13002
JS
229 err(1, "in path [%s]%s/%s/%s",
230 objformat_path, base_path, env_value, cmd);
645f509d 231 }
984263bc 232}