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