1#	$NetBSD: t_cxxruntime.sh,v 1.7 2022/06/12 15:08:38 skrll 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	cat > test.cpp << EOF
103#include <cstdlib>
104#include <iostream>
105int main(void) {std::cout << "hello world" << std::endl;exit(0);}
106EOF
107	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o hello test.cpp
108	atf_check -s exit:0 -o inline:"hello world\n" ./hello
109}
110
111cxxruntime_profile_32_body() {
112	# check whether this arch is 64bit
113	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
114		atf_skip "this is not a 64 bit architecture"
115	fi
116	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
117		atf_skip "c++ -m32 not supported on this architecture"
118	else
119		if fgrep -q _LP64 ./def32; then
120			atf_fail "c++ -m32 does not generate netbsd32 binaries"
121		fi
122	fi
123
124	cat > test.cpp << EOF
125#include <cstdlib>
126#include <iostream>
127int main(void) {std::cout << "hello world" << std::endl;exit(0);}
128EOF
129	atf_check -s exit:0 -o ignore -e ignore c++ -static -m32 -pg -o hello test.cpp
130	atf_check -s exit:0 -o inline:"hello world\n" ./hello
131}
132
133cxxruntime_static_body() {
134	cat > test.cpp << EOF
135#include <cstdlib>
136#include <iostream>
137int main(void) {std::cout << "hello world" << std::endl;exit(0);}
138EOF
139	atf_check -s exit:0 -o ignore -e ignore c++ -static -o hello test.cpp
140	atf_check -s exit:0 -o inline:"hello world\n" ./hello
141}
142
143cxxruntime_pic_body() {
144	cat > test.cpp << EOF
145#include <cstdlib>
146int callpic(void);
147int main(void) {callpic();exit(0);}
148EOF
149	cat > pic.cpp << EOF
150#include <iostream>
151int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
152EOF
153
154	atf_check -s exit:0 -o ignore -e ignore \
155	    c++ -fPIC -shared -o libtest.so pic.cpp
156	atf_check -s exit:0 -o ignore -e ignore \
157	    c++ -o hello test.cpp -L. -ltest
158
159	export LD_LIBRARY_PATH=.
160	atf_check -s exit:0 -o inline:"hello world\n" ./hello
161}
162
163cxxruntime_pic_32_body() {
164	# check whether this arch is 64bit
165	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
166		atf_skip "this is not a 64 bit architecture"
167	fi
168	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
169		atf_skip "c++ -m32 not supported on this architecture"
170	else
171		if fgrep -q _LP64 ./def32; then
172			atf_fail "c++ -m32 does not generate netbsd32 binaries"
173		fi
174	fi
175
176	cat > test.cpp << EOF
177#include <cstdlib>
178int callpic(void);
179int main(void) {callpic();exit(0);}
180EOF
181	cat > pic.cpp << EOF
182#include <iostream>
183int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
184EOF
185
186	atf_check -s exit:0 -o ignore -e ignore \
187	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
188	atf_check -s exit:0 -o ignore -e ignore \
189	    c++ -m32 -o hello test.cpp -L. -ltest
190
191	export LD_LIBRARY_PATH=.
192	atf_check -s exit:0 -o inline:"hello world\n" ./hello
193}
194
195cxxruntime_pic_profile_body() {
196	cat > test.cpp << EOF
197#include <cstdlib>
198int callpic(void);
199int main(void) {callpic();exit(0);}
200EOF
201	cat > pic.cpp << EOF
202#include <iostream>
203int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
204EOF
205
206	atf_check -s exit:0 -o ignore -e ignore \
207	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
208	atf_check -s exit:0 -o ignore -e ignore \
209	    c++ -pg -o hello test.cpp -L. -ltest
210
211	export LD_LIBRARY_PATH=.
212	atf_check -s exit:0 -o inline:"hello world\n" ./hello
213}
214
215cxxruntime_pic_profile_32_body() {
216	# check whether this arch is 64bit
217	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
218		atf_skip "this is not a 64 bit architecture"
219	fi
220	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
221		atf_skip "c++ -m32 not supported on this architecture"
222	else
223		if fgrep -q _LP64 ./def32; then
224			atf_fail "c++ -m32 does not generate netbsd32 binaries"
225		fi
226	fi
227
228	cat > test.cpp << EOF
229#include <cstdlib>
230int callpic(void);
231int main(void) {callpic();exit(0);}
232EOF
233	cat > pic.cpp << EOF
234#include <iostream>
235int callpic(void) {std::cout << "hello world" << std::endl;return 0;}
236EOF
237
238	atf_check -s exit:0 -o ignore -e ignore \
239	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
240	atf_check -s exit:0 -o ignore -e ignore \
241	    c++ -m32 -pg -o hello test.cpp -L. -ltest
242
243	export LD_LIBRARY_PATH=.
244	atf_check -s exit:0 -o inline:"hello world\n" ./hello
245}
246
247cxxruntime_pie_body() {
248	# check whether this arch supports -pie
249	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
250		atf_skip "c++ -pie not supported on this architecture"
251	fi
252	cat > test.cpp << EOF
253#include <cstdlib>
254#include <iostream>
255int main(void) {std::cout << "hello world" << std::endl;exit(0);}
256EOF
257	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
258	atf_check -s exit:0 -o inline:"hello world\n" ./hello
259}
260
261cxxruntime32_body() {
262	# check whether this arch is 64bit
263	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
264		atf_skip "this is not a 64 bit architecture"
265	fi
266	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
267		atf_skip "c++ -m32 not supported on this architecture"
268	else
269		if fgrep -q _LP64 ./def32; then
270			atf_fail "c++ -m32 does not generate netbsd32 binaries"
271		fi
272	fi
273
274	cat > test.cpp << EOF
275#include <cstdlib>
276#include <iostream>
277int main(void) {std::cout << "hello world" << std::endl;exit(0);}
278EOF
279	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
280	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
281	file -b ./hello32 > ./ftype32
282	file -b ./hello64 > ./ftype64
283	if diff ./ftype32 ./ftype64 >/dev/null; then
284		atf_fail "generated binaries do not differ"
285	fi
286	echo "32bit binaries on this platform are:"
287	cat ./ftype32
288	echo "While native (64bit) binaries are:"
289	cat ./ftype64
290	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
291
292	# do another test with static 32bit binaries
293	cat > test.cpp << EOF
294#include <cstdlib>
295#include <iostream>
296int main(void) {std::cout << "hello static world" << std::endl;exit(0);}
297EOF
298	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
299	    -static test.cpp
300	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
301}
302
303atf_init_test_cases()
304{
305	atf_add_test_case cxxruntime
306	atf_add_test_case cxxruntime_profile
307	atf_add_test_case cxxruntime_pic
308	atf_add_test_case cxxruntime_pie
309	atf_add_test_case cxxruntime32
310	atf_add_test_case cxxruntime_static
311	atf_add_test_case cxxruntime_pic_32
312	atf_add_test_case cxxruntime_pic_profile
313	atf_add_test_case cxxruntime_pic_profile_32
314	atf_add_test_case cxxruntime_profile_32
315}
316