build_python.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. ENVIRONMENT_ROOT="$CHIP_ROOT/out/python_env"
  33. declare chip_detail_logging=false
  34. declare enable_pybindings=false
  35. declare chip_mdns
  36. declare case_retry_delta
  37. declare install_wheel=no
  38. help() {
  39. echo "Usage: $file_name [ options ... ] [ -chip_detail_logging ChipDetailLoggingValue ] [ -chip_mdns ChipMDNSValue ] [-enable_pybindings EnableValue]"
  40. echo "General Options:
  41. -h, --help Display this information.
  42. Input Options:
  43. -d, --chip_detail_logging ChipDetailLoggingValue Specify ChipDetailLoggingValue as true or false.
  44. By default it is false.
  45. -m, --chip_mdns ChipMDNSValue Specify ChipMDNSValue as platform or minimal.
  46. By default it is minimal.
  47. -p, --enable_pybindings EnableValue Specify whether to enable pybindings as python controller.
  48. -t --time_between_case_retries MRPActiveRetryInterval Specify MRPActiveRetryInterval value
  49. Default is 300 ms
  50. -i, --install_wheel no|build-env|separate Where to install the Python wheel
  51. no: Do not install
  52. build-env: install to virtual env for build matter
  53. separate: install to another virtual env (out/python_env)
  54. --extra_packages PACKAGES Install extra Python packages from PyPI
  55. --include_yamltests Whether to install the matter_yamltests wheel.
  56. -z --pregen_dir DIRECTORY Directory where generated zap files have been pre-generated.
  57. "
  58. }
  59. file_name=${0##*/}
  60. while (($#)); do
  61. case $1 in
  62. --help | -h)
  63. help
  64. exit 1
  65. ;;
  66. --chip_detail_logging | -d)
  67. chip_detail_logging=$2
  68. shift
  69. ;;
  70. --chip_mdns | -m)
  71. chip_mdns=$2
  72. shift
  73. ;;
  74. --enable_pybindings | -p)
  75. enable_pybindings=$2
  76. shift
  77. ;;
  78. --time_between_case_retries | -t)
  79. chip_case_retry_delta=$2
  80. shift
  81. ;;
  82. --install_wheel | -i)
  83. install_wheel=$2
  84. shift
  85. ;;
  86. --extra_packages)
  87. extra_packages=$2
  88. shift
  89. ;;
  90. --include_yamltests)
  91. include_yamltests="yes"
  92. ;;
  93. --pregen_dir | -z)
  94. pregen_dir=$2
  95. shift
  96. ;;
  97. -*)
  98. help
  99. echo "Unknown Option \"$1\""
  100. exit 1
  101. ;;
  102. esac
  103. shift
  104. done
  105. # Print input values
  106. 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\""
  107. # Ensure we have a compilation environment
  108. source "$CHIP_ROOT/scripts/activate.sh"
  109. # Generates ninja files
  110. [[ -n "$chip_mdns" ]] && chip_mdns_arg="chip_mdns=\"$chip_mdns\"" || chip_mdns_arg=""
  111. [[ -n "$chip_case_retry_delta" ]] && chip_case_retry_arg="chip_case_retry_delta=$chip_case_retry_delta" || chip_case_retry_arg=""
  112. [[ -n "$pregen_dir" ]] && pregen_dir_arg="chip_code_pre_generated_directory=\"$pregen_dir\"" || pregen_dir_arg=""
  113. gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args="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"
  114. function ninja_target() {
  115. # Print the ninja target required to build a gn label.
  116. local GN_LABEL="$1"
  117. local NINJA_TARGET="$(gn ls "$OUTPUT_ROOT" --as=output "$GN_LABEL")"
  118. echo "$NINJA_TARGET"
  119. }
  120. function wheel_output_dir() {
  121. # Print the wheel output directory for a pw_python_package or
  122. # pw_python_distribution. The label must end in "._build_wheel".
  123. local GN_LABEL="$1"
  124. local NINJA_TARGET="$(ninja_target "$GN_LABEL")"
  125. local WHEEL_DIR="$OUTPUT_ROOT"/"$(dirname "$NINJA_TARGET")/$(basename -s .stamp "$NINJA_TARGET")"
  126. echo "$WHEEL_DIR"
  127. }
  128. # Compile Python wheels
  129. ninja -C "$OUTPUT_ROOT" python_wheels
  130. # Add wheels from chip_python_wheel_action templates.
  131. if [ "$enable_pybindings" == true ]; then
  132. WHEEL=("$OUTPUT_ROOT"/pybindings/pycontroller/pychip-*.whl)
  133. else
  134. WHEEL=("$OUTPUT_ROOT"/controller/python/chip*.whl)
  135. fi
  136. if [ -n "$include_yamltests" ]; then
  137. YAMLTESTS_GN_LABEL="//scripts:matter_yamltests_distribution._build_wheel"
  138. # Add wheels from pw_python_package or pw_python_distribution templates.
  139. WHEEL+=(
  140. "$(ls -tr "$(wheel_output_dir "$YAMLTESTS_GN_LABEL")"/*.whl | head -n 1)"
  141. )
  142. fi
  143. if [ -n "$extra_packages" ]; then
  144. WHEEL+=("$extra_packages")
  145. fi
  146. if [ "$install_wheel" = "no" ]; then
  147. exit 0
  148. elif [ "$install_wheel" = "separate" ]; then
  149. # Create a virtual environment that has access to the built python tools
  150. virtualenv --clear "$ENVIRONMENT_ROOT"
  151. source "$ENVIRONMENT_ROOT"/bin/activate
  152. "$ENVIRONMENT_ROOT"/bin/python -m pip install --upgrade pip
  153. "$ENVIRONMENT_ROOT"/bin/pip install --upgrade --force-reinstall "${WHEEL[@]}"
  154. echo ""
  155. echo_green "Compilation completed and WHL package installed in: "
  156. echo_blue " $ENVIRONMENT_ROOT"
  157. echo ""
  158. echo_green "To use please run:"
  159. echo_bold_white " source $ENVIRONMENT_ROOT/bin/activate"
  160. elif [ "$install_wheel" = "build-env" ]; then
  161. pip install --force-reinstall "${WHEEL[@]}"
  162. echo ""
  163. echo_green "Compilation completed and WHL package installed in virtualenv for building sdk"
  164. echo ""
  165. echo_green "To use please run:"
  166. echo_bold_white " source $CHIP_ROOT/scripts/activate.sh"
  167. fi