build_python.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2021 Project CHIP Authors
  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. #
  17. set -e
  18. _normpath() {
  19. python -c "import os.path; print(os.path.normpath('$@'))"
  20. }
  21. echo_green() {
  22. echo -e "\033[0;32m$*\033[0m"
  23. }
  24. echo_blue() {
  25. echo -e "\033[1;34m$*\033[0m"
  26. }
  27. echo_bold_white() {
  28. echo -e "\033[1;37m$*\033[0m"
  29. }
  30. CHIP_ROOT=$(_normpath "$(dirname "$0")/..")
  31. OUTPUT_ROOT="$CHIP_ROOT/out/python_lib"
  32. declare chip_detail_logging=false
  33. declare enable_pybindings=false
  34. declare chip_mdns
  35. declare case_retry_delta
  36. declare install_virtual_env
  37. declare clean_virtual_env=yes
  38. declare install_pytest_requirements=yes
  39. help() {
  40. echo "Usage: $file_name [ options ... ] [ -chip_detail_logging ChipDetailLoggingValue ] [ -chip_mdns ChipMDNSValue ] [-enable_pybindings EnableValue]"
  41. echo "General Options:
  42. -h, --help Display this information.
  43. Input Options:
  44. -d, --chip_detail_logging <true/false> Specify ChipDetailLoggingValue as true or false.
  45. By default it is false.
  46. -m, --chip_mdns ChipMDNSValue Specify ChipMDNSValue as platform or minimal.
  47. By default it is minimal.
  48. -p, --enable_pybindings <true/false> Specify whether to enable pybindings as python controller.
  49. -t --time_between_case_retries MRPActiveRetryInterval Specify MRPActiveRetryInterval value
  50. Default is 300 ms
  51. -i, --install_virtual_env <path> Create a virtual environment with the wheels installed
  52. <path> represents where the virtual environment is to be created.
  53. -c, --clean_virtual_env <yes|no> When installing a virtual environment, create/clean it first.
  54. Defaults to yes.
  55. --include_pytest_deps <yes|no> Install requirements.txt for running scripts/tests and
  56. src/python_testing scripts.
  57. Defaults to yes.
  58. --extra_packages PACKAGES Install extra Python packages from PyPI
  59. --include_yamltests Whether to install the matter_yamltests wheel.
  60. -z --pregen_dir DIRECTORY Directory where generated zap files have been pre-generated.
  61. "
  62. }
  63. file_name=${0##*/}
  64. while (($#)); do
  65. case $1 in
  66. --help | -h)
  67. help
  68. exit 1
  69. ;;
  70. --chip_detail_logging | -d)
  71. chip_detail_logging=$2
  72. if [[ "$chip_detail_logging" != "true" && "$chip_detail_logging" != "false" ]]; then
  73. echo "chip_detail_logging should have a true/false value, not '$chip_detail_logging'"
  74. exit
  75. fi
  76. shift
  77. ;;
  78. --chip_mdns | -m)
  79. chip_mdns=$2
  80. shift
  81. ;;
  82. --enable_pybindings | -p)
  83. enable_pybindings=$2
  84. if [[ "$enable_pybindings" != "true" && "$enable_pybindings" != "false" ]]; then
  85. echo "enable_pybindings should have a true/false value, not '$enable_pybindings'"
  86. exit
  87. fi
  88. shift
  89. ;;
  90. --time_between_case_retries | -t)
  91. chip_case_retry_delta=$2
  92. shift
  93. ;;
  94. --install_virtual_env | -i)
  95. install_virtual_env=$2
  96. shift
  97. ;;
  98. --clean_virtual_env | -c)
  99. clean_virtual_env=$2
  100. if [[ "$clean_virtual_env" != "yes" && "$clean_virtual_env" != "no" ]]; then
  101. echo "clean_virtual_env should have a yes/no value, not '$clean_virtual_env'"
  102. exit
  103. fi
  104. shift
  105. ;;
  106. --include_pytest_deps)
  107. install_pytest_requirements=$2
  108. if [[ "$install_pytest_requirements" != "yes" && "$install_pytest_requirements" != "no" ]]; then
  109. echo "install_pytest_requirements should have a yes/no value, not '$install_pytest_requirements'"
  110. exit
  111. fi
  112. shift
  113. ;;
  114. --extra_packages)
  115. extra_packages=$2
  116. shift
  117. ;;
  118. --include_yamltests)
  119. include_yamltests="yes"
  120. ;;
  121. --pregen_dir | -z)
  122. pregen_dir=$2
  123. shift
  124. ;;
  125. -*)
  126. help
  127. echo "Unknown Option \"$1\""
  128. exit 1
  129. ;;
  130. esac
  131. shift
  132. done
  133. # Print input values
  134. echo "Input values: chip_detail_logging = $chip_detail_logging , chip_mdns = \"$chip_mdns\", enable_pybindings = $enable_pybindings, chip_case_retry_delta=\"$chip_case_retry_delta\", pregen_dir=\"$pregen_dir\""
  135. # Ensure we have a compilation environment
  136. source "$CHIP_ROOT/scripts/activate.sh"
  137. # Generates ninja files
  138. [[ -n "$chip_mdns" ]] && chip_mdns_arg="chip_mdns=\"$chip_mdns\"" || chip_mdns_arg=""
  139. [[ -n "$chip_case_retry_delta" ]] && chip_case_retry_arg="chip_case_retry_delta=$chip_case_retry_delta" || chip_case_retry_arg=""
  140. [[ -n "$pregen_dir" ]] && pregen_dir_arg="chip_code_pre_generated_directory=\"$pregen_dir\"" || pregen_dir_arg=""
  141. # Make all possible human redable tracing available.
  142. tracing_options="matter_log_json_payload_hex=true matter_log_json_payload_decode_full=true matter_enable_tracing_support=true"
  143. gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args="$tracing_options chip_detail_logging=$chip_detail_logging enable_pylib=$enable_pybindings enable_rtti=$enable_pybindings chip_project_config_include_dirs=[\"//config/python\"] $chip_mdns_arg $chip_case_retry_arg $pregen_dir_arg"
  144. function ninja_target() {
  145. # Print the ninja target required to build a gn label.
  146. local GN_LABEL="$1"
  147. local NINJA_TARGET="$(gn ls "$OUTPUT_ROOT" --as=output "$GN_LABEL")"
  148. echo "$NINJA_TARGET"
  149. }
  150. function wheel_output_dir() {
  151. # Print the wheel output directory for a pw_python_package or
  152. # pw_python_distribution. The label must end in "._build_wheel".
  153. local GN_LABEL="$1"
  154. local NINJA_TARGET="$(ninja_target "$GN_LABEL")"
  155. local WHEEL_DIR="$OUTPUT_ROOT"/"$(dirname "$NINJA_TARGET")/$(basename -s .stamp "$NINJA_TARGET")"
  156. echo "$WHEEL_DIR"
  157. }
  158. # Compile Python wheels
  159. ninja -C "$OUTPUT_ROOT" python_wheels
  160. # Add wheels from chip_python_wheel_action templates.
  161. if [ "$enable_pybindings" == true ]; then
  162. WHEEL=("$OUTPUT_ROOT"/pybindings/pycontroller/pychip-*.whl)
  163. else
  164. WHEEL=("$OUTPUT_ROOT"/controller/python/chip*.whl)
  165. fi
  166. if [ -n "$include_yamltests" ]; then
  167. YAMLTESTS_GN_LABEL="//scripts:matter_yamltests_distribution._build_wheel"
  168. # Add wheels from pw_python_package or pw_python_distribution templates.
  169. WHEEL+=(
  170. "$(ls -tr "$(wheel_output_dir "$YAMLTESTS_GN_LABEL")"/*.whl | head -n 1)"
  171. )
  172. fi
  173. if [ -n "$extra_packages" ]; then
  174. WHEEL+=("$extra_packages")
  175. fi
  176. if [ -n "$install_virtual_env" ]; then
  177. ENVIRONMENT_ROOT="$install_virtual_env"
  178. if [ "$clean_virtual_env" = "yes" ]; then
  179. # Create a virtual environment that has access to the built python tools
  180. echo_blue "Creating a clear VirtualEnv in '$ENVIRONMENT_ROOT' ..."
  181. virtualenv --clear "$ENVIRONMENT_ROOT"
  182. elif [ ! -f "$ENVIRONMENT_ROOT"/bin/activate ]; then
  183. echo_blue "Creating a new VirtualEnv in '$ENVIRONMENT_ROOT' ..."
  184. virtualenv "$ENVIRONMENT_ROOT"
  185. fi
  186. source "$ENVIRONMENT_ROOT"/bin/activate
  187. "$ENVIRONMENT_ROOT"/bin/python -m pip install --upgrade pip
  188. "$ENVIRONMENT_ROOT"/bin/pip install --upgrade "${WHEEL[@]}"
  189. if [ "$install_pytest_requirements" = "yes" ]; then
  190. echo_blue "Installing python test dependencies ..."
  191. "$ENVIRONMENT_ROOT"/bin/pip install -r "$CHIP_ROOT/scripts/tests/requirements.txt"
  192. "$ENVIRONMENT_ROOT"/bin/pip install -r "$CHIP_ROOT/src/python_testing/requirements.txt"
  193. fi
  194. echo ""
  195. echo_green "Compilation completed and WHL package installed in: "
  196. echo_blue " $ENVIRONMENT_ROOT"
  197. echo ""
  198. echo_green "To use please run:"
  199. echo_bold_white " source $ENVIRONMENT_ROOT/bin/activate"
  200. fi