buildLibrary.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/bash
  2. # Compile all cpp files in a folder and all of its sub-folders into a static library using the GNU c++ compiler
  3. # The global command for running the compiler (tested with g++ and clang++)
  4. COMPILER=$1
  5. # The root of the source files
  6. SOURCE_FOLDER=$2
  7. # The target folder where the library will be created
  8. TARGET=$3
  9. # The name of your library without any path nor extension
  10. LIBRARY_NAME=$4
  11. OBJECT_POSTFIX="_$4_TEMP.o"
  12. # C++ version
  13. CPP_VERSION=$5
  14. # Optimization level
  15. O_LEVEL=$6
  16. # Debug -DDEBUG or Release -DNDEBUG
  17. MODE=$7
  18. # Use CLEAN to recompile everything
  19. # Use LAZY to only recompile if the source folder itself has changed
  20. # If the library depends on anything outside of its folder that changes, lazy compilation will fail
  21. # If you change modes a lot and compiler versions a lot, multiple temporary folders may be useful
  22. BUILD_METHOD=$8
  23. LIBRARY_FILENAME=${TARGET}/${LIBRARY_NAME}.a
  24. SUM_FILENAME=${TARGET}/${LIBRARY_NAME}.md5
  25. if [ ${BUILD_METHOD} = CLEAN ]
  26. then
  27. echo "Clean building ${LIBRARY_NAME}"
  28. # Remove the old library when clean building
  29. rm -f ${LIBRARY_FILENAME}
  30. fi
  31. if [ ${BUILD_METHOD} = LAZY ]
  32. then
  33. echo "Lazy building ${LIBRARY_NAME}"
  34. # Cat takes a filename and returns the content
  35. OLD_SUM="$(cat ${SUM_FILENAME})"
  36. # Use tar to create an archive and apply md5sum on the archive
  37. NEW_SUM="$(tar cf - ${SOURCE_FOLDER} | md5sum)"
  38. # Remove extra characters from the result
  39. NEW_SUM=$(echo $NEW_SUM | tr -d " \t\n\r-")
  40. echo " Old md5 checksum: ${OLD_SUM}"
  41. echo " New md5 checksum: $NEW_SUM"
  42. # Compare new and old checksum
  43. # Placed in quotes to prevent taking internal spaces as argument separators
  44. if [ "${NEW_SUM}" != "${OLD_SUM}" ]
  45. then
  46. echo " Checksums didn't match. Rebuilding whole library to be safe."
  47. rm -f ${LIBRARY_FILENAME}
  48. fi
  49. # Clear the checksum in case of aborting compilation
  50. echo "Compilation not completed..." > ${SUM_FILENAME}
  51. fi
  52. # Check if the target library already exists
  53. if [ ! -f ${LIBRARY_FILENAME} ]; then
  54. # Argument: $1 as the folder to compile recursively
  55. compileFolder() {
  56. # Compile files in the folder
  57. for file in "$1"/*.cpp
  58. do
  59. [ -e $file ] || continue
  60. # Get name without path
  61. name=${file##*/}
  62. # Get name without extension nor path
  63. base=${name%.cpp}
  64. echo " C++ ${file}"
  65. ${COMPILER} ${CPP_VERSION} ${O_LEVEL} ${MODE} -Wall -c ${file} -o ${TARGET}/${base}${OBJECT_POSTFIX}
  66. if [ $? -ne 0 ]
  67. then
  68. echo "Failed to compile ${file}!"
  69. exit 1
  70. fi
  71. done
  72. # Recursively compile other folders
  73. for folder in "$1"/*
  74. do
  75. if [ -d "$folder" ]
  76. then
  77. compileFolder "$folder"
  78. fi
  79. done
  80. }
  81. # Compiling temporary objects
  82. echo "Compiling cpp files into object files in $TARGET using $MODE mode."
  83. compileFolder ${SOURCE_FOLDER}
  84. # Assembling static library
  85. echo "Assembling object files into ${LIBRARY_NAME}.a."
  86. ar rcs ${LIBRARY_FILENAME} ${TARGET}/*${OBJECT_POSTFIX}
  87. # Cleaning up temporary objects
  88. echo "Cleaning up temporary ${LIBRARY_NAME} object files."
  89. rm -f ${TARGET}/*${OBJECT_POSTFIX}
  90. fi
  91. if [ ${BUILD_METHOD} = LAZY ]
  92. then
  93. # Save new checksum to file when done compiling
  94. echo ${NEW_SUM} > ${SUM_FILENAME}
  95. fi