winebuild: Don't include any Windows header to avoid data types dependencies.
[wine/multimedia.git] / tools / winebuild / utils.c
blob400f01aeb21e256b6f72489b21c9efbc9b39e0b6
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 "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 static const struct
42 const char *name;
43 enum target_cpu cpu;
44 } cpu_names[] =
46 { "i386", CPU_x86 },
47 { "i486", CPU_x86 },
48 { "i586", CPU_x86 },
49 { "i686", CPU_x86 },
50 { "i786", CPU_x86 },
51 { "x86_64", CPU_x86_64 },
52 { "sparc", CPU_SPARC },
53 { "alpha", CPU_ALPHA },
54 { "powerpc", CPU_POWERPC }
57 /* atexit handler to clean tmp files */
58 static void cleanup_tmp_files(void)
60 unsigned int i;
61 for (i = 0; i < MAX_TMP_FILES; i++) if (tmp_files[i]) unlink( tmp_files[i] );
65 void *xmalloc (size_t size)
67 void *res;
69 res = malloc (size ? size : 1);
70 if (res == NULL)
72 fprintf (stderr, "Virtual memory exhausted.\n");
73 exit (1);
75 return res;
78 void *xrealloc (void *ptr, size_t size)
80 void *res = realloc (ptr, size);
81 if (size && res == NULL)
83 fprintf (stderr, "Virtual memory exhausted.\n");
84 exit (1);
86 return res;
89 char *xstrdup( const char *str )
91 char *res = strdup( str );
92 if (!res)
94 fprintf (stderr, "Virtual memory exhausted.\n");
95 exit (1);
97 return res;
100 char *strupper(char *s)
102 char *p;
103 for (p = s; *p; p++) *p = toupper(*p);
104 return s;
107 int strendswith(const char* str, const char* end)
109 int l = strlen(str);
110 int m = strlen(end);
111 return l >= m && strcmp(str + l - m, end) == 0;
114 void fatal_error( 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 else fprintf( stderr, "winebuild: " );
126 vfprintf( stderr, msg, valist );
127 va_end( valist );
128 exit(1);
131 void fatal_perror( 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 perror( " " );
144 va_end( valist );
145 exit(1);
148 void error( const char *msg, ... )
150 va_list valist;
151 va_start( valist, msg );
152 if (input_file_name)
154 fprintf( stderr, "%s:", input_file_name );
155 if (current_line)
156 fprintf( stderr, "%d:", current_line );
157 fputc( ' ', stderr );
159 vfprintf( stderr, msg, valist );
160 va_end( valist );
161 nb_errors++;
164 void warning( const char *msg, ... )
166 va_list valist;
168 if (!display_warnings) return;
169 va_start( valist, msg );
170 if (input_file_name)
172 fprintf( stderr, "%s:", input_file_name );
173 if (current_line)
174 fprintf( stderr, "%d:", current_line );
175 fputc( ' ', stderr );
177 fprintf( stderr, "warning: " );
178 vfprintf( stderr, msg, valist );
179 va_end( valist );
182 int output( const char *format, ... )
184 int ret;
185 va_list valist;
187 va_start( valist, format );
188 ret = vfprintf( output_file, format, valist );
189 va_end( valist );
190 if (ret < 0) fatal_perror( "Output error" );
191 return ret;
194 /* find a build tool in the path, trying the various names */
195 char *find_tool( const char * const *names )
197 static char **dirs;
198 static unsigned int count, maxlen;
200 char *p, *file;
201 unsigned int i, len;
202 struct stat st;
204 if (!dirs)
206 char *path;
208 /* split the path in directories */
210 if (!getenv( "PATH" )) return NULL;
211 path = xstrdup( getenv( "PATH" ));
212 for (p = path, count = 2; *p; p++) if (*p == ':') count++;
213 dirs = xmalloc( count * sizeof(*dirs) );
214 count = 0;
215 dirs[count++] = p = path;
216 while (*p)
218 while (*p && *p != ':') p++;
219 if (!*p) break;
220 *p++ = 0;
221 dirs[count++] = p;
223 for (i = 0; i < count; i++) maxlen = max( maxlen, strlen(dirs[i])+2 );
226 while (*names)
228 len = strlen(*names) + 1;
229 file = xmalloc( maxlen + len );
231 for (i = 0; i < count; i++)
233 strcpy( file, dirs[i] );
234 p = file + strlen(file);
235 if (p == file) *p++ = '.';
236 if (p[-1] != '/') *p++ = '/';
237 strcpy( p, *names );
239 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
241 free( file );
242 names++;
244 return NULL;
247 const char *get_as_command(void)
249 if (!as_command)
251 if (target_alias)
253 as_command = xmalloc( strlen(target_alias) + sizeof("-as") );
254 strcpy( as_command, target_alias );
255 strcat( as_command, "-as" );
257 else
259 static const char * const commands[] = { "gas", "as", NULL };
260 if (!(as_command = find_tool( commands ))) as_command = xstrdup("as");
263 if (force_pointer_size)
265 const char *args = (force_pointer_size == 8) ? " --64" : " --32";
266 as_command = xrealloc( as_command, strlen(as_command) + strlen(args) + 1 );
267 strcat( as_command, args );
270 return as_command;
273 const char *get_ld_command(void)
275 if (!ld_command)
277 if (target_alias)
279 ld_command = xmalloc( strlen(target_alias) + sizeof("-ld") );
280 strcpy( ld_command, target_alias );
281 strcat( ld_command, "-ld" );
283 else
285 static const char * const commands[] = { "ld", "gld", NULL };
286 if (!(ld_command = find_tool( commands ))) ld_command = xstrdup("ld");
289 if (force_pointer_size)
291 const char *args = (force_pointer_size == 8) ? " -m elf_x86_64" : " -m elf_i386";
292 ld_command = xrealloc( ld_command, strlen(ld_command) + strlen(args) + 1 );
293 strcat( ld_command, args );
296 return ld_command;
299 const char *get_nm_command(void)
301 if (!nm_command)
303 if (target_alias)
305 nm_command = xmalloc( strlen(target_alias) + sizeof("-nm") );
306 strcpy( nm_command, target_alias );
307 strcat( nm_command, "-nm" );
309 else
311 static const char * const commands[] = { "nm", "gnm", NULL };
312 if (!(nm_command = find_tool( commands ))) nm_command = xstrdup("nm");
315 return nm_command;
318 /* get a name for a temp file, automatically cleaned up on exit */
319 char *get_temp_file_name( const char *prefix, const char *suffix )
321 char *name;
322 const char *ext;
323 int fd;
325 assert( nb_tmp_files < MAX_TMP_FILES );
326 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
328 if (!prefix || !prefix[0]) prefix = "winebuild";
329 if (!suffix) suffix = "";
330 if (!(ext = strchr( prefix, '.' ))) ext = prefix + strlen(prefix);
331 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
332 strcpy( name, "/tmp/" );
333 memcpy( name + 5, prefix, ext - prefix );
334 strcpy( name + 5 + (ext - prefix), ".XXXXXX" );
335 strcat( name, suffix );
337 /* first try without the /tmp/ prefix */
338 if ((fd = mkstemps( name + 5, strlen(suffix) )) != -1)
339 name += 5;
340 else if ((fd = mkstemps( name, strlen(suffix) )) == -1)
341 fatal_error( "could not generate a temp file\n" );
343 close( fd );
344 tmp_files[nb_tmp_files++] = name;
345 return name;
348 /* output a standard header for generated files */
349 void output_standard_file_header(void)
351 if (spec_file_name)
352 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
353 else
354 output( "/* File generated automatically; do not edit! */\n" );
355 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
358 /* dump a byte stream into the assembly code */
359 void dump_bytes( const void *buffer, unsigned int size )
361 unsigned int i;
362 const unsigned char *ptr = buffer;
364 if (!size) return;
365 output( "\t.byte " );
366 for (i = 0; i < size - 1; i++, ptr++)
368 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
369 else output( "0x%02x,", *ptr );
371 output( "0x%02x\n", *ptr );
375 /*******************************************************************
376 * open_input_file
378 * Open a file in the given srcdir and set the input_file_name global variable.
380 FILE *open_input_file( const char *srcdir, const char *name )
382 char *fullname;
383 FILE *file = fopen( name, "r" );
385 if (!file && srcdir)
387 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
388 strcpy( fullname, srcdir );
389 strcat( fullname, "/" );
390 strcat( fullname, name );
391 file = fopen( fullname, "r" );
393 else fullname = xstrdup( name );
395 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
396 input_file_name = fullname;
397 current_line = 1;
398 return file;
402 /*******************************************************************
403 * close_input_file
405 * Close the current input file (must have been opened with open_input_file).
407 void close_input_file( FILE *file )
409 fclose( file );
410 free( input_file_name );
411 input_file_name = NULL;
412 current_line = 0;
416 /*******************************************************************
417 * remove_stdcall_decoration
419 * Remove a possible @xx suffix from a function name.
420 * Return the numerical value of the suffix, or -1 if none.
422 int remove_stdcall_decoration( char *name )
424 char *p, *end = strrchr( name, '@' );
425 if (!end || !end[1] || end == name) return -1;
426 /* make sure all the rest is digits */
427 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
428 *end = 0;
429 return atoi( end + 1 );
433 /*******************************************************************
434 * assemble_file
436 * Run a file through the assembler.
438 void assemble_file( const char *src_file, const char *obj_file )
440 const char *prog = get_as_command();
441 char *cmd;
442 int err;
444 cmd = xmalloc( strlen(prog) + strlen(obj_file) + strlen(src_file) + 6 );
445 sprintf( cmd, "%s -o %s %s", prog, obj_file, src_file );
446 if (verbose) fprintf( stderr, "%s\n", cmd );
447 err = system( cmd );
448 if (err) fatal_error( "%s failed with status %d\n", prog, err );
449 free( cmd );
453 /*******************************************************************
454 * alloc_dll_spec
456 * Create a new dll spec file descriptor
458 DLLSPEC *alloc_dll_spec(void)
460 DLLSPEC *spec;
462 spec = xmalloc( sizeof(*spec) );
463 spec->file_name = NULL;
464 spec->dll_name = NULL;
465 spec->init_func = NULL;
466 spec->main_module = NULL;
467 spec->type = SPEC_WIN32;
468 spec->base = MAX_ORDINALS;
469 spec->limit = 0;
470 spec->stack_size = 0;
471 spec->heap_size = 0;
472 spec->nb_entry_points = 0;
473 spec->alloc_entry_points = 0;
474 spec->nb_names = 0;
475 spec->nb_resources = 0;
476 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
477 if (get_ptr_size() > 4)
478 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
479 else
480 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
481 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
482 spec->subsystem = 0;
483 spec->subsystem_major = 4;
484 spec->subsystem_minor = 0;
485 spec->entry_points = NULL;
486 spec->names = NULL;
487 spec->ordinals = NULL;
488 spec->resources = NULL;
489 return spec;
493 /*******************************************************************
494 * free_dll_spec
496 * Free dll spec file descriptor
498 void free_dll_spec( DLLSPEC *spec )
500 int i;
502 for (i = 0; i < spec->nb_entry_points; i++)
504 ORDDEF *odp = &spec->entry_points[i];
505 free( odp->name );
506 free( odp->export_name );
507 free( odp->link_name );
509 free( spec->file_name );
510 free( spec->dll_name );
511 free( spec->init_func );
512 free( spec->entry_points );
513 free( spec->names );
514 free( spec->ordinals );
515 free( spec->resources );
516 free( spec );
520 /*******************************************************************
521 * make_c_identifier
523 * Map a string to a valid C identifier.
525 const char *make_c_identifier( const char *str )
527 static char buffer[256];
528 char *p;
530 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
532 if (isalnum(*str)) *p = *str;
533 else *p = '_';
535 *p = 0;
536 return buffer;
540 /*******************************************************************
541 * get_stub_name
543 * Generate an internal name for a stub entry point.
545 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
547 static char buffer[256];
548 if (odp->name || odp->export_name)
550 char *p;
551 sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
552 /* make sure name is a legal C identifier */
553 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
554 if (!*p) return buffer;
556 sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
557 return buffer;
560 /* parse a cpu name and return the corresponding value */
561 enum target_cpu get_cpu_from_name( const char *name )
563 unsigned int i;
565 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
566 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
567 return -1;
570 /*****************************************************************
571 * Function: get_alignment
573 * Description:
574 * According to the info page for gas, the .align directive behaves
575 * differently on different systems. On some architectures, the
576 * argument of a .align directive is the number of bytes to pad to, so
577 * to align on an 8-byte boundary you'd say
578 * .align 8
579 * On other systems, the argument is "the number of low-order zero bits
580 * that the location counter must have after advancement." So to
581 * align on an 8-byte boundary you'd say
582 * .align 3
584 * The reason gas is written this way is that it's trying to mimick
585 * native assemblers for the various architectures it runs on. gas
586 * provides other directives that work consistently across
587 * architectures, but of course we want to work on all arches with or
588 * without gas. Hence this function.
591 * Parameters:
592 * align -- the number of bytes to align to. Must be a power of 2.
594 unsigned int get_alignment(unsigned int align)
596 unsigned int n;
598 assert( !(align & (align - 1)) );
600 switch(target_cpu)
602 case CPU_x86:
603 case CPU_x86_64:
604 case CPU_SPARC:
605 if (target_platform != PLATFORM_APPLE) return align;
606 /* fall through */
607 case CPU_POWERPC:
608 case CPU_ALPHA:
609 n = 0;
610 while ((1u << n) != align) n++;
611 return n;
613 /* unreached */
614 assert(0);
615 return 0;
618 /* return the page size for the target CPU */
619 unsigned int get_page_size(void)
621 switch(target_cpu)
623 case CPU_x86: return 4096;
624 case CPU_x86_64: return 4096;
625 case CPU_POWERPC: return 4096;
626 case CPU_SPARC: return 8192;
627 case CPU_ALPHA: return 8192;
629 /* unreached */
630 assert(0);
631 return 0;
634 /* return the size of a pointer on the target CPU */
635 unsigned int get_ptr_size(void)
637 switch(target_cpu)
639 case CPU_x86:
640 case CPU_POWERPC:
641 case CPU_SPARC:
642 case CPU_ALPHA:
643 return 4;
644 case CPU_x86_64:
645 return 8;
647 /* unreached */
648 assert(0);
649 return 0;
652 /* return the assembly name for a C symbol */
653 const char *asm_name( const char *sym )
655 static char buffer[256];
657 switch (target_platform)
659 case PLATFORM_APPLE:
660 case PLATFORM_WINDOWS:
661 if (sym[0] == '.' && sym[1] == 'L') return sym;
662 buffer[0] = '_';
663 strcpy( buffer + 1, sym );
664 return buffer;
665 default:
666 return sym;
670 /* return an assembly function declaration for a C function name */
671 const char *func_declaration( const char *func )
673 static char buffer[256];
675 switch (target_platform)
677 case PLATFORM_APPLE:
678 return "";
679 case PLATFORM_WINDOWS:
680 sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
681 break;
682 default:
683 sprintf( buffer, ".type %s,@function", func );
684 break;
686 return buffer;
689 /* output a size declaration for an assembly function */
690 void output_function_size( const char *name )
692 switch (target_platform)
694 case PLATFORM_APPLE:
695 case PLATFORM_WINDOWS:
696 break;
697 default:
698 output( "\t.size %s, .-%s\n", name, name );
699 break;
703 /* output the GNU note for non-exec stack */
704 void output_gnu_stack_note(void)
706 switch (target_platform)
708 case PLATFORM_WINDOWS:
709 case PLATFORM_APPLE:
710 break;
711 default:
712 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
713 break;
717 /* return a global symbol declaration for an assembly symbol */
718 const char *asm_globl( const char *func )
720 static char buffer[256];
722 switch (target_platform)
724 case PLATFORM_APPLE:
725 sprintf( buffer, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
726 return buffer;
727 case PLATFORM_WINDOWS:
728 sprintf( buffer, "\t.globl _%s\n_%s:", func, func );
729 return buffer;
730 default:
731 sprintf( buffer, "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
732 return buffer;
736 const char *get_asm_ptr_keyword(void)
738 switch(get_ptr_size())
740 case 4: return ".long";
741 case 8: return ".quad";
743 assert(0);
744 return NULL;
747 const char *get_asm_string_keyword(void)
749 switch (target_platform)
751 case PLATFORM_APPLE:
752 return ".asciz";
753 default:
754 return ".string";
758 const char *get_asm_short_keyword(void)
760 switch (target_platform)
762 default: return ".short";
766 const char *get_asm_rodata_section(void)
768 switch (target_platform)
770 case PLATFORM_APPLE: return ".const";
771 default: return ".section .rodata";
775 const char *get_asm_string_section(void)
777 switch (target_platform)
779 case PLATFORM_APPLE: return ".cstring";
780 default: return ".section .rodata";