-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydef_run.def
351 lines (317 loc) · 10.6 KB
/
mydef_run.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
include: version.def
include: custom_run.def?
########################################
#---- Run the mydef scripts directly
page: mydef_run
output_dir: script
type:
use MyDef
my (%config, $run_page, @pagelist)
$call parse_arg
$if $config{def_file}
$call guess_module
# reads in config file
MyDef::init(%config)
# reads in def file
MyDef::import_data($config{def_file})
$foreach my $pagename in @{$MyDef::def->{pagelist}}
my $page = $MyDef::def->{pages}->{$pagename}
MyDef::createpage($pagename)
$if ($run_page && $run_page eq $pagename) || (!$run_page && !$page->{skiprun})
push @pagelist, $page
$if !@pagelist
$print "No page found."
print "Expect page: $run_page\n" if $run_page
print "Available pages: ", join(", ", @{$MyDef::def->{pagelist}}), "\n"
die
$elif $config{_file}
my $page={outname=>$config{_file}}
$call parse_option_from_comment
push @pagelist, $page
$else
die "Usage: $0 input_defile.def\n"
$foreach $page in @pagelist
$call run_page
# -------------------------
subcode: parse_arg
$foreach $a in @ARGV
$if $a=~/-m(\w+)$/
$config{module}=$1
$elif $a=~/-rule$/
$config{dump_rule}=1
$elif $a=~/-silent$/
$config{silent}=1
$elif $a=~/(.*)\.def/
# compile && run_page
$config{def_file}=$a
$elif -f $a
# just run_page
$config{_file}=$a
$call @check_arg_version, $a
subcode: guess_module
my $guessed_module = "general"
# if we are certain (e.g. module: xxx, include: xxx/), set $config{module}
# if we are guessing, set $guessed_module
guess_module:
my $under_page
&call open_r, $config{def_file}
$if /^page:\s*([\w\.]+)/
$under_page = $1
# ----
$if /module:\s*(\w+)/
$if !$config{module}
$config{module}=$1
# last
$elif /^run:\s*(\S+\.def)/
$config{def_file}=$1
close In
goto guess_module
$elif /^run:\s*([\w\.]+)/
$run_page = $1
$elif /^prerun:\s*(.+)/
system $1
$elif /^filter:\s*(.+)/
system "$1 $config{def_file} /tmp/t.def"
$config{def_file} = "/tmp/t.def"
close In
goto guess_module
$elif /^\/\*\s*expect (\S+):\s*$/
$config{expect_type}=$1
$call grab_expect_block
$elif /^include:\s*(perl|c)\//
$config{module}=$1
$elif !$config{module}
$call @looklike_perl
$if !$config{module}
$config{module}=$MyDef::var->{module}
$if !$config{module}
$config{module}=$guessed_module
subcode: looklike_perl
$case /^\s*(my|our|\$global)\s*\(?[\$\@\%]\w+/
$guessed_module="perl"
# ------------------
subcode: guess_os
my $ostype
# $ENV{OSTYPE} dissappears in perl
my $uname = `uname`
$if $uname=~/CYGWIN/
$ostype = "cygwin"
$else
$ostype = "linux"
#-----------------------------------------------
subcode: run_page
# $call guess_os
my $file=$page->{outname}
my $file_dir
$if $file=~/(.*)\/(.+)/
$file_dir=$1
# chdir $1
# $file=$2
my ($name, $type)
$if $file=~/^(.*)\.(\w+)$/
($name, $type)=($1, $2)
my $exe = "$name"
$call @set_config_exe_type
# $print mydef_run: [$name] [$type]
$call @construct_cmd
# --------------------------------
$if $config{expect} and $config{expect_type} ne "output"
$call cmp_expect, $file
$elif $cmd
$call append_arg
$if $config{expect_type} eq "output"
$call run_cmd_expect
$else
$if !$config{silent}
$print $cmd
system $cmd
$else
warn "do not know how to run it\n"
# ---- construc $cmd based on $type ------
subcode: construct_cmd
my $cmd
$if $page->{cmd}
$cmd=$page->{cmd}
$elif $page->{CC}
$cmd = "$page->{CC} -o $exe $file $page->{lib_list}"
$call append_exe, $exe
$elif $type=~/^c(pp|xx|c|u)?$/
$call run_c
$call do_compile, java, javac $file, CD java $name
$call do_compile, f, gfortran -g -o$exe $file $page->{lib_list}, $exe
$call do_compile, f90, gfortran -g -o$exe $file $page->{lib_list}, $exe
$call do_compile, rs, rustc -o$exe $file, $exe
$call do_compile, hs, ghc -o$exe $file, $exe
$call do_compile, asm, nasm -f elf $file && ld -m elf_i386 $name.o -o$exe, $exe
$call do_compile, s, gcc -masm=intel -o$exe $file, $exe
$call do_compile, pas, fpc $file, $exe
# $call do_compile, prolog, gplc $name ? no idea how to make it work
$call do_script, sh, sh
$call do_script, bash, bash
$call do_script, pl, perl
$call do_script, php, php
$call do_script, py, python
$call do_script, go, go run
$call do_script, js, nodejs
$call do_script, tcl, tclsh
$call do_script, lua, lua
$call do_script, lisp, clisp
$call do_script, groovy, groovy
$call do_script, expect, expect -f
# $call do_script, tex, pdflatex # -interaction=batchmode
$call do_script, tex, run_tex
$call do_script, latex, run_tex
$call do_script, awk, awk -f
$call do_script, plt, gnuplot
$call do_script, gv, dot -Tpdf -o $page->{_pagename}.pdf
# --------------------
# support custom_run.def
# eg. java_COMPILE, rs_EXE, pl_RUN
# CC
subcode: do_compile(ext, cmd, exe)
$case $type eq "$(ext)"
$(if:$(ext)_COMPILE)
$(set:cmd=$($(ext)_COMPILE))
$(if:$(ext)_EXE)
$(set:exe=$($(ext)_EXE))
$(if:$(ext)_RUN)
$cmd = "$($(ext)_RUN))"
$(else)
$cmd="$(cmd)"
$call append_exe, $(exe)
subcode: do_script(ext, cmd)
$case $type eq "$(ext)"
$(if:$(ext)_RUN)
$cmd = "$($(ext)_RUN))"
$(else)
$cmd = "$(cmd) $file"
$if $page->{run}
$cmd = $page->{run} . ' ' . $cmd
# --------------------
subcode: run_c
my $default_cc
$(if:CC)
$default_cc="$(CC)"
$(else)
# $cc = "x86_64-w64-mingw32-gcc -O2 -Wl,--subsystem,windows"
$if $file=~/\.(cpp|cxx|cc)$/
$default_cc = "g++ -std=c++11 -g -O2"
$elif $file=~/\.(cu)$/
$default_cc = "nvcc"
$else
$default_cc = "gcc -std=c99 -g -O2"
$call set_config, $cc, cc, $default_cc
$call set_config, $cc, CC, $default_cc
# ---------------------
$call set_config, $cflags, CFLAGS
$if $cflags
$cc .= " $cflags"
# ---------------------
$cmd="$cc -o $exe $file $page->{lib_list}"
$if $config{dump_rule}
$if $exe=~/([^\/]+)$/
$exe = "script/$1"
$if $name=~/([^\/]+)$/
$name = "$1"
$cmd = '$'."{CC} -o \$@ \$< $page->{lib_list}"
print "\nCC=$cc\n\n"
print "$exe: $name\n"
print "\t$cmd\n"
exit 0
$else
$call append_exe, $exe
# ---------------------
subcode: set_config_exe_type
$if $page->{exe_type}
$exe .= "." . $page->{exe_type}
$elif $MyDef::var->{exe_type}
$exe .= "." . $MyDef::var->{exe_type}
$elif $config{module} eq "win32"
$exe .= ".exe"
subcode: append_exe(exe)
$(if:exe=$exe)
$if $exe!~/\//
$exe = "./$(exe)"
$call set_config, $exe, exe, "$(exe)"
$call set_config, $run, run
$if $run
$exe = $run . ' ' . $exe
$if $exe ne "none"
$if $exe=~/CD\s+(.*)/ -> $t
$if $file_dir
$t=~s/$file_dir\///g
$exe = "cd $file_dir && $t"
$else
$exe = $t
$cmd .= " && $exe"
subcode: append_arg
$if $page->{arg}
$cmd .= " $page->{arg}"
$elif $page->{args}
$cmd .= " $page->{args}"
subcode: set_config(var, key, @default)
my $t
$if $page->{$(key)}
$t = $page->{$(key)}
$elif $MyDef::var->{$(key)}
$t = $MyDef::var->{$(key)}
# ---
$(if:default)
my $(var) = $(default)
$(else)
my $(var)
$if $t
$t=~s/\$$(key)/$(var)/
$(var) = $t
# --------------------------
subcode: run_cmd_expect
$call cmp_expect, cmd
# ------------------------
#----------------------------------------
# If running a source code directly, try grap options from the comment
subcode: parse_option_from_comment
&call open_r, $config{_file}
$if /^\/\*\s*(cc|exe|cmd|run|arg|lib_list):\s*(.+)\s*\*\//i
$page->{$1} = $2
#----------------------------------------
subcode: grab_expect_block
my @expect
$while <In>
$if /^\*\//
$config{expect} = \@expect
last
$else
s/\\x(..)/chr(hex($1))/ge
$call @expect_normalize
push @expect, $_
subcode: cmp_expect(type)
my $expect = $config{expect}
my ($n_ok, $n_nok)
$(if:type=cmd)
$if !$config{silent}
$print $cmd
$(set:output="$cmd |")
$(else)
$(set:output=$(type))
my $i = 0
&call open_r, $(output)
print $_
$call expect_normalize
$if $expect->[$i] ne $_
$n_nok++
$print "not ok: [$expect->[$i]]"
$else
$n_ok++
$i++
print "--------------------------\n"
$if $n_ok>0
$print " $green{ok} $n_ok/$i"
$if $n_nok>0
$print " $red{not ok} $n_nok/$i"
exit 1
subcode: expect_normalize
$if /^\s*$/
next
chomp
s/\s+/ /g
s/\s*$//g