build_sdk.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #!/bin/bash
  2. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. sdk_root=$(cd "$(dirname "$0")/" && pwd)
  5. wamr_root_dir=${sdk_root}/..
  6. out_dir=${sdk_root}/out
  7. profile_path=${out_dir}/profile.cmake
  8. wamr_config_cmake_file=""
  9. wasi_sdk_home="/opt/wasi-sdk"
  10. # libc support, default builtin-libc
  11. LIBC_SUPPORT="BUILTIN"
  12. CM_DEXTRA_SDK_INCLUDE_PATH=""
  13. CM_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Release"
  14. CM_TOOLCHAIN=""
  15. # menuconfig will pass options to this script
  16. MENUCONFIG=""
  17. usage ()
  18. {
  19. echo "build.sh [options]"
  20. echo " -n [profile name]"
  21. echo " -x [config file path name]"
  22. echo " -t [cmake toolchain file]"
  23. echo " -e [extra include path], files under this path will be copied into SDK package"
  24. echo " -c, clean"
  25. echo " -d, debug mode"
  26. echo " -i, enter menu config settings"
  27. echo " -w [wasi-sdk installation path] it will be '/opt/wasi-sdk' if not set"
  28. exit 1
  29. }
  30. while getopts "e:x:n:t:icdw:" opt
  31. do
  32. case $opt in
  33. n)
  34. PROFILE=$OPTARG
  35. ;;
  36. t)
  37. CM_TOOLCHAIN="-DCMAKE_TOOLCHAIN_FILE=$OPTARG"
  38. ;;
  39. x)
  40. wamr_config_cmake_file=$OPTARG
  41. ;;
  42. e)
  43. CM_DEXTRA_SDK_INCLUDE_PATH="-DEXTRA_SDK_INCLUDE_PATH=${OPTARG}"
  44. ;;
  45. c)
  46. CLEAN="TRUE"
  47. ;;
  48. d)
  49. CM_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Debug"
  50. ;;
  51. i)
  52. MENUCONFIG="TRUE"
  53. ;;
  54. w)
  55. if [[ -n "${OPTARG}" ]]; then
  56. wasi_sdk_home=$(realpath "${OPTARG}")
  57. fi
  58. ;;
  59. ?)
  60. echo "Unknown arg: $arg"
  61. usage
  62. exit 1
  63. ;;
  64. esac
  65. done
  66. if [ ! -f "${wasi_sdk_home}/bin/clang" ]; then
  67. echo "Can not find clang under \"${wasi_sdk_home}/bin\"."
  68. exit 1
  69. else
  70. echo "Found WASI_SDK HOME ${wasi_sdk_home}"
  71. fi
  72. echo "download dependent external repositories.."
  73. ${wamr_root_dir}/core/deps/download.sh
  74. [ $? -eq 0 ] || exit $?
  75. if [ -z "$PROFILE" ]; then
  76. PROFILE="default"
  77. echo "PROFILE argument not set, using DEFAULT"
  78. if [[ -z "$wamr_config_cmake_file" ]]; then
  79. wamr_config_cmake_file=${sdk_root}/wamr_config_default.cmake
  80. echo "use default config file: [$wamr_config_cmake_file]"
  81. fi
  82. fi
  83. if [ ! -d "${out_dir}" ]; then
  84. mkdir -p ${out_dir}
  85. fi
  86. curr_profile_dir=${out_dir}/${PROFILE}
  87. wamr_app_out_dir=${curr_profile_dir}/app-sdk/wamr-app-framework
  88. sysroot_dir=${curr_profile_dir}/app-sdk/libc-builtin-sysroot
  89. echo "CM_DEXTRA_SDK_INCLUDE_PATH=${CM_DEXTRA_SDK_INCLUDE_PATH}"
  90. if [[ "$CLEAN" = "TRUE" ]]; then
  91. rm -rf ${curr_profile_dir}
  92. fi
  93. # cmake config file for wamr runtime:
  94. # 1. use the users provided the config cmake file path.
  95. # 2. if user set MENU CONFIG, enter menu config to generate
  96. # menu_config.cmake in the profile output folder
  97. # 3. If the menu_config.cmake is already in the profile folder, use it
  98. # 4. Use the default config cmake file
  99. #
  100. if [[ -n "$wamr_config_cmake_file" ]]; then
  101. if [[ ! -f $wamr_config_cmake_file ]]; then
  102. echo "user given file not exist: ${wamr_config_cmake_file}"
  103. exit 1
  104. fi
  105. echo "User config file: [${wamr_config_cmake_file}]"
  106. else
  107. wamr_config_cmake_file=${out_dir}/wamr_config_${PROFILE}.cmake
  108. # always rebuilt the sdk if user is not giving the config file
  109. if [ -d ${curr_profile_dir} ]; then
  110. rm -rf ${curr_profile_dir}
  111. fi
  112. if [[ "$MENUCONFIG" = "TRUE" ]] || [[ ! -f $wamr_config_cmake_file ]]; then
  113. echo "MENUCONFIG: [${wamr_config_cmake_file}]"
  114. ./menuconfig.sh -x ${wamr_config_cmake_file}
  115. [ $? -eq 0 ] || exit $?
  116. else
  117. echo "use existing config file: [$wamr_config_cmake_file]"
  118. fi
  119. fi
  120. mkdir -p ${curr_profile_dir}
  121. mkdir -p ${curr_profile_dir}/app-sdk
  122. mkdir -p ${curr_profile_dir}/runtime-sdk
  123. if [ "${BUILD_LLVM}" = "TRUE" ]; then
  124. if [ ! -d "${wamr_root_dir}/core/deps/llvm" ]; then
  125. echo -e "\n"
  126. echo "###### build llvm (this will take a long time) #######"
  127. echo ""
  128. cd ${wamr_root_dir}/wamr-compiler
  129. ./build_llvm.sh
  130. fi
  131. fi
  132. echo -e "\n\n"
  133. echo "############## Start to build wasm app sdk ###############"
  134. # If wgl module is selected, check if the extra SDK include dir is passed by the args, prompt user to input if not.
  135. app_all_selected=`cat ${wamr_config_cmake_file} | grep WAMR_APP_BUILD_ALL`
  136. app_wgl_selected=`cat ${wamr_config_cmake_file} | grep WAMR_APP_BUILD_WGL`
  137. if [[ -n "${app_wgl_selected}" ]] || [[ -n "${app_all_selected}" ]]; then
  138. if [ -z "${CM_DEXTRA_SDK_INCLUDE_PATH}" ]; then
  139. echo -e "\033[31mWGL module require lvgl config files, please input the path to the lvgl SDK include path:\033[0m"
  140. read -a extra_file_path
  141. if [[ -z "${extra_file_path}" ]] || [[ ! -d "${extra_file_path}" ]]; then
  142. echo -e "\033[31mThe extra SDK path is empty\033[0m"
  143. else
  144. CM_DEXTRA_SDK_INCLUDE_PATH="-DEXTRA_SDK_INCLUDE_PATH=${extra_file_path}"
  145. fi
  146. fi
  147. cd ${wamr_root_dir}/core/app-framework/wgl/app
  148. ./prepare_headers.sh
  149. fi
  150. cd ${sdk_root}/app
  151. rm -fr build && mkdir build
  152. cd build
  153. out=`grep WAMR_BUILD_LIBC_WASI ${wamr_config_cmake_file} |grep 1`
  154. if [ -n "$out" ]; then
  155. LIBC_SUPPORT="WASI"
  156. fi
  157. if [ "${LIBC_SUPPORT}" = "WASI" ]; then
  158. echo "using wasi toolchain"
  159. cmake .. $CM_DEXTRA_SDK_INCLUDE_PATH \
  160. -DWAMR_BUILD_SDK_PROFILE=${PROFILE} \
  161. -DCONFIG_PATH=${wamr_config_cmake_file} \
  162. -DWASI_SDK_DIR="${wasi_sdk_home}" \
  163. -DCMAKE_TOOLCHAIN_FILE=../wasi_toolchain.cmake
  164. else
  165. echo "using builtin libc toolchain"
  166. cmake .. $CM_DEXTRA_SDK_INCLUDE_PATH \
  167. -DWAMR_BUILD_SDK_PROFILE=${PROFILE} \
  168. -DCONFIG_PATH=${wamr_config_cmake_file} \
  169. -DWASI_SDK_DIR="${wasi_sdk_home}" \
  170. -DCMAKE_TOOLCHAIN_FILE=../wamr_toolchain.cmake
  171. fi
  172. [ $? -eq 0 ] || exit $?
  173. make
  174. if (( $? == 0 )); then
  175. echo -e "\033[32mSuccessfully built app-sdk under ${curr_profile_dir}/app-sdk\033[0m"
  176. else
  177. echo -e "\033[31mFailed to build app-sdk for wasm application\033[0m"
  178. exit 1
  179. fi
  180. cd ..
  181. rm -fr build
  182. echo -e "\n\n"
  183. echo "############## Start to build runtime sdk ###############"
  184. cd ${sdk_root}/runtime
  185. rm -fr build-runtime-sdk && mkdir build-runtime-sdk
  186. cd build-runtime-sdk
  187. cmake .. $CM_DEXTRA_SDK_INCLUDE_PATH \
  188. -DWAMR_BUILD_SDK_PROFILE=${PROFILE} \
  189. -DCONFIG_PATH=${wamr_config_cmake_file} \
  190. $CM_TOOLCHAIN $CM_BUILD_TYPE
  191. [ $? -eq 0 ] || exit $?
  192. make
  193. if (( $? == 0 )); then
  194. echo -e "\033[32mSuccessfully built runtime library under ${curr_profile_dir}/runtime-sdk/lib\033[0m"
  195. else
  196. echo -e "\033[31mFailed to build runtime sdk\033[0m"
  197. exit 1
  198. fi
  199. APP=`grep WAMR_BUILD_APP_FRAMEWORK ${wamr_config_cmake_file} |grep 1`
  200. if [ -n "$APP" ]; then
  201. # Generate defined-symbol list for app-sdk
  202. cd ${wamr_app_out_dir}/share
  203. cat ${curr_profile_dir}/runtime-sdk/include/*.inl | egrep "^ *EXPORT_WASM_API *[(] *[a-zA-Z_][a-zA-Z0-9_]* *?[)]" | cut -d '(' -f2 | cut -d ')' -f1 > defined-symbols.txt
  204. fi
  205. cd ..
  206. rm -fr build-runtime-sdk
  207. exit 0