]> git.ozlabs.org Git - ccan/blob - tools/create-ccan-tree
tools: ccan_depends requires a config.h
[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   -t, --exclude-tests         exclude test/ directories from ccan modules
11   -c, --exclude-configurator  exclude configurator. config.h must be
12                               supplied by another method (eg, autotools)
13 EOF
14 }
15
16 # parse options, setting the following flags
17 exclude_tests=
18 exclude_configurator=
19
20 opts=$(getopt -o tc --long exclude-tests,exclude-configurator -n $progname \
21                 -- "$@")
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                 -t|--exclude-tests)
35                         exclude_tests=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 # generate list of directories to copy
93 for module in $modules
94 do
95         # ccan_depends takes a directory name
96         module_dir="$srcdir/ccan/$module"
97
98         # we need the module itself...
99         echo "ccan/$module"
100
101         # .. plus dependencies
102         "$ccan_depends" "$module_dir"
103
104         if [ $? -ne 0 ]
105         then
106                 echo "Invalid ccan module '$module'?" >&2
107                 exit 1
108         fi
109 done |
110 sort -u |
111 while read dir
112 do
113         module_srcdir="$srcdir/$dir"
114         module_destdir="$tmpdir/$dir"
115         echo "Adding $dir"
116         mkdir -p "$(dirname "$module_destdir")"
117         cp -a "$module_srcdir" "$module_destdir"
118         if [ -n "$exclude_tests" ]
119         then
120                 rm -rf "$module_destdir/test"
121         fi
122 done
123
124 # we're done with the dependency-tracking, remove the tool from our
125 # temporary directory
126 rm "$ccan_depends"
127
128 echo "Adding licenses"
129 license_dir="$tmpdir/licenses"
130 mkdir "$license_dir"
131
132 find "$tmpdir" -type l -name LICENSE |
133 while read license
134 do
135         license_link=$(readlink "$license")
136         licence_file=$(basename "$license_link")
137         license_src="$srcdir/licenses/$licence_file"
138         license_dest="$license_dir/$license_file"
139         cp "$license_src" "$license_dest"
140 done
141
142 # add ccan Makefile
143 echo "Adding build infrastructure"
144 cp "$srcdir/Makefile-ccan" "$tmpdir/"
145
146 # add top-level Makefile
147 top_makefile="$tmpdir/Makefile"
148 cat > "$top_makefile" << EOF
149 all: libccan.a
150
151 include Makefile-ccan
152 EOF
153
154 # optionally add configurator, and relevant parts to top-level Makefile
155 if [ -z "$exclude_configurator" ]
156 then
157         echo "Adding configurator"
158         mkdir -p "$tmpdir/tools/configurator"
159         cp -a "$srcdir/tools/configurator" "$tmpdir/tools/"
160
161         cat >> "$top_makefile" <<EOF
162 tools/configurator/configurator: tools/configurator/configurator.c
163
164 config.h: tools/configurator/configurator Makefile Makefile-ccan
165         tools/configurator/configurator \$(CC) \$(CCAN_CFLAGS) > \$@ \\
166                 || rm -f \$@
167
168 objs = \$(patsubst %.c, %.o, \$(wildcard ccan/*/*.c))
169 \$(objs): config.h
170
171 EOF
172 fi
173
174 mv "$tmpdir" "$outdir"
175 echo "Done. ccan source tree built in $outdir"
176
177 trap - EXIT