xref: /freebsd/contrib/netbsd-tests/usr.bin/cc/t_hello.sh (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper#	$NetBSD: t_hello.sh,v 1.2 2012/07/21 12:30:55 martin Exp $
2*57718be8SEnji Cooper#
3*57718be8SEnji Cooper# Copyright (c) 2011 The NetBSD Foundation, Inc.
4*57718be8SEnji Cooper# All rights reserved.
5*57718be8SEnji Cooper#
6*57718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without
7*57718be8SEnji Cooper# modification, are permitted provided that the following conditions
8*57718be8SEnji Cooper# are met:
9*57718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright
10*57718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer.
11*57718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
12*57718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
13*57718be8SEnji Cooper#    documentation and/or other materials provided with the distribution.
14*57718be8SEnji Cooper#
15*57718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*57718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*57718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*57718be8SEnji Cooper# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*57718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*57718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*57718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*57718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*57718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*57718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*57718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE.
26*57718be8SEnji Cooper#
27*57718be8SEnji Cooper
28*57718be8SEnji Cooperatf_test_case hello
29*57718be8SEnji Cooperhello_head() {
30*57718be8SEnji Cooper	atf_set "descr" "compile and run \"hello world\""
31*57718be8SEnji Cooper	atf_set "require.progs" "cc"
32*57718be8SEnji Cooper}
33*57718be8SEnji Cooper
34*57718be8SEnji Cooperatf_test_case hello_pic
35*57718be8SEnji Cooperhello_pic_head() {
36*57718be8SEnji Cooper	atf_set "descr" "compile and run PIC \"hello world\""
37*57718be8SEnji Cooper	atf_set "require.progs" "cc"
38*57718be8SEnji Cooper}
39*57718be8SEnji Cooper
40*57718be8SEnji Cooperatf_test_case hello_pie
41*57718be8SEnji Cooperhello_pie_head() {
42*57718be8SEnji Cooper	atf_set "descr" "compile and run position independend (PIE) \"hello world\""
43*57718be8SEnji Cooper	atf_set "require.progs" "cc"
44*57718be8SEnji Cooper}
45*57718be8SEnji Cooper
46*57718be8SEnji Cooperatf_test_case hello32
47*57718be8SEnji Cooperhello32_head() {
48*57718be8SEnji Cooper	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
49*57718be8SEnji Cooper	atf_set "require.progs" "cc file diff cat"
50*57718be8SEnji Cooper}
51*57718be8SEnji Cooper
52*57718be8SEnji Cooperhello_body() {
53*57718be8SEnji Cooper	cat > test.c << EOF
54*57718be8SEnji Cooper#include <stdio.h>
55*57718be8SEnji Cooper#include <stdlib.h>
56*57718be8SEnji Cooperint main(void) {printf("hello world\n");exit(0);}
57*57718be8SEnji CooperEOF
58*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore cc -o hello test.c
59*57718be8SEnji Cooper	atf_check -s exit:0 -o inline:"hello world\n" ./hello
60*57718be8SEnji Cooper}
61*57718be8SEnji Cooper
62*57718be8SEnji Cooperhello_pic_body() {
63*57718be8SEnji Cooper	cat > test.c << EOF
64*57718be8SEnji Cooper#include <stdlib.h>
65*57718be8SEnji Cooperint main(void) {callpic();exit(0);}
66*57718be8SEnji CooperEOF
67*57718be8SEnji Cooper	cat > pic.c << EOF
68*57718be8SEnji Cooper#include <stdio.h>
69*57718be8SEnji Cooperint callpic(void) {printf("hello world\n");}
70*57718be8SEnji CooperEOF
71*57718be8SEnji Cooper
72*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore \
73*57718be8SEnji Cooper	    cc -fPIC -dPIC -shared -o libtest.so pic.c
74*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore \
75*57718be8SEnji Cooper	    cc -o hello test.c -L. -ltest
76*57718be8SEnji Cooper
77*57718be8SEnji Cooper	export LD_LIBRARY_PATH=.
78*57718be8SEnji Cooper	atf_check -s exit:0 -o inline:"hello world\n" ./hello
79*57718be8SEnji Cooper}
80*57718be8SEnji Cooper
81*57718be8SEnji Cooperhello_pie_body() {
82*57718be8SEnji Cooper	# check whether this arch supports -pie
83*57718be8SEnji Cooper	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
84*57718be8SEnji Cooper		atf_skip "cc -pie not supported on this architecture"
85*57718be8SEnji Cooper	fi
86*57718be8SEnji Cooper	cat > test.c << EOF
87*57718be8SEnji Cooper#include <stdio.h>
88*57718be8SEnji Cooper#include <stdlib.h>
89*57718be8SEnji Cooperint main(void) {printf("hello world\n");exit(0);}
90*57718be8SEnji CooperEOF
91*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore cc -fpie -pie -o hello test.c
92*57718be8SEnji Cooper	atf_check -s exit:0 -o inline:"hello world\n" ./hello
93*57718be8SEnji Cooper}
94*57718be8SEnji Cooper
95*57718be8SEnji Cooperhello32_body() {
96*57718be8SEnji Cooper	# check whether this arch is 64bit
97*57718be8SEnji Cooper	if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
98*57718be8SEnji Cooper		atf_skip "this is not a 64 bit architecture"
99*57718be8SEnji Cooper	fi
100*57718be8SEnji Cooper	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
101*57718be8SEnji Cooper		atf_skip "cc -m32 not supported on this architecture"
102*57718be8SEnji Cooper	else
103*57718be8SEnji Cooper		if fgrep -q _LP64 ./def32; then
104*57718be8SEnji Cooper			atf_fail "cc -m32 does not generate netbsd32 binaries"
105*57718be8SEnji Cooper		fi
106*57718be8SEnji Cooper	fi
107*57718be8SEnji Cooper
108*57718be8SEnji Cooper	cat > test.c << EOF
109*57718be8SEnji Cooper#include <stdio.h>
110*57718be8SEnji Cooper#include <stdlib.h>
111*57718be8SEnji Cooperint main(void) {printf("hello world\n");exit(0);}
112*57718be8SEnji CooperEOF
113*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore cc -o hello32 -m32 test.c
114*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore cc -o hello64 test.c
115*57718be8SEnji Cooper	file -b ./hello32 > ./ftype32
116*57718be8SEnji Cooper	file -b ./hello64 > ./ftype64
117*57718be8SEnji Cooper	if diff ./ftype32 ./ftype64 >/dev/null; then
118*57718be8SEnji Cooper		atf_fail "generated binaries do not differ"
119*57718be8SEnji Cooper	fi
120*57718be8SEnji Cooper	echo "32bit binaries on this platform are:"
121*57718be8SEnji Cooper	cat ./ftype32
122*57718be8SEnji Cooper	echo "While native (64bit) binaries are:"
123*57718be8SEnji Cooper	cat ./ftype64
124*57718be8SEnji Cooper	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
125*57718be8SEnji Cooper
126*57718be8SEnji Cooper	# do another test with static 32bit binaries
127*57718be8SEnji Cooper	cat > test.c << EOF
128*57718be8SEnji Cooper#include <stdio.h>
129*57718be8SEnji Cooper#include <stdlib.h>
130*57718be8SEnji Cooperint main(void) {printf("hello static world\n");exit(0);}
131*57718be8SEnji CooperEOF
132*57718be8SEnji Cooper	atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
133*57718be8SEnji Cooper	    -static test.c
134*57718be8SEnji Cooper	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
135*57718be8SEnji Cooper}
136*57718be8SEnji Cooper
137*57718be8SEnji Cooperatf_init_test_cases()
138*57718be8SEnji Cooper{
139*57718be8SEnji Cooper
140*57718be8SEnji Cooper	atf_add_test_case hello
141*57718be8SEnji Cooper	atf_add_test_case hello_pic
142*57718be8SEnji Cooper	atf_add_test_case hello_pie
143*57718be8SEnji Cooper	atf_add_test_case hello32
144*57718be8SEnji Cooper}
145