11.1Sjmmv# Copyright 2012 Google Inc.
21.1Sjmmv# All rights reserved.
31.1Sjmmv#
41.1Sjmmv# Redistribution and use in source and binary forms, with or without
51.1Sjmmv# modification, are permitted provided that the following conditions are
61.1Sjmmv# met:
71.1Sjmmv#
81.1Sjmmv# * Redistributions of source code must retain the above copyright
91.1Sjmmv#   notice, this list of conditions and the following disclaimer.
101.1Sjmmv# * Redistributions in binary form must reproduce the above copyright
111.1Sjmmv#   notice, this list of conditions and the following disclaimer in the
121.1Sjmmv#   documentation and/or other materials provided with the distribution.
131.1Sjmmv# * Neither the name of Google Inc. nor the names of its contributors
141.1Sjmmv#   may be used to endorse or promote products derived from this software
151.1Sjmmv#   without specific prior written permission.
161.1Sjmmv#
171.1Sjmmv# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181.1Sjmmv# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191.1Sjmmv# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201.1Sjmmv# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211.1Sjmmv# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221.1Sjmmv# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231.1Sjmmv# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241.1Sjmmv# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251.1Sjmmv# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261.1Sjmmv# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271.1Sjmmv# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281.1Sjmmv
291.1Sjmmv# Dumps a file to the test's stdout for debugging purposes.
301.1Sjmmvdump_file() {
311.1Sjmmv	local file="${1}"; shift
321.1Sjmmv
331.1Sjmmv	echo "==== BEGIN ${file}"
341.1Sjmmv	cat "${file}"
351.1Sjmmv	echo "==== END   ${file}"
361.1Sjmmv}
371.1Sjmmv
381.1Sjmmv# Creates a C source file with a single symbol in it.
391.1Sjmmv#
401.1Sjmmv# The file parameter specifies the path to the file to create, WITHOUT the
411.1Sjmmv# C extension.  Both a source file and a header file are created.  Any
421.1Sjmmv# intermediate directories are created too.
431.1Sjmmv#
441.1Sjmmv# The symbol parameter specifies the name of the symbol to place in the
451.1Sjmmv# module, which is defined as a string holding the name of the module.
461.1Sjmmvcreate_c_module() {
471.1Sjmmv	local file="${1}"; shift
481.1Sjmmv	local symbol="${1}"; shift
491.1Sjmmv
501.1Sjmmv	mkdir -p "$(dirname ${file})"
511.1Sjmmv	echo "extern const char *${symbol};" >"${file}.h"
521.1Sjmmv	echo "const char *${symbol} = \"${file}\";" >"${file}.c"
531.1Sjmmv
541.1Sjmmv	dump_file "${file}.h"
551.1Sjmmv	dump_file "${file}.c"
561.1Sjmmv}
571.1Sjmmv
581.1Sjmmv# Creates a main C source file that references a set of modules.
591.1Sjmmv#
601.1Sjmmv# The modules to be referenced should have been created with
611.1Sjmmv# create_c_module.  The generated source file ensures that all the modules
621.1Sjmmv# are referenced in some way, which helps in testing that the generated
631.1Sjmmv# binary holds all the necessary objects.
641.1Sjmmv#
651.1Sjmmv# The file parameter specifies the name of the file to create.
661.1Sjmmv#
671.1Sjmmv# The rest of the parameters are module:symbol pairs that specify the
681.1Sjmmv# module to include and the symbol within them to reference.
691.1Sjmmvcreate_main_using_modules() {
701.1Sjmmv	local file="${1}"; shift
711.1Sjmmv
721.1Sjmmv	local modules=
731.1Sjmmv	local symbols=
741.1Sjmmv	for spec in "${@}"; do
751.1Sjmmv		modules="${modules} $(echo ${spec} | cut -d : -f 1)"
761.1Sjmmv		symbols="${symbols} $(echo ${spec} | cut -d : -f 2)"
771.1Sjmmv	done
781.1Sjmmv
791.1Sjmmv	echo '#include <stdio.h>' >"${file}"
801.1Sjmmv	for module in ${modules}; do
811.1Sjmmv		echo "#include \"${module}\"" >>"${file}"
821.1Sjmmv	done
831.1Sjmmv	echo 'int main(void) {' >>"${file}"
841.1Sjmmv	for symbol in ${symbols}; do
851.1Sjmmv		echo "printf(\"%s\n\", ${symbol});" >>"${file}"
861.1Sjmmv	done
871.1Sjmmv	echo 'return 0; }' >>"${file}"
881.1Sjmmv
891.1Sjmmv	dump_file "${file}"
901.1Sjmmv}
911.1Sjmmv
921.1Sjmmv# Creates a mk.conf file and points MAKECONF to it.
931.1Sjmmv#
941.1Sjmmv# The first argument specifies the name of the configuration file to
951.1Sjmmv# create.
961.1Sjmmv#
971.1Sjmmv# The rest of the arguments include a collection of modifiers for the
981.1Sjmmv# generated configuration file and/or a collection of explicit variable
991.1Sjmmv# names and their values to set.
1001.1Sjmmv#
1011.1Sjmmv# The qualifiers can be one of:
1021.1Sjmmv# - owngrp: Override the *OWN and *GRP variables to point to the current
1031.1Sjmmv#   user.
1041.1Sjmmvcreate_make_conf() {
1051.1Sjmmv	local file="${1}"; shift
1061.1Sjmmv
1071.1Sjmmv	echo "# Test configuration file" >"${file}"
1081.1Sjmmv	for arg in "${@}"; do
1091.1Sjmmv		case "${arg}" in
1101.1Sjmmv		*=*)
1111.1Sjmmv			echo "${arg}" >>"${file}"
1121.1Sjmmv			;;
1131.1Sjmmv		owngrp)
1141.1Sjmmv			for class in BIN DOC LIB LINKS MAN; do
1151.1Sjmmv				echo "${class}OWN=$(id -un)" >>"${file}"
1161.1Sjmmv				echo "${class}GRP=$(id -gn)" >>"${file}"
1171.1Sjmmv			done
1181.1Sjmmv			;;
1191.1Sjmmv		esac
1201.1Sjmmv	done
1211.1Sjmmv
1221.1Sjmmv	case "${file}" in
1231.1Sjmmv	/*)
1241.1Sjmmv		MAKECONF="${file}"; export MAKECONF
1251.1Sjmmv		;;
1261.1Sjmmv	*)
1271.1Sjmmv		MAKECONF="$(pwd)/${file}"; export MAKECONF
1281.1Sjmmv		;;
1291.1Sjmmv	esac
1301.1Sjmmv
1311.1Sjmmv	dump_file "${file}"
1321.1Sjmmv}
133