1#	$NetBSD: t_cxxruntime.sh,v 1.8 2025/04/16 01:52:42 riastradh Exp $
2#
3# Copyright (c) 2017 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Kamil Rytarowski.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31atf_test_case cxxruntime
32cxxruntime_head() {
33	atf_set "descr" "compile and run \"hello world\""
34	atf_set "require.progs" "c++"
35}
36
37atf_test_case cxxruntime_profile
38cxxruntime_profile_head() {
39	atf_set "descr" "compile and run \"hello world\" with profiling option"
40	atf_set "require.progs" "c++"
41}
42
43atf_test_case cxxruntime_profile_32
44cxxruntime_profile_32_head() {
45	atf_set "descr" "compile and run 32-bit \"hello world\" with profiling option"
46	atf_set "require.progs" "c++"
47}
48
49atf_test_case cxxruntime_static
50cxxruntime_static_head() {
51	atf_set "descr" "compile and run \"hello world\" with static flags"
52	atf_set "require.progs" "c++"
53}
54
55atf_test_case cxxruntime_pic
56cxxruntime_pic_head() {
57	atf_set "descr" "compile and run PIC \"hello world\""
58	atf_set "require.progs" "c++"
59}
60
61atf_test_case cxxruntime_pic_32
62cxxruntime_pic_32_head() {
63	atf_set "descr" "compile and run 32-bit PIC \"hello world\""
64	atf_set "require.progs" "c++"
65}
66
67atf_test_case cxxruntime_pic_profile
68cxxruntime_pic_profile_head() {
69	atf_set "descr" "compile and run PIC \"hello world\" with profiling option"
70	atf_set "require.progs" "c++"
71}
72
73atf_test_case cxxruntime_pic_profile_32
74cxxruntime_pic_profile_32_head() {
75	atf_set "descr" "compile and run 32-bit PIC \"hello world\" with profiling option"
76	atf_set "require.progs" "c++"
77}
78
79atf_test_case cxxruntime_pie
80cxxruntime_pie_head() {
81	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
82	atf_set "require.progs" "c++"
83}
84
85atf_test_case cxxruntime32
86cxxruntime32_head() {
87	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
88	atf_set "require.progs" "c++ file diff cat"
89}
90
91cxxruntime_body() {
92	cat > test.cpp << EOF
93#include <cstdlib>
94#include <iostream>
95int main(void) {std::cout << "hello world" << std::endl;exit(0);}
96EOF
97	atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
98	atf_check -s exit:0 -o inline:"hello world\n" ./hello
99}
100
101cxxruntime_profile_body() {
102	case `uname -m` in
103	riscv)	atf_expect_fail "PR port-riscv/59301:" \
104		    " riscv: missing MKPROFILE=yes support"
105		;;
106	esac
107
108	cat > test.cpp << EOF
109#include <cstdlib>
110#include <iostream>
111int main(void) {std::cout << "hello world" << std::endl;exit(0);}
112EOF
113	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o hello test.cpp
114	atf_check -s exit:0 -o inline:"hello world\n" ./hello
115}
116
117cxxruntime_profile_32_body() {
118	# check whether this arch is 64bit
119	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
120		atf_skip "this is not a 64 bit architecture"
121	fi
122	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
123		atf_skip "c++ -m32 not supported on this architecture"
124	else
125		if fgrep -q _LP64 ./def32; then
126			atf_fail "c++ -m32 does not generate netbsd32 binaries"
127		fi
128	fi
129
130	case `uname -m` in
131	riscv)	atf_expect_fail "PR port-riscv/59301:" \
132		    " riscv: missing MKPROFILE=yes support"
133		;;
134	esac
135
136	cat > test.cpp << EOF
137#include <cstdlib>
138#include <iostream>
139int main(void) {std::cout << "hello world" << std::endl;exit(0);}
140EOF
141	atf_check -s exit:0 -o ignore -e ignore c++ -static -m32 -pg -o hello test.cpp
142	atf_check -s exit:0 -o inline:"hello world\n" ./hello
143}
144
145cxxruntime_static_body() {
146	cat > test.cpp << EOF
147#include <cstdlib>
148#include <iostream>
149int main(void) {std::cout << "hello world" << std::endl;exit(0);}
150EOF
151	atf_check -s exit:0 -o ignore -e ignore c++ -static -o hello test.cpp
152	atf_check -s exit:0 -o inline:"hello world\n" ./hello
153}
154
155cxxruntime_pic_body() {
156	cat > test.cpp << EOF
157#include <cstdlib>
158int callpic(void);
159int main(void) {callpic();exit(0);}
160EOF
161	cat > pic.cpp << EOF
162#include <iostream>
163int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
164EOF
165
166	atf_check -s exit:0 -o ignore -e ignore \
167	    c++ -fPIC -shared -o libtest.so pic.cpp
168	atf_check -s exit:0 -o ignore -e ignore \
169	    c++ -o hello test.cpp -L. -ltest
170
171	export LD_LIBRARY_PATH=.
172	atf_check -s exit:0 -o inline:"hello world\n" ./hello
173}
174
175cxxruntime_pic_32_body() {
176	# check whether this arch is 64bit
177	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
178		atf_skip "this is not a 64 bit architecture"
179	fi
180	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
181		atf_skip "c++ -m32 not supported on this architecture"
182	else
183		if fgrep -q _LP64 ./def32; then
184			atf_fail "c++ -m32 does not generate netbsd32 binaries"
185		fi
186	fi
187
188	cat > test.cpp << EOF
189#include <cstdlib>
190int callpic(void);
191int main(void) {callpic();exit(0);}
192EOF
193	cat > pic.cpp << EOF
194#include <iostream>
195int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
196EOF
197
198	atf_check -s exit:0 -o ignore -e ignore \
199	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
200	atf_check -s exit:0 -o ignore -e ignore \
201	    c++ -m32 -o hello test.cpp -L. -ltest
202
203	export LD_LIBRARY_PATH=.
204	atf_check -s exit:0 -o inline:"hello world\n" ./hello
205}
206
207cxxruntime_pic_profile_body() {
208	case `uname -m` in
209	riscv)	atf_expect_fail "PR port-riscv/59301:" \
210		    " riscv: missing MKPROFILE=yes support"
211		;;
212	esac
213
214	cat > test.cpp << EOF
215#include <cstdlib>
216int callpic(void);
217int main(void) {callpic();exit(0);}
218EOF
219	cat > pic.cpp << EOF
220#include <iostream>
221int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
222EOF
223
224	atf_check -s exit:0 -o ignore -e ignore \
225	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
226	atf_check -s exit:0 -o ignore -e ignore \
227	    c++ -pg -o hello test.cpp -L. -ltest
228
229	export LD_LIBRARY_PATH=.
230	atf_check -s exit:0 -o inline:"hello world\n" ./hello
231}
232
233cxxruntime_pic_profile_32_body() {
234	# check whether this arch is 64bit
235	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
236		atf_skip "this is not a 64 bit architecture"
237	fi
238	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
239		atf_skip "c++ -m32 not supported on this architecture"
240	else
241		if fgrep -q _LP64 ./def32; then
242			atf_fail "c++ -m32 does not generate netbsd32 binaries"
243		fi
244	fi
245
246	case `uname -m` in
247	riscv)	atf_expect_fail "PR port-riscv/59301:" \
248		    " riscv: missing MKPROFILE=yes support"
249		;;
250	esac
251
252	cat > test.cpp << EOF
253#include <cstdlib>
254int callpic(void);
255int main(void) {callpic();exit(0);}
256EOF
257	cat > pic.cpp << EOF
258#include <iostream>
259int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
260EOF
261
262	atf_check -s exit:0 -o ignore -e ignore \
263	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
264	atf_check -s exit:0 -o ignore -e ignore \
265	    c++ -m32 -pg -o hello test.cpp -L. -ltest
266
267	export LD_LIBRARY_PATH=.
268	atf_check -s exit:0 -o inline:"hello world\n" ./hello
269}
270
271cxxruntime_pie_body() {
272	# check whether this arch supports -pie
273	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
274		atf_skip "c++ -pie not supported on this architecture"
275	fi
276	cat > test.cpp << EOF
277#include <cstdlib>
278#include <iostream>
279int main(void) {std::cout << "hello world" << std::endl;exit(0);}
280EOF
281	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
282	atf_check -s exit:0 -o inline:"hello world\n" ./hello
283}
284
285cxxruntime32_body() {
286	# check whether this arch is 64bit
287	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
288		atf_skip "this is not a 64 bit architecture"
289	fi
290	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
291		atf_skip "c++ -m32 not supported on this architecture"
292	else
293		if fgrep -q _LP64 ./def32; then
294			atf_fail "c++ -m32 does not generate netbsd32 binaries"
295		fi
296	fi
297
298	cat > test.cpp << EOF
299#include <cstdlib>
300#include <iostream>
301int main(void) {std::cout << "hello world" << std::endl;exit(0);}
302EOF
303	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
304	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
305	file -b ./hello32 > ./ftype32
306	file -b ./hello64 > ./ftype64
307	if diff ./ftype32 ./ftype64 >/dev/null; then
308		atf_fail "generated binaries do not differ"
309	fi
310	echo "32bit binaries on this platform are:"
311	cat ./ftype32
312	echo "While native (64bit) binaries are:"
313	cat ./ftype64
314	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
315
316	# do another test with static 32bit binaries
317	cat > test.cpp << EOF
318#include <cstdlib>
319#include <iostream>
320int main(void) {std::cout << "hello static world" << std::endl;exit(0);}
321EOF
322	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
323	    -static test.cpp
324	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
325}
326
327atf_init_test_cases()
328{
329	atf_add_test_case cxxruntime
330	atf_add_test_case cxxruntime_profile
331	atf_add_test_case cxxruntime_pic
332	atf_add_test_case cxxruntime_pie
333	atf_add_test_case cxxruntime32
334	atf_add_test_case cxxruntime_static
335	atf_add_test_case cxxruntime_pic_32
336	atf_add_test_case cxxruntime_pic_profile
337	atf_add_test_case cxxruntime_pic_profile_32
338	atf_add_test_case cxxruntime_profile_32
339}
340