xref: /netbsd-src/tests/usr.bin/cc/t_hello.sh (revision ac615257a7f4243b5374551e57c46a4b929d81a3)
1#	$NetBSD: t_hello.sh,v 1.12 2022/06/12 15:08:38 skrll 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 -static -o hello -pg test.c
75	atf_check -s exit:0 -o inline:"hello world\n" ./hello
76	atf_check -s exit:0 -o ignore -e ignore cc -o hello2 -fprofile-generate test.c
77	atf_check -s exit:0 -o inline:"hello world\n" ./hello2
78}
79
80hello_pic_body() {
81	cat > test.c << EOF
82#include <stdlib.h>
83int callpic(void);
84int main(void) {callpic();exit(0);}
85EOF
86	cat > pic.c << EOF
87#include <stdio.h>
88int callpic(void) {printf("hello world\n");return 0;}
89EOF
90
91	atf_check -s exit:0 -o ignore -e ignore \
92	    cc -fPIC -shared -o libtest.so pic.c
93	atf_check -s exit:0 -o ignore -e ignore \
94	    cc -o hello test.c -L. -ltest
95
96	export LD_LIBRARY_PATH=.
97	atf_check -s exit:0 -o inline:"hello world\n" ./hello
98}
99
100hello_pie_body() {
101	# check whether this arch supports -pie
102	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
103		atf_skip "cc -pie not supported on this architecture"
104	fi
105	cat > test.c << 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 cc -fpie -pie -o hello test.c
111	atf_check -s exit:0 -o inline:"hello world\n" ./hello
112}
113
114hello32_body() {
115	# check whether this arch is 64bit
116	if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
117		atf_skip "this is not a 64 bit architecture"
118	fi
119	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
120		atf_skip "cc -m32 not supported on this architecture"
121	else
122		if fgrep -q _LP64 ./def32; then
123			atf_fail "cc -m32 does not generate netbsd32 binaries"
124		fi
125	fi
126
127	cat > test.c << EOF
128#include <stdio.h>
129#include <stdlib.h>
130int main(void) {printf("hello world\n");exit(0);}
131EOF
132	atf_check -s exit:0 -o ignore -e ignore cc -o hello32 -m32 test.c
133	atf_check -s exit:0 -o ignore -e ignore cc -o hello64 test.c
134	file -b ./hello32 > ./ftype32
135	file -b ./hello64 > ./ftype64
136	if diff ./ftype32 ./ftype64 >/dev/null; then
137		atf_fail "generated binaries do not differ"
138	fi
139	echo "32bit binaries on this platform are:"
140	cat ./ftype32
141	echo "While native (64bit) binaries are:"
142	cat ./ftype64
143	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
144
145	# do another test with static 32bit binaries
146	cat > test.c << EOF
147#include <stdio.h>
148#include <stdlib.h>
149int main(void) {printf("hello static world\n");exit(0);}
150EOF
151	atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
152	    -static test.c
153	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
154
155	# and another test with profile 32bit binaries
156	cat > test.c << EOF
157#include <stdio.h>
158#include <stdlib.h>
159int main(void) {printf("hello 32bit profile world\n");exit(0);}
160EOF
161	atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
162	    -pg test.c
163	atf_check -s exit:0 -o inline:"hello 32bit profile world\n" ./hello
164}
165
166atf_init_test_cases()
167{
168
169	atf_add_test_case hello
170	atf_add_test_case hello_profile
171	atf_add_test_case hello_pic
172	atf_add_test_case hello_pie
173	atf_add_test_case hello32
174}
175