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