-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmakedep.c
4693 lines (4060 loc) · 162 KB
/
makedep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Generate include file dependencies
*
* Copyright 1996, 2013, 2020 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "tools.h"
#include "wine/list.h"
enum incl_type
{
INCL_NORMAL, /* #include "foo.h" */
INCL_SYSTEM, /* #include <foo.h> */
INCL_IMPORT, /* idl import "foo.idl" */
INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
};
struct dependency
{
int line; /* source line where this header is included */
enum incl_type type; /* type of include */
char *name; /* header name */
};
struct file
{
struct list entry;
char *name; /* full file name relative to cwd */
void *args; /* custom arguments for makefile rule */
unsigned int flags; /* flags (see below) */
unsigned int deps_count; /* files in use */
unsigned int deps_size; /* total allocated size */
struct dependency *deps; /* all header dependencies */
};
struct incl_file
{
struct list entry;
struct list hash_entry;
struct file *file;
char *name;
char *filename;
char *basename; /* base target name for generated files */
char *sourcename; /* source file name for generated headers */
struct incl_file *included_by; /* file that included this one */
int included_line; /* line where this file was included */
enum incl_type type; /* type of include */
unsigned int arch; /* architecture for multi-arch files, otherwise 0 */
unsigned int use_msvcrt:1; /* put msvcrt headers in the search path? */
unsigned int is_external:1; /* file from external library? */
struct incl_file *owner;
unsigned int files_count; /* files in use */
unsigned int files_size; /* total allocated size */
struct incl_file **files;
struct strarray dependencies; /* file dependencies */
struct strarray importlibdeps; /* importlib dependencies */
};
#define FLAG_GENERATED 0x000001 /* generated file */
#define FLAG_INSTALL 0x000002 /* file to install */
#define FLAG_TESTDLL 0x000004 /* file is part of a TESTDLL resource */
#define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
#define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
#define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
#define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
#define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
#define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
#define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
#define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
#define FLAG_RC_PO 0x010000 /* rc file contains translations */
#define FLAG_RC_HEADER 0x020000 /* rc file is a header */
#define FLAG_C_IMPLIB 0x040000 /* file is part of an import library */
#define FLAG_C_UNIX 0x080000 /* file is part of a Unix library */
#define FLAG_SFD_FONTS 0x100000 /* sfd file generated bitmap fonts */
#define FLAG_ARM64EC_X64 0x200000 /* use x86_64 object on ARM64EC */
static const struct
{
unsigned int flag;
const char *ext;
} idl_outputs[] =
{
{ FLAG_IDL_TYPELIB, "_l.res" },
{ FLAG_IDL_REGTYPELIB, "_t.res" },
{ FLAG_IDL_CLIENT, "_c.c" },
{ FLAG_IDL_IDENT, "_i.c" },
{ FLAG_IDL_PROXY, "_p.c" },
{ FLAG_IDL_SERVER, "_s.c" },
{ FLAG_IDL_REGISTER, "_r.res" },
};
#define HASH_SIZE 197
static struct list files[HASH_SIZE];
static struct list global_includes[HASH_SIZE];
enum install_rules { INSTALL_LIB, INSTALL_DEV, INSTALL_TEST, NB_INSTALL_RULES };
static const char *install_targets[NB_INSTALL_RULES] = { "install-lib", "install-dev", "install-test" };
static const char *install_variables[NB_INSTALL_RULES] = { "INSTALL_LIB", "INSTALL_DEV", "INSTALL_TEST" };
#define MAX_ARCHS 6
/* variables common to all makefiles */
static struct strarray archs;
static struct strarray linguas;
static struct strarray dll_flags;
static struct strarray unix_dllflags;
static struct strarray msvcrt_flags;
static struct strarray cpp_flags;
static struct strarray lddll_flags;
static struct strarray libs;
static struct strarray enable_tests;
static struct strarray cmdline_vars;
static struct strarray subdirs;
static struct strarray delay_import_libs;
static struct strarray top_install[NB_INSTALL_RULES];
static const char *root_src_dir;
static const char *tools_dir;
static const char *tools_ext;
static const char *exe_ext;
static const char *fontforge;
static const char *convert;
static const char *flex;
static const char *bison;
static const char *rsvg;
static const char *icotool;
static const char *msgfmt;
static const char *ln_s;
static const char *sed_cmd;
static const char *wayland_scanner;
static const char *sarif_converter;
static int so_dll_supported;
static int unix_lib_supported;
/* per-architecture global variables */
static const char *dll_ext[MAX_ARCHS];
static const char *arch_dirs[MAX_ARCHS];
static const char *arch_pe_dirs[MAX_ARCHS];
static const char *arch_install_dirs[MAX_ARCHS];
static const char *strip_progs[MAX_ARCHS];
static const char *debug_flags[MAX_ARCHS];
static const char *delay_load_flags[MAX_ARCHS];
static struct strarray target_flags[MAX_ARCHS];
static struct strarray extra_cflags[MAX_ARCHS];
static struct strarray extra_cflags_extlib[MAX_ARCHS];
static struct strarray disabled_dirs[MAX_ARCHS];
static unsigned int native_archs[MAX_ARCHS];
static unsigned int hybrid_archs[MAX_ARCHS];
static struct strarray hybrid_target_flags[MAX_ARCHS];
struct makefile
{
/* values determined from input makefile */
struct strarray vars;
struct strarray include_paths;
struct strarray include_args;
struct strarray define_args;
struct strarray unix_cflags;
struct strarray programs;
struct strarray scripts;
struct strarray imports;
struct strarray delayimports;
struct strarray extradllflags;
struct strarray install[NB_INSTALL_RULES];
struct strarray extra_targets;
struct strarray extra_imports;
struct list sources;
struct list includes;
const char *src_dir;
const char *obj_dir;
const char *parent_dir;
const char *module;
const char *testdll;
const char *extlib;
const char *staticlib;
const char *importlib;
const char *unixlib;
int data_only;
int is_win16;
int is_exe;
int disabled[MAX_ARCHS];
/* values generated at output time */
struct strarray in_files;
struct strarray pot_files;
struct strarray test_files;
struct strarray sast_files;
struct strarray clean_files;
struct strarray distclean_files;
struct strarray maintainerclean_files;
struct strarray uninstall_files;
struct strarray unixobj_files;
struct strarray font_files;
struct strarray debug_files;
struct strarray dlldata_files;
struct strarray phony_targets;
struct strarray dependencies;
struct strarray object_files[MAX_ARCHS];
struct strarray implib_files[MAX_ARCHS];
struct strarray ok_files[MAX_ARCHS];
struct strarray res_files[MAX_ARCHS];
struct strarray all_targets[MAX_ARCHS];
struct strarray install_rules[NB_INSTALL_RULES];
};
static struct makefile *top_makefile;
static struct makefile *include_makefile;
static struct makefile **submakes;
static const char separator[] = "### Dependencies";
static const char *output_makefile_name = "Makefile";
static const char *input_file_name;
static const char *output_file_name;
static const char *temp_file_name;
static int compile_commands_mode;
static int silent_rules;
static int input_line;
static int output_column;
static FILE *output_file;
struct compile_command
{
struct list entry;
const char *cmd;
const char *source;
const char *obj;
struct strarray args;
};
static struct list compile_commands = LIST_INIT( compile_commands );
static const char Usage[] =
"Usage: makedep [options]\n"
"Options:\n"
" -C Generate compile_commands.json along with the makefile\n"
" -S Generate Automake-style silent rules\n"
" -fxxx Store output in file 'xxx' (default: Makefile)\n";
static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
/*******************************************************************
* fatal_error
*/
static void fatal_error( const char *msg, ... )
{
va_list valist;
va_start( valist, msg );
if (input_file_name)
{
fprintf( stderr, "%s:", input_file_name );
if (input_line) fprintf( stderr, "%d:", input_line );
fprintf( stderr, " error: " );
}
else fprintf( stderr, "makedep: error: " );
vfprintf( stderr, msg, valist );
va_end( valist );
exit(1);
}
/*******************************************************************
* fatal_perror
*/
static void fatal_perror( const char *msg, ... )
{
va_list valist;
va_start( valist, msg );
if (input_file_name)
{
fprintf( stderr, "%s:", input_file_name );
if (input_line) fprintf( stderr, "%d:", input_line );
fprintf( stderr, " error: " );
}
else fprintf( stderr, "makedep: error: " );
vfprintf( stderr, msg, valist );
perror( " " );
va_end( valist );
exit(1);
}
/*******************************************************************
* cleanup_files
*/
static void cleanup_files(void)
{
if (temp_file_name) unlink( temp_file_name );
if (output_file_name) unlink( output_file_name );
}
/*******************************************************************
* exit_on_signal
*/
static void exit_on_signal( int sig )
{
exit( 1 ); /* this will call the atexit functions */
}
/*******************************************************************
* output
*/
static void output( const char *format, ... )
{
int ret;
va_list valist;
va_start( valist, format );
ret = vfprintf( output_file, format, valist );
va_end( valist );
if (ret < 0) fatal_perror( "output" );
if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
else output_column += ret;
}
/*******************************************************************
* strarray_get_value
*
* Find a value in a name/value pair string array.
*/
static const char *strarray_get_value( const struct strarray *array, const char *name )
{
int pos, res, min = 0, max = array->count / 2 - 1;
while (min <= max)
{
pos = (min + max) / 2;
if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
if (res < 0) min = pos + 1;
else max = pos - 1;
}
return NULL;
}
/*******************************************************************
* strarray_set_value
*
* Define a value in a name/value pair string array.
*/
static void strarray_set_value( struct strarray *array, const char *name, const char *value )
{
int i, pos, res, min = 0, max = array->count / 2 - 1;
while (min <= max)
{
pos = (min + max) / 2;
if (!(res = strcmp( array->str[pos * 2], name )))
{
/* redefining a variable replaces the previous value */
array->str[pos * 2 + 1] = value;
return;
}
if (res < 0) min = pos + 1;
else max = pos - 1;
}
strarray_add( array, NULL );
strarray_add( array, NULL );
for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
array->str[min * 2] = name;
array->str[min * 2 + 1] = value;
}
/*******************************************************************
* output_filename
*/
static void output_filename( const char *name )
{
if (output_column + strlen(name) + 1 > 100)
{
output( " \\\n" );
output( " " );
}
else if (output_column) output( " " );
output( "%s", name );
}
/*******************************************************************
* output_filenames
*/
static void output_filenames( struct strarray array )
{
unsigned int i;
for (i = 0; i < array.count; i++) output_filename( array.str[i] );
}
/*******************************************************************
* output_rm_filenames
*/
static void output_rm_filenames( struct strarray array, const char *command )
{
static const unsigned int max_cmdline = 30000; /* to be on the safe side */
unsigned int i, len;
if (!array.count) return;
output( "\t%s", command );
for (i = len = 0; i < array.count; i++)
{
if (len > max_cmdline)
{
output( "\n" );
output( "\t%s", command );
len = 0;
}
output_filename( array.str[i] );
len += strlen( array.str[i] ) + 1;
}
output( "\n" );
}
/*******************************************************************
* get_extension
*/
static char *get_extension( char *filename )
{
char *ext = strrchr( filename, '.' );
if (ext && strchr( ext, '/' )) ext = NULL;
return ext;
}
/*******************************************************************
* get_base_name
*/
static const char *get_base_name( const char *name )
{
char *base;
if (!strchr( name, '.' )) return name;
base = xstrdup( name );
*strrchr( base, '.' ) = 0;
return base;
}
/*******************************************************************
* replace_filename
*/
static char *replace_filename( const char *path, const char *name )
{
const char *p;
char *ret;
size_t len;
if (!path) return xstrdup( name );
if (!(p = strrchr( path, '/' ))) return xstrdup( name );
len = p - path + 1;
ret = xmalloc( len + strlen( name ) + 1 );
memcpy( ret, path, len );
strcpy( ret + len, name );
return ret;
}
/*******************************************************************
* replace_substr
*/
static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
{
size_t pos = start - str;
char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
memcpy( ret, str, pos );
strcpy( ret + pos, replace );
strcat( ret + pos, start + len );
return ret;
}
/*******************************************************************
* get_root_relative_path
*
* Get relative path from obj dir to root.
*/
static const char *get_root_relative_path( struct makefile *make )
{
const char *dir = make->obj_dir;
char *ret, *p;
unsigned int dotdots = 0;
if (!dir) return ".";
while (*dir)
{
dotdots++;
while (*dir && *dir != '/') dir++;
while (*dir == '/') dir++;
}
ret = xmalloc( 3 * dotdots );
for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
p[-1] = 0; /* remove trailing slash */
return ret;
}
/*******************************************************************
* concat_paths
*/
static char *concat_paths( const char *base, const char *path )
{
int i, len;
char *ret;
if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
if (!path || !path[0]) return xstrdup( base );
if (path[0] == '/') return xstrdup( path );
len = strlen( base );
while (len && base[len - 1] == '/') len--;
while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
{
for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
if (i != len - 1 || base[i] != '.')
{
path += 2;
while (*path == '/') path++;
}
/* else ignore "." element */
while (i > 0 && base[i - 1] == '/') i--;
len = i;
}
if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
ret = xmalloc( len + strlen( path ) + 2 );
memcpy( ret, base, len );
ret[len++] = '/';
strcpy( ret + len, path );
return ret;
}
/*******************************************************************
* escape_cstring
*/
static const char *escape_cstring( const char *str )
{
char *ret;
unsigned int i = 0, j = 0;
if (!strpbrk( str, "\\\"" )) return str;
ret = xmalloc( 2 * strlen(str) + 1 );
while (str[i])
{
if (str[i] == '\\' || str[i] == '"') ret[j++] = '\\';
ret[j++] = str[i++];
}
ret[j] = 0;
return ret;
}
/*******************************************************************
* is_native_arch_disabled
*
* Check if the makefile was disabled for a PE arch that matches the native arch.
*/
static int is_native_arch_disabled( struct makefile *make )
{
unsigned int arch;
if (archs.count == 1) return 0;
if (!so_dll_supported) return 1;
for (arch = 1; arch < archs.count; arch++)
if (make->disabled[arch] && !strcmp( archs.str[0], archs.str[arch] ))
return 1;
return 0;
}
/*******************************************************************
* get_link_arch
*/
static int get_link_arch( const struct makefile *make, unsigned int arch, unsigned int *link_arch )
{
unsigned int hybrid_arch = hybrid_archs[arch];
if (native_archs[arch]) return 0;
if (hybrid_arch && make->disabled[hybrid_arch]) hybrid_arch = 0;
if (make->disabled[arch] && !hybrid_arch) return 0;
*link_arch = hybrid_arch ? hybrid_arch : arch;
return 1;
}
/*******************************************************************
* is_multiarch
*
* Check if arch is one of the PE architectures in multiarch.
* Also return TRUE for native arch iff there's no PE support.
*/
static int is_multiarch( unsigned int arch )
{
return archs.count == 1 || arch;
}
/*******************************************************************
* is_using_msvcrt
*
* Check if the files of a makefile use msvcrt by default.
*/
static int is_using_msvcrt( struct makefile *make )
{
return make->module || make->testdll;
}
/*******************************************************************
* arch_module_name
*/
static char *arch_module_name( const char *module, unsigned int arch )
{
return strmake( "%s%s%s", arch_dirs[arch], module, dll_ext[arch] );
}
/*******************************************************************
* arch_make_variable
*/
static char *arch_make_variable( const char *name, unsigned int arch )
{
return arch ? strmake( "$(%s_%s)", archs.str[arch], name ) : strmake( "$(%s)", name );
}
/*******************************************************************
* obj_dir_path
*/
static char *obj_dir_path( const struct makefile *make, const char *path )
{
return concat_paths( make->obj_dir, path );
}
/*******************************************************************
* src_dir_path
*/
static char *src_dir_path( const struct makefile *make, const char *path )
{
if (make->src_dir) return concat_paths( make->src_dir, path );
return obj_dir_path( make, path );
}
/*******************************************************************
* root_src_dir_path
*/
static char *root_src_dir_path( const char *path )
{
return concat_paths( root_src_dir, path );
}
/*******************************************************************
* tools_dir_path
*/
static char *tools_dir_path( const struct makefile *make, const char *path )
{
if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
return strmake( "tools/%s", path );
}
/*******************************************************************
* tools_path
*/
static char *tools_path( const struct makefile *make, const char *name )
{
return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
}
/*******************************************************************
* strarray_addall_path
*/
static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
{
unsigned int i;
for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
}
/*******************************************************************
* get_line
*/
static char *get_line( FILE *file )
{
static char *buffer;
static size_t size;
if (!size)
{
size = 1024;
buffer = xmalloc( size );
}
if (!fgets( buffer, size, file )) return NULL;
input_line++;
for (;;)
{
char *p = buffer + strlen(buffer);
/* if line is larger than buffer, resize buffer */
while (p == buffer + size - 1 && p[-1] != '\n')
{
buffer = xrealloc( buffer, size * 2 );
if (!fgets( buffer + size - 1, size + 1, file )) break;
p = buffer + strlen(buffer);
size *= 2;
}
if (p > buffer && p[-1] == '\n')
{
*(--p) = 0;
if (p > buffer && p[-1] == '\r') *(--p) = 0;
if (p > buffer && p[-1] == '\\')
{
*(--p) = 0;
/* line ends in backslash, read continuation line */
if (!fgets( p, size - (p - buffer), file )) return buffer;
input_line++;
continue;
}
}
return buffer;
}
}
/*******************************************************************
* hash_filename
*/
static unsigned int hash_filename( const char *name )
{
/* FNV-1 hash */
unsigned int ret = 2166136261u;
while (*name) ret = (ret * 16777619) ^ *name++;
return ret % HASH_SIZE;
}
/*******************************************************************
* add_file
*/
static struct file *add_file( const char *name )
{
struct file *file = xmalloc( sizeof(*file) );
memset( file, 0, sizeof(*file) );
file->name = xstrdup( name );
return file;
}
/*******************************************************************
* add_dependency
*/
static void add_dependency( struct file *file, const char *name, enum incl_type type )
{
if (file->deps_count >= file->deps_size)
{
file->deps_size *= 2;
if (file->deps_size < 16) file->deps_size = 16;
file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
}
file->deps[file->deps_count].line = input_line;
file->deps[file->deps_count].type = type;
file->deps[file->deps_count].name = xstrdup( name );
file->deps_count++;
}
/*******************************************************************
* find_src_file
*/
static struct incl_file *find_src_file( const struct makefile *make, const char *name )
{
struct incl_file *file;
if (make == include_makefile)
{
unsigned int hash = hash_filename( name );
LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
if (!strcmp( name, file->name )) return file;
return NULL;
}
LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
if (!strcmp( name, file->name )) return file;
return NULL;
}
/*******************************************************************
* find_include_file
*/
static struct incl_file *find_include_file( const struct makefile *make, const char *name )
{
struct incl_file *file;
LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
{
const char *filename = file->filename;
if (!filename) continue;
if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
{
filename += strlen(make->obj_dir);
while (*filename == '/') filename++;
}
if (!strcmp( name, filename )) return file;
}
return NULL;
}
/*******************************************************************
* add_include
*
* Add an include file if it doesn't already exists.
*/
static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
const char *name, int line, enum incl_type type )
{
struct incl_file *include;
if (parent->files_count >= parent->files_size)
{
parent->files_size *= 2;
if (parent->files_size < 16) parent->files_size = 16;
parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
}
LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
goto found;
include = xmalloc( sizeof(*include) );
memset( include, 0, sizeof(*include) );
include->name = xstrdup(name);
include->included_by = parent;
include->included_line = line;
include->type = type;
include->use_msvcrt = parent->use_msvcrt;
list_add_tail( &make->includes, &include->entry );
found:
parent->files[parent->files_count++] = include;
return include;
}
/*******************************************************************
* add_generated_source
*
* Add a generated source file to the list.
*/
static struct incl_file *add_generated_source( struct makefile *make, const char *name,
const char *filename, unsigned int arch )
{
struct incl_file *file = xmalloc( sizeof(*file) );
name = strmake( "%s%s", arch_dirs[arch], name );
memset( file, 0, sizeof(*file) );
file->file = add_file( name );
file->arch = arch;
file->name = xstrdup( name );
file->basename = xstrdup( filename ? filename : name );
file->filename = obj_dir_path( make, file->basename );
file->file->flags = FLAG_GENERATED;
file->use_msvcrt = is_using_msvcrt( make );
list_add_tail( &make->sources, &file->entry );
if (make == include_makefile)
{
unsigned int hash = hash_filename( name );
list_add_tail( &global_includes[hash], &file->hash_entry );
}
return file;
}
/*******************************************************************
* skip_spaces
*/
static char *skip_spaces( const char *p )
{
while (*p == ' ' || *p == '\t') p++;
return (char *)p;
}
/*******************************************************************
* parse_include_directive
*/
static void parse_include_directive( struct file *source, char *str )
{
char quote, *include, *p = skip_spaces( str );
if (*p != '\"' && *p != '<' ) return;
quote = *p++;
if (quote == '<') quote = '>';
include = p;
while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "malformed include directive '%s'\n", str );
*p = 0;
add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
}
/*******************************************************************
* parse_pragma_directive
*/
static void parse_pragma_directive( struct file *source, char *str )
{
char *flag, *p = str;
if (*p != ' ' && *p != '\t') return;
p = strtok( skip_spaces( p ), " \t" );
if (strcmp( p, "makedep" )) return;
while ((flag = strtok( NULL, " \t" )))
{
if (!strcmp( flag, "depend" ))
{
while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
return;
}
else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
else if (!strcmp( flag, "testdll" )) source->flags |= FLAG_TESTDLL;
if (strendswith( source->name, ".idl" ))
{
if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
}
else if (strendswith( source->name, ".rc" ))
{
if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
}
else if (strendswith( source->name, ".sfd" ))
{
if (!strcmp( flag, "font" ))
{
struct strarray *array = source->args;
if (!array)
{
source->args = array = xmalloc( sizeof(*array) );
*array = empty_strarray;
source->flags |= FLAG_SFD_FONTS;
}
strarray_add( array, xstrdup( strtok( NULL, "" )));
return;
}
}
else
{
if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
if (!strcmp( flag, "arm64ec_x64" )) source->flags |= FLAG_ARM64EC_X64;
}