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