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