summaryrefslogtreecommitdiff
path: root/tools/create-ccan-tree
blob: c03933400320342f91ee609d1337fa45864114fb (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
#!/bin/bash

progname=$(basename $0)

usage() {
	cat >&2 <<EOF
Usage: $progname [options] <outdir> <depends>...

options:
  -a, --copy-all         copy all files in module tree (not just sources
		         required for build)
  -b, --build-type=TYPE  generate build infrastructure of TYPE
                         (one of 'make', 'make+config', 'automake', 'waf')
EOF
}

# parse options, setting the following flags
build_type=

opts=$(getopt -o ab: --long copy-all,build-type: -n $progname -- "$@")

if [ $? != 0 ]
then
	usage
	exit 1
fi

eval set -- "$opts"
MODFILES_ARGS="--no-tests --no-other"

while :
do
	case "$1" in
		-a|--copy-all)
			MODFILES_ARGS=""
			shift
			;;
		-b|--build-type)
			build_type="$2"
			shift 2
			;;
		--)
			shift
			break
			;;
		*)
			echo "Internal error!">&2
			exit 1
			;;
	esac
done

# we need at least two non-option arguments: outdir and a list of ccan
# modules
if [ $# -lt 2 ]
then
	usage
	exit 1
fi

# check --build-type argument sanity
case "$build_type" in
	''|'make'|'make+config'|'automake'|'waf')
		;;
	*)
		echo "Invalid build type '$build_type'" >&2
		exit 1
esac

srcdir=$(dirname $0)/../
outdir="$1"
shift
modules="$@"

if [ -e "$outdir" ]
then
	echo "Output directory '$outdir' already exists" >&2
	exit 1
fi

tmpdir="$(mktemp -d)"
# sanity check, we don't want to be overwriting stuff in arbitrary dirs
[ $? -eq 0 -a -d "${tmpdir}" ] || exit 1

# We'll need the ccan_depends tool, but also a clean source tree. Build
# tools/ccan_depends, and store it in $tmpdir for later use

echo "Building ccan_depends, modfiles"
ccan_depends="$tmpdir/ccan_depends"
modfiles="$tmpdir/modfiles"
make -s -C "$srcdir" tools/ccan_depends tools/modfiles
[ $? -eq 0 ] || exit 1
cp "$srcdir/tools/ccan_depends" "$ccan_depends"
cp "$srcdir/tools/modfiles" "$modfiles"

echo "Cleaning source tree"
make -s -C "$srcdir" clean
[ $? -eq 0 ] || exit 1

# clean up on error
trap 'rm -rf $tmpdir' EXIT

copy_ccan_module() {
	module_dir="$1"
	module_srcdir="$srcdir/$module_dir"
	module_destdir="$tmpdir/$module_dir"

	mkdir -p "$module_destdir"
	# Copy license
	license="$module_srcdir/LICENSE"
	[ -e "$license" ] && cp -a "$license" "$module_destdir"
	for f in $("$modfiles" $MODULES_ARGS --no-license --git-only "$module_dir"); do
	    mkdir -p $(dirname "$module_destdir"/"$f")
	    cp "$module_srcdir"/$f "$module_destdir"/$f
	done
}

# generate list of directories to copy
for module in $modules
do
	# ccan_depends takes a directory name
	module_dir="$srcdir/ccan/$module"

	# we need the module itself...
	echo "ccan/$module"

	# .. plus dependencies
	"$ccan_depends" "$module_dir"

	if [ $? -ne 0 ]
	then
		echo "Invalid ccan module '$module'?" >&2
		exit 1
	fi
done |
sort -u |
while read dir
do
	echo "Adding $dir"
	copy_ccan_module $dir
done

# we're done with the dependency-tracking, remove the tool from our
# temporary directory
rm "$ccan_depends" "$modfiles"

echo "Adding licenses"
license_dir="$tmpdir/licenses"
mkdir "$license_dir"

find "$tmpdir" -type l -name LICENSE |
while read license
do
	license_link=$(readlink "$license")
	licence_file=$(basename "$license_link")
	license_src="$srcdir/licenses/$licence_file"
	license_dest="$license_dir/$license_file"
	cp "$license_src" "$license_dest"
done

echo "Adding build infrastructure"

# generate automake Makefile.am
automakefile="$tmpdir/Makefile.am"
if [ "$build_type" = "automake" ]
then
	(
		echo "noinst_LIBRARIES = libccan.a"
		echo "libccan_a_SOURCES = \\"
		cd "$tmpdir"
		find ccan -maxdepth 2 -name '*.[ch]' |
			sed -e 's,^,\t,;$!s,$, \\,'
	) > "$automakefile"
fi

makefile="$tmpdir/Makefile"
if [ "$build_type" = "make" -o "$build_type" = "make+config" ]
then
	# add ccan Makefile
	cp "$srcdir/Makefile-ccan" "$tmpdir/"

	# add top-level Makefile
	cat > "$makefile" << EOF
all: libccan.a

include Makefile-ccan
EOF
fi

# optionally add configurator, and relevant parts to top-level Makefile
if [ "$build_type" = "make+config" ]
then
	echo "Adding configurator"
	mkdir -p "$tmpdir/tools/configurator"
	cp -a "$srcdir/tools/configurator" "$tmpdir/tools/"

	cat >> "$makefile" <<EOF
tools/configurator/configurator: tools/configurator/configurator.c

config.h: tools/configurator/configurator Makefile Makefile-ccan
	tools/configurator/configurator \$(CC) \$(CCAN_CFLAGS) > \$@ \\
		|| rm -f \$@

objs = \$(patsubst %.c, %.o, \$(wildcard ccan/*/*.c))
\$(objs): config.h

EOF
fi

if [ "$build_type" = "waf" ]
then
	echo "Adding waf wscript"
	cat > "$tmpdir/wscript" << EOF
def build(ctx):
    ctx(features     = 'c cstlib',
        source       = ctx.path.ant_glob('**/*.c'),
        target       = 'ccan',
        includes     = '.')
EOF
fi

mv "$tmpdir" "$outdir"
echo "Done. ccan source tree built in $outdir"

trap - EXIT