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