setup_go_workspace_for_soong.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #!/bin/bash
  2. set -e
  3. # Copyright 2019 Google Inc. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Mounts the components of soong into a directory structure that Go tools
  17. # and editors expect.
  18. #####################################################################
  19. # Print the message to stderr with the prefix ERROR and abort this
  20. # script.
  21. #####################################################################
  22. function log_FATAL() {
  23. echo "ERROR:" "$*" >&2
  24. exit 1
  25. }
  26. #####################################################################
  27. # Print the message to stderr with the prefix WARN
  28. #####################################################################
  29. function log_WARN() {
  30. echo "WARN:" "$*" >&2
  31. }
  32. #####################################################################
  33. # Print the message with the prefix INFO.
  34. #####################################################################
  35. function log_INFO() {
  36. echo "INFO:" "$*"
  37. }
  38. #####################################################################
  39. # Find the root project directory of this repo. This is done by
  40. # finding the directory of where this script lives and then go up one
  41. # directory to check the ".repo" directory exist. If not, keep going
  42. # up until we find the ".repo" file or we reached to the filesystem
  43. # root. Project root directory is printed to stdout.
  44. #####################################################################
  45. function root_dir() (
  46. local dir
  47. if ! dir="$("${readlink}" -e $(dirname "$0"))"; then
  48. log_FATAL "failed to read the script's current directory."
  49. fi
  50. dir=${dir}/../../..
  51. if ! dir="$("${readlink}" -e "${dir}")"; then
  52. log_FATAL "Cannot find the root project directory"
  53. fi
  54. echo "${dir}"
  55. )
  56. #####################################################################
  57. # executes a shell command by printing out to the screen first and
  58. # then evaluating the command.
  59. #####################################################################
  60. function execute() {
  61. echo "$@"
  62. eval "$@"
  63. }
  64. #####################################################################
  65. # Returns the source directory of a passed in path from BIND_PATHS
  66. # array.
  67. #####################################################################
  68. function bind_path_src_dir() (
  69. local -r bind_path="$1"
  70. echo "${bind_path/%|*/}"
  71. )
  72. #####################################################################
  73. # Returns the destination directory of a passed in path from
  74. # BIND_PATHS array.
  75. #####################################################################
  76. function bind_path_dst_dir() (
  77. local -r bind_path="$1"
  78. echo "${bind_path/#*|}"
  79. )
  80. #####################################################################
  81. # Executes the bindfs command in linux. Expects $1 to be src
  82. # directory and $2 to be destination directory.
  83. #####################################################################
  84. function linux_bind_dir() (
  85. execute bindfs "$1" "$2"
  86. )
  87. #####################################################################
  88. # Executes the fusermount -u command in linux. Expects $1 to be the
  89. # destination directory.
  90. #####################################################################
  91. function linux_unbind_dir() (
  92. execute fusermount -u "$1"
  93. )
  94. #####################################################################
  95. # Executes the bindfs command in darwin. Expects $1 to be src
  96. # directory and $2 to be destination directory.
  97. #####################################################################
  98. function darwin_bind_dir() (
  99. execute bindfs -o allow_recursion -n "$1" "$2"
  100. )
  101. #####################################################################
  102. # Execute the umount command in darwin to unbind a directory. Expects
  103. # $1 to be the destination directory
  104. #####################################################################
  105. function darwin_unbind_dir() (
  106. execute umount -f "$1"
  107. )
  108. #####################################################################
  109. # Bind all the paths that are specified in the BIND_PATHS array.
  110. #####################################################################
  111. function bind_all() (
  112. local src_dir
  113. local dst_dir
  114. for path in ${BIND_PATHS[@]}; do
  115. src_dir=$(bind_path_src_dir "${path}")
  116. dst_dir=$(bind_path_dst_dir "${path}")
  117. mkdir -p "${dst_dir}"
  118. "${bind_dir}" ${src_dir} "${dst_dir}"
  119. done
  120. echo
  121. log_INFO "Created GOPATH-compatible directory structure at ${OUTPUT_PATH}."
  122. )
  123. #####################################################################
  124. # Unbind all the paths that are specified in the BIND_PATHS array.
  125. #####################################################################
  126. function unbind_all() (
  127. local dst_dir
  128. local exit_code=0
  129. # need to go into reverse since several parent directory may have been
  130. # first before the child one.
  131. for (( i=${#BIND_PATHS[@]}-1; i>=0; i-- )); do
  132. dst_dir=$(bind_path_dst_dir "${BIND_PATHS[$i]}")
  133. # continue to unmount even one of them fails
  134. if ! "${unbind_dir}" "${dst_dir}"; then
  135. log_WARN "Failed to umount ${dst_dir}."
  136. exit_code=1
  137. fi
  138. done
  139. if [[ ${exit_code} -ne 0 ]]; then
  140. exit ${exit_code}
  141. fi
  142. echo
  143. log_INFO "Unmounted the GOPATH-compatible directory structure at ${OUTPUT_PATH}."
  144. )
  145. #####################################################################
  146. # Asks the user to create the GOPATH-compatible directory structure.
  147. #####################################################################
  148. function confirm() (
  149. while true; do
  150. echo "Will create GOPATH-compatible directory structure at ${OUTPUT_PATH}"
  151. echo -n "Ok [Y/n]?"
  152. read decision
  153. if [ "${decision}" == "y" -o "${decision}" == "Y" -o "${decision}" == "" ]; then
  154. return 0
  155. else
  156. if [ "${decision}" == "n" ]; then
  157. return 1
  158. else
  159. log_WARN "Invalid choice ${decision}; choose either 'y' or 'n'"
  160. fi
  161. fi
  162. done
  163. )
  164. #####################################################################
  165. # Help function.
  166. #####################################################################
  167. function help() (
  168. cat <<EOF
  169. Mounts the components of soong into a directory structure that Go tools
  170. and editors expect.
  171. --help
  172. This help
  173. --bind
  174. Create the directory structure that Go tools and editors expect by
  175. binding the one to aosp build directory.
  176. --unbind
  177. Reverse operation of bind.
  178. If no flags were specified, the --bind one is selected by default.
  179. EOF
  180. )
  181. #####################################################################
  182. # Parse the arguments passed in to this script.
  183. #####################################################################
  184. function parse_arguments() {
  185. while [[ -n "$1" ]]; do
  186. case "$1" in
  187. --bind)
  188. ACTION="bind"
  189. shift
  190. ;;
  191. --unbind)
  192. ACTION="unbind"
  193. shift
  194. ;;
  195. --help )
  196. help
  197. shift
  198. exit 0
  199. ;;
  200. *)
  201. log_WARN "Unknown option: $1"
  202. help
  203. exit 1
  204. ;;
  205. esac
  206. done
  207. if [[ -z "${ACTION}" ]]; then
  208. ACTION=bind
  209. fi
  210. }
  211. #####################################################################
  212. # Verifies that a list of required binaries are installed in the
  213. # host in order to run this script.
  214. #####################################################################
  215. function check_exec_existence() (
  216. function check() {
  217. if ! hash "$1" &>/dev/null; then
  218. log_FATAL "missing $1"
  219. fi
  220. }
  221. local bins
  222. case "${os_type}" in
  223. Darwin)
  224. bins=("bindfs" "greadlink")
  225. ;;
  226. Linux)
  227. bins=("bindfs" "fusermount")
  228. ;;
  229. *)
  230. log_FATAL "${os_type} is not a recognized system."
  231. esac
  232. for bin in "${bins[@]}"; do
  233. check "${bin}"
  234. done
  235. )
  236. function main() {
  237. parse_arguments "$@"
  238. check_exec_existence
  239. if [[ "${ACTION}" == "bind" ]]; then
  240. if confirm; then
  241. echo
  242. bind_all
  243. else
  244. echo "skipping due to user request"
  245. exit 1
  246. fi
  247. else
  248. echo
  249. unbind_all
  250. fi
  251. }
  252. readonly os_type="$(uname -s)"
  253. case "${os_type}" in
  254. Darwin)
  255. bind_dir=darwin_bind_dir
  256. unbind_dir=darwin_unbind_dir
  257. readlink=greadlink
  258. ;;
  259. Linux)
  260. bind_dir=linux_bind_dir
  261. unbind_dir=linux_unbind_dir
  262. readlink=readlink
  263. ;;
  264. *)
  265. log_FATAL "${os_type} is not a recognized system."
  266. esac
  267. readonly bind_dir
  268. readonly unbind_dir
  269. readonly readlink
  270. if ! ANDROID_PATH="$(root_dir)"; then
  271. log_FATAL "failed to find the root of the repo checkout"
  272. fi
  273. readonly ANDROID_PATH
  274. #if GOPATH contains multiple paths, use the first one
  275. if ! OUTPUT_PATH="$(echo ${GOPATH} | sed 's/\:.*//')"; then
  276. log_FATAL "failed to extract the first GOPATH environment variable"
  277. fi
  278. readonly OUTPUT_PATH
  279. if [ -z "${OUTPUT_PATH}" ]; then
  280. log_FATAL "Could not determine the desired location at which to create a" \
  281. "Go-compatible workspace. Please update GOPATH to specify the" \
  282. "desired destination directory."
  283. fi
  284. # Below are the paths to bind from src to dst. The paths are separated by |
  285. # where the left side is the source and the right side is destination.
  286. readonly BIND_PATHS=(
  287. "${ANDROID_PATH}/build/blueprint|${OUTPUT_PATH}/src/github.com/google/blueprint"
  288. "${ANDROID_PATH}/build/soong|${OUTPUT_PATH}/src/android/soong"
  289. "${ANDROID_PATH}/art/build|${OUTPUT_PATH}/src/android/soong/art"
  290. "${ANDROID_PATH}/external/golang-protobuf|${OUTPUT_PATH}/src/google.golang.org/protobuf"
  291. "${ANDROID_PATH}/external/llvm/soong|${OUTPUT_PATH}/src/android/soong/llvm"
  292. "${ANDROID_PATH}/external/clang/soong|${OUTPUT_PATH}/src/android/soong/clang"
  293. "${ANDROID_PATH}/external/robolectric-shadows/soong|${OUTPUT_PATH}/src/android/soong/robolectric"
  294. )
  295. main "$@"