Remove the remainder of Makefile.sub handling. Make objformat aware of the
[dragonfly.git] / usr.bin / objformat / objformat.c
1 /*-
2  * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $
27  * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.7 2004/01/29 01:39:41 dillon 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 #define OBJFORMAT       0
38 #define COMPILER        1
39 #define BINUTILS1       2
40 #define BINUTILS2       3
41
42 #define arysize(ary)    (sizeof(ary)/sizeof((ary)[0]))
43
44 struct command {
45         const char *cmd;
46         int type;
47 };
48
49 static struct command commands[] = {
50         {"CC",          COMPILER},
51         {"c++",         COMPILER},
52         {"c++filt",     COMPILER},
53         {"cc",          COMPILER},
54         {"cpp",         BINUTILS1},
55         {"f77",         BINUTILS1},
56         {"g++",         COMPILER},
57         {"gcc",         COMPILER},
58         {"gcov",        COMPILER},
59         {"addr2line",   BINUTILS2},
60         {"ar",          BINUTILS2},
61         {"as",          BINUTILS2},
62         {"gasp",        BINUTILS2},
63         {"gdb",         BINUTILS2},
64         {"ld",          BINUTILS2},
65         {"nm",          BINUTILS2},
66         {"objcopy",     BINUTILS2},
67         {"objdump",     BINUTILS2},
68         {"ranlib",      BINUTILS2},
69         {"size",        BINUTILS2},
70         {"strings",     BINUTILS2},
71         {"strip",       BINUTILS2},
72         {"objformat",   OBJFORMAT}
73 };
74
75 int
76 main(int argc, char **argv)
77 {
78         struct command *cmds;
79         char objformat[32];
80         char *path, *chunk;
81         char *cmd, *newcmd = NULL;
82         char *objformat_path;
83         char *ccver;
84         const char *env_name = NULL;
85         const char *env_value;
86         const char *env_default = NULL;
87         const char *base_path = NULL;
88         int use_objformat = 0;
89
90         if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
91                 errx(1, "Invalid object format");
92
93         /*
94          * Get the last path elemenet of the program name being executed
95          */
96         cmd = strrchr(argv[0], '/');
97         if (cmd != NULL)
98                 cmd++;
99         else
100                 cmd = argv[0];
101
102         for (cmds = commands; cmds < &commands[arysize(commands)]; ++cmds) {
103                 if (strcmp(cmd, cmds->cmd) == 0)
104                         break;
105         }
106         if (cmds) {
107                 switch (cmds->type) {
108                 case COMPILER:
109                         env_name = "CCVER";
110                         env_default = "gcc2";
111                         base_path = "/usr/bin";
112                         use_objformat = 0;
113                         break;
114                 case BINUTILS2:
115                         use_objformat = 1;
116                         /* fall through */
117                 case BINUTILS1:
118                         env_default = "binutils212";
119                         env_name = "BINUTILSVER";
120                         base_path = "/usr/libexec";
121                         if ((ccver = getenv("CCVER")) != NULL) {
122                             if (strcmp(ccver, "gcc3") == 0)
123                                 ;/* change binutils env_default here XXX */
124                             /* etc */
125                         }
126                         break;
127                 case OBJFORMAT:
128                         break;
129                 default:
130                         err(1, "unknown command type");
131                         break;
132                 }
133         }
134
135         /*
136          * The objformat command itself doesn't need another exec
137          */
138         if (cmds->type == OBJFORMAT) {
139                 if (argc != 1) {
140                         fprintf(stderr, "Usage: objformat\n");
141                         exit(1);
142                 }
143
144                 printf("%s\n", objformat);
145                 exit(0);
146         }
147
148         /*
149          * make buildworld glue and CCVER overrides.
150          */
151         objformat_path = getenv("OBJFORMAT_PATH");
152         if (objformat_path == NULL)
153                 objformat_path = "";
154
155         if ((env_value = getenv(env_name)) == NULL)
156                 env_value = env_default;
157
158         path = strdup(objformat_path);
159
160         setenv("OBJFORMAT", objformat, 1);
161
162         /*
163          * objformat_path could be sequence of colon-separated paths.
164          */
165         while ((chunk = strsep(&path, ":")) != NULL) {
166                 if (newcmd != NULL) {
167                         free(newcmd);
168                         newcmd = NULL;
169                 }
170                 if (use_objformat) {
171                         asprintf(&newcmd, "%s%s/%s/%s/%s",
172                                 chunk, base_path, env_value, objformat, cmd);
173                 } else {
174                         asprintf(&newcmd, "%s%s/%s/%s",
175                                 chunk, base_path, env_value, cmd);
176                 }
177                 if (newcmd == NULL)
178                         err(1, "cannot allocate memory");
179
180                 argv[0] = newcmd;
181                 execv(newcmd, argv);
182         }
183         if (use_objformat) {
184                 err(1, "in path [%s]%s/%s/%s/%s",
185                         objformat_path, base_path, env_value, objformat, cmd);
186         } else {
187                 err(1, "in path [%s]%s/%s/%s",
188                         objformat_path, base_path, env_value, cmd);
189         }
190 }
191