xref: /dragonfly/usr.bin/objformat/objformat.c (revision fb151170)
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.
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 
30 #include <sys/param.h>
31 
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 
39 #ifndef CCVER_DEFAULT
40 #define CCVER_DEFAULT "gcc44"
41 #endif
42 
43 #ifndef BINUTILSVER_DEFAULT
44 #define	BINUTILSVER_DEFAULT "binutils222"
45 #endif
46 
47 #ifndef OBJFORMAT_PATH_DEFAULT
48 #define OBJFORMAT_PATH_DEFAULT ""
49 #endif
50 
51 enum cmd_type { OBJFORMAT, COMPILER, BINUTILS };
52 
53 struct command {
54 	const char *cmd;
55 	enum cmd_type type;
56 };
57 
58 static struct command commands[] = {
59 	{"CC",		COMPILER},
60 	{"c++",		COMPILER},
61 	{"cc",		COMPILER},
62 	{"cpp",		COMPILER},
63 	{"g++",		COMPILER},
64 	{"gcc",		COMPILER},
65 	{"gcov",	COMPILER},
66 	{"addr2line",	BINUTILS},
67 	{"ar",		BINUTILS},
68 	{"as",		BINUTILS},
69 	{"c++filt",	BINUTILS},
70 	{"elfedit",	BINUTILS},
71 	{"gprof",       BINUTILS},
72 	{"ld",		BINUTILS},
73 	{"nm",		BINUTILS},
74 	{"objcopy",	BINUTILS},
75 	{"objdump",	BINUTILS},
76 	{"ranlib",	BINUTILS},
77 	{"readelf",	BINUTILS},
78 	{"size",	BINUTILS},
79 	{"strings",	BINUTILS},
80 	{"strip",	BINUTILS},
81 	{"incremental-dump", BINUTILS},
82 	{"objformat",	OBJFORMAT},
83 	{"",		-1}
84 };
85 
86 int
87 main(int argc, char **argv)
88 {
89 	struct command *cmds;
90 	char objformat[32];
91 	char *path, *chunk;
92 	char *cmd, *newcmd = NULL;
93 	const char *objformat_path;
94 	const char *ccver;
95 	const char *buver;
96 	const char *env_value = NULL;
97 	const char *base_path = NULL;
98 	int use_objformat = 0;
99 
100 	if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
101 		errx(1, "Invalid object format");
102 
103 	/*
104 	 * Get the last path element of the program name being executed
105 	 */
106 	cmd = strrchr(argv[0], '/');
107 	if (cmd != NULL)
108 		cmd++;
109 	else
110 		cmd = argv[0];
111 
112 	for (cmds = commands; cmds < &commands[NELEM(commands) - 1]; ++cmds) {
113 		if (strcmp(cmd, cmds->cmd) == 0)
114 			break;
115 	}
116 
117 	if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
118 		ccver = CCVER_DEFAULT;
119 	if ((buver = getenv("BINUTILSVER")) == NULL) {
120 		buver = BINUTILSVER_DEFAULT;
121 	}
122 
123 	if (cmds) {
124 		switch (cmds->type) {
125 		case COMPILER:
126 			base_path = "/usr/libexec";
127 			use_objformat = 0;
128 			env_value = ccver;
129 			break;
130 		case BINUTILS:
131 			base_path = "/usr/libexec";
132 			use_objformat = 1;
133 			env_value = buver;
134 			break;
135 		case OBJFORMAT:
136 			break;
137 		default:
138 			errx(1, "unknown command type");
139 			break;
140 		}
141 	}
142 
143 	/*
144 	 * The objformat command itself doesn't need another exec
145 	 */
146 	if (cmds->type == OBJFORMAT) {
147 		if (argc != 1) {
148 			fprintf(stderr, "Usage: objformat\n");
149 			exit(1);
150 		}
151 
152 		printf("%s\n", objformat);
153 		exit(0);
154 	}
155 
156 	/*
157 	 * make buildworld glue and CCVER overrides.
158 	 */
159 	objformat_path = getenv("OBJFORMAT_PATH");
160 	if (objformat_path == NULL)
161 		objformat_path = OBJFORMAT_PATH_DEFAULT;
162 
163 again:
164 	path = strdup(objformat_path);
165 
166 	if (setenv("OBJFORMAT", objformat, 1) == -1)
167 		err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
168 
169 	/*
170 	 * objformat_path could be sequence of colon-separated paths.
171 	 */
172 	while ((chunk = strsep(&path, ":")) != NULL) {
173 		if (newcmd != NULL) {
174 			free(newcmd);
175 			newcmd = NULL;
176 		}
177 		if (use_objformat) {
178 			asprintf(&newcmd, "%s%s/%s/%s/%s",
179 				chunk, base_path, env_value, objformat, cmd);
180 		} else {
181 			asprintf(&newcmd, "%s%s/%s/%s",
182 				chunk, base_path, env_value, cmd);
183 		}
184 		if (newcmd == NULL)
185 			err(1, "cannot allocate memory");
186 
187 		argv[0] = newcmd;
188 		execv(newcmd, argv);
189 	}
190 
191 	/*
192 	 * Fallback:  if we're searching for a compiler, but didn't
193 	 * find any, try again using the custom compiler driver.
194 	 */
195 	if (cmds && cmds->type == COMPILER &&
196 	    strcmp(env_value, "custom") != 0) {
197 		env_value = "custom";
198 		goto again;
199 	}
200 
201 	if (use_objformat) {
202 		err(1, "in path [%s]%s/%s/%s/%s",
203 			objformat_path, base_path, env_value, objformat, cmd);
204 	} else {
205 		err(1, "in path [%s]%s/%s/%s",
206 			objformat_path, base_path, env_value, cmd);
207 	}
208 }
209