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