make.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/bash
  2. # This assumes that you're in the "build" of a cmake build process. E.g.,
  3. # you've already done something like:
  4. #
  5. # mkdir build
  6. # cd build
  7. # cmake ..
  8. #
  9. # Then this will (try to) circumvent cmake's aggressive (i.e., predefined macro
  10. # oblivious) dependency scanner by calling makedepend directly for every .cpp
  11. # file of every target.
  12. NUM_THREADS="1"
  13. while getopts ":C:j:h" opt; do
  14. case $opt in
  15. C)
  16. if ! cd "$OPTARG" 2>/dev/null
  17. then
  18. (>&2 echo "Failed to change directory to $OPTARG")
  19. exit 1
  20. fi
  21. ;;
  22. h)
  23. echo "
  24. Usage:
  25. make.sh [-j #] [-C dir] [project]"
  26. exit 1
  27. ;;
  28. j)
  29. NUM_THREADS="$OPTARG"
  30. ;;
  31. \?)
  32. echo "Invalid option: -$OPTARG" >&2
  33. ;;
  34. esac
  35. done
  36. # Shift so that $# makes sense
  37. shift $((OPTIND-1))
  38. if [ ! -f "Makefile" ]; then
  39. (>&2 echo "Makefile not found")
  40. exit 1
  41. fi
  42. # I don't want to do this if I don't have to:
  43. #make depend
  44. if [[ $# -eq 0 ]] ; then
  45. # Try to get project name
  46. PROJECTS=`find . -type d -depth 1 | sed -e "s/.\///g" | grep -v CMakeFiles | grep -v "^\."`
  47. else
  48. PROJECTS=`echo "$@" | tr ' ' '\n'`
  49. fi
  50. for PROJECT in $PROJECTS
  51. do
  52. PROJECT="${PROJECT%/}"
  53. if [ ! -d "$PROJECT" ]; then
  54. (>&2 echo "$PROJECT directory not found")
  55. exit 1
  56. fi
  57. for TARGET_DIR in `find "$PROJECT/CMakeFiles" -depth 1 | grep "\.dir"`
  58. do
  59. TARGET_DIR="${TARGET_DIR%/}"
  60. if ! grep --quiet "# make\.sh was here" "$TARGET_DIR/depend.make"
  61. then
  62. if grep --quiet "Empty dependencies" "$TARGET_DIR/depend.make"
  63. then
  64. make -f "$TARGET_DIR/build.make" "$TARGET_DIR/depend"
  65. fi
  66. if grep --quiet "Empty dependencies" "$TARGET_DIR/depend.make"
  67. then
  68. (>&2 echo "cmake's make failed to build a dependency file")
  69. (>&2 echo "[Consider issuing \`make depend\`]")
  70. continue
  71. fi
  72. fi
  73. TARGET=`basename -s .dir $TARGET_DIR`
  74. echo -e "\033[1;35mHacking dependencies of target $TARGET\033[0m"
  75. SRC_FILES=`cat "$TARGET_DIR/depend.make" | sed -n "s~^#*\($TARGET_DIR\)\(.*\)\.o: \2~\2~gp"`
  76. if grep --quiet "CMAKE generated file" "$TARGET_DIR/depend.make"
  77. then
  78. mv "$TARGET_DIR/depend.make"{,.bk}
  79. fi
  80. # Create new, empty depend.make file
  81. echo "# make.sh was here" > "$TARGET_DIR/depend.make"
  82. for SRC in $SRC_FILES
  83. do
  84. EXT="${SRC##*.}"
  85. makedepend -DIGL_STATIC_LIBRARY -p "$TARGET_DIR" -o .${EXT}.o -a -f "$TARGET_DIR/depend.make" -w0 $SRC 2>/dev/null
  86. # Add commented self so that next run of make.sh can use this depend.make file
  87. echo "#$TARGET_DIR$SRC.o: $SRC" >> "$TARGET_DIR/depend.make"
  88. done
  89. #echo -e "\033[0;35mHanding off to make $TARGET\033[0m"
  90. make -j$NUM_THREADS -f "${TARGET_DIR}/build.make" "${TARGET_DIR}/build"
  91. done
  92. done