-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheesemake
executable file
·751 lines (612 loc) · 10.8 KB
/
cheesemake
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
#! /usr/bin/env bash
set -Eeu
SCRIPT_NAME="$(basename "$0")"
RECIPE=recipe.json
CHEESE_DIR="$(dirname "$(readlink -f "$0")")"
RUN_DIR="$(pwd)"
UPDATE_FLAG="$(if cp -u "$0" /dev/null 2> /dev/null; then echo -u; fi)"
export LD_LIBRARY_PATH=build/lib
map()
{
while read -r line; do
if [[ -n $line ]]; then
$@ $line
fi
done
}
parmap()
{
local pids=()
while read -r line; do
if [[ -n $line ]]; then
$@ $line &
pids+=($!)
fi
done
for pid in ${pids[@]}; do
wait "$pid"
done
}
filter()
{
while read -r line; do
if $@ $line &> /dev/null; then
echo $line
fi
done
}
not()
{
while read -r line; do
if ! $@ $line &> /dev/null; then
echo $line
fi
done
}
all()
{
while read -r line; do
if ! $@ $line &> /dev/null; then
return 1
fi
done
}
verbose()
{
echo "$@"
$@
}
identity()
{
cat
}
blank()
{
echo ''
}
non_empty()
{
sed "/^$/d"
}
is_null()
{
echo "$1" | grep -q null
}
get_modules()
{
jq -c .modules "$RECIPE" | not is_null | jq -r .[]
}
clean_module()
{
(cd "$1"; clean)
}
clean()
{
get_modules | map clean_module
rm -rf build
}
object_file()
{
echo "build/$1.o"
}
get_output()
{
echo "$1" | tr '.' ' ' | map object_file
}
has_object()
{
test -f "$(get_output "$1")"
}
is_executable()
{
jq -r .packaging "$RECIPE" | grep -q executable
}
is_shared()
{
jq -r .packaging "$RECIPE" | grep -q shared
}
is_archive()
{
jq -r .packaging "$RECIPE" | grep -q archive
}
get_name()
{
jq -r .name "$RECIPE"
}
get_executable_name()
{
get_name | echo "build/bin/$(cat)"
}
get_shared_name()
{
get_name | echo "build/lib/lib$(cat).so"
}
get_archive_name()
{
get_name | echo "build/lib/lib$(cat).a"
}
get_binaries()
{
if is_executable; then
get_executable_name
fi
if is_shared; then
get_shared_name
fi
if is_archive; then
get_archive_name
fi
}
has_file()
{
test -f "$1"
}
has_module()
{
if ! [[ -d "$1" ]]; then
return 1
fi
(cd "$1"; get_binaries) | all has_file
}
has_object_or_module()
{
has_object "$1" || has_module "$1"
}
filter_old_hashes()
{
cat build/hashes 2> /dev/null | filter "$1" > build/oldhashes
mv build/oldhashes build/hashes
}
format_hash()
{
echo "$1 $2"
}
hash_submodule()
{
sort "$1/build/hashes" | echo "$1" "$(cat | openssl dgst -r)" | map format_hash
}
get_sources()
{
find "$1" -name "*.c*"
}
all_sources()
{
get_sources src
get_sources test
}
copy_all()
{
cp -R $UPDATE_FLAG "$1" "$2"
}
copy_binaries_in()
{
copy_all build/lib "$1/build"
copy_all build/bin "$1/build"
copy_all build/include "$1/build"
}
match_include()
{
echo "$(expr "$*" : "#[[:space:]]*include[[:space:]]\{1,\}\"\(.\{1,\}\)\"" 2> /dev/null)"
}
first()
{
echo "$1"
}
first_directory()
{
echo "$1" | tr '/' ' ' | map first
}
header_file()
{
echo "$(dirname "$1")/$2"
echo "$(dirname "$1" | map first_directory)/$2"
}
shift_directory()
{
shift
echo "$@"
}
within_directory()
{
echo "$1" | tr -d ' ' | tr '/' ' ' | map shift_directory | tr ' ' '/'
}
find_header()
{
local path="$(echo "$1" | map within_directory)"
if [[ -f "$1" ]]; then
echo "$1"
elif [[ -f "src/$path" ]]; then
echo "src/$path"
elif [[ -f "build/include/$path" ]]; then
echo "build/include/$path"
fi
}
with_headers()
{
cat "$1" | map match_include | map header_file "$1" | map find_header
echo "$1"
}
read_file()
{
cat "$1" 2> /dev/null
}
generate_hash()
{
echo "$1" | map with_headers | map read_file | echo "$1" "$(cat | openssl dgst -r)" | map format_hash
}
module_sources()
{
get_modules | map hash_submodule
all_sources | map generate_hash
}
hash_module()
{
(cd "$1"; module_sources | sort | echo "$1" "$(cat | openssl dgst -r)" | map format_hash)
}
new_hash()
{
echo "$1" | grep -v "^---" | grep "^-"
}
shift_character()
{
echo "${1:1}"
}
hash_name()
{
echo "$1" | map shift_character
}
remove_hash()
{
grep -v "$1" build/hashes > build/oldhashes || true
mv build/oldhashes build/hashes || true
echo "$1"
}
rebuild_module()
{
(cd "$1"; verify_recipe)
hash_submodule "$1" >> build/hashes
}
copy_binaries_out()
{
copy_all "$1/build/lib" build
copy_all "$1/build/bin" build
copy_all "$1/build/include" build
}
verify_module()
{
copy_binaries_in "$1" || true
(cd "$1"; verify_modules)
echo "$1" | map hash_module | sort | diff -u - <(sort build/hashes 2> /dev/null) | filter new_hash | map hash_name | map remove_hash | map rebuild_module
copy_binaries_out "$1" || true
}
verify_modules()
{
all_sources > /dev/null
mkdir -p build/bin
mkdir -p build/lib
mkdir -p build/include
filter_old_hashes has_object_or_module
get_modules | map verify_module
}
print_making()
{
echo "-------------------------------------------------------
Making $*
-------------------------------------------------------"
}
get_plugin_and_config()
{
echo "$(echo "$@" | jq -r .name).chevre" "$(echo "$@" | jq -c .config)"
}
run_plugin()
{
local plugin=$1
shift
if [[ -f "$plugin" ]]; then
(source "$plugin" "$@")
elif [[ -f "$RUN_DIR/$plugin" ]]; then
(source "$RUN_DIR/$plugin" "$@")
else
(source "$CHEESE_DIR/$plugin" "$@")
fi
}
run_plugins()
{
jq -c .plugins "$RECIPE" | not is_null | jq -c .[] | filter "$1" | map get_plugin_and_config | map run_plugin
}
is_validation()
{
echo "$@" | jq .phase | grep -q validate
}
validate_recipe()
{
print_making "$(get_name)"
run_plugins is_validation
}
get_packages()
{
jq -c .dependencies "$RECIPE" | not is_null | jq -c .[]
}
is_static_dependency()
{
echo "$@" | jq .scope | grep -q static
}
is_module_dependency()
{
echo "$@" | jq .scope | grep -q module
}
configure_package()
{
pkg-config "$($1)" "$2"
}
get_package()
{
jq -r .package | map configure_package "$1"
}
include_packages()
{
echo --cflags-only-I
}
get_includes()
{
get_packages | not is_module_dependency | "$1" | get_package include_packages
}
fail_on_null()
{
if [[ "$1" == null ]]; then
return 1
else
echo "$@"
fi
}
get_linker()
{
jq -r .linker "$RECIPE" | map fail_on_null
}
get_compiler()
{
jq -r .compiler "$RECIPE"
}
define_flag()
{
local value
local key="$1"
shift
value=$($@)
if [[ $? -eq 0 ]]; then
if [[ -n "$value" ]]; then
echo "-D$key=$value"
else
echo "-D$key"
fi
fi
}
get_defines()
{
jq -c .define "$RECIPE" | not is_null | jq -r "to_entries[] | [.key, .value] | @tsv" | map define_flag
}
compile()
{
verbose $(get_compiler) $(get_defines) -c $($1) -o $(get_output "$4") $($2 $4) -Isrc -Ibuild/include $(get_includes "$3") $4
if [[ $? -eq 0 ]]; then
generate_hash "$4" >> build/hashes
fi
}
get_missing_sources()
{
get_sources "$1" | map generate_hash | sort | diff -u - <(sort build/hashes) | filter new_hash | map hash_name
}
make_directory()
{
mkdir -p "build/$(dirname "$1")"
echo "$1"
}
compilation_flag()
{
if is_shared; then
echo -fPIC
fi
}
is_test_dependency()
{
echo "$@" | jq .scope | grep -q test
}
source_dependencies()
{
not is_test_dependency
}
compile_source()
{
get_missing_sources src | map remove_hash | map make_directory | parmap compile compilation_flag blank source_dependencies
}
compile_recipe()
{
validate_recipe
mkdir -p build/src
compile_source
}
in_source()
{
echo "-Isrc/$1"
}
include_source()
{
echo "-Itest $(dirname "$1" | map within_directory | map in_source)"
}
compile_tests()
{
get_missing_sources test | map remove_hash | map make_directory | parmap compile blank include_source identity
}
library_packages()
{
echo --libs
}
is_valid_flag()
{
echo "int main(void) {return 0;}" | "$(get_compiler | map first)" -x c -E - "$1"
}
filter_valid_flags()
{
tr ' ' '\n' | filter is_valid_flag
}
get_links()
{
get_packages | not is_static_dependency | not is_module_dependency | "$1" | get_package library_packages | filter_valid_flags
}
static_library_packages()
{
echo --static --libs
}
get_static_links()
{
get_packages | filter is_static_dependency | not is_module_dependency | "$1" | get_package static_library_packages | filter_valid_flags
}
get_link()
{
echo "-l$1"
}
get_module_links()
{
get_packages | filter is_module_dependency | "$1" | jq -r .package | map get_link
}
link_statically()
{
echo -Wl,-Bstatic
echo "$@"
}
link()
{
verbose $(get_linker || get_compiler | map first) $($1) -o $4 $5 $($2) -Lbuild/lib $(get_module_links "$3") $(get_links "$3") $(get_static_links "$3" | non_empty | map link_statically)
}
is_executable_object()
{
nm "$1" | grep -q "T main$"
}
executable_file()
{
echo "$1"
}
get_executable()
{
echo "$1" | tr '.' ' ' | map executable_file
}
get_executable_and_object()
{
echo "$(get_executable "$1") $1"
}
get_test_objects()
{
all_sources | map get_output | not is_executable_object
}
link_tests()
{
get_sources test | map get_output | filter is_executable_object | map get_executable_and_object | parmap link blank get_test_objects identity
}
run_tests()
{
get_sources test | map get_output | filter is_executable_object | map get_executable | map verbose
}
test_recipe()
{
compile_recipe
mkdir -p build/test
compile_tests
link_tests
run_tests
}
archive()
{
verbose ar rcs $(get_archive_name) $(get_source_objects)
}
shared_flag()
{
echo -shared
}
get_source_objects()
{
get_sources src | map get_output
}
make_include_directory()
{
mkdir -p "build/include/$(dirname "$1")"
echo "$1"
}
copy_header()
{
cp "src/$1" "build/include/$1"
}
copy_headers()
{
find src -name "*.h*" | map within_directory | map make_include_directory | map copy_header
}
package_recipe()
{
test_recipe
if is_executable; then
link blank get_source_objects source_dependencies "$(get_executable_name)" ""
fi
if is_shared; then
link shared_flag get_source_objects source_dependencies "$(get_shared_name)" ""
fi
if is_archive; then
archive
fi
copy_headers
}
is_verification()
{
echo "$@" | jq .phase | grep -q verify
}
verify_recipe()
{
package_recipe
run_plugins is_verification
}
get_arguments()
{
jq -r .args "$RECIPE" | not is_null
}
run_recipe()
{
verify_recipe
if is_executable; then
verbose $(get_executable_name) $(get_arguments)
elif is_shared; then
echo "Library $(get_shared_name) is not executable."
exit 1
elif is_archive; then
echo "Library $(get_archive_name) is not executable."
exit 1
fi
}
print_usage()
{
echo "usage: $SCRIPT_NAME [clean] <phase>
Cheesemake runs ALL phases in order up to the one specified.
Phases:
validate Run plugins such as static analysis tools.
compile Compile sources in the src directory and output them to build/src.
test Compile and run tests in the test directory.
package Create a binary in build/bin or build/lib.
verify Run plugins such as dynamic analysis tools.
run Run build/bin/<recipe name>."
}
do_task()
{
case $1 in
clean) clean;;
validate) verify_modules; validate_recipe;;
compile) verify_modules; compile_recipe;;
test) verify_modules; test_recipe;;
package) verify_modules; package_recipe;;
verify) verify_modules; verify_recipe;;
run) verify_modules; run_recipe;;
*) print_usage; exit 1;;
esac
}
for task in "$@"; do
do_task "$task"
done
if [[ $# -eq 0 ]]; then
print_usage
fi