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