]> git.ozlabs.org Git - ccan/blob - tools/create-ccan-tree
tools/create-ccan-tree: replace --exclude-tests with --copy-all option
[ccan] / tools / create-ccan-tree
1 #!/bin/bash
2
3 progname=$(basename $0)
4
5 usage() {
6         cat >&2 <<EOF
7 Usage: $progname [options] <outdir> <depends>...
8
9 options:
10   -a, --copy-all              copy all files in module tree (not just
11                               sources required for build)
12   -c, --exclude-configurator  exclude configurator. config.h must be
13                               supplied by another method (eg, autotools)
14 EOF
15 }
16
17 # parse options, setting the following flags
18 copy_all=
19 exclude_configurator=
20
21 opts=$(getopt -o ac --long copy-all,exclude-configurator -n $progname -- "$@")
22
23 if [ $? != 0 ]
24 then
25         usage
26         exit 1
27 fi
28
29 eval set -- "$opts"
30
31 while :
32 do
33         case "$1" in
34                 -a|--copy-all)
35                         copy_all=1
36                         shift
37                         ;;
38                 -c|--exclude-configurator)
39                         exclude_configurator=1
40                         shift
41                         ;;
42                 --)
43                         shift
44                         break
45                         ;;
46                 *)
47                         echo "Internal error!">&2
48                         exit 1
49                         ;;
50         esac
51 done
52
53 # we need at least two non-option arguments: outdir and a list of ccan
54 # modules
55 if [ $# -lt 2 ]
56 then
57         usage
58         exit 1
59 fi
60
61 srcdir=$(dirname $0)/../
62 outdir="$1"
63 shift
64 modules="$@"
65
66 if [ -e "$outdir" ]
67 then
68         echo "Output directory '$outdir' already exists" >&2
69         exit 1
70 fi
71
72 tmpdir="$(mktemp -d)"
73 # sanity check, we don't want to be overwriting stuff in arbitrary dirs
74 [ $? -eq 0 -a -d "${tmpdir}" ] || exit 1
75
76 # We'll need the ccan_depends tool, but also a clean source tree. Build
77 # tools/ccan_depends, and store it in $tmpdir for later use
78
79 echo "Building ccan_depends"
80 ccan_depends="$tmpdir/ccan_depends"
81 make -s -C "$srcdir" tools/ccan_depends
82 [ $? -eq 0 ] || exit 1
83 cp "$srcdir/tools/ccan_depends" "$ccan_depends"
84
85 echo "Cleaning source tree"
86 make -s -C "$srcdir" clean
87 [ $? -eq 0 ] || exit 1
88
89 # clean up on error
90 trap 'rm -rf $tmpdir' EXIT
91
92 copy_ccan_module() {
93         module_dir="$1"
94         module_srcdir="$srcdir/$module_dir"
95         module_destdir="$tmpdir/$module_dir"
96
97         if [ -n "$copy_all" ]
98         then
99                 # bulk copy
100                 mkdir -p "$(dirname "$module_destdir")"
101                 cp -a "$module_srcdir" "$module_destdir"
102         else
103                 mkdir -p "$module_destdir"
104                 # only copy sources & license
105                 license="$module_srcdir/LICENSE"
106                 cp -a "$module_srcdir"/*.[ch] "$module_destdir"
107                 [ -e "$license" ] && cp -a "$license" "$module_destdir"
108         fi
109 }
110
111 # generate list of directories to copy
112 for module in $modules
113 do
114         # ccan_depends takes a directory name
115         module_dir="$srcdir/ccan/$module"
116
117         # we need the module itself...
118         echo "ccan/$module"
119
120         # .. plus dependencies
121         "$ccan_depends" "$module_dir"
122
123         if [ $? -ne 0 ]
124         then
125                 echo "Invalid ccan module '$module'?" >&2
126                 exit 1
127         fi
128 done |
129 sort -u |
130 while read dir
131 do
132         echo "Adding $dir"
133         copy_ccan_module $dir
134 done
135
136 # we're done with the dependency-tracking, remove the tool from our
137 # temporary directory
138 rm "$ccan_depends"
139
140 echo "Adding licenses"
141 license_dir="$tmpdir/licenses"
142 mkdir "$license_dir"
143
144 find "$tmpdir" -type l -name LICENSE |
145 while read license
146 do
147         license_link=$(readlink "$license")
148         licence_file=$(basename "$license_link")
149         license_src="$srcdir/licenses/$licence_file"
150         license_dest="$license_dir/$license_file"
151         cp "$license_src" "$license_dest"
152 done
153
154 # add ccan Makefile
155 echo "Adding build infrastructure"
156 cp "$srcdir/Makefile-ccan" "$tmpdir/"
157
158 # add top-level Makefile
159 top_makefile="$tmpdir/Makefile"
160 cat > "$top_makefile" << EOF
161 all: libccan.a
162
163 include Makefile-ccan
164 EOF
165
166 # optionally add configurator, and relevant parts to top-level Makefile
167 if [ -z "$exclude_configurator" ]
168 then
169         echo "Adding configurator"
170         mkdir -p "$tmpdir/tools/configurator"
171         cp -a "$srcdir/tools/configurator" "$tmpdir/tools/"
172
173         cat >> "$top_makefile" <<EOF
174 tools/configurator/configurator: tools/configurator/configurator.c
175
176 config.h: tools/configurator/configurator Makefile Makefile-ccan
177         tools/configurator/configurator \$(CC) \$(CCAN_CFLAGS) > \$@ \\
178                 || rm -f \$@
179
180 objs = \$(patsubst %.c, %.o, \$(wildcard ccan/*/*.c))
181 \$(objs): config.h
182
183 EOF
184 fi
185
186 mv "$tmpdir" "$outdir"
187 echo "Done. ccan source tree built in $outdir"
188
189 trap - EXIT