File: tup.sh

package info (click to toggle)
tup 0.8-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,076 kB
  • sloc: ansic: 256,651; sh: 19,101; perl: 184; python: 67; lisp: 63; makefile: 56
file content (821 lines) | stat: -rwxr-xr-x 15,638 bytes parent folder | download | duplicates (2)
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
#! /bin/sh
# tup - A file-based build system
#
# Copyright (C) 2008-2024  Mike Shal <marfey@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

tupcurdir=$PWD

# Prefix PATH so the test cases run the local tup
PATH=$PWD/..:$PATH
export PATH

testname=`echo $0 | sed 's/.*\///; s/\.sh//'`
tuptestdir="tuptesttmp-$testname"

# tupos may be set already by test.sh - this is an optimization to
# avoid extra forks in win32
: ${tupos:=`uname -s`}

rm -rf $tuptestdir
mkdir $tuptestdir
cd $tuptestdir
tup init --no-sync --force

generate_script_name="build.sh"
case $tupos in
CYGWIN*)
	# Avoid problems with CR/LF vs LF
	alias diff='diff -b'
	in_windows=1
	generate_script_name="build.bat"
;;
Linux)
	# Workaround for Linux using low-resolution filesystem timestamps based
	# on CONFIG_HZ.
	TUP_TESTING_HZ=$(zcat /proc/config.gz 2>/dev/null | grep '^CONFIG_HZ=' | sed 's/.*=//')
	if [ "$TUP_TESTING_HZ" = "" ]; then
		TUP_TESTING_HZ=$(cat /boot/config-$(uname -r) | grep '^CONFIG_HZ=' | sed 's/.*=//')
		if [ "$TUP_TESTING_HZ" = "" ]; then
			TUP_TESTING_HZ=100
		fi
	fi
	export TUP_TESTING_HZ
;;
esac

# override any user settings for grep
GREP_OPTIONS=""
export GREP_OPTIONS

symeotup()
{
	echo "'touch -h' is not supported - skipping test." 1>&2
	eotup
}

symtouch()
{
# Some machines may not support 'touch -h' to change the timestamp on the
# symlink. In these cases, just quit the test.
	touch -h $@ || symeotup
}

check_empty_tupdirs()
{
	if tup flags_exists; then
		:
	else
		echo "*** Nodes shouldn't have flags set" 1>&2
		exit 1
	fi
}

sym_check()
{
	f=$1
	shift
	if [ ! -f $f ]; then
		echo "*** Object file does not exist: $f" 1>&2
		exit 1
	fi
	while [ $# -gt 0 ]; do
		sym=$1
		if echo $sym | grep '^^' > /dev/null; then
			sym=`echo $sym | sed 's/^^//'`
			if nm $f | grep $sym > /dev/null; then
				echo "*** '$sym' shouldn't exist in '$f'" 1>&2
				exit 1
			fi
		else
			if nm $f | grep $sym > /dev/null; then
				:
			else
				echo "*** No symbol '$sym' in object '$f'" 1>&2
				exit 1
			fi
		fi
		shift
	done
}

check_exist()
{
	while [ $# -gt 0 ]; do
		if [ ! -e $1 ]; then
			echo "*** File '$1' does not exist when it should" 1>&2
			exit 1
		fi
		shift
	done
}

check_not_exist()
{
	while [ $# -gt 0 ]; do
		if [ -e $1 ]; then
			echo "*** File '$1' exists when it shouldn't" 1>&2
			exit 1
		fi
		shift
	done
}

tup_object_exist()
{
	dir=$1
	shift
	if [ $# -le 0 ]; then
		echo "*** tup_object_exist needs a dir and files" 1>&2
		exit 1
	fi
	while [ $# -gt 0 ]; do
		if tup node_exists $dir "$1"; then
			:
		else
			echo "*** Missing node \"$1\" from .tup/db" 1>&2
			exit 1
		fi
		shift
	done
}

tup_object_no_exist()
{
	dir=$1
	shift
	if [ $# -le 0 ]; then
		echo "*** tup_object_no_exist needs a dir and files" 1>&2
		exit 1
	fi
	while [ $# -gt 0 ]; do
		# stderr redirection is to ignore warnings about non-existant
		# directories when checking for ghost files
		if tup node_exists $dir "$1" 2>/dev/null; then
			echo "*** Node \"$1\" exists in .tup/db when it shouldn't" 1>&2
			exit 1
		fi
		shift
	done
}

dep_exist_internal()
{
	link_type=$1
	shift
	set +e
	tup ${link_type}_exists "$1" "$2" "$3" "$4"
	rc=$?
	set -e
	if [ "$rc" = "11" ]; then
		:
	elif [ "$rc" = "0" ]; then
		echo "*** Dependency from $2 [$1] -> $4 [$3] does not exist" 1>&2
		exit 1
	else
		echo "*** link_exists() failed." 1>&2
		exit 1
	fi
}

dep_no_exist_internal()
{
	link_type=$1
	shift
	if tup node_exists "$1" "$2"; then
		if tup node_exists "$3" "$4"; then
			set +e
			tup ${link_type}_exists "$1" "$2" "$3" "$4"
			rc=$?
			set -e
			if [ "$rc" = "11" ]; then
				echo "*** Dependency from $2 [$1] -> $4 [$3] exists when it shouldn't" 1>&2
				exit 1
			elif [ "$rc" = "0" ]; then
				:
			else
				echo "*** link_exists() failed." 1>&2
				exit 1
			fi
		else
			echo "*** Object $4 [$3] does not exist" 1>&2
			exit 1;
		fi
	else
		echo "*** Object $2 [$1] does not exist" 1>&2
		exit 1;
	fi
}

tup_dep_exist()
{
	dep_exist_internal normal "$1" "$2" "$3" "$4"
}

tup_dep_no_exist()
{
	dep_no_exist_internal normal "$1" "$2" "$3" "$4"
}

tup_sticky_exist()
{
	dep_exist_internal sticky "$1" "$2" "$3" "$4"
}

tup_sticky_no_exist()
{
	dep_no_exist_internal sticky "$1" "$2" "$3" "$4"
}

set_leak_check()
{
	leak_check=$1
}

__update()
{
	server=`tup server`
	if [ "$server" = "fuse" -o "$server" = "fuse3" ]; then
		sim_hints="--sim-hints=fuse-compatible"
	fi
	if [ -n "$TUP_VALGRIND" ]; then
		cmd="valgrind -q --error-exitcode=11 $sim_hints --track-fds=yes --track-origins=yes --leak-check=${leak_check-full} tup"
	elif [ -n "$TUP_HELGRIND" ]; then
		cmd="valgrind -q --error-exitcode=12 $sim_hints --tool=helgrind tup"
	else
		cmd="tup"
	fi

	set +e
	$cmd $@
	rc=$?
	set -e

	if [ "$rc" = "11" ]; then
		echo "*** Failed valgrind!" 1>&2
		exit 1
	fi
	if [ "$rc" = "12" ]; then
		echo "*** Failed helgrind!" 1>&2
		exit 1
	fi

	if [ "$rc" = "0" ]; then
		:
	else
		echo "*** Failed to update!" 1>&2
	fi
	return "$rc"
}

update()
{
	if ! __update $@; then
		exit 1
	fi
	check_empty_tupdirs
}

update_partial()
{
	if ! __update $@; then
		exit 1
	fi
}

update_fail()
{
	set_leak_check no
	if __update $@ 2>/dev/null; then
		echo "*** Expected update to fail, but didn't" 1>&2
		exit 1
	else
		echo "Update expected to fail, and did"
	fi
	set_leak_check full
}

update_fail_msg()
{
	set_leak_check no
	if __update 2>.tup/.tupoutput; then
		echo "*** Expected update to fail, but didn't" 1>&2
		exit 1
	else
		while [ $# -gt 0 ]; do
			if grep "$1" .tup/.tupoutput > /dev/null; then
				echo "Update expected to fail, and failed for the right reason."
			else
				echo "*** Update expected to fail because of: $1" 1>&2
				echo "*** But failed because of:" 1>&2
				cat .tup/.tupoutput 1>&2
				exit 1
			fi
			shift
		done
	fi
	set_leak_check full
}

update_null()
{
	if ! __update > .tup/.tupoutput 2>&1; then
		cat .tup/.tupoutput
		echo "Error: Expected update_null() to exit successfully." 1>&1
		exit 1
	fi
	for i in "Tupfiles" "files" "commands"; do
		if ! grep "No $i" .tup/.tupoutput > /dev/null; then
			cat .tup/.tupoutput
			echo "Error: $1 (Expected \"No $i\" in tup output)" 1>&2
			exit 1
		fi
	done
}

parse()
{
	if ! __update parse; then
		exit 1
	fi
}

parse_fail_msg()
{
	if tup parse 2>.tup/.tupoutput; then
		echo "*** Expected parsing to fail, but didn't" 1>&2
		exit 1
	else
		if grep "$1" .tup/.tupoutput > /dev/null; then
			echo "Parsing expected to fail, and failed for the right reason."
		else
			echo "*** Parsing expected to fail because of: $1" 1>&2
			echo "*** But failed because of:" 1>&2
			cat .tup/.tupoutput 1>&2
			exit 1
		fi
	fi
}

refactor()
{
	if ! tup refactor $@; then
		exit 1
	fi
}

refactor_fail_msg()
{
	if tup refactor 2>.tup/.tupoutput; then
		echo "*** Expected refactoring to fail, but didn't" 1>&2
		exit 1
	else
		if grep "$1" .tup/.tupoutput > /dev/null; then
			echo "Refactoring expected to fail, and failed for the right reason."
		else
			echo "*** Refactoring expected to fail because of: $1" 1>&2
			echo "*** But failed because of:" 1>&2
			cat .tup/.tupoutput 1>&2
			exit 1
		fi
	fi
}

check_same_link()
{
	if stat $* | grep Inode | awk 'BEGIN{x=-1} {if(x == -1) {x=$4} if(x != $4) {exit 1}}'; then
		:
	else
		echo "*** Files '$*' are not the same inode." 1>&2
		exit 1
	fi
}

check_updates()
{
	if [ ! -f $2 ]; then
		echo "check_updates($1, $2) failed because $2 doesn't already exist"
		exit 1
	fi
	rm $2
	tup touch $1
	update --no-scan
	if [ ! -f $2 ]; then
		echo "check_updates($1, $2) failed to re-create $2"
		exit 1
	fi
}

check_no_updates()
{
	if [ ! -f $2 ]; then
		echo "check_no_updates($1, $2) failed because $2 doesn't already exist"
		exit 1
	fi
	mv $2 $2_check_no_updates.bak
	tup touch $1
	update --no-scan
	if [ -f $2 ]; then
		echo "check_no_updates($1, $2) re-created $2 when it shouldn't have"
		exit 1
	fi
	mv $2_check_no_updates.bak $2
}

gitignore_bad()
{
	if grep "$1" $2 > /dev/null; then
		echo "Error: $1 found in $2" 1>&2
		exit 1
	fi
}

gitignore_good()
{
	if grep "$1" $2 > /dev/null; then
		:
	else
		echo "Error: $1 not found in $2" 1>&2
		exit 1
	fi
}

log_good()
{
	gitignore_good "$1" .tup/log/debug.log.0
}

log_graph_good()
{
	gitignore_good "$2" .tup/log/$1.dot.0
}

log_graph_bad()
{
	gitignore_bad "$2" .tup/log/$1.dot.0
}

vardict_exist()
{
	if grep "\<$1\>" .tup/vardict > /dev/null; then
		:
	else
		echo "Error: $1 not found in vardict file" 1>&2
		exit 1
	fi
}

vardict_no_exist()
{
	if grep "\<$1\>" .tup/vardict > /dev/null 2>/dev/null; then
		echo "Error: $1 found in vardict file when it shouldn't" 1>&2
		exit 1
	fi
}

varsetall()
{
	rm -f tup.config
	while [ $# -gt 0 ]; do
		if echo "$1" | grep "=n$" > /dev/null; then
			var=`echo "$1" | sed 's/=.*//'`
			echo "# CONFIG_$var is not set" >> tup.config
		else
			echo CONFIG_$1 >> tup.config
		fi
		shift
	done
}

monitor()
{
	server=`tup server`
	if [ "$server"  = "fuse" -o "$server" = "fuse3" ]; then
		sim_hints="--sim-hints=fuse-compatible"
	fi
	if [ -n "$TUP_VALGRIND" ]; then
		cmd="valgrind -q --error-exitcode=11 $sim_hints --track-fds=yes --track-origins=yes --leak-check=full tup monitor -f"
	elif [ -n "$TUP_HELGRIND" ]; then
		cmd="valgrind -q --error-exitcode=12 $sim_hints --tool=helgrind tup monitor -f"
	else
		cmd="tup monitor -f"
	fi

	if $cmd "$@" & then
		monitor_pid=$!
		# The monitor may not actually have the lock yet - use waitmon to wait
		# until it is started
		tup waitmon
	else
		echo "*** Failed to run the monitor!" 1>&2
		exit 1
	fi
	monitor_running=1
}

wait_monitor()
{
	wait $monitor_pid
	if [ $? != 0 ]; then
		echo "Error: monitor (pid $monitor_pid) exited with error code $?" 1>&2
		exit 1
	fi
	monitor_running=0
}

stop_monitor()
{
	tup flush
	if tup stop; then :; else
		echo "Error: tup monitor no longer running when it should be" 1>&2
		exit 1
	fi
	wait_monitor
}

signal_monitor()
{
	if [ -f .tup/monitor.pid ]; then
		# It's really confusing if you happen to 'kill -USR1 -1', cuz
		# everything disappears.
		text=`cat .tup/monitor.pid`
		if echo "$text" | grep '\-1' > /dev/null; then
			echo "Error: Monitor is not running - unable to signal" 1>&2
			exit 1
		fi

		kill -USR1 $text
	else
		echo "Error: No monitor.pid file running - unable to signal" 1>&2
		exit 1
	fi
}

generate()
{
	if [ -n "$TUP_VALGRIND" ]; then
		cmd="valgrind -q --error-exitcode=11 --sim-hints=fuse-compatible --track-fds=yes --track-origins=yes --leak-check=full tup generate"
	elif [ -n "$TUP_HELGRIND" ]; then
		cmd="valgrind -q --error-exitcode=12 --sim-hints=fuse-compatible --tool=helgrind tup generate"
	else
		cmd="tup generate"
	fi
	$cmd "$@"
}

generate_fail_msg()
{
	msg=$1
	shift
	if tup generate "$@" 2>.tupoutput; then
		echo "*** Expected generate to fail, but didn't" 1>&2
		exit 1
	else
		if grep "$msg" .tupoutput > /dev/null; then
			echo "Generate expected to fail, and failed for the right reason."
		else
			echo "*** Generate expected to fail because of: $msg" 1>&2
			echo "*** But failed because of:" 1>&2
			cat .tupoutput 1>&2
			exit 1
		fi
	fi
	rm .tupoutput
}

compiledb()
{
	if [ -n "$TUP_VALGRIND" ]; then
		cmd="valgrind -q --error-exitcode=11 --sim-hints=fuse-compatible --track-fds=yes --track-origins=yes --leak-check=full tup compiledb"
	elif [ -n "$TUP_HELGRIND" ]; then
		cmd="valgrind -q --error-exitcode=12 --sim-hints=fuse-compatible --tool=helgrind tup compiledb"
	else
		cmd="tup compiledb"
	fi
	$cmd "$@"
}

re_init()
{
	rm -rf .tup
	tup init --no-sync --force
}

make_tup_client()
{
	cat > client.c << HERE
#include "../../tup_client.h"
#include <stdio.h>

int main(int argc, char **argv)
{
	const char *value;
	if(tup_vardict_init() < 0)
		return 1;
	while(argc > 1) {
		value = tup_config_var(argv[1], -1);
		if(value)
			printf("%s\n", value);
		argc--;
		argv++;
	}
	return 0;
}
HERE

	plat_ldflags=""
	if [ "$tupos" = "SunOS" ]; then
		plat_ldflags="$plat_ldflags -lsocket"
	fi
	if ldd ../../tup | grep 'libasan' > /dev/null; then
		plat_ldflags="$plat_ldflags -lasan -lubsan"
	fi
	gcc client.c ../../libtup_client.a -o client $plat_ldflags -ldl
}

check_monitor_supported()
{
	if tup monitor_supported; then
		:
	else
		echo "Monitor is not supported. Skipping test."
		eotup
	fi
}

check_windows()
{
	case $tupos in
	CYGWIN*)
		return
		;;
	MINGW*)
		return
		;;
	esac
	echo "Only supported in Windows. Skipping test."
	eotup
}

check_no_ldpreload()
{
	case `tup server` in
	ldpreload)
		echo "Skipping test for LD_PRELOAD shim in Linux: $1"
		eotup
	esac
}

check_no_windows()
{
	case `tup server` in
	ldpreload)
		# The LD_PRELOAD shim doesn't support run-scripts or the client library.
		for var in "$@"; do
			if [ "$var" = "run-script" ]; then
				echo "Skipping run-script test for LD_PRELOAD shim"
				eotup
			elif [ "$var" = "client" ]; then
				echo "Skipping client test for LD_PRELOAD shim"
				eotup
			fi
		done
	esac

	case $tupos in
	CYGWIN*)
		echo "Not supported in Windows. Skipping test."
		eotup
		;;
	MINGW*)
		echo "Not supported in Windows. Skipping test."
		eotup
		;;
	esac
}

check_no_osx()
{
	case $tupos in
	Darwin*)
		echo "Not supported in OSX. Skipping test."
		eotup
		;;
	esac
}

check_no_freebsd()
{
	case $tupos in
	FreeBSD*)
		echo "Not supported in FreeBSD. Skipping test."
		eotup
		;;
	esac
}

check_tup_no_suid()
{
	if ! tup privileged; then
		echo "Tup is privileged, but this test requires that it does not. Skipping test."
		eotup
	fi
}

check_tup_suid()
{
	if tup privileged; then
		echo "Tup needs to be privileged for this test to run. Skipping test."
		eotup
	fi
}

check_python()
{
	if ! which python3 > /dev/null 2>&1; then
		echo "No python3 found - skipping test."
		eotup
	fi
}

check_bash()
{
    if [ ! -e /usr/bin/env ]; then
		echo "/usr/bin/env not found - skipping test."
		eotup
    elif ! which bash > /dev/null 2>&1; then
		echo "No bash found - skipping test."
		eotup
    fi
}

check_ccache_version()
{
	min_version="4.7"
	version=$( (echo $min_version; ccache --version | grep 'ccache version' | sed 's/ccache version //') | sort -V | head -1)
	if [ "$version" != "$min_version" ]; then
		echo "Skipping test: Expected ccache version >= $min_version"
		eotup
	fi
}

single_threaded()
{
	(echo "[updater]"; echo "num_jobs=1") >> .tup/options
}

set_autoupdate()
{
	(echo "[monitor]"; echo "autoupdate=1") >> .tup/options
}

set_full_deps()
{
	(echo "[updater]"; echo "full_deps=1") >> .tup/options
}

set_quiet()
{
	(echo "[display]"; echo "quiet=1") >> .tup/options
}

clear_full_deps()
{
	(echo "[updater]"; echo "full_deps=0") >> .tup/options
}

tup_ln_cmd() {
	printf "!tup_ln $1 $2"
}

eotup()
{
	if [ "$monitor_running" = "1" ]; then
		stop_monitor
	fi
	cd $tupcurdir
	if [ -f "$tuptestdir/.tup/mnt/.metadata_never_index" ]; then
		rm "$tuptestdir/.tup/mnt/.metadata_never_index"
	fi
	if [ -d "$tuptestdir/.tup/mnt" ]; then
		mntdir=`find $tuptestdir/.tup/mnt -maxdepth 0 -not -empty`
		if [ "$mntdir" != "" ]; then
			echo "Error: $mntdir is not empty yet. Is fuse still mounted?" 1>&2
			exit 1
		fi
	fi
	rm -rf $tuptestdir
	exit 0
}