Release 1.1.14.
[wine/gsoc-2012-control.git] / tools / winebuild / utils.c
blob39162fa6fa9983e645a2043e80eb2c17d53ab8e9
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "windef.h"
35 #include "winnt.h"
36 #include "build.h"
38 #define MAX_TMP_FILES 8
39 static const char *tmp_files[MAX_TMP_FILES];
40 static unsigned int nb_tmp_files;
42 static const struct
44 const char *name;
45 enum target_cpu cpu;
46 } cpu_names[] =
48 { "i386", CPU_x86 },
49 { "i486", CPU_x86 },
50 { "i586", CPU_x86 },
51 { "i686", CPU_x86 },
52 { "i786", CPU_x86 },
53 { "x86_64", CPU_x86_64 },
54 { "sparc", CPU_SPARC },
55 { "alpha", CPU_ALPHA },
56 { "powerpc", CPU_POWERPC }
59 /* atexit handler to clean tmp files */
60 static void cleanup_tmp_files(void)
62 unsigned int i;
63 for (i = 0; i < MAX_TMP_FILES; i++) if (tmp_files[i]) unlink( tmp_files[i] );
67 void *xmalloc (size_t size)
69 void *res;
71 res = malloc (size ? size : 1);
72 if (res == NULL)
74 fprintf (stderr, "Virtual memory exhausted.\n");
75 exit (1);
77 return res;
80 void *xrealloc (void *ptr, size_t size)
82 void *res = realloc (ptr, size);
83 if (size && res == NULL)
85 fprintf (stderr, "Virtual memory exhausted.\n");
86 exit (1);
88 return res;
91 char *xstrdup( const char *str )
93 char *res = strdup( str );
94 if (!res)
96 fprintf (stderr, "Virtual memory exhausted.\n");
97 exit (1);
99 return res;
102 char *strupper(char *s)
104 char *p;
105 for (p = s; *p; p++) *p = toupper(*p);
106 return s;
109 int strendswith(const char* str, const char* end)
111 int l = strlen(str);
112 int m = strlen(end);
113 return l >= m && strcmp(str + l - m, end) == 0;
116 void fatal_error( const char *msg, ... )
118 va_list valist;
119 va_start( valist, msg );
120 if (input_file_name)
122 fprintf( stderr, "%s:", input_file_name );
123 if (current_line)
124 fprintf( stderr, "%d:", current_line );
125 fputc( ' ', stderr );
127 else fprintf( stderr, "winebuild: " );
128 vfprintf( stderr, msg, valist );
129 va_end( valist );
130 exit(1);
133 void fatal_perror( const char *msg, ... )
135 va_list valist;
136 va_start( valist, msg );
137 if (input_file_name)
139 fprintf( stderr, "%s:", input_file_name );
140 if (current_line)
141 fprintf( stderr, "%d:", current_line );
142 fputc( ' ', stderr );
144 vfprintf( stderr, msg, valist );
145 perror( " " );
146 va_end( valist );
147 exit(1);
150 void error( const char *msg, ... )
152 va_list valist;
153 va_start( valist, msg );
154 if (input_file_name)
156 fprintf( stderr, "%s:", input_file_name );
157 if (current_line)
158 fprintf( stderr, "%d:", current_line );
159 fputc( ' ', stderr );
161 vfprintf( stderr, msg, valist );
162 va_end( valist );
163 nb_errors++;
166 void warning( const char *msg, ... )
168 va_list valist;
170 if (!display_warnings) return;
171 va_start( valist, msg );
172 if (input_file_name)
174 fprintf( stderr, "%s:", input_file_name );
175 if (current_line)
176 fprintf( stderr, "%d:", current_line );
177 fputc( ' ', stderr );
179 fprintf( stderr, "warning: " );
180 vfprintf( stderr, msg, valist );
181 va_end( valist );
184 int output( const char *format, ... )
186 int ret;
187 va_list valist;
189 va_start( valist, format );
190 ret = vfprintf( output_file, format, valist );
191 va_end( valist );
192 if (ret < 0) fatal_perror( "Output error" );
193 return ret;
196 /* get a name for a temp file, automatically cleaned up on exit */
197 char *get_temp_file_name( const char *prefix, const char *suffix )
199 char *name;
200 const char *ext;
201 int fd;
203 assert( nb_tmp_files < MAX_TMP_FILES );
204 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
206 if (!prefix || !prefix[0]) prefix = "winebuild";
207 if (!suffix) suffix = "";
208 if (!(ext = strchr( prefix, '.' ))) ext = prefix + strlen(prefix);
209 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
210 strcpy( name, "/tmp/" );
211 memcpy( name + 5, prefix, ext - prefix );
212 strcpy( name + 5 + (ext - prefix), ".XXXXXX" );
213 strcat( name, suffix );
215 /* first try without the /tmp/ prefix */
216 if ((fd = mkstemps( name + 5, strlen(suffix) )) != -1)
217 name += 5;
218 else if ((fd = mkstemps( name, strlen(suffix) )) == -1)
219 fatal_error( "could not generate a temp file\n" );
221 close( fd );
222 tmp_files[nb_tmp_files++] = name;
223 return name;
226 /* output a standard header for generated files */
227 void output_standard_file_header(void)
229 if (spec_file_name)
230 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
231 else
232 output( "/* File generated automatically; do not edit! */\n" );
233 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
236 /* dump a byte stream into the assembly code */
237 void dump_bytes( const void *buffer, unsigned int size )
239 unsigned int i;
240 const unsigned char *ptr = buffer;
242 if (!size) return;
243 output( "\t.byte " );
244 for (i = 0; i < size - 1; i++, ptr++)
246 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
247 else output( "0x%02x,", *ptr );
249 output( "0x%02x\n", *ptr );
253 /*******************************************************************
254 * open_input_file
256 * Open a file in the given srcdir and set the input_file_name global variable.
258 FILE *open_input_file( const char *srcdir, const char *name )
260 char *fullname;
261 FILE *file = fopen( name, "r" );
263 if (!file && srcdir)
265 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
266 strcpy( fullname, srcdir );
267 strcat( fullname, "/" );
268 strcat( fullname, name );
269 file = fopen( fullname, "r" );
271 else fullname = xstrdup( name );
273 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
274 input_file_name = fullname;
275 current_line = 1;
276 return file;
280 /*******************************************************************
281 * close_input_file
283 * Close the current input file (must have been opened with open_input_file).
285 void close_input_file( FILE *file )
287 fclose( file );
288 free( input_file_name );
289 input_file_name = NULL;
290 current_line = 0;
294 /*******************************************************************
295 * remove_stdcall_decoration
297 * Remove a possible @xx suffix from a function name.
298 * Return the numerical value of the suffix, or -1 if none.
300 int remove_stdcall_decoration( char *name )
302 char *p, *end = strrchr( name, '@' );
303 if (!end || !end[1] || end == name) return -1;
304 /* make sure all the rest is digits */
305 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
306 *end = 0;
307 return atoi( end + 1 );
311 /*******************************************************************
312 * assemble_file
314 * Run a file through the assembler.
316 void assemble_file( const char *src_file, const char *obj_file )
318 char *cmd;
319 int err;
321 if (!as_command) as_command = xstrdup("as");
322 cmd = xmalloc( strlen(as_command) + strlen(obj_file) + strlen(src_file) + 6 );
323 sprintf( cmd, "%s -o %s %s", as_command, obj_file, src_file );
324 if (verbose) fprintf( stderr, "%s\n", cmd );
325 err = system( cmd );
326 if (err) fatal_error( "%s failed with status %d\n", as_command, err );
327 free( cmd );
331 /*******************************************************************
332 * alloc_dll_spec
334 * Create a new dll spec file descriptor
336 DLLSPEC *alloc_dll_spec(void)
338 DLLSPEC *spec;
340 spec = xmalloc( sizeof(*spec) );
341 spec->file_name = NULL;
342 spec->dll_name = NULL;
343 spec->init_func = NULL;
344 spec->type = SPEC_WIN32;
345 spec->base = MAX_ORDINALS;
346 spec->limit = 0;
347 spec->stack_size = 0;
348 spec->heap_size = 0;
349 spec->nb_entry_points = 0;
350 spec->alloc_entry_points = 0;
351 spec->nb_names = 0;
352 spec->nb_resources = 0;
353 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
354 if (get_ptr_size() > 4)
355 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
356 else
357 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
358 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
359 spec->subsystem = 0;
360 spec->subsystem_major = 4;
361 spec->subsystem_minor = 0;
362 spec->entry_points = NULL;
363 spec->names = NULL;
364 spec->ordinals = NULL;
365 spec->resources = NULL;
366 return spec;
370 /*******************************************************************
371 * free_dll_spec
373 * Free dll spec file descriptor
375 void free_dll_spec( DLLSPEC *spec )
377 int i;
379 for (i = 0; i < spec->nb_entry_points; i++)
381 ORDDEF *odp = &spec->entry_points[i];
382 free( odp->name );
383 free( odp->export_name );
384 free( odp->link_name );
386 free( spec->file_name );
387 free( spec->dll_name );
388 free( spec->init_func );
389 free( spec->entry_points );
390 free( spec->names );
391 free( spec->ordinals );
392 free( spec->resources );
393 free( spec );
397 /*******************************************************************
398 * make_c_identifier
400 * Map a string to a valid C identifier.
402 const char *make_c_identifier( const char *str )
404 static char buffer[256];
405 char *p;
407 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
409 if (isalnum(*str)) *p = *str;
410 else *p = '_';
412 *p = 0;
413 return buffer;
417 /*******************************************************************
418 * get_stub_name
420 * Generate an internal name for a stub entry point.
422 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
424 static char buffer[256];
425 if (odp->name || odp->export_name)
427 char *p;
428 sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
429 /* make sure name is a legal C identifier */
430 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
431 if (!*p) return buffer;
433 sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
434 return buffer;
437 /* parse a cpu name and return the corresponding value */
438 enum target_cpu get_cpu_from_name( const char *name )
440 unsigned int i;
442 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
443 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
444 return -1;
447 /*****************************************************************
448 * Function: get_alignment
450 * Description:
451 * According to the info page for gas, the .align directive behaves
452 * differently on different systems. On some architectures, the
453 * argument of a .align directive is the number of bytes to pad to, so
454 * to align on an 8-byte boundary you'd say
455 * .align 8
456 * On other systems, the argument is "the number of low-order zero bits
457 * that the location counter must have after advancement." So to
458 * align on an 8-byte boundary you'd say
459 * .align 3
461 * The reason gas is written this way is that it's trying to mimick
462 * native assemblers for the various architectures it runs on. gas
463 * provides other directives that work consistently across
464 * architectures, but of course we want to work on all arches with or
465 * without gas. Hence this function.
468 * Parameters:
469 * align -- the number of bytes to align to. Must be a power of 2.
471 unsigned int get_alignment(unsigned int align)
473 unsigned int n;
475 assert( !(align & (align - 1)) );
477 switch(target_cpu)
479 case CPU_x86:
480 case CPU_x86_64:
481 case CPU_SPARC:
482 if (target_platform != PLATFORM_APPLE) return align;
483 /* fall through */
484 case CPU_POWERPC:
485 case CPU_ALPHA:
486 n = 0;
487 while ((1u << n) != align) n++;
488 return n;
490 /* unreached */
491 assert(0);
492 return 0;
495 /* return the page size for the target CPU */
496 unsigned int get_page_size(void)
498 switch(target_cpu)
500 case CPU_x86: return 4096;
501 case CPU_x86_64: return 4096;
502 case CPU_POWERPC: return 4096;
503 case CPU_SPARC: return 8192;
504 case CPU_ALPHA: return 8192;
506 /* unreached */
507 assert(0);
508 return 0;
511 /* return the size of a pointer on the target CPU */
512 unsigned int get_ptr_size(void)
514 switch(target_cpu)
516 case CPU_x86:
517 case CPU_POWERPC:
518 case CPU_SPARC:
519 case CPU_ALPHA:
520 return 4;
521 case CPU_x86_64:
522 return 8;
524 /* unreached */
525 assert(0);
526 return 0;
529 /* return the assembly name for a C symbol */
530 const char *asm_name( const char *sym )
532 static char buffer[256];
534 switch (target_platform)
536 case PLATFORM_APPLE:
537 case PLATFORM_WINDOWS:
538 buffer[0] = '_';
539 strcpy( buffer + 1, sym );
540 return buffer;
541 default:
542 return sym;
546 /* return an assembly function declaration for a C function name */
547 const char *func_declaration( const char *func )
549 static char buffer[256];
551 switch (target_platform)
553 case PLATFORM_APPLE:
554 return "";
555 case PLATFORM_WINDOWS:
556 sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
557 break;
558 default:
559 sprintf( buffer, ".type %s,@function", func );
560 break;
562 return buffer;
565 /* output a size declaration for an assembly function */
566 void output_function_size( const char *name )
568 switch (target_platform)
570 case PLATFORM_APPLE:
571 case PLATFORM_WINDOWS:
572 break;
573 default:
574 output( "\t.size %s, .-%s\n", name, name );
575 break;
579 /* output the GNU note for non-exec stack */
580 void output_gnu_stack_note(void)
582 switch (target_platform)
584 case PLATFORM_WINDOWS:
585 case PLATFORM_APPLE:
586 break;
587 default:
588 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
589 break;
593 /* return a global symbol declaration for an assembly symbol */
594 const char *asm_globl( const char *func )
596 static char buffer[256];
598 switch (target_platform)
600 case PLATFORM_APPLE:
601 sprintf( buffer, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
602 return buffer;
603 case PLATFORM_WINDOWS:
604 sprintf( buffer, "\t.globl _%s\n_%s:", func, func );
605 return buffer;
606 default:
607 sprintf( buffer, "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
608 return buffer;
612 const char *get_asm_ptr_keyword(void)
614 switch(get_ptr_size())
616 case 4: return ".long";
617 case 8: return ".quad";
619 assert(0);
620 return NULL;
623 const char *get_asm_string_keyword(void)
625 switch (target_platform)
627 case PLATFORM_APPLE:
628 return ".asciz";
629 default:
630 return ".string";
634 const char *get_asm_short_keyword(void)
636 switch (target_platform)
638 default: return ".short";
642 const char *get_asm_rodata_section(void)
644 switch (target_platform)
646 case PLATFORM_APPLE: return ".const";
647 default: return ".section .rodata";
651 const char *get_asm_string_section(void)
653 switch (target_platform)
655 case PLATFORM_APPLE: return ".cstring";
656 default: return ".section .rodata";