11.7Sriastrad#	$NetBSD: t_call_once.sh,v 1.7 2025/04/16 01:52:42 riastradh Exp $
21.1Skamil#
31.1Skamil# Copyright (c) 2018 The NetBSD Foundation, Inc.
41.1Skamil# All rights reserved.
51.1Skamil#
61.1Skamil# Redistribution and use in source and binary forms, with or without
71.1Skamil# modification, are permitted provided that the following conditions
81.1Skamil# are met:
91.1Skamil# 1. Redistributions of source code must retain the above copyright
101.1Skamil#    notice, this list of conditions and the following disclaimer.
111.1Skamil# 2. Redistributions in binary form must reproduce the above copyright
121.1Skamil#    notice, this list of conditions and the following disclaimer in the
131.1Skamil#    documentation and/or other materials provided with the distribution.
141.1Skamil#
151.1Skamil# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
161.1Skamil# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
171.1Skamil# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
181.1Skamil# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
191.1Skamil# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
201.1Skamil# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
211.1Skamil# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
221.1Skamil# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
231.1Skamil# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
241.1Skamil# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
251.1Skamil# POSSIBILITY OF SUCH DAMAGE.
261.1Skamil#
271.1Skamil
281.1Skamilatf_test_case call_once
291.1Skamilcall_once_head() {
301.1Skamil	atf_set "descr" "compile and run std::call_once"
311.1Skamil	atf_set "require.progs" "c++"
321.1Skamil}
331.1Skamil
341.1Skamilatf_test_case call_once_profile
351.1Skamilcall_once_profile_head() {
361.1Skamil	atf_set "descr" "compile and run std::call_once with profiling option"
371.1Skamil	atf_set "require.progs" "c++"
381.1Skamil}
391.1Skamil
401.1Skamilatf_test_case call_once_pic
411.1Skamilcall_once_pic_head() {
421.1Skamil	atf_set "descr" "compile and run PIC std::call_once"
431.1Skamil	atf_set "require.progs" "c++"
441.1Skamil}
451.1Skamil
461.1Skamilatf_test_case call_once_pic_32
471.1Skamilcall_once_pic_32_head() {
481.1Skamil	atf_set "descr" "compile and run 32-bit PIC std::call_once"
491.1Skamil	atf_set "require.progs" "c++"
501.1Skamil}
511.1Skamil
521.1Skamilatf_test_case call_once_pic_profile
531.1Skamilcall_once_pic_head() {
541.1Skamil	atf_set "descr" "compile and run PIC std::call_once with profiling flag"
551.1Skamil	atf_set "require.progs" "c++"
561.1Skamil}
571.1Skamil
581.1Skamilatf_test_case call_once_pic_profile_32
591.1Skamilcall_once_pic_profile_32_head() {
601.1Skamil	atf_set "descr" "compile and run 32-bit PIC std::call_once with profiling flag"
611.1Skamil	atf_set "require.progs" "c++"
621.1Skamil}
631.1Skamil
641.1Skamilatf_test_case call_once_profile_32
651.1Skamilcall_once_profile_32_head() {
661.1Skamil	atf_set "descr" "compile and run 32-bit std::call_once with profiling flag"
671.1Skamil	atf_set "require.progs" "c++"
681.1Skamil}
691.1Skamil
701.1Skamilatf_test_case call_once_pie
711.1Skamilcall_once_pie_head() {
721.1Skamil	atf_set "descr" "compile and run position independent (PIE) std::call_once"
731.1Skamil	atf_set "require.progs" "c++"
741.1Skamil}
751.1Skamil
761.1Skamilatf_test_case call_once_32
771.1Skamilcall_once_32_head() {
781.1Skamil	atf_set "descr" "compile and run std::call_once for/in netbsd32 emulation"
791.1Skamil	atf_set "require.progs" "c++ file diff cat"
801.1Skamil}
811.1Skamil
821.1Skamilatf_test_case call_once_static
831.1Skamilcall_once_static_head() {
841.1Skamil	atf_set "descr" "compile and run std::call_once with static flag"
851.1Skamil	atf_set "require.progs" "c++"
861.1Skamil}
871.1Skamil
881.1Skamilcall_once_body() {
891.1Skamil	cat > test.cpp << EOF
901.1Skamil#include <cstdio>
911.1Skamil#include <thread>
921.1Skamil#include <mutex>
931.1Skamilstd::once_flag flag;
941.1Skamilint main(void) {
951.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
961.1Skamil        return 0;
971.1Skamil}
981.1SkamilEOF
991.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once test.cpp -pthread
1001.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
1011.1Skamil}
1021.1Skamil
1031.1Skamilcall_once_profile_body() {
1041.7Sriastrad	case `uname -m` in
1051.7Sriastrad	riscv)	atf_expect_fail "PR port-riscv/59301:" \
1061.7Sriastrad		    " riscv: missing MKPROFILE=yes support"
1071.7Sriastrad		;;
1081.7Sriastrad	esac
1091.7Sriastrad
1101.1Skamil	cat > test.cpp << EOF
1111.1Skamil#include <cstdio>
1121.1Skamil#include <thread>
1131.1Skamil#include <mutex>
1141.1Skamilstd::once_flag flag;
1151.1Skamilint main(void) {
1161.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
1171.1Skamil        return 0;
1181.1Skamil}
1191.1SkamilEOF
1201.6Sskrll	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o call_once test.cpp -pthread
1211.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
1221.1Skamil}
1231.1Skamil
1241.1Skamilcall_once_profile_32_body() {
1251.1Skamil	# check whether this arch is 64bit
1261.1Skamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
1271.1Skamil		atf_skip "this is not a 64 bit architecture"
1281.1Skamil	fi
1291.1Skamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
1301.1Skamil		atf_skip "c++ -m32 not supported on this architecture"
1311.1Skamil	else
1321.1Skamil		if fgrep -q _LP64 ./def32; then
1331.1Skamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
1341.1Skamil		fi
1351.1Skamil	fi
1361.1Skamil
1371.7Sriastrad	case `uname -m` in
1381.7Sriastrad	riscv)	atf_expect_fail "PR port-riscv/59301:" \
1391.7Sriastrad		    " riscv: missing MKPROFILE=yes support"
1401.7Sriastrad		;;
1411.7Sriastrad	esac
1421.7Sriastrad
1431.1Skamil	cat > test.cpp << EOF
1441.1Skamil#include <cstdio>
1451.1Skamil#include <thread>
1461.1Skamil#include <mutex>
1471.1Skamilstd::once_flag flag;
1481.1Skamilint main(void) {
1491.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
1501.1Skamil        return 0;
1511.1Skamil}
1521.1SkamilEOF
1531.6Sskrll	atf_check -s exit:0 -o ignore -e ignore c++ -static -m32 -pg -o call_once test.cpp -pthread
1541.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
1551.1Skamil}
1561.1Skamil
1571.1Skamilcall_once_pic_body() {
1581.1Skamil	cat > test.cpp << EOF
1591.1Skamil#include <stdlib.h>
1601.1Skamilint callpic(void);
1611.1Skamilint main(void) {callpic();exit(0);}
1621.1SkamilEOF
1631.1Skamil	cat > pic.cpp << EOF
1641.1Skamil#include <cstdio>
1651.1Skamil#include <thread>
1661.1Skamil#include <mutex>
1671.1Skamilstd::once_flag flag;
1681.1Skamilint callpic(void) {
1691.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
1701.1Skamil        return 0;
1711.1Skamil}
1721.1SkamilEOF
1731.1Skamil
1741.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
1751.1Skamil	    c++ -fPIC -shared -o libtest.so pic.cpp
1761.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
1771.1Skamil	    c++ -o call_once test.cpp -L. -ltest -pthread
1781.1Skamil
1791.1Skamil	export LD_LIBRARY_PATH=.
1801.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
1811.1Skamil}
1821.1Skamil
1831.1Skamilcall_once_pic_32_body() {
1841.1Skamil	# check whether this arch is 64bit
1851.1Skamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
1861.1Skamil		atf_skip "this is not a 64 bit architecture"
1871.1Skamil	fi
1881.1Skamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
1891.1Skamil		atf_skip "c++ -m32 not supported on this architecture"
1901.1Skamil	else
1911.1Skamil		if fgrep -q _LP64 ./def32; then
1921.1Skamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
1931.1Skamil		fi
1941.1Skamil	fi
1951.1Skamil
1961.1Skamil	cat > test.cpp << EOF
1971.1Skamil#include <stdlib.h>
1981.1Skamilint callpic(void);
1991.1Skamilint main(void) {callpic();exit(0);}
2001.1SkamilEOF
2011.1Skamil	cat > pic.cpp << EOF
2021.1Skamil#include <cstdio>
2031.1Skamil#include <thread>
2041.1Skamil#include <mutex>
2051.1Skamilstd::once_flag flag;
2061.1Skamilint callpic(void) {
2071.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
2081.1Skamil        return 0;
2091.1Skamil}
2101.1SkamilEOF
2111.1Skamil
2121.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
2131.1Skamil	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
2141.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
2151.1Skamil	    c++ -m32 -o call_once test.cpp -L. -ltest -pthread
2161.1Skamil
2171.1Skamil	export LD_LIBRARY_PATH=.
2181.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
2191.1Skamil}
2201.1Skamil
2211.1Skamilcall_once_pic_profile_body() {
2221.7Sriastrad	case `uname -m` in
2231.7Sriastrad	riscv)	atf_expect_fail "PR port-riscv/59301:" \
2241.7Sriastrad		    " riscv: missing MKPROFILE=yes support"
2251.7Sriastrad		;;
2261.7Sriastrad	esac
2271.7Sriastrad
2281.1Skamil	cat > test.cpp << EOF
2291.1Skamil#include <stdlib.h>
2301.1Skamilint callpic(void);
2311.1Skamilint main(void) {callpic();exit(0);}
2321.1SkamilEOF
2331.1Skamil	cat > pic.cpp << EOF
2341.1Skamil#include <cstdio>
2351.1Skamil#include <thread>
2361.1Skamil#include <mutex>
2371.1Skamilstd::once_flag flag;
2381.1Skamilint callpic(void) {
2391.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
2401.1Skamil        return 0;
2411.1Skamil}
2421.1SkamilEOF
2431.1Skamil
2441.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
2451.1Skamil	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
2461.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
2471.1Skamil	    c++ -pg -o call_once test.cpp -L. -ltest -pthread
2481.1Skamil
2491.1Skamil	export LD_LIBRARY_PATH=.
2501.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
2511.1Skamil}
2521.1Skamil
2531.1Skamilcall_once_pic_profile_32_body() {
2541.1Skamil	# check whether this arch is 64bit
2551.1Skamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
2561.1Skamil		atf_skip "this is not a 64 bit architecture"
2571.1Skamil	fi
2581.1Skamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
2591.1Skamil		atf_skip "c++ -m32 not supported on this architecture"
2601.1Skamil	else
2611.1Skamil		if fgrep -q _LP64 ./def32; then
2621.1Skamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
2631.1Skamil		fi
2641.1Skamil	fi
2651.1Skamil
2661.7Sriastrad	case `uname -m` in
2671.7Sriastrad	riscv)	atf_expect_fail "PR port-riscv/59301:" \
2681.7Sriastrad		    " riscv: missing MKPROFILE=yes support"
2691.7Sriastrad		;;
2701.7Sriastrad	esac
2711.7Sriastrad
2721.1Skamil	cat > test.cpp << EOF
2731.1Skamil#include <stdlib.h>
2741.1Skamilint callpic(void);
2751.1Skamilint main(void) {callpic();exit(0);}
2761.1SkamilEOF
2771.1Skamil	cat > pic.cpp << EOF
2781.1Skamil#include <cstdio>
2791.1Skamil#include <thread>
2801.1Skamil#include <mutex>
2811.1Skamilstd::once_flag flag;
2821.1Skamilint callpic(void) {
2831.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
2841.1Skamil        return 0;
2851.1Skamil}
2861.1SkamilEOF
2871.1Skamil
2881.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
2891.1Skamil	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
2901.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
2911.1Skamil	    c++ -m32 -pg -o call_once test.cpp -L. -ltest -pthread
2921.1Skamil
2931.1Skamil	export LD_LIBRARY_PATH=.
2941.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
2951.1Skamil}
2961.1Skamil
2971.1Skamilcall_once_pie_body() {
2981.1Skamil	# check whether this arch supports -pie
2991.1Skamil	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
3001.1Skamil		atf_skip "c++ -pie not supported on this architecture"
3011.1Skamil	fi
3021.1Skamil	cat > test.cpp << EOF
3031.1Skamil#include <cstdio>
3041.1Skamil#include <thread>
3051.1Skamil#include <mutex>
3061.1Skamilstd::once_flag flag;
3071.1Skamilint main(void) {
3081.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
3091.1Skamil        return 0;
3101.1Skamil}
3111.1SkamilEOF
3121.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o call_once test.cpp -pthread
3131.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
3141.1Skamil}
3151.1Skamil
3161.1Skamilcall_once_32_body() {
3171.1Skamil	# check whether this arch is 64bit
3181.1Skamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
3191.1Skamil		atf_skip "this is not a 64 bit architecture"
3201.1Skamil	fi
3211.1Skamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
3221.1Skamil		atf_skip "c++ -m32 not supported on this architecture"
3231.1Skamil	else
3241.1Skamil		if fgrep -q _LP64 ./def32; then
3251.1Skamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
3261.1Skamil		fi
3271.1Skamil	fi
3281.1Skamil
3291.1Skamil	cat > test.cpp << EOF
3301.1Skamil#include <cstdio>
3311.1Skamil#include <thread>
3321.1Skamil#include <mutex>
3331.1Skamilstd::once_flag flag;
3341.1Skamilint main(void) {
3351.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
3361.1Skamil        return 0;
3371.1Skamil}
3381.1SkamilEOF
3391.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once_32 -m32 test.cpp -pthread
3401.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once_64 test.cpp -pthread
3411.1Skamil	file -b ./call_once_32 > ./ftype32
3421.1Skamil	file -b ./call_once_64 > ./ftype64
3431.1Skamil	if diff ./ftype32 ./ftype64 >/dev/null; then
3441.1Skamil		atf_fail "generated binaries do not differ"
3451.1Skamil	fi
3461.1Skamil	echo "32bit binaries on this platform are:"
3471.1Skamil	cat ./ftype32
3481.1Skamil	echo "While native (64bit) binaries are:"
3491.1Skamil	cat ./ftype64
3501.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once_32
3511.1Skamil
3521.1Skamil	# do another test with static 32bit binaries
3531.1Skamil	cat > test.cpp << EOF
3541.1Skamil#include <cstdio>
3551.1Skamil#include <thread>
3561.1Skamil#include <mutex>
3571.1Skamilstd::once_flag flag;
3581.1Skamilint main(void) {
3591.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
3601.1Skamil        return 0;
3611.1Skamil}
3621.1SkamilEOF
3631.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once -m32 -pthread \
3641.1Skamil	    -static test.cpp
3651.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
3661.1Skamil}
3671.1Skamil
3681.1Skamilcall_once_static_body() {
3691.1Skamil	cat > test.cpp << EOF
3701.1Skamil#include <cstdio>
3711.1Skamil#include <thread>
3721.1Skamil#include <mutex>
3731.1Skamilstd::once_flag flag;
3741.1Skamilint main(void) {
3751.1Skamil        std::call_once(flag, [](){ printf("hello, world!\n"); });
3761.1Skamil        return 0;
3771.1Skamil}
3781.1SkamilEOF
3791.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -static -o call_once test.cpp -pthread
3801.1Skamil	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
3811.1Skamil}
3821.1Skamil
3831.1Skamilatf_init_test_cases()
3841.1Skamil{
3851.1Skamil
3861.1Skamil	atf_add_test_case call_once
3871.1Skamil	atf_add_test_case call_once_profile
3881.1Skamil	atf_add_test_case call_once_pic
3891.1Skamil	atf_add_test_case call_once_pie
3901.1Skamil	atf_add_test_case call_once_32
3911.1Skamil	atf_add_test_case call_once_static
3921.1Skamil	atf_add_test_case call_once_pic_32
3931.1Skamil	atf_add_test_case call_once_pic_profile
3941.1Skamil	atf_add_test_case call_once_pic_profile_32
3951.1Skamil	atf_add_test_case call_once_profile_32
3961.1Skamil}
397