4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
35 ORDDEF
*EntryPoints
[MAX_ORDINALS
];
36 ORDDEF
*Ordinals
[MAX_ORDINALS
];
37 ORDDEF
*Names
[MAX_ORDINALS
];
39 SPEC_MODE SpecMode
= SPEC_MODE_DLL
;
40 int Base
= MAX_ORDINALS
;
45 int nb_entry_points
= 0;
47 int nb_debug_channels
= 0;
49 int display_warnings
= 0;
51 /* we only support relay debugging on i386 */
52 #if defined(__i386__) && !defined(NO_TRACE_MSGS)
61 char *init_func
= NULL
;
62 char **debug_channels
= NULL
;
63 char **lib_path
= NULL
;
65 char *input_file_name
= NULL
;
66 const char *output_file_name
= NULL
;
68 static FILE *input_file
;
69 static FILE *output_file
;
70 static const char *current_src_dir
;
83 } exec_mode
= MODE_NONE
;
85 /* set the dll file name from the input file name */
86 static void set_dll_file_name( const char *name
)
90 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
91 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
92 strcpy( DLLFileName
, name
);
93 if ((p
= strrchr( DLLFileName
, '.' )) && !strcmp( p
, ".spec" )) *p
= 0;
94 if (!strchr( DLLFileName
, '.' )) strcat( DLLFileName
, ".dll" );
97 /* cleanup on program exit */
98 static void cleanup(void)
100 if (output_file_name
) unlink( output_file_name
);
104 /*******************************************************************
105 * command-line option handling
116 static void do_output( const char *arg
);
117 static void do_usage(void);
118 static void do_warnings(void);
119 static void do_f_flags( const char *arg
);
120 static void do_define( const char *arg
);
121 static void do_include( const char *arg
);
122 static void do_k_flags( const char *arg
);
123 static void do_exe_mode( const char *arg
);
124 static void do_module( const char *arg
);
125 static void do_name( const char *arg
);
126 static void do_spec( const char *arg
);
127 static void do_def( const char *arg
);
128 static void do_exe( const char *arg
);
129 static void do_glue(void);
130 static void do_relay16(void);
131 static void do_relay32(void);
132 static void do_debug(void);
133 static void do_sym(void);
134 static void do_chdir( const char *arg
);
135 static void do_lib( const char *arg
);
136 static void do_import( const char *arg
);
137 static void do_dimport( const char *arg
);
138 static void do_rsrc( const char *arg
);
140 static const struct option_descr option_table
[] =
142 { "-h", 0, do_usage
, "-h Display this help message" },
143 { "-w", 0, do_warnings
,"-w Turn on warnings" },
144 { "-C", 1, do_chdir
, "-C dir Change directory to <dir> before opening source files" },
145 { "-f", 1, do_f_flags
, "-f flags Compiler flags (only -fPIC is supported)" },
146 { "-D", 1, do_define
, "-D sym Ignored for C flags compatibility" },
147 { "-I", 1, do_include
, "-I dir Ignored for C flags compatibility" },
148 { "-K", 1, do_k_flags
, "-K flags Compiler flags (only -KPIC is supported)" },
149 { "-m", 1, do_exe_mode
,"-m mode Set the executable mode (cui|gui|cuiw|guiw)" },
150 { "-M", 1, do_module
, "-M module Set the name of the main (Win32) module for a Win16 dll" },
151 { "-L", 1, do_lib
, "-L directory Look for imports libraries in 'directory'" },
152 { "-l", 1, do_import
, "-l lib.dll Import the specified library" },
153 { "-dl", 1, do_dimport
, "-dl lib.dll Delay-import the specified library" },
154 { "-N", 1, do_name
, "-N dllname Set the DLL name (default: set from input file name)" },
155 { "-res", 1, do_rsrc
, "-res rsrc.res Load resources from rsrc.res" },
156 { "-o", 1, do_output
, "-o name Set the output file name (default: stdout)\n" },
157 { "-sym", 0, do_sym
, NULL
}, /* ignored for backwards compatibility */
158 { "-spec", 1, do_spec
, "-spec file.spec Build a .c file from a spec file" },
159 { "-def", 1, do_def
, "-def file.spec Build a .def file from a spec file" },
160 { "-exe", 1, do_exe
, "-exe name Build a .c file from the named executable" },
161 { "-debug", 0, do_debug
, "-debug [files] Build a .c file containing debug channels declarations" },
162 { "-glue", 0, do_glue
, "-glue [files] Build the 16-bit glue for the source files" },
163 { "-relay16", 0, do_relay16
, "-relay16 Build the 16-bit relay assembly routines" },
164 { "-relay32", 0, do_relay32
, "-relay32 Build the 32-bit relay assembly routines" },
165 { NULL
, 0, NULL
, NULL
}
168 static void do_output( const char *arg
)
170 if ( ( unlink ( arg
) ) == -1 && ( errno
!= ENOENT
) )
172 fprintf ( stderr
, "Unable to create output file '%s'\n", arg
);
175 if (!(output_file
= fopen( arg
, "w" )))
177 fprintf( stderr
, "Unable to create output file '%s'\n", arg
);
180 output_file_name
= arg
;
181 atexit( cleanup
); /* make sure we remove the output file on exit */
184 static void do_usage(void)
186 const struct option_descr
*opt
;
187 fprintf( stderr
, "Usage: winebuild [options]\n\n" );
188 fprintf( stderr
, "Options:\n" );
189 for (opt
= option_table
; opt
->name
; opt
++)
190 if (opt
->usage
) fprintf( stderr
, " %s\n", opt
->usage
);
192 fprintf( stderr
, "\nExactly one of -spec, -def, -exe, -debug, -glue, -relay16 or -relay32 must be specified.\n\n" );
196 static void do_warnings(void)
198 display_warnings
= 1;
201 static void do_f_flags( const char *arg
)
203 if (!strcmp( arg
, "PIC" )) UsePIC
= 1;
204 /* ignore all other flags */
207 static void do_define( const char *arg
)
212 static void do_include( const char *arg
)
217 static void do_k_flags( const char *arg
)
219 /* Ignored, because cc generates correct code. */
220 /* if (!strcmp( arg, "PIC" )) UsePIC = 1; */
221 /* ignore all other flags */
224 static void do_name( const char *arg
)
226 strncpy( DLLName
, arg
, sizeof(DLLName
) );
227 DLLName
[sizeof(DLLName
) - 1] = 0;
230 static void do_spec( const char *arg
)
232 if (exec_mode
!= MODE_NONE
|| !arg
[0]) do_usage();
233 exec_mode
= MODE_SPEC
;
234 input_file
= open_input_file( NULL
, arg
);
235 set_dll_file_name( arg
);
238 static void do_def( const char *arg
)
240 if (exec_mode
!= MODE_NONE
|| !arg
[0]) do_usage();
241 exec_mode
= MODE_DEF
;
242 input_file
= open_input_file( NULL
, arg
);
243 set_dll_file_name( arg
);
246 static void do_exe( const char *arg
)
250 if (exec_mode
!= MODE_NONE
|| !arg
[0]) do_usage();
251 exec_mode
= MODE_EXE
;
252 if ((p
= strrchr( arg
, '/' ))) p
++;
254 strcpy( DLLFileName
, p
);
255 if (!strchr( DLLFileName
, '.' )) strcat( DLLFileName
, ".exe" );
256 if (SpecMode
== SPEC_MODE_DLL
) SpecMode
= SPEC_MODE_GUIEXE
;
259 static void do_exe_mode( const char *arg
)
261 if (!strcmp( arg
, "gui" )) SpecMode
= SPEC_MODE_GUIEXE
;
262 else if (!strcmp( arg
, "cui" )) SpecMode
= SPEC_MODE_CUIEXE
;
263 else if (!strcmp( arg
, "guiw" )) SpecMode
= SPEC_MODE_GUIEXE_UNICODE
;
264 else if (!strcmp( arg
, "cuiw" )) SpecMode
= SPEC_MODE_CUIEXE_UNICODE
;
268 static void do_module( const char *arg
)
270 strcpy( owner_name
, arg
);
273 static void do_glue(void)
275 if (exec_mode
!= MODE_NONE
) do_usage();
276 exec_mode
= MODE_GLUE
;
279 static void do_debug(void)
281 if (exec_mode
!= MODE_NONE
) do_usage();
282 exec_mode
= MODE_DEBUG
;
285 static void do_chdir( const char *arg
)
287 current_src_dir
= arg
;
290 static void do_relay16(void)
292 if (exec_mode
!= MODE_NONE
) do_usage();
293 exec_mode
= MODE_RELAY16
;
296 static void do_relay32(void)
298 if (exec_mode
!= MODE_NONE
) do_usage();
299 exec_mode
= MODE_RELAY32
;
302 static void do_sym(void)
307 static void do_lib( const char *arg
)
309 lib_path
= xrealloc( lib_path
, (nb_lib_paths
+1) * sizeof(*lib_path
) );
310 lib_path
[nb_lib_paths
++] = xstrdup( arg
);
313 static void do_import( const char *arg
)
315 add_import_dll( arg
, 0 );
318 static void do_dimport( const char *arg
)
320 add_import_dll( arg
, 1 );
323 static void do_rsrc( const char *arg
)
325 load_res32_file( arg
);
328 /* parse options from the argv array and remove all the recognized ones */
329 static void parse_options( char *argv
[] )
331 const struct option_descr
*opt
;
333 const char* arg
=NULL
;
335 for (ptr
= last
= argv
+ 1; *ptr
; ptr
++)
337 for (opt
= option_table
; opt
->name
; opt
++)
339 if (opt
->has_arg
&& !strncmp( *ptr
, opt
->name
, strlen(opt
->name
) ))
341 arg
=*ptr
+strlen(opt
->name
);
349 if (!strcmp( *ptr
, opt
->name
))
358 if (opt
->has_arg
&& arg
!= NULL
) opt
->func( arg
);
359 else opt
->func( "" );
361 else /* keep this argument */
363 if (last
!= ptr
) *last
= *ptr
;
371 /*******************************************************************
374 int main(int argc
, char **argv
)
376 output_file
= stdout
;
377 parse_options( argv
);
382 switch (ParseTopLevel( input_file
, 0 ))
386 fatal_error( "file argument '%s' not allowed in this mode\n", argv
[1] );
387 BuildSpec16File( output_file
);
390 read_undef_symbols( argv
+ 1 );
391 BuildSpec32File( output_file
);
397 read_undef_symbols( argv
+ 1 );
398 BuildSpec32File( output_file
);
401 if (argv
[1]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[1] );
402 switch (ParseTopLevel( input_file
, 1 ))
405 fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
408 BuildDef32File( output_file
);
414 BuildDebugFile( output_file
, current_src_dir
, argv
+ 1 );
417 BuildGlue( output_file
, current_src_dir
, argv
+ 1 );
420 if (argv
[1]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[1] );
421 BuildRelays16( output_file
);
424 if (argv
[1]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[1] );
425 BuildRelays32( output_file
);
431 if (output_file_name
)
433 fclose( output_file
);
434 output_file_name
= NULL
;