xref: /freebsd-src/contrib/netbsd-tests/share/mk/common.subr (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper# Copyright 2012 Google Inc.
2*57718be8SEnji Cooper# All rights reserved.
3*57718be8SEnji Cooper#
4*57718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without
5*57718be8SEnji Cooper# modification, are permitted provided that the following conditions are
6*57718be8SEnji Cooper# met:
7*57718be8SEnji Cooper#
8*57718be8SEnji Cooper# * Redistributions of source code must retain the above copyright
9*57718be8SEnji Cooper#   notice, this list of conditions and the following disclaimer.
10*57718be8SEnji Cooper# * Redistributions in binary form must reproduce the above copyright
11*57718be8SEnji Cooper#   notice, this list of conditions and the following disclaimer in the
12*57718be8SEnji Cooper#   documentation and/or other materials provided with the distribution.
13*57718be8SEnji Cooper# * Neither the name of Google Inc. nor the names of its contributors
14*57718be8SEnji Cooper#   may be used to endorse or promote products derived from this software
15*57718be8SEnji Cooper#   without specific prior written permission.
16*57718be8SEnji Cooper#
17*57718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*57718be8SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*57718be8SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*57718be8SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*57718be8SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*57718be8SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*57718be8SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*57718be8SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*57718be8SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*57718be8SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*57718be8SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*57718be8SEnji Cooper
29*57718be8SEnji Cooper# Dumps a file to the test's stdout for debugging purposes.
30*57718be8SEnji Cooperdump_file() {
31*57718be8SEnji Cooper	local file="${1}"; shift
32*57718be8SEnji Cooper
33*57718be8SEnji Cooper	echo "==== BEGIN ${file}"
34*57718be8SEnji Cooper	cat "${file}"
35*57718be8SEnji Cooper	echo "==== END   ${file}"
36*57718be8SEnji Cooper}
37*57718be8SEnji Cooper
38*57718be8SEnji Cooper# Creates a C source file with a single symbol in it.
39*57718be8SEnji Cooper#
40*57718be8SEnji Cooper# The file parameter specifies the path to the file to create, WITHOUT the
41*57718be8SEnji Cooper# C extension.  Both a source file and a header file are created.  Any
42*57718be8SEnji Cooper# intermediate directories are created too.
43*57718be8SEnji Cooper#
44*57718be8SEnji Cooper# The symbol parameter specifies the name of the symbol to place in the
45*57718be8SEnji Cooper# module, which is defined as a string holding the name of the module.
46*57718be8SEnji Coopercreate_c_module() {
47*57718be8SEnji Cooper	local file="${1}"; shift
48*57718be8SEnji Cooper	local symbol="${1}"; shift
49*57718be8SEnji Cooper
50*57718be8SEnji Cooper	mkdir -p "$(dirname ${file})"
51*57718be8SEnji Cooper	echo "extern const char *${symbol};" >"${file}.h"
52*57718be8SEnji Cooper	echo "const char *${symbol} = \"${file}\";" >"${file}.c"
53*57718be8SEnji Cooper
54*57718be8SEnji Cooper	dump_file "${file}.h"
55*57718be8SEnji Cooper	dump_file "${file}.c"
56*57718be8SEnji Cooper}
57*57718be8SEnji Cooper
58*57718be8SEnji Cooper# Creates a main C source file that references a set of modules.
59*57718be8SEnji Cooper#
60*57718be8SEnji Cooper# The modules to be referenced should have been created with
61*57718be8SEnji Cooper# create_c_module.  The generated source file ensures that all the modules
62*57718be8SEnji Cooper# are referenced in some way, which helps in testing that the generated
63*57718be8SEnji Cooper# binary holds all the necessary objects.
64*57718be8SEnji Cooper#
65*57718be8SEnji Cooper# The file parameter specifies the name of the file to create.
66*57718be8SEnji Cooper#
67*57718be8SEnji Cooper# The rest of the parameters are module:symbol pairs that specify the
68*57718be8SEnji Cooper# module to include and the symbol within them to reference.
69*57718be8SEnji Coopercreate_main_using_modules() {
70*57718be8SEnji Cooper	local file="${1}"; shift
71*57718be8SEnji Cooper
72*57718be8SEnji Cooper	local modules=
73*57718be8SEnji Cooper	local symbols=
74*57718be8SEnji Cooper	for spec in "${@}"; do
75*57718be8SEnji Cooper		modules="${modules} $(echo ${spec} | cut -d : -f 1)"
76*57718be8SEnji Cooper		symbols="${symbols} $(echo ${spec} | cut -d : -f 2)"
77*57718be8SEnji Cooper	done
78*57718be8SEnji Cooper
79*57718be8SEnji Cooper	echo '#include <stdio.h>' >"${file}"
80*57718be8SEnji Cooper	for module in ${modules}; do
81*57718be8SEnji Cooper		echo "#include \"${module}\"" >>"${file}"
82*57718be8SEnji Cooper	done
83*57718be8SEnji Cooper	echo 'int main(void) {' >>"${file}"
84*57718be8SEnji Cooper	for symbol in ${symbols}; do
85*57718be8SEnji Cooper		echo "printf(\"%s\n\", ${symbol});" >>"${file}"
86*57718be8SEnji Cooper	done
87*57718be8SEnji Cooper	echo 'return 0; }' >>"${file}"
88*57718be8SEnji Cooper
89*57718be8SEnji Cooper	dump_file "${file}"
90*57718be8SEnji Cooper}
91*57718be8SEnji Cooper
92*57718be8SEnji Cooper# Creates a mk.conf file and points MAKECONF to it.
93*57718be8SEnji Cooper#
94*57718be8SEnji Cooper# The first argument specifies the name of the configuration file to
95*57718be8SEnji Cooper# create.
96*57718be8SEnji Cooper#
97*57718be8SEnji Cooper# The rest of the arguments include a collection of modifiers for the
98*57718be8SEnji Cooper# generated configuration file and/or a collection of explicit variable
99*57718be8SEnji Cooper# names and their values to set.
100*57718be8SEnji Cooper#
101*57718be8SEnji Cooper# The qualifiers can be one of:
102*57718be8SEnji Cooper# - owngrp: Override the *OWN and *GRP variables to point to the current
103*57718be8SEnji Cooper#   user.
104*57718be8SEnji Coopercreate_make_conf() {
105*57718be8SEnji Cooper	local file="${1}"; shift
106*57718be8SEnji Cooper
107*57718be8SEnji Cooper	echo "# Test configuration file" >"${file}"
108*57718be8SEnji Cooper	for arg in "${@}"; do
109*57718be8SEnji Cooper		case "${arg}" in
110*57718be8SEnji Cooper		*=*)
111*57718be8SEnji Cooper			echo "${arg}" >>"${file}"
112*57718be8SEnji Cooper			;;
113*57718be8SEnji Cooper		owngrp)
114*57718be8SEnji Cooper			for class in BIN DOC LIB LINKS MAN; do
115*57718be8SEnji Cooper				echo "${class}OWN=$(id -un)" >>"${file}"
116*57718be8SEnji Cooper				echo "${class}GRP=$(id -gn)" >>"${file}"
117*57718be8SEnji Cooper			done
118*57718be8SEnji Cooper			;;
119*57718be8SEnji Cooper		esac
120*57718be8SEnji Cooper	done
121*57718be8SEnji Cooper
122*57718be8SEnji Cooper	case "${file}" in
123*57718be8SEnji Cooper	/*)
124*57718be8SEnji Cooper		MAKECONF="${file}"; export MAKECONF
125*57718be8SEnji Cooper		;;
126*57718be8SEnji Cooper	*)
127*57718be8SEnji Cooper		MAKECONF="$(pwd)/${file}"; export MAKECONF
128*57718be8SEnji Cooper		;;
129*57718be8SEnji Cooper	esac
130*57718be8SEnji Cooper
131*57718be8SEnji Cooper	dump_file "${file}"
132*57718be8SEnji Cooper}
133