Remove NO_OPENSSH variable functionality
[dragonfly.git] / usr.bin / objformat / objformat.c
blob07869ef47fa03f7c10b7a6648f7a1875f7fe0457
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 "gcc50"
41 #endif
43 #ifndef BINUTILSVER_DEFAULT
44 #define BINUTILSVER_DEFAULT "binutils225"
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 {"nm", BINUTILS},
84 {"objcopy", BINUTILS},
85 {"objdump", BINUTILS},
86 {"ranlib", BINUTILS},
87 {"readelf", BINUTILS},
88 {"size", BINUTILS},
89 {"strings", BINUTILS},
90 {"strip", BINUTILS},
91 {"incremental-dump", BINUTILS},
92 {"objformat", OBJFORMAT},
93 {"", -1}
96 int
97 main(int argc, char **argv)
99 char ld_def[] = LINKER_DEFAULT;
100 char ld_alt[] = LINKER_ALT;
101 struct command *cmds;
102 char objformat[32];
103 char *path, *chunk;
104 char *cmd, *newcmd = NULL;
105 char *ldcmd = ld_def;
106 const char *objformat_path;
107 const char *ccver;
108 const char *buver;
109 const char *ldver;
110 const char *env_value = NULL;
111 const char *base_path = NULL;
112 int use_objformat = 0;
114 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
115 errx(1, "Invalid object format");
118 * Get the last path element of the program name being executed
120 cmd = strrchr(argv[0], '/');
121 if (cmd != NULL)
122 cmd++;
123 else
124 cmd = argv[0];
126 for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
127 if (strcmp(cmd, cmds->cmd) == 0)
128 break;
131 if (cmds) {
132 switch (cmds->type) {
133 case COMPILER:
134 ccver = getenv("CCVER");
135 if ((ccver == NULL) || ccver[0] == 0)
136 ccver = CCVER_DEFAULT;
137 base_path = "/usr/libexec";
138 use_objformat = 0;
139 env_value = ccver;
140 break;
141 case BINUTILS:
142 buver = getenv("BINUTILSVER");
143 if (buver == NULL)
144 buver = BINUTILSVER_DEFAULT;
145 base_path = "/usr/libexec";
146 use_objformat = 1;
147 env_value = buver;
148 break;
149 case LINKER:
150 buver = getenv("BINUTILSVER");
151 if (buver == NULL)
152 buver = BINUTILSVER_DEFAULT;
153 ldver = getenv("LDVER");
154 if ((ldver != NULL) && (strcmp(ldver, ld_alt) == 0))
155 ldcmd = ld_alt;
156 base_path = "/usr/libexec";
157 use_objformat = 1;
158 env_value = buver;
159 cmd = ldcmd;
160 break;
161 case OBJFORMAT:
162 break;
163 default:
164 errx(1, "unknown command type");
165 break;
170 * The objformat command itself doesn't need another exec
172 if (cmds->type == OBJFORMAT) {
173 if (argc != 1) {
174 fprintf(stderr, "Usage: objformat\n");
175 exit(1);
178 printf("%s\n", objformat);
179 exit(0);
183 * make buildworld glue and CCVER overrides.
186 * CCVER=clang check temporary; only for base clang import work
188 if (cmds && cmds->type == COMPILER &&
189 strcmp (env_value, "clang") == 0) {
190 objformat_path = "";
191 } else {
192 objformat_path = getenv("OBJFORMAT_PATH");
193 if (objformat_path == NULL)
194 objformat_path = OBJFORMAT_PATH_DEFAULT;
197 again:
198 path = strdup(objformat_path);
200 if (setenv("OBJFORMAT", objformat, 1) == -1)
201 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
204 * objformat_path could be sequence of colon-separated paths.
206 while ((chunk = strsep(&path, ":")) != NULL) {
207 if (newcmd != NULL) {
208 free(newcmd);
209 newcmd = NULL;
211 if (use_objformat) {
212 asprintf(&newcmd, "%s%s/%s/%s/%s",
213 chunk, base_path, env_value, objformat, cmd);
214 } else {
215 asprintf(&newcmd, "%s%s/%s/%s",
216 chunk, base_path, env_value, cmd);
218 if (newcmd == NULL)
219 err(1, "cannot allocate memory");
221 argv[0] = newcmd;
222 execv(newcmd, argv);
226 * Fallback: if we're searching for a compiler, but didn't
227 * find any, try again using the custom compiler driver.
229 if (cmds && cmds->type == COMPILER &&
230 strcmp(env_value, "custom") != 0) {
231 env_value = "custom";
232 goto again;
235 if (use_objformat) {
236 err(1, "in path [%s]%s/%s/%s/%s",
237 objformat_path, base_path, env_value, objformat, cmd);
238 } else {
239 err(1, "in path [%s]%s/%s/%s",
240 objformat_path, base_path, env_value, cmd);