t_hello.sh revision 1.13 1 # $NetBSD: t_hello.sh,v 1.13 2025/04/16 01:52:42 riastradh Exp $
2 #
3 # Copyright (c) 2011 The NetBSD Foundation, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 atf_test_case hello
29 hello_head() {
30 atf_set "descr" "compile and run \"hello world\""
31 atf_set "require.progs" "cc"
32 }
33
34 atf_test_case hello_profile
35 hello_profile_head() {
36 atf_set "descr" "compile and run \"hello world\" with profiling option"
37 atf_set "require.progs" "cc"
38 }
39
40 atf_test_case hello_pic
41 hello_pic_head() {
42 atf_set "descr" "compile and run PIC \"hello world\""
43 atf_set "require.progs" "cc"
44 }
45
46 atf_test_case hello_pie
47 hello_pie_head() {
48 atf_set "descr" "compile and run position independent (PIE) \"hello world\""
49 atf_set "require.progs" "cc"
50 }
51
52 atf_test_case hello32
53 hello32_head() {
54 atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
55 atf_set "require.progs" "cc file diff cat"
56 }
57
58 hello_body() {
59 cat > test.c << EOF
60 #include <stdio.h>
61 #include <stdlib.h>
62 int main(void) {printf("hello world\n");exit(0);}
63 EOF
64 atf_check -s exit:0 -o ignore -e ignore cc -o hello test.c
65 atf_check -s exit:0 -o inline:"hello world\n" ./hello
66 }
67
68 hello_profile_body() {
69 case `uname -m` in
70 riscv) atf_expect_fail "PR port-riscv/59301:" \
71 " riscv: missing MKPROFILE=yes support"
72 ;;
73 esac
74
75 cat > test.c << EOF
76 #include <stdio.h>
77 #include <stdlib.h>
78 int main(void) {printf("hello world\n");exit(0);}
79 EOF
80 atf_check -s exit:0 -o ignore -e ignore cc -static -o hello -pg test.c
81 atf_check -s exit:0 -o inline:"hello world\n" ./hello
82 atf_check -s exit:0 -o ignore -e ignore cc -o hello2 -fprofile-generate test.c
83 atf_check -s exit:0 -o inline:"hello world\n" ./hello2
84 }
85
86 hello_pic_body() {
87 cat > test.c << EOF
88 #include <stdlib.h>
89 int callpic(void);
90 int main(void) {callpic();exit(0);}
91 EOF
92 cat > pic.c << EOF
93 #include <stdio.h>
94 int callpic(void) {printf("hello world\n");return 0;}
95 EOF
96
97 atf_check -s exit:0 -o ignore -e ignore \
98 cc -fPIC -shared -o libtest.so pic.c
99 atf_check -s exit:0 -o ignore -e ignore \
100 cc -o hello test.c -L. -ltest
101
102 export LD_LIBRARY_PATH=.
103 atf_check -s exit:0 -o inline:"hello world\n" ./hello
104 }
105
106 hello_pie_body() {
107 # check whether this arch supports -pie
108 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
109 atf_skip "cc -pie not supported on this architecture"
110 fi
111 cat > test.c << EOF
112 #include <stdio.h>
113 #include <stdlib.h>
114 int main(void) {printf("hello world\n");exit(0);}
115 EOF
116 atf_check -s exit:0 -o ignore -e ignore cc -fpie -pie -o hello test.c
117 atf_check -s exit:0 -o inline:"hello world\n" ./hello
118 }
119
120 hello32_body() {
121 # check whether this arch is 64bit
122 if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
123 atf_skip "this is not a 64 bit architecture"
124 fi
125 if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
126 atf_skip "cc -m32 not supported on this architecture"
127 else
128 if fgrep -q _LP64 ./def32; then
129 atf_fail "cc -m32 does not generate netbsd32 binaries"
130 fi
131 fi
132
133 cat > test.c << EOF
134 #include <stdio.h>
135 #include <stdlib.h>
136 int main(void) {printf("hello world\n");exit(0);}
137 EOF
138 atf_check -s exit:0 -o ignore -e ignore cc -o hello32 -m32 test.c
139 atf_check -s exit:0 -o ignore -e ignore cc -o hello64 test.c
140 file -b ./hello32 > ./ftype32
141 file -b ./hello64 > ./ftype64
142 if diff ./ftype32 ./ftype64 >/dev/null; then
143 atf_fail "generated binaries do not differ"
144 fi
145 echo "32bit binaries on this platform are:"
146 cat ./ftype32
147 echo "While native (64bit) binaries are:"
148 cat ./ftype64
149 atf_check -s exit:0 -o inline:"hello world\n" ./hello32
150
151 # do another test with static 32bit binaries
152 cat > test.c << EOF
153 #include <stdio.h>
154 #include <stdlib.h>
155 int main(void) {printf("hello static world\n");exit(0);}
156 EOF
157 atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
158 -static test.c
159 atf_check -s exit:0 -o inline:"hello static world\n" ./hello
160
161 # and another test with profile 32bit binaries
162 cat > test.c << EOF
163 #include <stdio.h>
164 #include <stdlib.h>
165 int main(void) {printf("hello 32bit profile world\n");exit(0);}
166 EOF
167 atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
168 -pg test.c
169 atf_check -s exit:0 -o inline:"hello 32bit profile world\n" ./hello
170 }
171
172 atf_init_test_cases()
173 {
174
175 atf_add_test_case hello
176 atf_add_test_case hello_profile
177 atf_add_test_case hello_pic
178 atf_add_test_case hello_pie
179 atf_add_test_case hello32
180 }
181