ntdll: Copy floor() implementation from msvcrt.
[wine.git] / tools / winegcc / utils.c
blobf378a17976aa603a16cf68038e12af57e570ce2e
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 char buf[sizeof(res_sig)];
67 int fd, cnt;
69 fd = open( filename, O_RDONLY );
70 if (fd == -1) return file_na;
71 cnt = read(fd, buf, sizeof(buf));
72 close( fd );
73 if (cnt == -1) return file_na;
75 if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
76 if (strendswith(filename, ".o")) return file_obj;
77 if (strendswith(filename, ".obj")) return file_obj;
78 if (strendswith(filename, ".a")) return file_arh;
79 if (strendswith(filename, ".res")) return file_res;
80 if (strendswith(filename, ".so")) return file_so;
81 if (strendswith(filename, ".dylib")) return file_so;
82 if (strendswith(filename, ".def")) return file_def;
83 if (strendswith(filename, ".spec")) return file_spec;
84 if (strendswith(filename, ".rc")) return file_rc;
85 if (cnt >= sizeof(elf_sig) && !memcmp(buf, elf_sig, sizeof(elf_sig))) return file_so; /* ELF lib */
86 if (cnt >= sizeof(unsigned int) &&
87 (*(unsigned int *)buf == 0xfeedface || *(unsigned int *)buf == 0xcefaedfe ||
88 *(unsigned int *)buf == 0xfeedfacf || *(unsigned int *)buf == 0xcffaedfe))
89 return file_so; /* Mach-O lib */
91 return file_other;
94 static char* try_lib_path(const char* dir, const char* pre,
95 const char* library, const char* ext,
96 file_type expected_type)
98 char *fullname;
99 file_type type;
101 /* first try a subdir named from the library we are looking for */
102 fullname = strmake("%s/%s/%s%s%s", dir, library, pre, library, ext);
103 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
104 type = get_file_type(fullname);
105 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
106 if (type == expected_type) return fullname;
107 free( fullname );
109 fullname = strmake("%s/%s%s%s", dir, pre, library, ext);
110 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
111 type = get_file_type(fullname);
112 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
113 if (type == expected_type) return fullname;
114 free( fullname );
115 return 0;
118 static file_type guess_lib_type(enum target_platform platform, const char* dir,
119 const char* library, const char *prefix, const char *suffix, char** file)
121 if (platform != PLATFORM_WINDOWS && platform != PLATFORM_MINGW && platform != PLATFORM_CYGWIN)
123 /* Unix shared object */
124 if ((*file = try_lib_path(dir, prefix, library, ".so", file_so)))
125 return file_so;
127 /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
128 if ((*file = try_lib_path(dir, prefix, library, ".dylib", file_so)))
129 return file_so;
131 /* Windows DLL */
132 if ((*file = try_lib_path(dir, prefix, library, ".def", file_def)))
133 return file_dll;
136 /* static archives */
137 if ((*file = try_lib_path(dir, prefix, library, suffix, file_arh)))
138 return file_arh;
140 return file_na;
143 file_type get_lib_type(enum target_platform platform, struct strarray path, const char *library,
144 const char *prefix, const char *suffix, char** file)
146 unsigned int i;
148 if (!suffix) suffix = ".a";
149 for (i = 0; i < path.count; i++)
151 file_type type = guess_lib_type(platform, path.str[i], library, prefix, suffix, file);
152 if (type != file_na) return type;
154 return file_na;
157 const char *find_binary( struct strarray prefix, const char *name )
159 char *file_name, *args;
160 struct strarray dirs = empty_strarray;
161 static struct strarray path;
162 unsigned int i;
164 if (strchr( name, '/' )) return name;
166 file_name = xstrdup( name );
167 if ((args = strchr( file_name, ' ' ))) *args++ = 0;
169 if (!path.count) strarray_addall( &path, strarray_frompath( getenv( "PATH" )));
171 strarray_addall( &dirs, prefix );
172 strarray_addall( &dirs, path );
173 for (i = 0; i < dirs.count; i++)
175 struct stat st;
176 char *prog = strmake( "%s/%s%s", dirs.str[i], file_name, EXEEXT );
177 if (stat( prog, &st ) == 0 && S_ISREG( st.st_mode ) && (st.st_mode & 0111))
178 return args ? strmake( "%s %s", prog, args ) : prog;
179 free( prog );
181 return NULL;
184 int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
186 int status;
188 args.str[0] = find_binary( prefix, args.str[0] );
189 if (verbose) strarray_trace( args );
191 if ((status = strarray_spawn( args )) && !ignore_errors)
193 if (status > 0) error("%s failed\n", args.str[0]);
194 else perror("winegcc");
195 exit(3);
198 return status;