usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / usr.bin / objformat / objformat.c
blob44e8e227d157a7487b18910b680187018cc04e64
1 /*-
2 * Copyright (c) 2004, The DragonFly Project. All rights reserved.
3 * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au>
4 * All rights reserved.
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.
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.
27 * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $
30 #include <sys/param.h>
32 #include <err.h>
33 #include <objformat.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #ifndef CCVER_DEFAULT
40 #define CCVER_DEFAULT "gcc80"
41 #endif
43 #ifndef BINUTILSVER_DEFAULT
44 #define BINUTILSVER_DEFAULT "binutils234"
45 #endif
47 #define LINKER_BFD "ld.bfd"
48 #define LINKER_GOLD "ld.gold"
49 #define LINKER_DEFAULT LINKER_GOLD
50 #define LINKER_ALT LINKER_BFD
52 #ifndef OBJFORMAT_PATH_DEFAULT
53 #define OBJFORMAT_PATH_DEFAULT ""
54 #endif
56 /* Macro for array size */
57 #ifndef NELEM
58 #define NELEM(ary) (sizeof(ary) / sizeof((ary)[0]))
59 #endif
61 enum cmd_type { OBJFORMAT, COMPILER, BINUTILS, LINKER };
63 struct command {
64 const char *cmd;
65 enum cmd_type type;
68 static struct command commands[] = {
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},
83 {"ld.bfd", BINUTILS},
84 {"ld.gold", BINUTILS},
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 {"objformat", OBJFORMAT},
94 {"", -1}
97 int
98 main(int argc, char **argv)
100 char ld_def[] = LINKER_DEFAULT;
101 char ld_alt[] = LINKER_ALT;
102 struct command *cmds;
103 char objformat[32];
104 char *path, *chunk;
105 char *cmd, *newcmd = NULL;
106 char *ldcmd = ld_def;
107 const char *objformat_path;
108 const char *ccver;
109 const char *buver;
110 const char *ldver;
111 const char *env_value = NULL;
112 const char *base_path = NULL;
113 int use_objformat = 0;
115 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
116 errx(1, "Invalid object format");
119 * Get the last path element of the program name being executed
121 cmd = strrchr(argv[0], '/');
122 if (cmd != NULL)
123 cmd++;
124 else
125 cmd = argv[0];
127 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
128 if (strcmp(cmd, cmds->cmd) == 0)
129 break;
132 if (cmds) {
133 switch (cmds->type) {
134 case COMPILER:
135 ccver = getenv("CCVER");
136 if ((ccver == NULL) || ccver[0] == 0)
137 ccver = CCVER_DEFAULT;
138 base_path = "/usr/libexec";
139 use_objformat = 0;
140 env_value = ccver;
141 break;
142 case BINUTILS:
143 buver = getenv("BINUTILSVER");
144 if (buver == NULL)
145 buver = BINUTILSVER_DEFAULT;
146 base_path = "/usr/libexec";
147 use_objformat = 1;
148 env_value = buver;
149 break;
150 case LINKER:
151 buver = getenv("BINUTILSVER");
152 if (buver == NULL)
153 buver = BINUTILSVER_DEFAULT;
154 ldver = getenv("LDVER");
155 if ((ldver != NULL) && (strcmp(ldver, ld_alt) == 0))
156 ldcmd = ld_alt;
157 base_path = "/usr/libexec";
158 use_objformat = 1;
159 env_value = buver;
160 cmd = ldcmd;
161 break;
162 case OBJFORMAT:
163 break;
164 default:
165 errx(1, "unknown command type");
166 break;
171 * The objformat command itself doesn't need another exec
173 if (cmds->type == OBJFORMAT) {
174 if (argc != 1) {
175 fprintf(stderr, "Usage: objformat\n");
176 exit(1);
179 printf("%s\n", objformat);
180 exit(0);
184 * make buildworld glue and CCVER overrides.
186 objformat_path = getenv("OBJFORMAT_PATH");
187 if (objformat_path == NULL)
188 objformat_path = OBJFORMAT_PATH_DEFAULT;
190 again:
191 path = strdup(objformat_path);
193 if (setenv("OBJFORMAT", objformat, 1) == -1)
194 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
197 * objformat_path could be sequence of colon-separated paths.
199 while ((chunk = strsep(&path, ":")) != NULL) {
200 if (newcmd != NULL) {
201 free(newcmd);
202 newcmd = NULL;
204 if (use_objformat) {
205 asprintf(&newcmd, "%s%s/%s/%s/%s",
206 chunk, base_path, env_value, objformat, cmd);
207 } else {
208 asprintf(&newcmd, "%s%s/%s/%s",
209 chunk, base_path, env_value, cmd);
211 if (newcmd == NULL)
212 err(1, "cannot allocate memory");
214 argv[0] = newcmd;
215 execv(newcmd, argv);
219 * Fallback: if we're searching for a compiler, but didn't
220 * find any, try again using the custom compiler driver.
222 if (cmds && cmds->type == COMPILER &&
223 strcmp(env_value, "custom") != 0) {
224 env_value = "custom";
225 goto again;
228 if (use_objformat) {
229 err(1, "in path [%s]%s/%s/%s/%s",
230 objformat_path, base_path, env_value, objformat, cmd);
231 } else {
232 err(1, "in path [%s]%s/%s/%s",
233 objformat_path, base_path, env_value, cmd);