xref: /dflybsd-src/usr.bin/objformat/objformat.c (revision 770899e9c633f5aeaebae171f27676258a386117)
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  * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.22 2007/08/09 22:15:22 corecode Exp $
29  */
30 
31 #include <err.h>
32 #include <objformat.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #ifndef CCVER_DEFAULT
39 #define CCVER_DEFAULT "gcc41"
40 #endif
41 
42 #ifndef BINUTILSVER_DEFAULT
43 #define	BINUTILSVER_DEFAULT "binutils217"
44 #endif
45 
46 #define OBJFORMAT	0
47 #define COMPILER	1
48 #define BINUTILS1	2
49 #define BINUTILS2	3
50 
51 #define arysize(ary)	(sizeof(ary)/sizeof((ary)[0]))
52 
53 struct command {
54 	const char *cmd;
55 	int type;
56 };
57 
58 static struct command commands[] = {
59 	{"CC",		COMPILER},
60 	{"c++",		COMPILER},
61 	{"c++filt",	COMPILER},
62 	{"cc",		COMPILER},
63 	{"cpp",		COMPILER},
64 	{"f77",		COMPILER},
65 	{"g77",		COMPILER},
66 	{"g++",		COMPILER},
67 	{"gcc",		COMPILER},
68 	{"gcov",	COMPILER},
69 	{"addr2line",	BINUTILS2},
70 	{"ar",		BINUTILS2},
71 	{"as",		BINUTILS2},
72 	{"gasp",	BINUTILS2},
73 	{"gdb",		BINUTILS2},
74 	{"ld",		BINUTILS2},
75 	{"nm",		BINUTILS2},
76 	{"objcopy",	BINUTILS2},
77 	{"objdump",	BINUTILS2},
78 	{"ranlib",	BINUTILS2},
79 	{"size",	BINUTILS2},
80 	{"strings",	BINUTILS2},
81 	{"strip",	BINUTILS2},
82 	{"objformat",	OBJFORMAT}
83 };
84 
85 int
86 main(int argc, char **argv)
87 {
88 	struct command *cmds;
89 	char objformat[32];
90 	char *path, *chunk;
91 	char *cmd, *newcmd = NULL;
92 	const char *objformat_path;
93 	const char *ccver;
94 	const char *buver;
95 	const char *env_value = NULL;
96 	const char *base_path = NULL;
97 	int use_objformat = 0;
98 
99 	if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
100 		errx(1, "Invalid object format");
101 
102 	/*
103 	 * Get the last path elemenet of the program name being executed
104 	 */
105 	cmd = strrchr(argv[0], '/');
106 	if (cmd != NULL)
107 		cmd++;
108 	else
109 		cmd = argv[0];
110 
111 	for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) {
112 		if (strcmp(cmd, cmds->cmd) == 0)
113 			break;
114 	}
115 
116 	if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
117 		ccver = CCVER_DEFAULT;
118 	if ((buver = getenv("BINUTILSVER")) == NULL) {
119 		buver = BINUTILSVER_DEFAULT;
120 	}
121 
122 	if (cmds) {
123 		switch (cmds->type) {
124 		case COMPILER:
125 			base_path = "/usr/libexec";
126 			use_objformat = 0;
127 			env_value = ccver;
128 			break;
129 		case BINUTILS2:
130 			use_objformat = 1;
131 			/* fall through */
132 		case BINUTILS1:
133 			env_value = buver;
134 			base_path = "/usr/libexec";
135 			break;
136 		case OBJFORMAT:
137 			break;
138 		default:
139 			err(1, "unknown command type");
140 			break;
141 		}
142 	}
143 
144 	/*
145 	 * The objformat command itself doesn't need another exec
146 	 */
147 	if (cmds->type == OBJFORMAT) {
148 		if (argc != 1) {
149 			fprintf(stderr, "Usage: objformat\n");
150 			exit(1);
151 		}
152 
153 		printf("%s\n", objformat);
154 		exit(0);
155 	}
156 
157 	/*
158 	 * make buildworld glue and CCVER overrides.
159 	 */
160 	objformat_path = getenv("OBJFORMAT_PATH");
161 	if (objformat_path == NULL)
162 		objformat_path = "";
163 
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 	if (use_objformat) {
191 		err(1, "in path [%s]%s/%s/%s/%s",
192 			objformat_path, base_path, env_value, objformat, cmd);
193 	} else {
194 		err(1, "in path [%s]%s/%s/%s",
195 			objformat_path, base_path, env_value, cmd);
196 	}
197 }
198 
199