-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydef_make.def
588 lines (531 loc) · 17.4 KB
/
mydef_make.def
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
include: version.def
include: modules.def
include: macros_util/path.def
include: macros_util/debug.def
include: macros_make/makefile_c.def
include: macros_make/makefile_perl.def
# mydef_make will recursively look into each subdir
# unless there is a skipmake file in the subdir.
page: mydef_make
output_dir: script
type:
$call parse_args
$global $module="perl"
$global %module_type=($(module_type))
$call @sub_make
# ---- in current directory ----------
$call read_or_create_config
$print " output_dir: $config_outputdir"
# -- recursive make folders, copylist
$call read_copylist
$call collect_files
# -- dependency list, individual pages, output folders
my %h_page;
my %folder;
# ---- Build dependency, Group pages into folders, default to "toproot"
my @extrafiles
$call read_eachfile
$call check_eachpage
$call expand_dependency
# ----
$call write_makefile
$call module_specific
$call create_outputdir
# ----------------------------------------
subcode: sub_make
$global @master_config
$if @ARGV and -d $ARGV[0]
my $d=$ARGV[0];
open In, "config";
@master_config=<In>;
close In;
chdir $d or die "can't chdir $d\n";
subcode: read_or_create_config
$if !-f "config"
$call create_config
$call read_config
subcode: parse_args
$foreach $a in @ARGV
$if $a eq "-f"
$global $flag_force
$flag_force = 1
$call @check_arg_version, $a
## --------- config -----------
subcode: create_config
&call open_w, config
$if @master_config
$call copy_master_config
$else
$call create_new_config
# -----------------------
subcode: copy_master_config
$foreach my $l in @master_config
$if $l=~/^include_path:\s*(\S+)/
$call amend_path
print Out "include_path: $path\n"
$else
print Out $l
# ----
subcode: amend_path
my @t=split /:/, $1
my @tt;
push @tt, "..";
$foreach $s in @t
$if $s!~/^\// or -d $s
push @tt, "../$s";
$else
push @tt, $s;
my $path=join(":", @tt)
# -----------------------
subcode: create_new_config
$config_outputdir=prompt("Please enter the path to compile into [out]: ");
print Out "output_dir: $config_outputdir\n";
$module=prompt("Please enter module type [perl]: ");
print Out "module: $module\n";
#----
subcode: read_config
$global $config_outputdir
$global $config_outputdir_make=0
$global $config_filetype
&call open_r, config
$if /module:\s+(\w+)/
$module=$1;
$elif /output_(dir|path): (\S+)/
$config_outputdir=$2;
$elif /filetype:\s*(\S+)/
$config_filetype=$1
$elif /^include_path:\s*(\S+)/
add_path($1)
$elif /^include:\s*(\S+)/
$global @include_default
my $t=$1;
@include_default=split /[:,]\s*/, $t;
$elif /^make-(\w+):\s*(.*\S)/
$global %make_targets
$make_targets{$1}=$2
$elif /^macro_(\w+):\s*(.*\S)/
$macros{$1}=$2;
add_path($ENV{MYDEFLIB})
## ----------------------------------------------
subcode: read_copylist
$global %h_copylist
$if -f "copylist"
my $location;
&call open_r, copylist
my $l=$_
$if /^(\S+):\s*(.*)/
$location=$1
$l=$2;
my $t="$config_outputdir/$location"
$if !-d $t
mkdir $t
$if $l
my @tlist=split /\s+/, $l;
$foreach $t in @tlist
$t=~s/^\s+//
$t=~s/\s+$//
$if $t=~/\*/
my @all=glob($t)
$foreach $a in @all
$h_copylist{$a}=$location
$elif -f $t
$h_copylist{$t}=$location
my $count=keys %h_copylist
$print " copylist: loaded $count entries"
subcode: write_var_copylist
$if %h_copylist
print Out "COPY=";
$while my ($f, $l) = each %h_copylist
my $sep="/";
if($l){$sep="/$l/"};
$if $f=~/(.+)\/(.+)/
print Out $config_outputdir, $sep, $2, " ";
$else
print Out $config_outputdir, $sep, $f, " ";
print Out "\n";
push @tlist, "\${COPY}";
print Out "\n";
subcode: write_rule_copylist
# copylist
if(%h_copylist){
while( my ($f, $l) = each %h_copylist){
my $sep="/";
if($l){$sep="/$l/"};
if($f=~/(.+)\/(.+)/){
print Out $config_outputdir, "$sep$2: $f\n";
print Out "\t cp $f $config_outputdir$sep$2\n";
}
else{
print Out $config_outputdir, "/$l/$f: templates/$f\n";
print Out "\t cp templates/$f $config_outputdir/$l/$f\n";
}
}
print Out "\n";
}
## ----------------------------------------------------------------------
subcode: _autoload
my $script=$0;
subcode: collect_files
$global @make_folders
my @files;
my @allfiles=glob("*");
$foreach my $f in @allfiles
$if $f=~/.def$/
# ---- file ----
push @files, $f;
$elif -d $f
# ---- folder ----
$if -e "$f/skipmake"
print " Skip folder $f\n"
$elif $f =~ /^(cmp|bootstrap|old|tests|macros|deflib|dist|macros_.*)$/
print " Skip folder $f\n"
$elif $f eq $config_outputdir and -f "$f/Makefile"
$config_outputdir_make = 1
unshift @make_folders, $f
$else
my @t=glob("$f/*.def");
$if @t
print "$script $f ... \n";
system("$script $f")==0 or die "Failed to spawn sub make: $?\n";
$if -f "$f/Makefile"
push @make_folders, $f;
#----------------------------------------
#- %h_def forms a dependency graph
subcode: def_file_need_deplist
$global %h_def
$if !$h_def{$f} or $h_def{$f} == 1
my $deplist=[];
$h_def{$f}=$deplist;
BLOCK
subcode: include_file(f)
my $f=find_file($(f))
$if $f
push @$deplist, $f;
$if !$h_def{$f}
push @files, $f
$h_def{$f}=1
subcode: expand_dependency
$while my ($f, $l) = each %h_def
my %track;
$foreach $t in @$l
$track{$t}=1
&call iter_l
$foreach $tt in @$ll
$if !$track{$tt}
$track{$tt}=1;
push @$l, $tt;
$j++;
subcode: iter_l
my $j=0;
$while $j<@$l
my $t=$l->[$j];
my $ll=$h_def{$t};
BLOCK
#----------------------------------------
subcode: read_eachfile
$while my $f=pop @files
my @page_list;
my $page;
my $output_dir
&call def_file_need_deplist
# ---- read the file ----
$call @read_def_file
$if $output_dir
$foreach $page in @page_list
$if !$page->{output_dir}
$page->{output_dir}=$output_dir
subcode: read_def_file
my $got_page=0
# ----
my $inpage=0;
&call open_r, $f
$if $inpage
$call parse_inpage
$if !$inpage
$call parse_global
# ----
$if $got_page
$if @include_default
$foreach $i in @include_default
$call include_file, $i
# ------------------
subcode: parse_global
$if /^include:?\s*(\S+\.def)/
$call include_file, $1
$elif /^page: .*\$\d.*/
$inpage=1;
$page={};
push @page_list, $page;
$elif /^page: ([\w\.-]+)/
$if $1 eq "test"
$print skipping page: test
$else
$inpage=1;
$got_page=1
$page={};
push @page_list, $page;
$page->{page}=$1;
$page->{def}=$f;
$page->{include}={};
my $key="$f-$1";
$while $h_page{$key}
$key.='1';
$h_page{$key}=$page;
$elif /^output_dir: (\S+)/
$output_dir=$1
# ---------------------
subcode: parse_inpage
$if /^\S/
$inpage=0;
$elif $inpage==1 and /^(\s*)(\w+):/
$if /^\s*output_dir: (\S+)/
$call outputdir_inpage, $1
$elif /^\s*\$include\s+(\S*)/ and $module ne "c" and -f $1
$page->{include}->{$1}=1;
$elif /^\s*(type|module):\s*(\w+)/
$if !$page->{$1}
$page->{$1}=$2
$elif /^\s*type:\s*$/
$page->{type}="none"
$elif /^\s*package:/
$if $module eq "perl"
$page->{type}="pm"
$elif /^\s*(make_dep|make_cmd|other|CC|CFLAGS|LIB):\s+(.*)/
$page->{$1}=$2
$else
$inpage=2
# ----
subcode: outputdir_inpage(t)
my $dir=expand_macros($(t));
$if $dir !~/^[\/\.]/ and $output_dir
$dir=$output_dir."/".$dir
$page->{output_dir}=$dir;
$call add_dir_to_folder
subcode: add_dir_to_folder
my $key = "$f-$page->{page}"
# $print "Add $key -> $dir "
my $tlist=$folder{$dir};
$if $tlist
push @$tlist, $key
$else
$folder{$dir}=[$key];
# Assign to Makefile Variable $dir
# (which is grouped by output folder)
$page->{in_var}=$dir;
subcode: check_eachpage
$while my ($p, $h) = each %h_page
$call @get_file_type
$call @add_to_toproot
$call @set_output_name
# --------------------
subcode: get_file_type
# $print get_file_type: $h->{page} - $h->{type}
$if $h->{page}=~/\.\w+$/
$h->{type}="none"
$if !$h->{type}
$if $config_filetype
$h->{type}=$config_filetype
$else
my $t_module=$module
$if $h->{module}
$t_module=$h->{module}
$if $module_type{$t_module}
$h->{type}=$module_type{$t_module}
$else
$h->{type}=$t_module
# ----
subcode: set_output_name
$h->{path}=$h->{page};
$if $h->{output_dir}
$h->{path}=$h->{output_dir}."/".$h->{path};
$if $config_outputdir and $h->{path}!~/^[\/\.]/
$h->{path}=$config_outputdir."/".$h->{path};
$if $h->{type} and $h->{type} ne "none"
$h->{path}.=".$h->{type}"
# ----
subcode: add_to_toproot
$if !$h->{in_var}
$h->{in_var}="toproot";
$if $folder{toproot}
my $tlist=$folder{toproot};
push @$tlist, $p;
$else
$folder{toproot}=[$p];
subcode: write_makefile
&call open_w, Makefile
$(set:print_to=Out)
$if $0=~/(bootstrap\/script)\/mydef_make/
# android termux, /usr/bin/perl won't work
print Out "MakePage=perl $1/mydef_page\n";
$else
print Out "MakePage=mydef_page\n";
print Out "\n";
$call write_var_folder
$call write_var_copylist
print Out "all_targets: ", join(" ", @tlist), "\n\n"
$if @make_folders
print Out "all: all_targets ", join(' ', @make_folders), "\n\n"
# Since multiple targets may result from a single .def file, let's disable parallel build
print Out ".NOTPARALLEL:\n\n"
# expand def dependcy tree
$call write_rule_copylist
$call write_rule_pages
$call write_rule_install
$call write_rule_test
$call write_rule_submake
$call write_rule_targets
# ----------------
subcode: write_rule_targets
$foreach %make_targets
print Out "$k:\n"
print Out "\t$v\n"
print Out "\n"
subcode: write_rule_install
$if -f "install_def.sh"
print Out "install:\n"
&call open_r, install_def.sh
$if /^(mydef_install.*)/
print Out "\t\@$1\n"
print Out "\n"
$elif $make_folders[0] eq $config_outputdir
print Out "install: $config_outputdir\n"
print Out "\tmake -C $config_outputdir install\n"
print Out "\n"
subcode: write_rule_test
$if -f "tests/TESTS"
$print "test:\n\tcd tests; mydef_test\n\n"
subcode: write_var_folder
my %var_hash;
my @tlist;
$while my ($f, $l) = each %folder
my $name;
$if $f=~/.*\/(.*)/
$name=uc($1);
$else
$name=uc($f);
$if !$name
$name="ROOT"
$if $var_hash{$name}
my $j=2;
while($var_hash{"$name$j"}){$j++;}
$name="$name$j";
$var_hash{$name}=1;
push @tlist, "\${$name}";
print Out "$name=";
$foreach my $p in @$l
print Out $h_page{$p}->{path}, " ";
print Out "\n";
subcode: write_rule_pages
$while my ($p, $h)=each %h_page
my $def=$h->{def};
my $inc=$h->{include};
my $inc_dep=join(" ", keys %$inc);
my $extra_dep=''
$if $h->{make_dep}
$extra_dep=$h->{make_dep}
$if $h->{path}
my @t;
my $l=$h_def{$def};
$foreach $tt in @$l
$if !$inc->{$tt} and -f $tt
push @t, $tt;
$inc->{$tt}=1
print Out $h->{path}, ": ", $def, " ", join(" ", @t), " $inc_dep $extra_dep\n";
$if $h->{make_cmd}
print Out "\t$h->{make_cmd}\n"
$elif $h->{module} and ($h->{module} ne $module)
print Out "\t\${MakePage} -m$h->{module} $def $h->{path}\n";
$else
print Out "\t\${MakePage} $def $h->{path}\n";
print Out "\n";
#----------------------------------------
subcode: _autoload
my $nosub=0;
$if @ARGV and $ARGV[0] eq 'nosub'
shift @ARGV;
$nosub=1;
subcode: write_rule_submake
# ---- recursive make rule
$if @make_folders
$foreach $f in @make_folders
$if !$nosub
print Out "$f: force_look\n"
# print Out "\t cd $f; make\n"
print Out "\tmake -C $f\n"
print Out "\n"
print Out "force_look:\n\ttrue\n";
subcode: module_specific
$while my ($dir, $list) = each %folder
$if $dir=~/toproot|ROOT/
$dir = $config_outputdir
$elif $dir!~/^\/|\.\.?\//
$dir = "$config_outputdir/$dir"
$if $dir and !-e "$dir/skipmake"
$if $module eq "c" || $module eq "win32"
&call need_makefile
$call makefile_c, $dir
$(if:0)
$elif $module eq "perl" and $dir=~/^[A-Z]/
$call makefile_perl, $dir
$elif $module eq "xs" and $dir=~/^[A-Z]/
$call makefile_xs, $dir
subcode: need_makefile
# $print need_makefile: $dir ...
$if !-d $dir
mkdir $dir;
$if $flag_force || !-f "$dir/Makefile"
$print "Create $dir/Makefile"
BLOCK
subcode: get_target_list
my @target_list
$if $list
$foreach $t in @$list
my $t_page = $h_page{$t}
$if $t_page->{make} or $t_page->{other}
$call add_page, $t
$if !@target_list
$call add_page, $list->[0]
subcode: add_page(t)
my $page=$h_page{$(t)}
my $name=$page->{path}
$if $page->{path}=~/.*\/(.*)\.(\w+)/
$name = $1
push @target_list, [$page, $name]
$if !$t_module and $page->{module}
$t_module = $page->{module}
$call @target_page_specific
subcode: create_outputdir
$if $config_outputdir
$if !-d $config_outputdir
print "Create output folder $config_outputdir ...\n";
mkdir $config_outputdir;
$else
$config_outputdir="."
$foreach $f in keys(%folder)
$if $f=~/toproot|ROOT/
next
$if $f !~/^[\/\.]/
$f=$config_outputdir."/".$f
$if !-d $f
print "Create output folder $f ...\n";
system "mkdir -p $f"
#----------------------------------------
fncode: prompt($msg)
$while 1
print "$msg\n";
my $t=<STDIN>;
chomp $t;
$if $t
return $t
$elif $msg=~/\[(.*)\]: $/
return $1
$else
die "Must reply $msg\n"
fncode: expand_macros($t)
$global %macros
$if $t=~/\$\((\w+)\)/
$if $macros{$1}
$t=$`.$macros{$1}.$';
$else
die "Unknown Macro in $t\n";
return $t