Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # A general compile script for my C/C++ projects
  4. # This is what I use instead of makefiles
  5. # It's just a simple script that works well for small projects and
  6. # can be modified to suit your needs. The nice thing is that you only have to
  7. # know one language, bash, not make or cmake or anything else, to modify it.
  8. #
  9. # To customize this script for your projects you will most likely only need to
  10. # change the variables at the top of the file.
  11. # COMPILER, COMPILE_FLAGS, FILE_EXTENSION, SOURCE_LOCATIONS, OUTPUT_NAME
  12. #
  13. # Currently, this does not support multiple file extensions (ex. .c, .cpp, etc.)
  14. # since that would involve a different compiler for each filetype. I might add it
  15. # later if I need that functionality.
  16. # It compiles the release and debug builds to the same location, only keeping the latest one.
  17. #
  18. # Usage: (commands implemented so far)
  19. # ./project [build] [release] (Builds the project, release version if specified)
  20. # ./project clean [release] (Removes all .o files in obj/debug, obj/release if specified)
  21. # ./project delete <filename> (Deletes **/filename.cpp in all directories that contain code and their corresponding object files)
  22. #
  23. # Examples:
  24. # ./project OR ./project build
  25. # compiles all object files to obj/debug/ then the binary to bin/
  26. # ./project clean
  27. # deletes all object files in obj/debug/
  28. # ./project release OR ./project build release
  29. # same as ./project but with obj/release/ for the object files
  30. # ./project delete ABC
  31. # deletes **/ABC.cpp, obj/debug/ABC.o, obj/release/ABC.o
  32. #
  33. # Recommended Use
  34. # ./project build && bin/outfile
  35.  
  36.  
  37. COMPILER=clang++
  38. COMPILE_FLAGS="--std=c++1z -Wall -fno-exceptions" # A -DDEBUG or -DRELEASE will be appened to the end of this before building
  39.  
  40. FILE_EXTENSION="cpp"
  41. SOURCE_LOCATIONS="src" # any directories that contain source code (ex. "src src/extra_features lib")
  42. OUTPUT_NAME=outfile
  43.  
  44. # ================================
  45.  
  46. GREEN='\033[0;32m'
  47. YELLOW='\033[0;33m'
  48. NO_COLOR='\033[0m'
  49.  
  50. echoColored() {
  51. echo -e "${GREEN}$@${NO_COLOR}"
  52. }
  53.  
  54. setCompileFlags() {
  55. if [[ $1 == release ]]; then
  56. OPTIM="-O2"
  57. mode="release"
  58. MODE_FLAG="RELEASE"
  59. else
  60. OPTIM=""
  61. mode="debug"
  62. MODE_FLAG="DEBUG"
  63. fi
  64. }
  65.  
  66. if [[ $1 == delete ]]; then
  67. filename=$2
  68.  
  69. if [[ $filename != "" ]]; then
  70. DELETED_FILE=0
  71. if [[ -f obj/debug/${filename}.o ]]; then
  72. echo -e "${GREEN}Removing${NO_COLOR} obj/debug/${filename}.o"
  73. rm obj/debug/${filename}.o
  74. DELETED_FILE=1
  75. fi
  76.  
  77. if [[ -f obj/release/${filename}.o ]]; then
  78. echo -e "${GREEN}Removing${NO_COLOR} obj/release/${filename}.o"
  79. rm obj/release/${filename}.o
  80. DELETED_FILE=1
  81. fi
  82.  
  83. for dir in ${SOURCE_LOCATIONS}; do
  84. if [[ -f $dir/${filename}.${FILE_EXTENSION} ]]; then
  85. echo -e "${GREEN}Removing${NO_COLOR} $dir/${filename}.${FILE_EXTENSION}"
  86. rm $dir/${filename}.${FILE_EXTENSION}
  87. DELETED_FILE=1
  88. fi
  89. done
  90.  
  91. if [[ $DELETED_FILE == 0 ]]; then
  92. echoColored "No files named ${filename}"
  93. fi
  94. else
  95. echoColored "No file specified for delete"
  96. fi
  97. elif [[ $1 == clean ]]; then
  98. setCompileFlags $2
  99.  
  100. if [[ $(ls obj/${mode}) != "" ]]; then
  101. echoColored "Cleaning obj/${mode}"
  102. rm obj/${mode}/*.o
  103. else
  104. echoColored "obj/${mode} is empty"
  105. fi
  106. else
  107. # compile
  108. if [[ $1 == build ]]; then
  109. m=$2
  110. else
  111. m=$1
  112. fi
  113.  
  114. setCompileFlags $m
  115.  
  116. COMPILE_FLAGS="${COMPILE_FLAGS} -D${MODE_FLAG}"
  117.  
  118. if ! [[ -d bin ]]; then
  119. mkdir bin
  120. fi
  121.  
  122. if ! [[ -d obj/${mode} ]]; then
  123. mkdir -p obj/${mode}
  124. fi
  125.  
  126. CHANGE=0 # were any files recompiled?
  127. for dir in ${SOURCE_LOCATIONS}; do
  128. for file in ${dir}/*.${FILE_EXTENSION}; do
  129. filename=$(basename -- $file)
  130. filename="${filename%.*}"
  131. # -nt = newer than
  132. if [[ $file -nt obj/${mode}/$filename.o ]]; then
  133. echo -e "${GREEN}Compiling${NO_COLOR} ${file}"
  134. ${COMPILER} ${COMPILE_FLAGS} ${OPTIM} -c $file -o obj/${mode}/$filename.o || exit 1
  135. CHANGE=1
  136. fi
  137. done
  138. done
  139.  
  140. if [[ $CHANGE != 0 ]]; then
  141. echo -e "${YELLOW}---------------------------------${NO_COLOR}"
  142. echo -e "${GREEN}Compiling${NO_COLOR} ${OUTPUT_NAME}"
  143.  
  144. ${COMPILER} ${OPTIM} obj/${mode}/*.o -o bin/${OUTPUT_NAME}
  145. echoColored "Done"
  146. else
  147. echoColored "Nothing to do"
  148. fi
  149. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement