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