xref: /netbsd-src/tests/usr.bin/cc/t_hello.sh (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1#	$NetBSD: t_hello.sh,v 1.9 2020/02/11 06:26:19 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" "cc"
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" "cc"
38}
39
40atf_test_case hello_pic
41hello_pic_head() {
42	atf_set "descr" "compile and run PIC \"hello world\""
43	atf_set "require.progs" "cc"
44}
45
46atf_test_case hello_pie
47hello_pie_head() {
48	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
49	atf_set "require.progs" "cc"
50}
51
52atf_test_case hello32
53hello32_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
58hello_body() {
59	cat > test.c << EOF
60#include <stdio.h>
61#include <stdlib.h>
62int main(void) {printf("hello world\n");exit(0);}
63EOF
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
68hello_profile_body() {
69	cat > test.c << EOF
70#include <stdio.h>
71#include <stdlib.h>
72int main(void) {printf("hello world\n");exit(0);}
73EOF
74	atf_check -s exit:0 -o ignore -e ignore cc -o hello -pg test.c
75	case `uname -p` in
76	aarch64)
77		atf_expect_fail 'cc -pg is busted on aarch64'
78		;;
79	esac
80	atf_check -s exit:0 -o inline:"hello world\n" ./hello
81	atf_check -s exit:0 -o ignore -e ignore cc -o hello2 -fprofile-generate test.c
82	atf_check -s exit:0 -o inline:"hello world\n" ./hello2
83}
84
85hello_pic_body() {
86	cat > test.c << EOF
87#include <stdlib.h>
88int callpic(void);
89int main(void) {callpic();exit(0);}
90EOF
91	cat > pic.c << EOF
92#include <stdio.h>
93int callpic(void) {printf("hello world\n");return 0;}
94EOF
95
96	atf_check -s exit:0 -o ignore -e ignore \
97	    cc -fPIC -shared -o libtest.so pic.c
98	atf_check -s exit:0 -o ignore -e ignore \
99	    cc -o hello test.c -L. -ltest
100
101	export LD_LIBRARY_PATH=.
102	atf_check -s exit:0 -o inline:"hello world\n" ./hello
103}
104
105hello_pie_body() {
106	# check whether this arch supports -pie
107	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
108		atf_skip "cc -pie not supported on this architecture"
109	fi
110	cat > test.c << EOF
111#include <stdio.h>
112#include <stdlib.h>
113int main(void) {printf("hello world\n");exit(0);}
114EOF
115	atf_check -s exit:0 -o ignore -e ignore cc -fpie -pie -o hello test.c
116	atf_check -s exit:0 -o inline:"hello world\n" ./hello
117}
118
119hello32_body() {
120	# check whether this arch is 64bit
121	if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
122		atf_skip "this is not a 64 bit architecture"
123	fi
124	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
125		atf_skip "cc -m32 not supported on this architecture"
126	else
127		if fgrep -q _LP64 ./def32; then
128			atf_fail "cc -m32 does not generate netbsd32 binaries"
129		fi
130	fi
131
132	cat > test.c << 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 cc -o hello32 -m32 test.c
138	atf_check -s exit:0 -o ignore -e ignore cc -o hello64 test.c
139	file -b ./hello32 > ./ftype32
140	file -b ./hello64 > ./ftype64
141	if diff ./ftype32 ./ftype64 >/dev/null; then
142		atf_fail "generated binaries do not differ"
143	fi
144	echo "32bit binaries on this platform are:"
145	cat ./ftype32
146	echo "While native (64bit) binaries are:"
147	cat ./ftype64
148	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
149
150	# do another test with static 32bit binaries
151	cat > test.c << EOF
152#include <stdio.h>
153#include <stdlib.h>
154int main(void) {printf("hello static world\n");exit(0);}
155EOF
156	atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
157	    -static test.c
158	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
159
160	# and another test with profile 32bit binaries
161	cat > test.c << EOF
162#include <stdio.h>
163#include <stdlib.h>
164int main(void) {printf("hello 32bit profile world\n");exit(0);}
165EOF
166	atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
167	    -pg test.c
168	atf_check -s exit:0 -o inline:"hello 32bit profile world\n" ./hello
169}
170
171atf_init_test_cases()
172{
173
174	atf_add_test_case hello
175	atf_add_test_case hello_profile
176	atf_add_test_case hello_pic
177	atf_add_test_case hello_pie
178	atf_add_test_case hello32
179}
180