xref: /aosp_15_r20/external/llvm/utils/check-each-file (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker#!/bin/sh
2*9880d681SAndroid Build Coastguard Worker# check-each-file
3*9880d681SAndroid Build Coastguard Worker# Used to narrow down a miscompilation to one .o file from a list. Please read
4*9880d681SAndroid Build Coastguard Worker# the usage procedure, below, for command-line syntax (or run it with --help).
5*9880d681SAndroid Build Coastguard Worker# This script depends on the llvm-native-gcc script.
6*9880d681SAndroid Build Coastguard Worker
7*9880d681SAndroid Build Coastguard Workerif [ x$1 = x--make-linker-script ]
8*9880d681SAndroid Build Coastguard Workerthen
9*9880d681SAndroid Build Coastguard Worker	program=$2
10*9880d681SAndroid Build Coastguard Worker	linker=./link-$program
11*9880d681SAndroid Build Coastguard Worker	echo "Building $program with llvm-native-gcc"
12*9880d681SAndroid Build Coastguard Worker	rm -f $program
13*9880d681SAndroid Build Coastguard Worker	gmake -e $program CC=llvm-native-gcc CXX=llvm-native-gxx
14*9880d681SAndroid Build Coastguard Worker	echo "Erasing $program and re-linking it"
15*9880d681SAndroid Build Coastguard Worker	rm -f $program
16*9880d681SAndroid Build Coastguard Worker	echo "rm -f $program" > $linker
17*9880d681SAndroid Build Coastguard Worker	gmake -n $program >> $linker
18*9880d681SAndroid Build Coastguard Worker	chmod 755 $linker
19*9880d681SAndroid Build Coastguard Worker	echo "Linker script created in $linker; testing it out"
20*9880d681SAndroid Build Coastguard Worker	output=`./$linker 2>&1`
21*9880d681SAndroid Build Coastguard Worker	case "$output" in
22*9880d681SAndroid Build Coastguard Worker		*undefined*reference*__main*)
23*9880d681SAndroid Build Coastguard Worker			echo "$program appears to need a dummy __main function; adding one"
24*9880d681SAndroid Build Coastguard Worker			echo "void __main () { }" > __main.c
25*9880d681SAndroid Build Coastguard Worker			gcc -c __main.c
26*9880d681SAndroid Build Coastguard Worker			echo "Done; rebuilding $linker"
27*9880d681SAndroid Build Coastguard Worker			echo "rm -f $program" > $linker
28*9880d681SAndroid Build Coastguard Worker			gmake -n $program 2>&1 | sed '/gcc/s/$/__main.o/' >> $linker
29*9880d681SAndroid Build Coastguard Worker			./$linker > /dev/null 2>&1
30*9880d681SAndroid Build Coastguard Worker			if [ ! -x $program ]
31*9880d681SAndroid Build Coastguard Worker			then
32*9880d681SAndroid Build Coastguard Worker				echo "WARNING: linker script didn't work"
33*9880d681SAndroid Build Coastguard Worker			fi
34*9880d681SAndroid Build Coastguard Worker			;;
35*9880d681SAndroid Build Coastguard Worker		*)
36*9880d681SAndroid Build Coastguard Worker			if [ ! -x $program ]
37*9880d681SAndroid Build Coastguard Worker			then
38*9880d681SAndroid Build Coastguard Worker				echo "WARNING: linker script didn't work"
39*9880d681SAndroid Build Coastguard Worker			fi
40*9880d681SAndroid Build Coastguard Worker			;;
41*9880d681SAndroid Build Coastguard Worker	esac
42*9880d681SAndroid Build Coastguard Worker	echo "Linker script created in $linker; please check it manually"
43*9880d681SAndroid Build Coastguard Worker	exit 0
44*9880d681SAndroid Build Coastguard Workerfi
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Workercheckfiles="$1"
47*9880d681SAndroid Build Coastguard Workerprogram="$2"
48*9880d681SAndroid Build Coastguard Workerlinker="$3"
49*9880d681SAndroid Build Coastguard Workerchecker="$4"
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Workerusage () {
52*9880d681SAndroid Build Coastguard Worker	myname=`basename $0`
53*9880d681SAndroid Build Coastguard Worker	echo "$myname --make-linker-script PROGRAM"
54*9880d681SAndroid Build Coastguard Worker	echo "$myname OBJECTS-FILE PROGRAM LINKER CHECKER"
55*9880d681SAndroid Build Coastguard Worker	echo ""
56*9880d681SAndroid Build Coastguard Worker	echo "OBJECTS-FILE is a text file containing the names of all the .o files"
57*9880d681SAndroid Build Coastguard Worker	echo "PROGRAM is the name of the executable under test"
58*9880d681SAndroid Build Coastguard Worker	echo "(there must also exist a Makefile in the current directory which"
59*9880d681SAndroid Build Coastguard Worker	echo "has PROGRAM as a target)"
60*9880d681SAndroid Build Coastguard Worker	echo "LINKER is the script that builds PROGRAM; try --make-linker-script"
61*9880d681SAndroid Build Coastguard Worker	echo "to automatically generate it"
62*9880d681SAndroid Build Coastguard Worker	echo "CHECKER is the script that exits 0 if PROGRAM is ok, 1 if it is not OK"
63*9880d681SAndroid Build Coastguard Worker	echo "(LINKER and CHECKER must be in your PATH, or you should specify ./)"
64*9880d681SAndroid Build Coastguard Worker	echo ""
65*9880d681SAndroid Build Coastguard Worker	echo "Bugs to <[email protected]>."
66*9880d681SAndroid Build Coastguard Worker	exit 1
67*9880d681SAndroid Build Coastguard Worker}
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Workerif [ x$1 = x--help ]
70*9880d681SAndroid Build Coastguard Workerthen
71*9880d681SAndroid Build Coastguard Worker	usage
72*9880d681SAndroid Build Coastguard Workerfi
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Workerif [ -z "$checkfiles" ]
75*9880d681SAndroid Build Coastguard Workerthen
76*9880d681SAndroid Build Coastguard Worker	echo "ERROR: Must specify name of file w/ list of objects as 1st arg."
77*9880d681SAndroid Build Coastguard Worker	echo "(got \"$checkfiles\")"
78*9880d681SAndroid Build Coastguard Worker	usage
79*9880d681SAndroid Build Coastguard Workerfi
80*9880d681SAndroid Build Coastguard Workerif [ ! -f "$checkfiles" ]
81*9880d681SAndroid Build Coastguard Workerthen
82*9880d681SAndroid Build Coastguard Worker	echo "ERROR: $checkfiles not found"
83*9880d681SAndroid Build Coastguard Worker	usage
84*9880d681SAndroid Build Coastguard Workerfi
85*9880d681SAndroid Build Coastguard Workerif [ -z "$program" ]
86*9880d681SAndroid Build Coastguard Workerthen
87*9880d681SAndroid Build Coastguard Worker	echo "ERROR: Must specify name of program as 2nd arg."
88*9880d681SAndroid Build Coastguard Worker	usage
89*9880d681SAndroid Build Coastguard Workerfi
90*9880d681SAndroid Build Coastguard Workerif [ -z "$linker" ]
91*9880d681SAndroid Build Coastguard Workerthen
92*9880d681SAndroid Build Coastguard Worker	echo "ERROR: Must specify name of link script as 3rd arg."
93*9880d681SAndroid Build Coastguard Worker	usage
94*9880d681SAndroid Build Coastguard Workerfi
95*9880d681SAndroid Build Coastguard Workerif [ ! -x "$linker" ]
96*9880d681SAndroid Build Coastguard Workerthen
97*9880d681SAndroid Build Coastguard Worker	echo "ERROR: $linker not found or not executable"
98*9880d681SAndroid Build Coastguard Worker	echo "You may wish to try: $0 --make-linker-script $program"
99*9880d681SAndroid Build Coastguard Worker	usage
100*9880d681SAndroid Build Coastguard Workerfi
101*9880d681SAndroid Build Coastguard Workerif [ -z "$checker" ]
102*9880d681SAndroid Build Coastguard Workerthen
103*9880d681SAndroid Build Coastguard Worker	echo "ERROR: Must specify name of $program check script as 3rd arg."
104*9880d681SAndroid Build Coastguard Worker	usage
105*9880d681SAndroid Build Coastguard Workerfi
106*9880d681SAndroid Build Coastguard Workerif [ ! -x "$checker" ]
107*9880d681SAndroid Build Coastguard Workerthen
108*9880d681SAndroid Build Coastguard Worker	echo "ERROR: $checker not found or not executable"
109*9880d681SAndroid Build Coastguard Worker	usage
110*9880d681SAndroid Build Coastguard Workerfi
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Workerfiles=`cat $checkfiles`
113*9880d681SAndroid Build Coastguard Workerecho "Recompiling everything with llvm-native-gcc"
114*9880d681SAndroid Build Coastguard Workerfor f in $files
115*9880d681SAndroid Build Coastguard Workerdo
116*9880d681SAndroid Build Coastguard Worker	rm -f $f
117*9880d681SAndroid Build Coastguard Worker	gmake $f CC=llvm-native-gcc CXX=llvm-native-gxx
118*9880d681SAndroid Build Coastguard Workerdone
119*9880d681SAndroid Build Coastguard Workerrm -f $program
120*9880d681SAndroid Build Coastguard Worker$linker
121*9880d681SAndroid Build Coastguard Workerif $checker
122*9880d681SAndroid Build Coastguard Workerthen
123*9880d681SAndroid Build Coastguard Worker	echo "Sorry, I can't help you, $program is OK when compiled with llvm-native-gcc"
124*9880d681SAndroid Build Coastguard Worker	exit 1
125*9880d681SAndroid Build Coastguard Workerfi
126*9880d681SAndroid Build Coastguard Workerfor f in $files
127*9880d681SAndroid Build Coastguard Workerdo
128*9880d681SAndroid Build Coastguard Worker	echo Trying to compile $f with native gcc and rebuild $program
129*9880d681SAndroid Build Coastguard Worker	mv ${f} ${f}__OLD__
130*9880d681SAndroid Build Coastguard Worker	gmake ${f} CC=gcc > /dev/null 2>&1
131*9880d681SAndroid Build Coastguard Worker	$linker
132*9880d681SAndroid Build Coastguard Worker	echo Checking validity of new $program
133*9880d681SAndroid Build Coastguard Worker	if $checker
134*9880d681SAndroid Build Coastguard Worker	then
135*9880d681SAndroid Build Coastguard Worker		echo Program is OK
136*9880d681SAndroid Build Coastguard Worker		okfiles="$okfiles $f"
137*9880d681SAndroid Build Coastguard Worker	else
138*9880d681SAndroid Build Coastguard Worker		echo Program is not OK
139*9880d681SAndroid Build Coastguard Worker		notokfiles="$notokfiles $f"
140*9880d681SAndroid Build Coastguard Worker	fi
141*9880d681SAndroid Build Coastguard Worker	mv ${f}__OLD__ ${f}
142*9880d681SAndroid Build Coastguard Workerdone
143*9880d681SAndroid Build Coastguard Workerecho ""
144*9880d681SAndroid Build Coastguard Workerecho "Program is OK when these files are recompiled with native gcc: "
145*9880d681SAndroid Build Coastguard Workerecho "$okfiles"
146*9880d681SAndroid Build Coastguard Workerecho ""
147*9880d681SAndroid Build Coastguard Workerecho "Program is not OK when these files are recompiled with native gcc: "
148*9880d681SAndroid Build Coastguard Workerecho "$notokfiles"
149*9880d681SAndroid Build Coastguard Workerecho ""
150*9880d681SAndroid Build Coastguard Workerexit 0
151