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