d3dx10: Add support for ID3DX10ThreadPump parameter in D3DX10CreateTextureFromMemory.
[wine.git] / tools / winegcc / utils.c
blob4b4498a59faca001480cc5cb516be7fcacf3c751
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* 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", dir, library, 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 if (target.platform != PLATFORM_WINDOWS &&
124 target.platform != PLATFORM_MINGW &&
125 target.platform != PLATFORM_CYGWIN)
127 /* Unix shared object */
128 if ((*file = try_lib_path(dir, prefix, library, ".so", file_so)))
129 return file_so;
131 /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
132 if ((*file = try_lib_path(dir, prefix, library, ".dylib", file_so)))
133 return file_so;
135 /* Windows DLL */
136 if ((*file = try_lib_path(dir, prefix, library, ".def", file_def)))
137 return file_dll;
140 /* static archives */
141 if ((*file = try_lib_path(dir, prefix, library, suffix, file_arh)))
142 return file_arh;
144 return file_na;
147 file_type get_lib_type(struct target target, struct strarray path, const char *library,
148 const char *prefix, const char *suffix, char** file)
150 unsigned int i;
152 if (!suffix) suffix = ".a";
153 for (i = 0; i < path.count; i++)
155 file_type type = guess_lib_type(target, path.str[i], library, prefix, suffix, file);
156 if (type != file_na) return type;
158 return file_na;
161 const char *find_binary( struct strarray prefix, const char *name )
163 char *file_name, *args;
164 struct strarray dirs = empty_strarray;
165 static struct strarray path;
166 unsigned int i;
168 if (strchr( name, '/' )) return name;
170 file_name = xstrdup( name );
171 if ((args = strchr( file_name, ' ' ))) *args++ = 0;
173 if (!path.count) strarray_addall( &path, strarray_frompath( getenv( "PATH" )));
175 strarray_addall( &dirs, prefix );
176 strarray_addall( &dirs, path );
177 for (i = 0; i < dirs.count; i++)
179 struct stat st;
180 char *prog = strmake( "%s/%s%s", dirs.str[i], file_name, EXEEXT );
181 if (stat( prog, &st ) == 0 && S_ISREG( st.st_mode ) && (st.st_mode & 0111))
182 return args ? strmake( "%s %s", prog, args ) : prog;
183 free( prog );
185 return NULL;
188 int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
190 int status;
192 args.str[0] = find_binary( prefix, args.str[0] );
193 if (verbose) strarray_trace( args );
195 if ((status = strarray_spawn( args )) && !ignore_errors)
197 if (status > 0) error("%s failed\n", args.str[0]);
198 else perror("winegcc");
199 exit(3);
202 return status;