diff options
Diffstat (limited to 'scripts/qemuimage-tests/tools')
-rw-r--r-- | scripts/qemuimage-tests/tools/compiler_test.sh | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/scripts/qemuimage-tests/tools/compiler_test.sh b/scripts/qemuimage-tests/tools/compiler_test.sh new file mode 100644 index 0000000000..9c30d6d78b --- /dev/null +++ b/scripts/qemuimage-tests/tools/compiler_test.sh | |||
@@ -0,0 +1,137 @@ | |||
1 | #!/bin/bash | ||
2 | # compiler test script running in target | ||
3 | # | ||
4 | # Author: Jiajun Xu <jiajun.xu@intel.com> | ||
5 | # | ||
6 | # This file is licensed under the GNU General Public License, | ||
7 | # Version 2. | ||
8 | # | ||
9 | |||
10 | # Prepare test folder for compiler test | ||
11 | COMPILE_FOLDER="/opt/test/compile_test" | ||
12 | TEST_FILE="$COMPILE_FOLDER/compile_test.c" | ||
13 | EXECUTE_FILE="$COMPILE_FOLDER/compile_test" | ||
14 | TEST_MAKEFILE="$COMPILE_FOLDER/makefile" | ||
15 | TEST_LIST="gcc g++ make" | ||
16 | |||
17 | if [ ! -d $COMPILE_FOLDER ]; then | ||
18 | mkdir -p $COMPILE_FOLDER | ||
19 | fi | ||
20 | |||
21 | Target_Info() | ||
22 | { | ||
23 | echo -e "\tTARGET: $*" | ||
24 | } | ||
25 | |||
26 | Target_Err() | ||
27 | { | ||
28 | echo -e "\tTARGET: ##### Error Log #####" | ||
29 | $@ | ||
30 | echo -e "\tTARGET: ##### End #####" | ||
31 | } | ||
32 | |||
33 | # Function to generate a c test file for compiler testing | ||
34 | Gen_File() | ||
35 | { | ||
36 | temp=`mktemp` | ||
37 | |||
38 | # Generate c/c++ test file for compiler testing | ||
39 | echo "#include <stdio.h>" >> $temp | ||
40 | echo "#include <math.h>" >> $temp | ||
41 | echo "" >> $temp | ||
42 | echo "double" >> $temp | ||
43 | echo "convert(long long l)" >> $temp | ||
44 | echo "{" >> $temp | ||
45 | echo " return (double)l; // or double(l)" >> $temp | ||
46 | echo "}" >> $temp | ||
47 | echo "" >> $temp | ||
48 | echo "int" >> $temp | ||
49 | echo "main(int argc, char * argv[])" >> $temp | ||
50 | echo "{" >> $temp | ||
51 | echo " long long l = 10;" >> $temp | ||
52 | echo " double f;" >> $temp | ||
53 | echo "" >> $temp | ||
54 | echo " f = convert(l);" >> $temp | ||
55 | echo " printf(\"convert: %lld => %f\n\", l, f);" >> $temp | ||
56 | echo "" >> $temp | ||
57 | echo " f = 1234.67;" >> $temp | ||
58 | echo " printf(\"floorf(%f) = %f\n\", f, floorf(f));" >> $temp | ||
59 | echo " return 0;" >> $temp | ||
60 | echo "}" >> $temp | ||
61 | echo $temp | ||
62 | } | ||
63 | |||
64 | # Function to generate a makefile for compiler testing | ||
65 | Gen_Makefile() | ||
66 | { | ||
67 | temp=`mktemp` | ||
68 | basename=`basename $EXECUTE_FILE` | ||
69 | |||
70 | echo -e "$basename: $basename.o" >> $temp | ||
71 | echo -e "\tgcc -o $basename $basename.o -lm" >> $temp | ||
72 | echo -e "$basename.o: $basename.c" >> $temp | ||
73 | echo -e "\tgcc -c $basename.c" >> $temp | ||
74 | |||
75 | echo $temp | ||
76 | } | ||
77 | |||
78 | # Generate a c test file for compiler testing | ||
79 | test_file=`Gen_File` | ||
80 | |||
81 | MOVE=`which mv` | ||
82 | $MOVE $test_file $TEST_FILE | ||
83 | |||
84 | # Begin compiler test in target | ||
85 | for cmd in $TEST_LIST | ||
86 | do | ||
87 | which $cmd | ||
88 | if [ $? -ne 0 ]; then | ||
89 | Target_Info "No $cmd command found" | ||
90 | exit 1 | ||
91 | fi | ||
92 | |||
93 | if [ "$cmd" == "make" ]; then | ||
94 | rm -rf $EXECUTE_FILE | ||
95 | |||
96 | # For makefile test, we need to generate a makefile and run with a c file | ||
97 | makefile=`Gen_Makefile` | ||
98 | $MOVE $makefile $TEST_MAKEFILE | ||
99 | |||
100 | cd `dirname $TEST_MAKEFILE` | ||
101 | make | ||
102 | |||
103 | if [ $? -ne 0 ]; then | ||
104 | Target_Info "$cmd running with error, Pls. check error in following" | ||
105 | Target_Err make | ||
106 | exit 1 | ||
107 | fi | ||
108 | else | ||
109 | rm -rf $EXECUTE_FILE | ||
110 | |||
111 | # For gcc/g++, we compile a c test file and check the output | ||
112 | $cmd $TEST_FILE -o $EXECUTE_FILE -lm | ||
113 | |||
114 | if [ $? -ne 0 ]; then | ||
115 | Target_Info "$cmd running with error, Pls. check error in following" | ||
116 | Target_Err $cmd $TEST_FILE -o $EXECUTE_FILE -lm | ||
117 | exit 1 | ||
118 | fi | ||
119 | fi | ||
120 | |||
121 | # Check if the binary file generated by $cmd can work without error | ||
122 | if [ -f $EXECUTE_FILE ]; then | ||
123 | $EXECUTE_FILE | ||
124 | if [ $? -ne 0 ]; then | ||
125 | Target_Info "$EXECUTE_FILE running with error, Pls. check error in following" | ||
126 | Target_Err $EXECUTE_FILE | ||
127 | exit 1 | ||
128 | else | ||
129 | Target_Info "$cmd can work without problem in target" | ||
130 | fi | ||
131 | else | ||
132 | Target_Info "No executalbe file $EXECUTE_FILE found, Pls. check the error log" | ||
133 | exit 1 | ||
134 | fi | ||
135 | done | ||
136 | |||
137 | exit 0 | ||