summaryrefslogtreecommitdiff
path: root/configure
blob: 8866fdab0cee363aa23f80881c9666054d0578e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash

#
# Enhanced Seccomp Library Configure Script
#
# Copyright (c) 2012 Red Hat <pmoore@redhat.com>
# Author: Paul Moore <pmoore@redhat.com>
#

#
# This library is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License as
# published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, see <http://www.gnu.org/licenses>.
#

# configuration defaults
opt_prefix="/usr/local"
opt_libdir=""
opt_sysinc_seccomp="yes"
opt_bindings_python="no"

# output files
cnf_mk_file="configure.mk"
cnf_h_file="configure.h"

####
# functions

function test_deps() {
	[[ -z "$1" ]] && return 0
	which "$1" >& /dev/null && return 0
	return 1
}

function verify_deps() {
	[[ -z "$1" ]] && return
	if ! test_deps "$1"; then
		echo "error: install \"$1\" and include it in your \$PATH"
		exit 1
	fi
}

function msg_usage() {
	cat << EOF
Configure the enhanced seccomp library, libseccomp, for this system.

Usage:
  ./configure <options>

Options:
* general configuration
  -h, --help		display this help and exit
* installation configuration
  --prefix=PREFIX	installation base [/usr/local]
  --libdir=DIR		library directory [/usr/local/lib]
* build options
  --enable-python	build the python bindings, requires cython
EOF
}

function msg_summary() {
	cat << EOF
 CONFIGURATION SUMMARY
  libseccomp version:	${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_MICRO}
  installation base:	$opt_prefix
  library directory:	$opt_libdir
  use system includes:	$opt_sysinc_seccomp
  python bindings:	$opt_bindings_python
EOF
}

function msg_error() {
	echo "error: $@"
}

function cnf_mk_header() {
	echo "# generated by configure on $(date -R)" >> $cnf_mk_file
	echo "#   options: \"$opt_str\"" >> $cnf_mk_file
	echo "" >> $cnf_mk_file
}

function cnf_mk_footer() {
	echo "" >> $cnf_mk_file
}

function cnf_mk_entry() {
	[[ $# -ne 2 ]] && return
	case "$2" in
	no)
		echo "$1 = 0" >> $cnf_mk_file
		;;
	yes)
		echo "$1 = 1" >> $cnf_mk_file
		;;
	*)
		echo "$1 = \"$2\"" >> $cnf_mk_file
	esac
}

function cnf_h_header() {
	echo "/* generated by configure on $(date -R) */" >> $cnf_h_file
	echo "/*   options: \"$opt_str\" */" >> $cnf_h_file
	echo "" >> $cnf_h_file
	echo "#ifndef _CONFIGURE_H" >> $cnf_h_file
	echo "#define _CONFIGURE_H" >> $cnf_h_file
	echo "" >> $cnf_h_file
}

function cnf_h_footer() {
	echo "" >> $cnf_h_file
	echo "#endif" >> $cnf_h_file
	echo "" >> $cnf_h_file
}

function cnf_h_entry() {
	[[ $# -ne 2 ]] && return
	case "$2" in
	no)
		echo "#undef $1" >> $cnf_h_file
		;;
	yes)
		echo "#define $1	1" >> $cnf_h_file
		;;
	*)
		echo "#define $1	$2" >> $cnf_h_file
	esac
}

function cnf_reset() {
	cat /dev/null > $cnf_mk_file
	cat /dev/null > $cnf_h_file
}

function cnf_header() {
	cnf_mk_header
	cnf_h_header
}

function cnf_entry() {
	cnf_mk_entry "$1" "$2"
	cnf_h_entry "$1" "$2"
}

function cnf_footer() {
	cnf_mk_footer
	cnf_h_footer
}

function tmpl_filter() {
	name="echo \$$1"
	val="$(eval $name)"
	cat - | sed -e 's/%%'"$1"'%%/'"${val//\//\\/}"'/g;'
}

####
# main

#
# setup
#

# verify script dependencies
verify_deps getopt

# parse the command line options
opt_str="$@"
opt=$(getopt -n "$0" --options "h" --longoptions "help,prefix:,libdir:,enable-python" -- "$@")
eval set -- "$opt"
while [[ $# -gt 0 ]]; do
	case "$1" in
	--prefix)
		opt_prefix="$2"
		shift 2
		;;
	--libdir)
		opt_libdir="$2"
		shift 2
		;;
	--enable-python)
		opt_bindings_python="yes"
		shift
		;;
	-h|--help)
		msg_usage
		exit 0
		;;
	--)
		shift
		;;
	*)
		msg_usage
		exit 1
	esac
done

# validate the options
if [[ -e "$opt_prefix" && ! -d "$opt_prefix" ]]; then
	msg_error "install prefix ($opt_prefix) is not a directory"
	exit 1
fi
if [[ -z $opt_libdir ]]; then
	opt_libdir="$opt_prefix/lib"
fi
if [[ -e "$opt_libdir" && ! -d "$opt_libdir" ]]; then
	msg_error "libdir ($opt_libdir) is not a directory"
	exit 1
fi
if [[ "$opt_bindings_python" = "yes" ]]; then
	if ! test_deps cython; then
		msg_error "python bindings require the cython package"
		exit 1
	fi
fi

#
# automatic configuration
#

# system seccomp includes
if [[ -r "/usr/include/linux/seccomp.h" ]]; then
	opt_sysinc_seccomp="yes"
else
	opt_sysinc_seccomp="no"
fi

# generate the version files
. ./version_info
VERSION_RELEASE="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_MICRO}"
rm -f ./version_info.mk
echo "# generated by configure on $(date -R)" >> ./version_info.mk
echo "VERSION_MAJOR=$VERSION_MAJOR" >> ./version_info.mk
echo "VERSION_MINOR=$VERSION_MINOR" >> ./version_info.mk
echo "VERSION_MICRO=$VERSION_MICRO" >> ./version_info.mk
echo "VERSION_RELEASE=$VERSION_RELEASE" >> ./version_info.mk

# generate the pkg-config metadata
INSTALL_PREFIX="$opt_prefix"
INSTALL_LIBDIR="$opt_libdir"
rm -f ./libseccomp.pc
cat ./libseccomp.pc.in | \
	tmpl_filter INSTALL_PREFIX | \
	tmpl_filter INSTALL_LIBDIR | \
	tmpl_filter VERSION_RELEASE \
	>> ./libseccomp.pc

#
# finish
#

# reset the configuration files
cnf_reset
cnf_header

# output the configuration files
cnf_mk_entry "CONF_INSTALL_PREFIX" "$opt_prefix"
cnf_mk_entry "CONF_INSTALL_LIBDIR" "$opt_libdir"
cnf_entry "CONF_SYSINC_SECCOMP" "$opt_sysinc_seccomp"
cnf_entry "CONF_BINDINGS_PYTHON" "$opt_bindings_python"

# configuration footer
cnf_footer

# display a summary and exit
msg_summary
exit 0