imxlinux_example.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2022 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. set -x
  19. if [ "$#" != 2 && "$#" != 3 ]; then
  20. exit -1
  21. fi
  22. source "$(dirname "$0")/../../scripts/activate.sh"
  23. if [ "$IMX_SDK_ROOT" = "" -o ! -d "$IMX_SDK_ROOT" ]; then
  24. echo "the Yocto SDK path is not specified with the shell env IMX_SDK_ROOT or an invalid path is specified"
  25. exit -1
  26. fi
  27. env
  28. entries="$(echo "$(ls "$IMX_SDK_ROOT")" | tr -s '\n' ',')"
  29. IFS=',' read -ra entry_array <<<"$entries"
  30. for entry in "${entry_array[@]}"; do
  31. if [ "$(echo "$entry" | grep -E "^environment-setup-")" != "" ]; then
  32. env_setup_script=$entry
  33. break
  34. fi
  35. done
  36. if [ -z "$env_setup_script" ]; then
  37. echo "The SDK environment setup script is not found, make sure the env IMX_SDK_ROOT is correctly set."
  38. exit 1
  39. fi
  40. while read line; do
  41. # trim the potential whitespaces
  42. line=$(echo "$line" | xargs)
  43. if [ "$(echo "$line" | grep -E "^export SDKTARGETSYSROOT=")" != "" ]; then
  44. sdk_target_sysroot=${line#"export SDKTARGETSYSROOT="}
  45. fi
  46. if [ "$(echo "$line" | grep -E "^export CC=")" != "" ]; then
  47. cc=${line#"export CC="}
  48. cc=${cc#"\""}
  49. cc=${cc%"\""}
  50. cc=${cc/"\$SDKTARGETSYSROOT"/$sdk_target_sysroot}
  51. fi
  52. if [ "$(echo "$line" | grep -E "^export CXX=")" != "" ]; then
  53. cxx=${line#"export CXX="}
  54. cxx=${cxx#"\""}
  55. cxx=${cxx%"\""}
  56. cxx=${cxx/"\$SDKTARGETSYSROOT"/$sdk_target_sysroot}
  57. fi
  58. if [ "$(echo "$line" | grep -E "^export ARCH=")" != "" ]; then
  59. target_cpu=${line#"export ARCH="}
  60. if [ "$target_cpu" = "arm64" ]; then
  61. arm_arch="armv8-a"
  62. elif [ "$target_cpu" = "arm" ]; then
  63. arm_arch="armv7ve"
  64. else
  65. echo "ARCH should be arm64 or arm in the SDK environment setup script."
  66. exit 1
  67. fi
  68. fi
  69. if [ "$(echo "$line" | grep -E "^export CROSS_COMPILE=")" != "" ]; then
  70. cross_compile=${line#"export CROSS_COMPILE="}
  71. cross_compile=${cross_compile%"-"}
  72. fi
  73. done <"$IMX_SDK_ROOT/$env_setup_script"
  74. if [ -z "$sdk_target_sysroot" ]; then
  75. echo "SDKTARGETSYSROOT is not found in the SDK environment setup script."
  76. exit 1
  77. fi
  78. if [ -z "$cc" -o -z "$cxx" ]; then
  79. echo "CC and/or CXX are not found in the SDK environment setup script."
  80. exit 1
  81. fi
  82. if [ -z "$target_cpu" -o -z "$cross_compile" ]; then
  83. echo "ARCH and/or CROSS_COMPILE are not found in the SDK environment setup script."
  84. exit 1
  85. fi
  86. release_build=true
  87. if [ "$3" = "debug" ]; then
  88. release_build=false
  89. fi
  90. PLATFORM_CFLAGS='-DCHIP_DEVICE_CONFIG_WIFI_STATION_IF_NAME=\"mlan0\"", "-DCHIP_DEVICE_CONFIG_LINUX_DHCPC_CMD=\"udhcpc -b -i %s \"'
  91. gn gen --check --fail-on-unused-args --root="$1" "$2" --args="target_os=\"linux\" target_cpu=\"$target_cpu\" arm_arch=\"$arm_arch\"
  92. treat_warnings_as_errors=false
  93. import(\"//build_overrides/build.gni\")
  94. sysroot=\"$sdk_target_sysroot\"
  95. target_cflags=[ \"$PLATFORM_CFLAGS\" ]
  96. custom_toolchain=\"\${build_root}/toolchain/custom\"
  97. target_cc=\"$IMX_SDK_ROOT/sysroots/x86_64-pokysdk-linux/usr/bin/$cross_compile/$cc\"
  98. target_cxx=\"$IMX_SDK_ROOT/sysroots/x86_64-pokysdk-linux/usr/bin/$cross_compile/$cxx\"
  99. target_ar=\"$IMX_SDK_ROOT/sysroots/x86_64-pokysdk-linux/usr/bin/$cross_compile/$cross_compile-ar\"
  100. $(if [ "$release_build" = "true" ]; then echo "is_debug=false"; else echo "optimize_debug=true"; fi)"
  101. ninja -C "$2"