wpcap: Ensure unixlib function tables and enum stay in sync.
[wine.git] / tools / winegcc / utils.c
blobf79bfdf587795734e599ef5324556e58d3146639
1 /*
2 * Useful functions for winegcc
4 * Copyright 2000 Francois Gouget
5 * Copyright 2002 Dimitrie O. Paun
6 * Copyright 2003 Richard Cohen
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <errno.h>
31 #include "utils.h"
33 int verbose = 0;
35 void error(const char* s, ...)
37 va_list ap;
39 va_start(ap, s);
40 fprintf(stderr, "winegcc: ");
41 vfprintf(stderr, s, ap);
42 va_end(ap);
43 exit(2);
46 void create_file(const char* name, int mode, const char* fmt, ...)
48 va_list ap;
49 FILE *file;
51 if (verbose) printf("Creating file %s\n", name);
52 va_start(ap, fmt);
53 if ( !(file = fopen(name, "w")) )
54 error("Unable to open %s for writing\n", name);
55 vfprintf(file, fmt, ap);
56 va_end(ap);
57 fclose(file);
58 chmod(name, mode);
61 file_type get_file_type(const char* filename)
63 /* see tools/winebuild/res32.c: check_header for details */
64 static const char res_sig[] = { 0,0,0,0, 32,0,0,0, 0xff,0xff, 0,0, 0xff,0xff, 0,0, 0,0,0,0, 0,0, 0,0, 0,0,0,0, 0,0,0,0 };
65 static const char elf_sig[4] = "\177ELF";
66 static const char ar_sig[8] = "!<arch>\n";
67 char buf[sizeof(res_sig)];
68 int fd, cnt;
70 fd = open( filename, O_RDONLY );
71 if (fd == -1) return file_na;
72 cnt = read(fd, buf, sizeof(buf));
73 close( fd );
74 if (cnt == -1) return file_na;
76 if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
77 if (strendswith(filename, ".o")) return file_obj;
78 if (strendswith(filename, ".obj")) return file_obj;
79 if (strendswith(filename, ".a")) return file_arh;
80 if (strendswith(filename, ".res")) return file_res;
81 if (strendswith(filename, ".so")) return file_so;
82 if (strendswith(filename, ".dylib")) return file_so;
83 if (strendswith(filename, ".def")) return file_def;
84 if (strendswith(filename, ".spec")) return file_spec;
85 if (strendswith(filename, ".rc")) return file_rc;
86 if (cnt >= sizeof(elf_sig) && !memcmp(buf, elf_sig, sizeof(elf_sig))) return file_so; /* ELF lib */
87 if (cnt >= sizeof(ar_sig) && !memcmp(buf, ar_sig, sizeof(ar_sig))) return file_arh;
88 if (cnt >= sizeof(unsigned int) &&
89 (*(unsigned int *)buf == 0xfeedface || *(unsigned int *)buf == 0xcefaedfe ||
90 *(unsigned int *)buf == 0xfeedfacf || *(unsigned int *)buf == 0xcffaedfe))
91 return file_so; /* Mach-O lib */
93 return file_other;
96 static char* try_lib_path(const char *dir, const char *arch_dir, const char *pre,
97 const char* library, const char* ext,
98 file_type expected_type)
100 char *fullname;
101 file_type type;
103 /* first try a subdir named from the library we are looking for */
104 fullname = strmake("%s/%s%s/%s%s%s", dir, library, arch_dir, pre, library, ext);
105 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
106 type = get_file_type(fullname);
107 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
108 if (type == expected_type) return fullname;
109 free( fullname );
111 fullname = strmake("%s/%s%s%s", dir, pre, library, ext);
112 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
113 type = get_file_type(fullname);
114 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
115 if (type == expected_type) return fullname;
116 free( fullname );
117 return 0;
120 static file_type guess_lib_type(struct target target, const char* dir,
121 const char* library, const char *prefix, const char *suffix, char** file)
123 const char *arch_dir = "";
125 if (target.platform != PLATFORM_WINDOWS &&
126 target.platform != PLATFORM_MINGW &&
127 target.platform != PLATFORM_CYGWIN)
129 /* Unix shared object */
130 if ((*file = try_lib_path(dir, "", prefix, library, ".so", file_so)))
131 return file_so;
133 /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
134 if ((*file = try_lib_path(dir, "", prefix, library, ".dylib", file_so)))
135 return file_so;
137 /* Windows DLL */
138 if ((*file = try_lib_path(dir, "", prefix, library, ".def", file_def)))
139 return file_dll;
141 else
143 arch_dir = get_arch_dir( target );
144 if (!strcmp( suffix, ".a" )) /* try Mingw-style .dll.a import lib */
146 if ((*file = try_lib_path(dir, arch_dir, prefix, library, ".dll.a", file_arh)))
147 return file_arh;
151 /* static archives */
152 if ((*file = try_lib_path(dir, arch_dir, prefix, library, suffix, file_arh)))
153 return file_arh;
155 return file_na;
158 file_type get_lib_type(struct target target, struct strarray path, const char *library,
159 const char *prefix, const char *suffix, char** file)
161 unsigned int i;
163 if (!suffix) suffix = ".a";
164 for (i = 0; i < path.count; i++)
166 file_type type = guess_lib_type(target, path.str[i], library, prefix, suffix, file);
167 if (type != file_na) return type;
169 return file_na;
172 const char *find_binary( struct strarray prefix, const char *name )
174 char *file_name, *args;
175 struct strarray dirs = empty_strarray;
176 static struct strarray path;
177 unsigned int i;
179 if (strchr( name, '/' )) return name;
181 file_name = xstrdup( name );
182 if ((args = strchr( file_name, ' ' ))) *args++ = 0;
184 if (!path.count) strarray_addall( &path, strarray_frompath( getenv( "PATH" )));
186 strarray_addall( &dirs, prefix );
187 strarray_addall( &dirs, path );
188 for (i = 0; i < dirs.count; i++)
190 struct stat st;
191 char *prog = strmake( "%s/%s%s", dirs.str[i], file_name, EXEEXT );
192 if (stat( prog, &st ) == 0 && S_ISREG( st.st_mode ) && (st.st_mode & 0111))
193 return args ? strmake( "%s %s", prog, args ) : prog;
194 free( prog );
196 return NULL;
199 int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
201 int status;
203 args.str[0] = find_binary( prefix, args.str[0] );
204 if (verbose) strarray_trace( args );
206 if ((status = strarray_spawn( args )) && !ignore_errors)
208 if (status > 0) error("%s failed\n", args.str[0]);
209 else perror("winegcc");
210 exit(3);
213 return status;