mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. #!/bin/bash
  2. #
  3. # AutoPCH: Automatically use pre-compiled headers
  4. #
  5. # Simply use this script instead of calling g++ directly.
  6. # This is a dirty hack. So don't wonder if it does not work
  7. # out of the box with every package.
  8. #
  9. # example using kdegames:
  10. # -----------------------
  11. #
  12. # time make CXX="autopch"
  13. # real 30m41.945s
  14. # user 25m30.924s
  15. # sys 3m9.300s
  16. #
  17. # without autopch:
  18. # real 42m59.061s
  19. # user 36m50.630s
  20. # sys 2m41.806s
  21. cxx="${AUTOPCHCXX:-g++}"
  22. [ "$cxx" == "$0" ] && exit 2
  23. cppfile="$( echo " $* " | sed -r 's,((.* )([^ ]*\.(cc|cp|cpp|CPP|cxx|c\+\+|C))) .*,\3,' )"
  24. cppdir="$( echo "$cppfile" | sed -r 's,[^/]*$,,'; )"
  25. cppargs="$( echo " $* " | sed -r 's, ([^- ]|-[MCSEco])[^ ]*,,g'; )"
  26. echo "AutoPCH> $cxx $*" >&2
  27. echo "AutoPCH - cppargs> \"$cppargs\"" >&2
  28. echo "AutoPCH - cppdir, cppfile> \"$cppdir\", \"$cppfile\"" >&2
  29. if [ ! -f "$cppfile" ]; then
  30. exec $cxx "$@"
  31. exit 1
  32. fi
  33. autopch="$cppdir${cppdir:+/}autopch"
  34. if [ ! -f "${autopch}.h" -a ! -f "${autopch}_oops.h" ]; then
  35. {
  36. echo "#ifndef _AUTOPCH_H_"
  37. echo "#define _AUTOPCH_H_"
  38. echo "#warning AutoPCH: THEWARNING"
  39. [ -f "${autopch}_incl.h" ] && cat "${autopch}_incl.h"
  40. sed -r '/^( | )*#( | )*(include|if|endif|define|undef)/ {
  41. : label /\\$/ { N ; P ; b label ; } ; p
  42. } ; d' "$cppdir"${cppdir:+/}*.{cc,cp,cpp,CPP,cxx,c++,C} 2&>1
  43. # \
  44. # | egrep -v "[<\"](${AUTOPCHEXCL:-autopch.h})[\">]"
  45. echo "#endif /* _AUTOPCH_H_ */"
  46. } > "${autopch}.h.plain"
  47. sed 's,THEWARNING,New pre-compiled header.,' \
  48. < "${autopch}.h.plain" > "${autopch}.h"
  49. # Precompile the autopch.h file
  50. echo "exec $cxx -I. $cppargs -x c++ -c \"${autopch}.h\"" \
  51. > "${autopch}.sh"
  52. if ! sh "${autopch}.sh" ; then
  53. mv -f "${autopch}.h" "${autopch}_oops.h"
  54. fi
  55. sed 's,THEWARNING,Pre-compiled header not used!,' \
  56. < "${autopch}.h.plain" > "${autopch}.h"
  57. fi
  58. if test -f ${autopch}.h && ! $cxx -include ${autopch}.h "$@"; then
  59. echo "AutoPCH: Fallback to non pre-compiled headers!" >&2
  60. exec $cxx "$@"
  61. exit 1
  62. fi
  63. exit 0