objformat: Add LINKERVER environment variable recognition
[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 ((ccver = getenv("CCVER")) == NULL || ccver[0] == 0)
125                 ccver = CCVER_DEFAULT;
126         if ((buver = getenv("BINUTILSVER")) == NULL)
127                 buver = BINUTILSVER_DEFAULT;
128
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 BINUTILS:
137                         base_path = "/usr/libexec";
138                         use_objformat = 1;
139                         env_value = buver;
140                         break;
141                 case LINKER:
142                         ldver = getenv("LINKERVER");
143                         if ((ldver != NULL) && (strcmp(ldver, ld_gold) == 0))
144                             ldcmd = ld_gold;
145                         base_path = "/usr/libexec";
146                         use_objformat = 1;
147                         env_value = buver;
148                         cmd = ldcmd;
149                         break;
150                 case OBJFORMAT:
151                         break;
152                 default:
153                         errx(1, "unknown command type");
154                         break;
155                 }
156         }
157
158         /*
159          * The objformat command itself doesn't need another exec
160          */
161         if (cmds->type == OBJFORMAT) {
162                 if (argc != 1) {
163                         fprintf(stderr, "Usage: objformat\n");
164                         exit(1);
165                 }
166
167                 printf("%s\n", objformat);
168                 exit(0);
169         }
170
171         /*
172          * make buildworld glue and CCVER overrides.
173          */
174         objformat_path = getenv("OBJFORMAT_PATH");
175         if (objformat_path == NULL)
176                 objformat_path = OBJFORMAT_PATH_DEFAULT;
177
178 again:
179         path = strdup(objformat_path);
180
181         if (setenv("OBJFORMAT", objformat, 1) == -1)
182                 err(1, "setenv: cannot set OBJFORMAT=%s", objformat);
183
184         /*
185          * objformat_path could be sequence of colon-separated paths.
186          */
187         while ((chunk = strsep(&path, ":")) != NULL) {
188                 if (newcmd != NULL) {
189                         free(newcmd);
190                         newcmd = NULL;
191                 }
192                 if (use_objformat) {
193                         asprintf(&newcmd, "%s%s/%s/%s/%s",
194                                 chunk, base_path, env_value, objformat, cmd);
195                 } else {
196                         asprintf(&newcmd, "%s%s/%s/%s",
197                                 chunk, base_path, env_value, cmd);
198                 }
199                 if (newcmd == NULL)
200                         err(1, "cannot allocate memory");
201
202                 argv[0] = newcmd;
203                 execv(newcmd, argv);
204         }
205
206         /*
207          * Fallback:  if we're searching for a compiler, but didn't
208          * find any, try again using the custom compiler driver.
209          */
210         if (cmds && cmds->type == COMPILER &&
211             strcmp(env_value, "custom") != 0) {
212                 env_value = "custom";
213                 goto again;
214         }
215
216         if (use_objformat) {
217                 err(1, "in path [%s]%s/%s/%s/%s",
218                         objformat_path, base_path, env_value, objformat, cmd);
219         } else {
220                 err(1, "in path [%s]%s/%s/%s",
221                         objformat_path, base_path, env_value, cmd);
222         }
223 }