create-ccan-tree 3.5 KB

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