xref: /freebsd/contrib/netbsd-tests/usr.bin/cc/t_hello.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#	$NetBSD: t_hello.sh,v 1.3 2016/04/03 14:41:30 gson 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_pic
35hello_pic_head() {
36	atf_set "descr" "compile and run PIC \"hello world\""
37	atf_set "require.progs" "cc"
38}
39
40atf_test_case hello_pie
41hello_pie_head() {
42	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
43	atf_set "require.progs" "cc"
44}
45
46atf_test_case hello32
47hello32_head() {
48	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
49	atf_set "require.progs" "cc file diff cat"
50}
51
52hello_body() {
53	cat > test.c << EOF
54#include <stdio.h>
55#include <stdlib.h>
56int main(void) {printf("hello world\n");exit(0);}
57EOF
58	atf_check -s exit:0 -o ignore -e ignore cc -o hello test.c
59	atf_check -s exit:0 -o inline:"hello world\n" ./hello
60}
61
62hello_pic_body() {
63	cat > test.c << EOF
64#include <stdlib.h>
65int main(void) {callpic();exit(0);}
66EOF
67	cat > pic.c << EOF
68#include <stdio.h>
69int callpic(void) {printf("hello world\n");}
70EOF
71
72	atf_check -s exit:0 -o ignore -e ignore \
73	    cc -fPIC -dPIC -shared -o libtest.so pic.c
74	atf_check -s exit:0 -o ignore -e ignore \
75	    cc -o hello test.c -L. -ltest
76
77	export LD_LIBRARY_PATH=.
78	atf_check -s exit:0 -o inline:"hello world\n" ./hello
79}
80
81hello_pie_body() {
82	# check whether this arch supports -pie
83	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
84		atf_skip "cc -pie not supported on this architecture"
85	fi
86	cat > test.c << EOF
87#include <stdio.h>
88#include <stdlib.h>
89int main(void) {printf("hello world\n");exit(0);}
90EOF
91	atf_check -s exit:0 -o ignore -e ignore cc -fpie -pie -o hello test.c
92	atf_check -s exit:0 -o inline:"hello world\n" ./hello
93}
94
95hello32_body() {
96	# check whether this arch is 64bit
97	if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
98		atf_skip "this is not a 64 bit architecture"
99	fi
100	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
101		atf_skip "cc -m32 not supported on this architecture"
102	else
103		if fgrep -q _LP64 ./def32; then
104			atf_fail "cc -m32 does not generate netbsd32 binaries"
105		fi
106	fi
107
108	cat > test.c << EOF
109#include <stdio.h>
110#include <stdlib.h>
111int main(void) {printf("hello world\n");exit(0);}
112EOF
113	atf_check -s exit:0 -o ignore -e ignore cc -o hello32 -m32 test.c
114	atf_check -s exit:0 -o ignore -e ignore cc -o hello64 test.c
115	file -b ./hello32 > ./ftype32
116	file -b ./hello64 > ./ftype64
117	if diff ./ftype32 ./ftype64 >/dev/null; then
118		atf_fail "generated binaries do not differ"
119	fi
120	echo "32bit binaries on this platform are:"
121	cat ./ftype32
122	echo "While native (64bit) binaries are:"
123	cat ./ftype64
124	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
125
126	# do another test with static 32bit binaries
127	cat > test.c << EOF
128#include <stdio.h>
129#include <stdlib.h>
130int main(void) {printf("hello static world\n");exit(0);}
131EOF
132	atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
133	    -static test.c
134	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
135}
136
137atf_init_test_cases()
138{
139
140	atf_add_test_case hello
141	atf_add_test_case hello_pic
142	atf_add_test_case hello_pie
143	atf_add_test_case hello32
144}
145