Sync with FreeBSD:
[dragonfly/port-amd64.git] / usr.bin / objformat / objformat.c
blob6c615c25aa06cad707cd25a45a9aa42ea498dd11
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 $
28 * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.19 2006/01/12 13:43:11 corecode Exp $
31 #include <err.h>
32 #include <objformat.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #ifndef CCVER_DEFAULT
39 #define CCVER_DEFAULT "gcc34"
40 #endif
42 #ifndef BINUTILSVER_DEFAULT
43 #define BINUTILSVER_DEFAULT "binutils215"
44 #endif
45 #define BINUTILSVER_GCC2 "binutils212"
46 #define BINUTILSVER_GCC34 "binutils215"
48 #define OBJFORMAT 0
49 #define COMPILER 1
50 #define BINUTILS1 2
51 #define BINUTILS2 3
53 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
55 struct command {
56 const char *cmd;
57 int type;
60 static struct command commands[] = {
61 {"CC", COMPILER},
62 {"c++", COMPILER},
63 {"c++filt", COMPILER},
64 {"cc", COMPILER},
65 {"cpp", COMPILER},
66 {"f77", COMPILER},
67 {"g77", COMPILER},
68 {"g++", COMPILER},
69 {"gcc", COMPILER},
70 {"gcov", COMPILER},
71 {"addr2line", BINUTILS2},
72 {"ar", BINUTILS2},
73 {"as", BINUTILS2},
74 {"gasp", BINUTILS2},
75 {"gdb", BINUTILS2},
76 {"ld", BINUTILS2},
77 {"nm", BINUTILS2},
78 {"objcopy", BINUTILS2},
79 {"objdump", BINUTILS2},
80 {"ranlib", BINUTILS2},
81 {"size", BINUTILS2},
82 {"strings", BINUTILS2},
83 {"strip", BINUTILS2},
84 {"objformat", OBJFORMAT}
87 int
88 main(int argc, char **argv)
90 struct command *cmds;
91 char objformat[32];
92 char *path, *chunk;
93 char *cmd, *newcmd = NULL;
94 char *objformat_path;
95 char *ccver;
96 char *buver;
97 const char *env_value = NULL;
98 const char *base_path = NULL;
99 int use_objformat = 0;
101 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
102 errx(1, "Invalid object format");
105 * Get the last path elemenet of the program name being executed
107 cmd = strrchr(argv[0], '/');
108 if (cmd != NULL)
109 cmd++;
110 else
111 cmd = argv[0];
113 for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) {
114 if (strcmp(cmd, cmds->cmd) == 0)
115 break;
118 if ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
119 ccver = CCVER_DEFAULT;
120 if ((buver = getenv("BINUTILSVER")) == NULL) {
121 if (strcmp(ccver, "gcc2") == 0)
122 buver = BINUTILSVER_GCC2;
123 else if (strcmp(ccver, "gcc34") == 0)
124 buver = BINUTILSVER_GCC34;
125 else
126 buver = BINUTILSVER_DEFAULT; /* fall through */
129 if (cmds) {
130 switch (cmds->type) {
131 case COMPILER:
132 base_path = "/usr/libexec";
133 use_objformat = 0;
134 env_value = ccver;
135 break;
136 case BINUTILS2:
137 use_objformat = 1;
138 /* fall through */
139 case BINUTILS1:
140 env_value = buver;
141 base_path = "/usr/libexec";
142 break;
143 case OBJFORMAT:
144 break;
145 default:
146 err(1, "unknown command type");
147 break;
152 * The objformat command itself doesn't need another exec
154 if (cmds->type == OBJFORMAT) {
155 if (argc != 1) {
156 fprintf(stderr, "Usage: objformat\n");
157 exit(1);
160 printf("%s\n", objformat);
161 exit(0);
165 * make buildworld glue and CCVER overrides.
167 objformat_path = getenv("OBJFORMAT_PATH");
168 if (objformat_path == NULL)
169 objformat_path = "";
171 path = strdup(objformat_path);
173 if (setenv("OBJFORMAT", objformat, 1) == -1)
174 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
177 * objformat_path could be sequence of colon-separated paths.
179 while ((chunk = strsep(&path, ":")) != NULL) {
180 if (newcmd != NULL) {
181 free(newcmd);
182 newcmd = NULL;
184 if (use_objformat) {
185 asprintf(&newcmd, "%s%s/%s/%s/%s",
186 chunk, base_path, env_value, objformat, cmd);
187 } else {
188 asprintf(&newcmd, "%s%s/%s/%s",
189 chunk, base_path, env_value, cmd);
191 if (newcmd == NULL)
192 err(1, "cannot allocate memory");
194 argv[0] = newcmd;
195 execv(newcmd, argv);
197 if (use_objformat) {
198 err(1, "in path [%s]%s/%s/%s/%s",
199 objformat_path, base_path, env_value, objformat, cmd);
200 } else {
201 err(1, "in path [%s]%s/%s/%s",
202 objformat_path, base_path, env_value, cmd);