build.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env bash
  2. # _____ _
  3. # |_ _| |_ _ _ ___ ___ _ __ __ _
  4. # | | | ' \| '_/ -_) -_) ' \/ _` |_
  5. # |_| |_||_|_| \___\___|_|_|_\__,_(_)
  6. #
  7. # Threema iOS Client
  8. # Copyright (c) 2020 Threema GmbH
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License, version 3,
  12. # as published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. set -euo pipefail
  22. if [[ $# = 0 ]]; then
  23. echo "Usage: ./build.sh [--dependencies | --dependencies-force | --generate-protobuf | [--switch-webrtc-to-debug | --switch-webrtc-to-release] | [--build & --work]] [<relative project path>]"
  24. echo ""
  25. echo "Options to build the app and its dependencies:
  26. --dependencies Check out and build Carthage dependencies, and download debug/release WebRTC binaries
  27. and SaltyRTC binary if they are missing.
  28. (https://github.com/Carthage/Carthage#installing-carthage)
  29. --dependencies-force Rebuild Carthage dependencies, download debug/release version of
  30. WebRTC binaries and SaltyRTC binary.
  31. --generate-protobuf Parse Protobuf files and generate Swift source code.
  32. (https://github.com/apple/swift-protobuf/#alternatively-install-via-homebrew)
  33. --switch-webrtc-to-debug Switch WebRTC binary to debug version.
  34. --switch-webrtc-to-release Switch WebRTC binary to release version.
  35. --build Build debug version of target 'Threema'.
  36. --build --work Build debug version of target 'Threema Work'.
  37. Example: ./build.sh --dependencies --switch-webrtc-to-debug --build .."
  38. exit 0
  39. fi
  40. # Initialize project directory
  41. if [[ $# -gt 0 ]] && [[ ${!#} != '--dependencies' ]] && [[ ${!#} != '--dependencies-force' ]] && [[ ${!#} != '--generate-protobuf' ]] && [[ ${!#} != '--switch-webrtc-to-debug' ]] && [[ ${!#} != '--switch-webrtc-to-release' ]] && [[ ${!#} != '--build' ]] && [[ ${!#} != '--work' ]]; then
  42. project_dir="$PWD/${!#}"
  43. else
  44. project_dir="$PWD"
  45. fi
  46. if [[ -d "$project_dir/Threema.xcodeproj/project.xcworkspace" ]]; then
  47. echo "Project $project_dir/Threema.xcodeproj/project.xcworkspace"
  48. else
  49. echo "Project $project_dir/Threema.xcodeproj/project.xcworkspace not found"
  50. exit 1
  51. fi
  52. # Initialize script arguments
  53. dependencies_arg=0
  54. dependencies_force_arg=0
  55. generate_protobuf_arg=0
  56. switch_webrtc_to_debug_arg=0
  57. switch_webrtc_to_release_arg=0
  58. build_arg=0
  59. work_arg=0
  60. for arg in "$@"; do
  61. if [[ "$arg" = '--dependencies' ]]; then
  62. dependencies_arg=1
  63. elif [[ "$arg" = '--dependencies-force' ]]; then
  64. dependencies_force_arg=1
  65. elif [[ "$arg" = '--generate-protobuf' ]]; then
  66. generate_protobuf_arg=1
  67. elif [[ "$arg" = '--switch-webrtc-to-debug' ]]; then
  68. switch_webrtc_to_debug_arg=1
  69. elif [[ "$arg" = '--switch-webrtc-to-release' ]]; then
  70. switch_webrtc_to_release_arg=1
  71. elif [[ "$arg" = '--build' ]]; then
  72. build_arg=1
  73. elif [[ "$arg" = '--work' ]]; then
  74. work_arg=1
  75. fi
  76. done
  77. if [[ "$dependencies_arg" = 1 ]] || [[ "$dependencies_force_arg" = 1 ]]; then
  78. carthage version
  79. # Build carthage dependencies
  80. if [[ "$dependencies_force_arg" = 1 ]]; then
  81. "$project_dir/scripts/carthage.sh" bootstrap --platform iOS --no-use-binaries --project-directory "$project_dir"
  82. else
  83. "$project_dir/scripts/carthage.sh" bootstrap --platform iOS --no-use-binaries --cache-builds --project-directory "$project_dir"
  84. fi
  85. # Delete or reset download directories
  86. # $1: Local directory
  87. reset() {
  88. if [[ -d "$project_dir/$1" ]]; then
  89. if [[ "$dependencies_force_arg" = 1 ]]; then
  90. rm -R "$project_dir/$1"
  91. elif [[ $1 == 'WebRTC' ]]; then
  92. if [[ -d "WebRTC-debug" ]]; then
  93. mv "$project_dir/WebRTC" "$project_dir/WebRTC-release"
  94. else
  95. mv "$project_dir/WebRTC" "$project_dir/WebRTC-debug"
  96. fi
  97. fi
  98. fi
  99. }
  100. download() {
  101. download_dir="$project_dir/$1"
  102. download_url="https://oss.threema.ch/ios/$2/$3/$1.zip"
  103. echo "$download_url -> $download_dir"
  104. mkdir "$download_dir"
  105. curl "$download_url" -o "$download_dir/$1.zip"
  106. unzip -q "$download_dir/$1.zip" -d "$download_dir"
  107. rm "$download_dir/$1.zip"
  108. }
  109. # $1: Local directory
  110. # $2: Dependency name on oss
  111. # $3: Dependency version on oss
  112. check() {
  113. if [[ -d "$project_dir/$1" ]]; then
  114. if [[ "$dependencies_force_arg" = 1 ]]; then
  115. rm -R "$project_dir/$1"
  116. fi
  117. fi
  118. if [[ -d "$project_dir/$1" ]]; then
  119. echo "Cache found for $1"
  120. else
  121. download $1 $2 $3
  122. fi
  123. }
  124. reset "WebRTC"
  125. check "WebRTC-debug" "webrtc" "84.1.1"
  126. check "WebRTC-release" "webrtc" "84.1.1"
  127. reset "SaltyRTC"
  128. check "SaltyRTC" "saltyrtc" "0.2.0"
  129. fi
  130. if [[ "$generate_protobuf_arg" = 1 ]]; then
  131. protobuf_submodule_path="./protobuf"
  132. protobuf_source_path="./ThreemaFramework/Protobuf"
  133. protoc --version
  134. if [[ -d "$project_dir/$protobuf_submodule_path" ]]; then
  135. if [[ -d "$project_dir/$protobuf_source_path" ]]; then
  136. echo "Protobuf source path $project_dir/$protobuf_source_path"
  137. else
  138. mkdir "$project_dir/$protobuf_source_path"
  139. fi
  140. for file in "$project_dir/$protobuf_submodule_path"/*.proto
  141. do
  142. protoc --swift_out="$protobuf_source_path" --proto_path="$protobuf_submodule_path" "$(basename -- "$file")"
  143. done
  144. fi
  145. fi
  146. if [[ "$switch_webrtc_to_debug_arg" = 1 ]]; then
  147. echo "Switch to WebRTC debug binary"
  148. if [[ -d "$project_dir/WebRTC-debug" ]]; then
  149. if [[ -d "$project_dir/WebRTC" ]]; then
  150. mv "$project_dir/WebRTC" "$project_dir/WebRTC-release"
  151. fi
  152. mv "$project_dir/WebRTC-debug" "$project_dir/WebRTC"
  153. fi
  154. elif [[ "$switch_webrtc_to_release_arg" = 1 ]]; then
  155. echo "Switch to WebRTC release binary"
  156. if [[ -d "$project_dir/WebRTC-release" ]]; then
  157. if [[ -d "$project_dir/WebRTC" ]]; then
  158. mv "$project_dir/WebRTC" "$project_dir/WebRTC-debug"
  159. fi
  160. mv "$project_dir/WebRTC-release" "$project_dir/WebRTC"
  161. fi
  162. fi
  163. if [[ "$build_arg" = 1 ]]; then
  164. if [[ "$work_arg" = 1 ]]; then
  165. scheme="Threema Work"
  166. else
  167. scheme="Threema"
  168. fi
  169. echo "Build $scheme"
  170. xcodebuild build -workspace "$project_dir/Threema.xcodeproj/project.xcworkspace" -scheme "$scheme" -configuration Debug -destination "generic/platform=iOS"
  171. fi