Use correct notification for drive selection change.
[wine/multimedia.git] / tools / winebuild / utils.c
blob6e6dc00bea60e249867f670b2f9a7c96d4e5ebb8
1 /*
2 * Small utility functions for winebuild
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "build.h"
36 #define MAX_TMP_FILES 8
37 static const char *tmp_files[MAX_TMP_FILES];
38 static unsigned int nb_tmp_files;
40 /* atexit handler to clean tmp files */
41 static void cleanup_tmp_files(void)
43 unsigned int i;
44 for (i = 0; i < MAX_TMP_FILES; i++) if (tmp_files[i]) unlink( tmp_files[i] );
48 void *xmalloc (size_t size)
50 void *res;
52 res = malloc (size ? size : 1);
53 if (res == NULL)
55 fprintf (stderr, "Virtual memory exhausted.\n");
56 exit (1);
58 return res;
61 void *xrealloc (void *ptr, size_t size)
63 void *res = realloc (ptr, size);
64 if (size && res == NULL)
66 fprintf (stderr, "Virtual memory exhausted.\n");
67 exit (1);
69 return res;
72 char *xstrdup( const char *str )
74 char *res = strdup( str );
75 if (!res)
77 fprintf (stderr, "Virtual memory exhausted.\n");
78 exit (1);
80 return res;
83 char *strupper(char *s)
85 char *p;
86 for (p = s; *p; p++) *p = toupper(*p);
87 return s;
90 int strendswith(const char* str, const char* end)
92 int l = strlen(str);
93 int m = strlen(end);
94 return l >= m && strcmp(str + l - m, end) == 0;
97 void fatal_error( const char *msg, ... )
99 va_list valist;
100 va_start( valist, msg );
101 if (input_file_name)
103 fprintf( stderr, "%s:", input_file_name );
104 if (current_line)
105 fprintf( stderr, "%d:", current_line );
106 fputc( ' ', stderr );
108 else fprintf( stderr, "winebuild: " );
109 vfprintf( stderr, msg, valist );
110 va_end( valist );
111 exit(1);
114 void fatal_perror( const char *msg, ... )
116 va_list valist;
117 va_start( valist, msg );
118 if (input_file_name)
120 fprintf( stderr, "%s:", input_file_name );
121 if (current_line)
122 fprintf( stderr, "%d:", current_line );
123 fputc( ' ', stderr );
125 vfprintf( stderr, msg, valist );
126 perror( " " );
127 va_end( valist );
128 exit(1);
131 void error( const char *msg, ... )
133 va_list valist;
134 va_start( valist, msg );
135 if (input_file_name)
137 fprintf( stderr, "%s:", input_file_name );
138 if (current_line)
139 fprintf( stderr, "%d:", current_line );
140 fputc( ' ', stderr );
142 vfprintf( stderr, msg, valist );
143 va_end( valist );
144 nb_errors++;
147 void warning( const char *msg, ... )
149 va_list valist;
151 if (!display_warnings) return;
152 va_start( valist, msg );
153 if (input_file_name)
155 fprintf( stderr, "%s:", input_file_name );
156 if (current_line)
157 fprintf( stderr, "%d:", current_line );
158 fputc( ' ', stderr );
160 fprintf( stderr, "warning: " );
161 vfprintf( stderr, msg, valist );
162 va_end( valist );
165 /* get a name for a temp file, automatically cleaned up on exit */
166 char *get_temp_file_name( const char *prefix, const char *suffix )
168 char *name;
169 const char *ext;
170 int fd;
172 assert( nb_tmp_files < MAX_TMP_FILES );
173 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
175 if (!prefix || !prefix[0]) prefix = "winebuild";
176 if (!suffix) suffix = "";
177 if (!(ext = strchr( prefix, '.' ))) ext = prefix + strlen(prefix);
178 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
179 strcpy( name, "/tmp/" );
180 memcpy( name + 5, prefix, ext - prefix );
181 strcpy( name + 5 + (ext - prefix), ".XXXXXX" );
182 strcat( name, suffix );
184 /* first try without the /tmp/ prefix */
185 if ((fd = mkstemps( name + 5, strlen(suffix) )) != -1)
186 name += 5;
187 else if ((fd = mkstemps( name, strlen(suffix) )) == -1)
188 fatal_error( "could not generate a temp file\n" );
190 close( fd );
191 tmp_files[nb_tmp_files++] = name;
192 return name;
195 /* output a standard header for generated files */
196 void output_standard_file_header( FILE *outfile )
198 if (spec_file_name)
199 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
200 spec_file_name );
201 else
202 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
203 fprintf( outfile,
204 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
207 /* dump a byte stream into the assembly code */
208 void dump_bytes( FILE *outfile, const void *buffer, unsigned int size )
210 unsigned int i;
211 const unsigned char *ptr = buffer;
213 if (!size) return;
214 fprintf( outfile, "\t.byte " );
215 for (i = 0; i < size - 1; i++, ptr++)
217 if ((i % 16) == 15) fprintf( outfile, "0x%02x\n\t.byte ", *ptr );
218 else fprintf( outfile, "0x%02x,", *ptr );
220 fprintf( outfile, "0x%02x\n", *ptr );
224 /*******************************************************************
225 * open_input_file
227 * Open a file in the given srcdir and set the input_file_name global variable.
229 FILE *open_input_file( const char *srcdir, const char *name )
231 char *fullname;
232 FILE *file = fopen( name, "r" );
234 if (!file && srcdir)
236 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
237 strcpy( fullname, srcdir );
238 strcat( fullname, "/" );
239 strcat( fullname, name );
240 file = fopen( fullname, "r" );
242 else fullname = xstrdup( name );
244 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
245 input_file_name = fullname;
246 current_line = 1;
247 return file;
251 /*******************************************************************
252 * close_input_file
254 * Close the current input file (must have been opened with open_input_file).
256 void close_input_file( FILE *file )
258 fclose( file );
259 free( input_file_name );
260 input_file_name = NULL;
261 current_line = 0;
265 /*******************************************************************
266 * remove_stdcall_decoration
268 * Remove a possible @xx suffix from a function name.
269 * Return the numerical value of the suffix, or -1 if none.
271 int remove_stdcall_decoration( char *name )
273 char *p, *end = strrchr( name, '@' );
274 if (!end || !end[1] || end == name) return -1;
275 /* make sure all the rest is digits */
276 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
277 *end = 0;
278 return atoi( end + 1 );
282 /*******************************************************************
283 * assemble_file
285 * Run a file through the assembler.
287 void assemble_file( const char *src_file, const char *obj_file )
289 char *cmd;
290 int err;
292 if (!as_command) as_command = xstrdup("as");
293 cmd = xmalloc( strlen(as_command) + strlen(obj_file) + strlen(src_file) + 6 );
294 sprintf( cmd, "%s -o %s %s", as_command, obj_file, src_file );
295 if (verbose) fprintf( stderr, "%s\n", cmd );
296 err = system( cmd );
297 if (err) fatal_error( "%s failed with status %d\n", as_command, err );
298 free( cmd );
302 /*******************************************************************
303 * alloc_dll_spec
305 * Create a new dll spec file descriptor
307 DLLSPEC *alloc_dll_spec(void)
309 DLLSPEC *spec;
311 spec = xmalloc( sizeof(*spec) );
312 spec->file_name = NULL;
313 spec->dll_name = NULL;
314 spec->init_func = NULL;
315 spec->type = SPEC_WIN32;
316 spec->base = MAX_ORDINALS;
317 spec->limit = 0;
318 spec->stack_size = 0;
319 spec->heap_size = 0;
320 spec->nb_entry_points = 0;
321 spec->alloc_entry_points = 0;
322 spec->nb_names = 0;
323 spec->nb_resources = 0;
324 spec->characteristics = 0;
325 spec->subsystem = 0;
326 spec->subsystem_major = 4;
327 spec->subsystem_minor = 0;
328 spec->entry_points = NULL;
329 spec->names = NULL;
330 spec->ordinals = NULL;
331 spec->resources = NULL;
332 return spec;
336 /*******************************************************************
337 * free_dll_spec
339 * Free dll spec file descriptor
341 void free_dll_spec( DLLSPEC *spec )
343 int i;
345 for (i = 0; i < spec->nb_entry_points; i++)
347 ORDDEF *odp = &spec->entry_points[i];
348 free( odp->name );
349 free( odp->export_name );
350 free( odp->link_name );
352 free( spec->file_name );
353 free( spec->dll_name );
354 free( spec->init_func );
355 free( spec->entry_points );
356 free( spec->names );
357 free( spec->ordinals );
358 free( spec->resources );
359 free( spec );
363 /*******************************************************************
364 * make_c_identifier
366 * Map a string to a valid C identifier.
368 const char *make_c_identifier( const char *str )
370 static char buffer[256];
371 char *p;
373 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
375 if (isalnum(*str)) *p = *str;
376 else *p = '_';
378 *p = 0;
379 return buffer;
383 /*******************************************************************
384 * get_stub_name
386 * Generate an internal name for a stub entry point.
388 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
390 static char buffer[256];
391 if (odp->name || odp->export_name)
393 char *p;
394 sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
395 /* make sure name is a legal C identifier */
396 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
397 if (!*p) return buffer;
399 sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
400 return buffer;
404 /*****************************************************************
405 * Function: get_alignment
407 * Description:
408 * According to the info page for gas, the .align directive behaves
409 * differently on different systems. On some architectures, the
410 * argument of a .align directive is the number of bytes to pad to, so
411 * to align on an 8-byte boundary you'd say
412 * .align 8
413 * On other systems, the argument is "the number of low-order zero bits
414 * that the location counter must have after advancement." So to
415 * align on an 8-byte boundary you'd say
416 * .align 3
418 * The reason gas is written this way is that it's trying to mimick
419 * native assemblers for the various architectures it runs on. gas
420 * provides other directives that work consistantly across
421 * architectures, but of course we want to work on all arches with or
422 * without gas. Hence this function.
425 * Parameters:
426 * align -- the number of bytes to align to. Must be a power of 2.
428 unsigned int get_alignment(unsigned int align)
430 unsigned int n;
432 assert( !(align & (align - 1)) );
434 switch(target_cpu)
436 case CPU_x86:
437 case CPU_x86_64:
438 case CPU_SPARC:
439 if (target_platform != PLATFORM_APPLE) return align;
440 /* fall through */
441 case CPU_POWERPC:
442 case CPU_ALPHA:
443 n = 0;
444 while ((1 << n) != align) n++;
445 return n;
447 /* unreached */
448 assert(0);
449 return 0;
452 /* return the page size for the target CPU */
453 unsigned int get_page_size(void)
455 switch(target_cpu)
457 case CPU_x86: return 4096;
458 case CPU_x86_64: return 4096;
459 case CPU_POWERPC: return 4096;
460 case CPU_SPARC: return 8192;
461 case CPU_ALPHA: return 8192;
463 /* unreached */
464 assert(0);
465 return 0;
468 /* return the size of a pointer on the target CPU */
469 unsigned int get_ptr_size(void)
471 switch(target_cpu)
473 case CPU_x86:
474 case CPU_POWERPC:
475 case CPU_SPARC:
476 case CPU_ALPHA:
477 return 4;
478 case CPU_x86_64:
479 return 8;
481 /* unreached */
482 assert(0);
483 return 0;
486 /* return the assembly name for a C symbol */
487 const char *asm_name( const char *sym )
489 static char buffer[256];
491 switch (target_platform)
493 case PLATFORM_APPLE:
494 case PLATFORM_WINDOWS:
495 buffer[0] = '_';
496 strcpy( buffer + 1, sym );
497 return buffer;
498 default:
499 return sym;
503 /* return an assembly function declaration for a C function name */
504 const char *func_declaration( const char *func )
506 static char buffer[256];
508 switch (target_platform)
510 case PLATFORM_APPLE:
511 return "";
512 case PLATFORM_WINDOWS:
513 sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
514 break;
515 case PLATFORM_SVR4:
516 sprintf( buffer, ".type %s,2", func );
517 break;
518 default:
519 sprintf( buffer, ".type %s,@function", func );
520 break;
522 return buffer;
525 /* output a size declaration for an assembly function */
526 void output_function_size( FILE *outfile, const char *name )
528 switch (target_platform)
530 case PLATFORM_APPLE:
531 case PLATFORM_WINDOWS:
532 break;
533 default:
534 fprintf( outfile, "\t.size %s, .-%s\n", name, name );
535 break;
539 /* return a global symbol declaration for an assembly symbol */
540 const char *asm_globl( const char *func )
542 static char buffer[256];
544 switch (target_platform)
546 case PLATFORM_APPLE:
547 sprintf( buffer, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
548 return buffer;
549 case PLATFORM_WINDOWS:
550 sprintf( buffer, "\t.globl _%s\n_%s:", func, func );
551 return buffer;
552 default:
553 sprintf( buffer, "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
554 return buffer;
558 const char *get_asm_ptr_keyword(void)
560 switch(get_ptr_size())
562 case 4: return ".long";
563 case 8: return ".quad";
565 assert(0);
566 return NULL;
569 const char *get_asm_string_keyword(void)
571 switch (target_platform)
573 case PLATFORM_APPLE:
574 case PLATFORM_SVR4:
575 return ".asciz";
576 default:
577 return ".string";
581 const char *get_asm_short_keyword(void)
583 switch (target_platform)
585 case PLATFORM_SVR4: return ".half";
586 default: return ".short";
590 const char *get_asm_string_section(void)
592 switch (target_platform)
594 case PLATFORM_APPLE: return ".cstring";
595 default: return ".section .rodata";