xref: /netbsd-src/tests/usr.bin/c++/t_hello.sh (revision ac615257a7f4243b5374551e57c46a4b929d81a3)
1*ac615257Sskrll#	$NetBSD: t_hello.sh,v 1.7 2022/06/12 15:08:38 skrll Exp $
2b971227cSkamil#
3b971227cSkamil# Copyright (c) 2011 The NetBSD Foundation, Inc.
4b971227cSkamil# All rights reserved.
5b971227cSkamil#
6b971227cSkamil# Redistribution and use in source and binary forms, with or without
7b971227cSkamil# modification, are permitted provided that the following conditions
8b971227cSkamil# are met:
9b971227cSkamil# 1. Redistributions of source code must retain the above copyright
10b971227cSkamil#    notice, this list of conditions and the following disclaimer.
11b971227cSkamil# 2. Redistributions in binary form must reproduce the above copyright
12b971227cSkamil#    notice, this list of conditions and the following disclaimer in the
13b971227cSkamil#    documentation and/or other materials provided with the distribution.
14b971227cSkamil#
15b971227cSkamil# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16b971227cSkamil# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17b971227cSkamil# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18b971227cSkamil# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19b971227cSkamil# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b971227cSkamil# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b971227cSkamil# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b971227cSkamil# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b971227cSkamil# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b971227cSkamil# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b971227cSkamil# POSSIBILITY OF SUCH DAMAGE.
26b971227cSkamil#
27b971227cSkamil
28b971227cSkamilatf_test_case hello
29b971227cSkamilhello_head() {
30b971227cSkamil	atf_set "descr" "compile and run \"hello world\""
31b971227cSkamil	atf_set "require.progs" "c++"
32b971227cSkamil}
33b971227cSkamil
344fcb013cSmartinatf_test_case hello_profile
3577dc83faSkamilhello_profile_head() {
364fcb013cSmartin	atf_set "descr" "compile and run \"hello world\" with profiling option"
374fcb013cSmartin	atf_set "require.progs" "c++"
384fcb013cSmartin}
394fcb013cSmartin
4077dc83faSkamilatf_test_case hello_profile
4177dc83faSkamilhello_profile_32_head() {
4277dc83faSkamil	atf_set "descr" "compile and run 32-bit \"hello world\" with profiling option"
4377dc83faSkamil	atf_set "require.progs" "c++"
4477dc83faSkamil}
4577dc83faSkamil
4677dc83faSkamilatf_test_case hello_static
4777dc83faSkamilhello_static_head() {
4877dc83faSkamil	atf_set "descr" "compile and run \"hello world\" with static option"
4977dc83faSkamil	atf_set "require.progs" "c++"
5077dc83faSkamil}
5177dc83faSkamil
52b971227cSkamilatf_test_case hello_pic
53b971227cSkamilhello_pic_head() {
54b971227cSkamil	atf_set "descr" "compile and run PIC \"hello world\""
55b971227cSkamil	atf_set "require.progs" "c++"
56b971227cSkamil}
57b971227cSkamil
5877dc83faSkamilatf_test_case hello_pic_32
5977dc83faSkamilhello_pic_32_head() {
6077dc83faSkamil	atf_set "descr" "compile and run 32-bit PIC \"hello world\""
6177dc83faSkamil	atf_set "require.progs" "c++"
6277dc83faSkamil}
6377dc83faSkamil
6477dc83faSkamilatf_test_case hello_pic_profile
6577dc83faSkamilhello_pic_profile_head() {
6677dc83faSkamil	atf_set "descr" "compile and run PIC \"hello world\" with profiling option"
6777dc83faSkamil	atf_set "require.progs" "c++"
6877dc83faSkamil}
6977dc83faSkamil
7077dc83faSkamilatf_test_case hello_pic_profile_32
7177dc83faSkamilhello_pic_profile_32_head() {
7277dc83faSkamil	atf_set "descr" "compile and run 32-bit PIC \"hello world\" with profiling option"
7377dc83faSkamil	atf_set "require.progs" "c++"
7477dc83faSkamil}
7577dc83faSkamil
76b971227cSkamilatf_test_case hello_pie
77b971227cSkamilhello_pie_head() {
78b971227cSkamil	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
79b971227cSkamil	atf_set "require.progs" "c++"
80b971227cSkamil}
81b971227cSkamil
82b971227cSkamilatf_test_case hello32
83b971227cSkamilhello32_head() {
84b971227cSkamil	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
85b971227cSkamil	atf_set "require.progs" "c++ file diff cat"
86b971227cSkamil}
87b971227cSkamil
88b971227cSkamilhello_body() {
89b971227cSkamil	cat > test.cpp << EOF
90b971227cSkamil#include <stdio.h>
91b971227cSkamil#include <stdlib.h>
92b971227cSkamilint main(void) {printf("hello world\n");exit(0);}
93b971227cSkamilEOF
94b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
95b971227cSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
96b971227cSkamil}
97b971227cSkamil
984fcb013cSmartinhello_profile_body() {
994fcb013cSmartin	cat > test.cpp << EOF
1004fcb013cSmartin#include <stdio.h>
1014fcb013cSmartin#include <stdlib.h>
1024fcb013cSmartinint main(void) {printf("hello world\n");exit(0);}
1034fcb013cSmartinEOF
104*ac615257Sskrll	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o hello test.cpp
1054fcb013cSmartin	atf_check -s exit:0 -o inline:"hello world\n" ./hello
1064fcb013cSmartin}
1074fcb013cSmartin
10877dc83faSkamilhello_profile_32_body() {
10977dc83faSkamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
11077dc83faSkamil		atf_skip "this is not a 64 bit architecture"
11177dc83faSkamil	fi
11277dc83faSkamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
11377dc83faSkamil		atf_skip "c++ -m32 not supported on this architecture"
11477dc83faSkamil	else
11577dc83faSkamil		if fgrep -q _LP64 ./def32; then
11677dc83faSkamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
11777dc83faSkamil		fi
11877dc83faSkamil	fi
11977dc83faSkamil
12077dc83faSkamil	cat > test.cpp << EOF
12177dc83faSkamil#include <stdio.h>
12277dc83faSkamil#include <stdlib.h>
12377dc83faSkamilint main(void) {printf("hello world\n");exit(0);}
12477dc83faSkamilEOF
125*ac615257Sskrll	atf_check -s exit:0 -o ignore -e ignore c++ -static  -m32 -pg -o hello test.cpp
12677dc83faSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
12777dc83faSkamil}
12877dc83faSkamil
12977dc83faSkamil
13077dc83faSkamilhello_static_body() {
13177dc83faSkamil	cat > test.cpp << EOF
13277dc83faSkamil#include <stdio.h>
13377dc83faSkamil#include <stdlib.h>
13477dc83faSkamilint main(void) {printf("hello world\n");exit(0);}
13577dc83faSkamilEOF
13677dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore c++ -static -o hello test.cpp
13777dc83faSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
13877dc83faSkamil}
13977dc83faSkamil
140b971227cSkamilhello_pic_body() {
141b971227cSkamil	cat > test.cpp << EOF
142b971227cSkamil#include <stdlib.h>
143b971227cSkamilint callpic(void);
144b971227cSkamilint main(void) {callpic();exit(0);}
145b971227cSkamilEOF
146b971227cSkamil	cat > pic.cpp << EOF
147b971227cSkamil#include <stdio.h>
148b971227cSkamilint callpic(void) {printf("hello world\n");return 0;}
149b971227cSkamilEOF
150b971227cSkamil
151b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore \
152b971227cSkamil	    c++ -fPIC -shared -o libtest.so pic.cpp
153b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore \
154b971227cSkamil	    c++ -o hello test.cpp -L. -ltest
155b971227cSkamil
156b971227cSkamil	export LD_LIBRARY_PATH=.
157b971227cSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
158b971227cSkamil}
159b971227cSkamil
16077dc83faSkamilhello_pic_32_body() {
16177dc83faSkamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
16277dc83faSkamil		atf_skip "this is not a 64 bit architecture"
16377dc83faSkamil	fi
16477dc83faSkamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
16577dc83faSkamil		atf_skip "c++ -m32 not supported on this architecture"
16677dc83faSkamil	else
16777dc83faSkamil		if fgrep -q _LP64 ./def32; then
16877dc83faSkamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
16977dc83faSkamil		fi
17077dc83faSkamil	fi
17177dc83faSkamil	cat > test.cpp << EOF
17277dc83faSkamil#include <stdlib.h>
17377dc83faSkamilint callpic(void);
17477dc83faSkamilint main(void) {callpic();exit(0);}
17577dc83faSkamilEOF
17677dc83faSkamil	cat > pic.cpp << EOF
17777dc83faSkamil#include <stdio.h>
17877dc83faSkamilint callpic(void) {printf("hello world\n");return 0;}
17977dc83faSkamilEOF
18077dc83faSkamil
18177dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore \
18277dc83faSkamil	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
18377dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore \
18477dc83faSkamil	    c++ -m32 -o hello test.cpp -L. -ltest
18577dc83faSkamil
18677dc83faSkamil	export LD_LIBRARY_PATH=.
18777dc83faSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
18877dc83faSkamil}
18977dc83faSkamil
19077dc83faSkamilhello_pic_profile_body() {
19177dc83faSkamil	cat > test.cpp << EOF
19277dc83faSkamil#include <stdlib.h>
19377dc83faSkamilint callpic(void);
19477dc83faSkamilint main(void) {callpic();exit(0);}
19577dc83faSkamilEOF
19677dc83faSkamil	cat > pic.cpp << EOF
19777dc83faSkamil#include <stdio.h>
19877dc83faSkamilint callpic(void) {printf("hello world\n");return 0;}
19977dc83faSkamilEOF
20077dc83faSkamil
20177dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore \
20277dc83faSkamil	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
20377dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore \
20477dc83faSkamil	    c++ -pg -o hello test.cpp -L. -ltest
20577dc83faSkamil
20677dc83faSkamil	export LD_LIBRARY_PATH=.
20777dc83faSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
20877dc83faSkamil}
20977dc83faSkamil
21077dc83faSkamilhello_pic_profile_32_body() {
21177dc83faSkamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
21277dc83faSkamil		atf_skip "this is not a 64 bit architecture"
21377dc83faSkamil	fi
21477dc83faSkamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
21577dc83faSkamil		atf_skip "c++ -m32 not supported on this architecture"
21677dc83faSkamil	else
21777dc83faSkamil		if fgrep -q _LP64 ./def32; then
21877dc83faSkamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
21977dc83faSkamil		fi
22077dc83faSkamil	fi
22177dc83faSkamil
22277dc83faSkamil	cat > test.cpp << EOF
22377dc83faSkamil#include <stdlib.h>
22477dc83faSkamilint callpic(void);
22577dc83faSkamilint main(void) {callpic();exit(0);}
22677dc83faSkamilEOF
22777dc83faSkamil	cat > pic.cpp << EOF
22877dc83faSkamil#include <stdio.h>
22977dc83faSkamilint callpic(void) {printf("hello world\n");return 0;}
23077dc83faSkamilEOF
23177dc83faSkamil
23277dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore \
23377dc83faSkamil	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
23477dc83faSkamil	atf_check -s exit:0 -o ignore -e ignore \
23577dc83faSkamil	    c++ -m32 -pg -o hello test.cpp -L. -ltest
23677dc83faSkamil
23777dc83faSkamil	export LD_LIBRARY_PATH=.
23877dc83faSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
23977dc83faSkamil}
24077dc83faSkamil
241b971227cSkamilhello_pie_body() {
242b971227cSkamil	# check whether this arch supports -pie
243b971227cSkamil	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
244b971227cSkamil		atf_skip "c++ -pie not supported on this architecture"
245b971227cSkamil	fi
246b971227cSkamil	cat > test.cpp << EOF
247b971227cSkamil#include <stdio.h>
248b971227cSkamil#include <stdlib.h>
249b971227cSkamilint main(void) {printf("hello world\n");exit(0);}
250b971227cSkamilEOF
251b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
252b971227cSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello
253b971227cSkamil}
254b971227cSkamil
255b971227cSkamilhello32_body() {
256b971227cSkamil	# check whether this arch is 64bit
257b971227cSkamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
258b971227cSkamil		atf_skip "this is not a 64 bit architecture"
259b971227cSkamil	fi
260b971227cSkamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
261b971227cSkamil		atf_skip "c++ -m32 not supported on this architecture"
262b971227cSkamil	else
263b971227cSkamil		if fgrep -q _LP64 ./def32; then
264b971227cSkamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
265b971227cSkamil		fi
266b971227cSkamil	fi
267b971227cSkamil
268b971227cSkamil	cat > test.cpp << EOF
269b971227cSkamil#include <stdio.h>
270b971227cSkamil#include <stdlib.h>
271b971227cSkamilint main(void) {printf("hello world\n");exit(0);}
272b971227cSkamilEOF
273b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
274b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
275b971227cSkamil	file -b ./hello32 > ./ftype32
276b971227cSkamil	file -b ./hello64 > ./ftype64
277b971227cSkamil	if diff ./ftype32 ./ftype64 >/dev/null; then
278b971227cSkamil		atf_fail "generated binaries do not differ"
279b971227cSkamil	fi
280b971227cSkamil	echo "32bit binaries on this platform are:"
281b971227cSkamil	cat ./ftype32
282b971227cSkamil	echo "While native (64bit) binaries are:"
283b971227cSkamil	cat ./ftype64
284b971227cSkamil	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
285b971227cSkamil
286b971227cSkamil	# do another test with static 32bit binaries
287b971227cSkamil	cat > test.cpp << EOF
288b971227cSkamil#include <stdio.h>
289b971227cSkamil#include <stdlib.h>
290b971227cSkamilint main(void) {printf("hello static world\n");exit(0);}
291b971227cSkamilEOF
292b971227cSkamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
293b971227cSkamil	    -static test.cpp
294b971227cSkamil	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
295b971227cSkamil}
296b971227cSkamil
297b971227cSkamilatf_init_test_cases()
298b971227cSkamil{
299b971227cSkamil
300b971227cSkamil	atf_add_test_case hello
3014fcb013cSmartin	atf_add_test_case hello_profile
302b971227cSkamil	atf_add_test_case hello_pic
303b971227cSkamil	atf_add_test_case hello_pie
304b971227cSkamil	atf_add_test_case hello32
30577dc83faSkamil	atf_add_test_case hello_static
30677dc83faSkamil	atf_add_test_case hello_pic_32
30777dc83faSkamil	atf_add_test_case hello_pic_profile
30877dc83faSkamil	atf_add_test_case hello_pic_profile_32
30977dc83faSkamil	atf_add_test_case hello_profile_32
310b971227cSkamil}
311