bootstrap.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #
  2. # Copyright (c) 2020 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. _install_additional_pip_requirements() {
  17. _SETUP_PLATFORM=$1
  18. shift
  19. # figure out additional pip install items
  20. while [ $# -gt 0 ]; do
  21. case $1 in
  22. -p | --platform)
  23. _SETUP_PLATFORM=$2
  24. shift # argument
  25. shift # value
  26. ;;
  27. *)
  28. shift
  29. ;;
  30. esac
  31. done
  32. if [ -n "$_SETUP_PLATFORM" ]; then
  33. _OLD_IFS=$IFS
  34. IFS=","
  35. if [ -n "$ZSH_VERSION" ]; then
  36. setopt local_options shwordsplit
  37. fi
  38. for platform in ${_SETUP_PLATFORM}; do
  39. # Allow none as an alias of nothing extra installed (like -p none)
  40. if [ "$platform" != "none" ]; then
  41. echo "Installing pip requirements for $platform..."
  42. pip install -q \
  43. -r "$_CHIP_ROOT/scripts/setup/requirements.$platform.txt" \
  44. -c "$_CHIP_ROOT/scripts/setup/constraints.txt"
  45. fi
  46. done
  47. IFS=$_OLD_IFS
  48. unset _OLD_IFS
  49. unset _PLATFORMS
  50. fi
  51. unset _SETUP_PLATFORM
  52. }
  53. _bootstrap_or_activate() {
  54. if [ -n "$BASH" ]; then
  55. local _BOOTSTRAP_PATH="${BASH_SOURCE[0]}"
  56. else
  57. local _BOOTSTRAP_PATH="$1"
  58. fi
  59. local _BOOTSTRAP_NAME="${_BOOTSTRAP_PATH##*/}"
  60. local _BOOTSTRAP_DIR="${_BOOTSTRAP_PATH%/*}"
  61. # Strip off the 'scripts[/setup]' directory, leaving the root of the repo.
  62. _CHIP_ROOT="$(cd "${_BOOTSTRAP_DIR%/setup}/.." && pwd)"
  63. local _CONFIG_FILE="scripts/setup/environment.json"
  64. if [ -n "$PW_CONFIG_FILE" ]; then
  65. _CONFIG_FILE="$PW_CONFIG_FILE"
  66. unset PW_CONFIG_FILE
  67. fi
  68. if [ ! -f "$_CHIP_ROOT/third_party/pigweed/repo/pw_env_setup/util.sh" ]; then
  69. # Make sure our submodule remotes are correct for this revision.
  70. git submodule sync --recursive
  71. git submodule update --init
  72. elif [ "$_BOOTSTRAP_NAME" = "bootstrap.sh" ]; then
  73. # In this case, only update already checked out submodules.
  74. git submodule sync --recursive
  75. git submodule update
  76. fi
  77. PW_BRANDING_BANNER="$_CHIP_ROOT/scripts/setup/banner.txt"
  78. export PW_BRANDING_BANNER
  79. PW_PROJECT_ROOT="$_CHIP_ROOT"
  80. export PW_PROJECT_ROOT
  81. PW_ROOT="$_CHIP_ROOT/third_party/pigweed/repo"
  82. export PW_ROOT
  83. . "$_CHIP_ROOT/third_party/pigweed/repo/pw_env_setup/util.sh"
  84. _chip_bootstrap_banner() {
  85. if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
  86. cat "$PW_BRANDING_BANNER"
  87. echo
  88. fi
  89. }
  90. local _PW_BANNER_FUNC="_chip_bootstrap_banner"
  91. # Force the Pigweed environment directory to be '.environment'
  92. if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
  93. export PW_ENVIRONMENT_ROOT="$PW_PROJECT_ROOT/.environment"
  94. fi
  95. export _PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
  96. local _SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
  97. export PW_DOCTOR_SKIP_CIPD_CHECKS=1
  98. export PATH # https://bugs.chromium.org/p/pigweed/issues/detail?id=281
  99. if test -n "$GITHUB_ACTION"; then
  100. mkdir -p "$_PW_ACTUAL_ENVIRONMENT_ROOT"
  101. tee <<EOF >"${_PW_ACTUAL_ENVIRONMENT_ROOT}/pip.conf"
  102. [global]
  103. cache-dir = ${_PW_ACTUAL_ENVIRONMENT_ROOT}/pip-cache
  104. EOF
  105. export PIP_CONFIG_FILE="$_PW_ACTUAL_ENVIRONMENT_ROOT/pip.conf"
  106. fi
  107. if [ "$_BOOTSTRAP_NAME" = "bootstrap.sh" ] ||
  108. [ ! -f "$_SETUP_SH" ] ||
  109. [ ! -s "$_SETUP_SH" ]; then
  110. pw_bootstrap --shell-file "$_SETUP_SH" \
  111. --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" \
  112. --config-file "$_CHIP_ROOT/$_CONFIG_FILE" \
  113. --virtualenv-gn-out-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT/gn_out"
  114. pw_finalize bootstrap "$_SETUP_SH"
  115. _ACTION_TAKEN="bootstrap"
  116. else
  117. pw_activate
  118. pw_finalize activate "$_SETUP_SH"
  119. _ACTION_TAKEN="activate"
  120. fi
  121. }
  122. # remember PW_ENVIRONMENT_ROOT so that things like another
  123. # bootstrap or run_in_build_env.sh can be executed in a build env
  124. _ORIGINAL_PW_ENVIRONMENT_ROOT="$PW_ENVIRONMENT_ROOT"
  125. _bootstrap_or_activate "$0"
  126. if [ "$_ACTION_TAKEN" = "bootstrap" ]; then
  127. # By default, install all extra pip dependencies even if slow. -p/--platform
  128. # arguments may override this default.
  129. _install_additional_pip_requirements "all" "$@"
  130. else
  131. _install_additional_pip_requirements "none" "$@"
  132. fi
  133. # Load bash completion helper if running bash
  134. if [ -n "$BASH" ]; then
  135. . "$_CHIP_ROOT/scripts/helpers/bash-completion.sh"
  136. fi
  137. unset -f _bootstrap_or_activate
  138. unset -f _install_additional_pip_requirements
  139. pw_cleanup
  140. unset _ACTION_TAKEN
  141. unset _CHIP_ROOT
  142. unset PW_CIPD_INSTALL_DIR
  143. unset _PW_BANNER_FUNC
  144. unset _PW_TEXT
  145. unset PW_DOCTOR_SKIP_CIPD_CHECKS
  146. unset -f _chip_bootstrap_banner
  147. if [ -n "$_ORIGINAL_PW_ENVIRONMENT_ROOT" ]; then
  148. export PW_ENVIRONMENT_ROOT="$_ORIGINAL_PW_ENVIRONMENT_ROOT"
  149. fi