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