1#	$NetBSD: t_hello.sh,v 1.8 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
28atf_test_case hello
29hello_head() {
30	atf_set "descr" "compile and run \"hello world\""
31	atf_set "require.progs" "c++"
32}
33
34atf_test_case hello_profile
35hello_profile_head() {
36	atf_set "descr" "compile and run \"hello world\" with profiling option"
37	atf_set "require.progs" "c++"
38}
39
40atf_test_case hello_profile
41hello_profile_32_head() {
42	atf_set "descr" "compile and run 32-bit \"hello world\" with profiling option"
43	atf_set "require.progs" "c++"
44}
45
46atf_test_case hello_static
47hello_static_head() {
48	atf_set "descr" "compile and run \"hello world\" with static option"
49	atf_set "require.progs" "c++"
50}
51
52atf_test_case hello_pic
53hello_pic_head() {
54	atf_set "descr" "compile and run PIC \"hello world\""
55	atf_set "require.progs" "c++"
56}
57
58atf_test_case hello_pic_32
59hello_pic_32_head() {
60	atf_set "descr" "compile and run 32-bit PIC \"hello world\""
61	atf_set "require.progs" "c++"
62}
63
64atf_test_case hello_pic_profile
65hello_pic_profile_head() {
66	atf_set "descr" "compile and run PIC \"hello world\" with profiling option"
67	atf_set "require.progs" "c++"
68}
69
70atf_test_case hello_pic_profile_32
71hello_pic_profile_32_head() {
72	atf_set "descr" "compile and run 32-bit PIC \"hello world\" with profiling option"
73	atf_set "require.progs" "c++"
74}
75
76atf_test_case hello_pie
77hello_pie_head() {
78	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
79	atf_set "require.progs" "c++"
80}
81
82atf_test_case hello32
83hello32_head() {
84	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
85	atf_set "require.progs" "c++ file diff cat"
86}
87
88hello_body() {
89	cat > test.cpp << EOF
90#include <stdio.h>
91#include <stdlib.h>
92int main(void) {printf("hello world\n");exit(0);}
93EOF
94	atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
95	atf_check -s exit:0 -o inline:"hello world\n" ./hello
96}
97
98hello_profile_body() {
99	case `uname -m` in
100	riscv)	atf_expect_fail "PR port-riscv/59301:" \
101		    " riscv: missing MKPROFILE=yes support"
102		;;
103	esac
104
105	cat > test.cpp << EOF
106#include <stdio.h>
107#include <stdlib.h>
108int main(void) {printf("hello world\n");exit(0);}
109EOF
110	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o hello test.cpp
111	atf_check -s exit:0 -o inline:"hello world\n" ./hello
112}
113
114hello_profile_32_body() {
115	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
116		atf_skip "this is not a 64 bit architecture"
117	fi
118	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
119		atf_skip "c++ -m32 not supported on this architecture"
120	else
121		if fgrep -q _LP64 ./def32; then
122			atf_fail "c++ -m32 does not generate netbsd32 binaries"
123		fi
124	fi
125
126	case `uname -m` in
127	riscv)	atf_expect_fail "PR port-riscv/59301:" \
128		    " riscv: missing MKPROFILE=yes support"
129		;;
130	esac
131
132	cat > test.cpp << EOF
133#include <stdio.h>
134#include <stdlib.h>
135int main(void) {printf("hello world\n");exit(0);}
136EOF
137	atf_check -s exit:0 -o ignore -e ignore c++ -static  -m32 -pg -o hello test.cpp
138	atf_check -s exit:0 -o inline:"hello world\n" ./hello
139}
140
141
142hello_static_body() {
143	cat > test.cpp << EOF
144#include <stdio.h>
145#include <stdlib.h>
146int main(void) {printf("hello world\n");exit(0);}
147EOF
148	atf_check -s exit:0 -o ignore -e ignore c++ -static -o hello test.cpp
149	atf_check -s exit:0 -o inline:"hello world\n" ./hello
150}
151
152hello_pic_body() {
153	cat > test.cpp << EOF
154#include <stdlib.h>
155int callpic(void);
156int main(void) {callpic();exit(0);}
157EOF
158	cat > pic.cpp << EOF
159#include <stdio.h>
160int callpic(void) {printf("hello world\n");return 0;}
161EOF
162
163	atf_check -s exit:0 -o ignore -e ignore \
164	    c++ -fPIC -shared -o libtest.so pic.cpp
165	atf_check -s exit:0 -o ignore -e ignore \
166	    c++ -o hello test.cpp -L. -ltest
167
168	export LD_LIBRARY_PATH=.
169	atf_check -s exit:0 -o inline:"hello world\n" ./hello
170}
171
172hello_pic_32_body() {
173	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
174		atf_skip "this is not a 64 bit architecture"
175	fi
176	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
177		atf_skip "c++ -m32 not supported on this architecture"
178	else
179		if fgrep -q _LP64 ./def32; then
180			atf_fail "c++ -m32 does not generate netbsd32 binaries"
181		fi
182	fi
183	cat > test.cpp << EOF
184#include <stdlib.h>
185int callpic(void);
186int main(void) {callpic();exit(0);}
187EOF
188	cat > pic.cpp << EOF
189#include <stdio.h>
190int callpic(void) {printf("hello world\n");return 0;}
191EOF
192
193	atf_check -s exit:0 -o ignore -e ignore \
194	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
195	atf_check -s exit:0 -o ignore -e ignore \
196	    c++ -m32 -o hello test.cpp -L. -ltest
197
198	export LD_LIBRARY_PATH=.
199	atf_check -s exit:0 -o inline:"hello world\n" ./hello
200}
201
202hello_pic_profile_body() {
203	case `uname -m` in
204	riscv)	atf_expect_fail "PR port-riscv/59301:" \
205		    " riscv: missing MKPROFILE=yes support"
206		;;
207	esac
208
209	cat > test.cpp << EOF
210#include <stdlib.h>
211int callpic(void);
212int main(void) {callpic();exit(0);}
213EOF
214	cat > pic.cpp << EOF
215#include <stdio.h>
216int callpic(void) {printf("hello world\n");return 0;}
217EOF
218
219	atf_check -s exit:0 -o ignore -e ignore \
220	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
221	atf_check -s exit:0 -o ignore -e ignore \
222	    c++ -pg -o hello test.cpp -L. -ltest
223
224	export LD_LIBRARY_PATH=.
225	atf_check -s exit:0 -o inline:"hello world\n" ./hello
226}
227
228hello_pic_profile_32_body() {
229	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
230		atf_skip "this is not a 64 bit architecture"
231	fi
232	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
233		atf_skip "c++ -m32 not supported on this architecture"
234	else
235		if fgrep -q _LP64 ./def32; then
236			atf_fail "c++ -m32 does not generate netbsd32 binaries"
237		fi
238	fi
239
240	case `uname -m` in
241	riscv)	atf_expect_fail "PR port-riscv/59301:" \
242		    " riscv: missing MKPROFILE=yes support"
243		;;
244	esac
245
246	cat > test.cpp << EOF
247#include <stdlib.h>
248int callpic(void);
249int main(void) {callpic();exit(0);}
250EOF
251	cat > pic.cpp << EOF
252#include <stdio.h>
253int callpic(void) {printf("hello world\n");return 0;}
254EOF
255
256	atf_check -s exit:0 -o ignore -e ignore \
257	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
258	atf_check -s exit:0 -o ignore -e ignore \
259	    c++ -m32 -pg -o hello test.cpp -L. -ltest
260
261	export LD_LIBRARY_PATH=.
262	atf_check -s exit:0 -o inline:"hello world\n" ./hello
263}
264
265hello_pie_body() {
266	# check whether this arch supports -pie
267	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
268		atf_skip "c++ -pie not supported on this architecture"
269	fi
270	cat > test.cpp << EOF
271#include <stdio.h>
272#include <stdlib.h>
273int main(void) {printf("hello world\n");exit(0);}
274EOF
275	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
276	atf_check -s exit:0 -o inline:"hello world\n" ./hello
277}
278
279hello32_body() {
280	# check whether this arch is 64bit
281	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
282		atf_skip "this is not a 64 bit architecture"
283	fi
284	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
285		atf_skip "c++ -m32 not supported on this architecture"
286	else
287		if fgrep -q _LP64 ./def32; then
288			atf_fail "c++ -m32 does not generate netbsd32 binaries"
289		fi
290	fi
291
292	cat > test.cpp << EOF
293#include <stdio.h>
294#include <stdlib.h>
295int main(void) {printf("hello world\n");exit(0);}
296EOF
297	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
298	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
299	file -b ./hello32 > ./ftype32
300	file -b ./hello64 > ./ftype64
301	if diff ./ftype32 ./ftype64 >/dev/null; then
302		atf_fail "generated binaries do not differ"
303	fi
304	echo "32bit binaries on this platform are:"
305	cat ./ftype32
306	echo "While native (64bit) binaries are:"
307	cat ./ftype64
308	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
309
310	# do another test with static 32bit binaries
311	cat > test.cpp << EOF
312#include <stdio.h>
313#include <stdlib.h>
314int main(void) {printf("hello static world\n");exit(0);}
315EOF
316	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
317	    -static test.cpp
318	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
319}
320
321atf_init_test_cases()
322{
323
324	atf_add_test_case hello
325	atf_add_test_case hello_profile
326	atf_add_test_case hello_pic
327	atf_add_test_case hello_pie
328	atf_add_test_case hello32
329	atf_add_test_case hello_static
330	atf_add_test_case hello_pic_32
331	atf_add_test_case hello_pic_profile
332	atf_add_test_case hello_pic_profile_32
333	atf_add_test_case hello_profile_32
334}
335