wolfssl-arduino.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/bin/sh
  2. # this script will reformat the wolfSSL source code to be compatible with
  3. # an Arduino project
  4. # run as bash ./wolfssl-arduino.sh [INSTALL] [path]
  5. #
  6. # The default is to install to a local wolfSSL directory (`ROOT_DIR`).
  7. # If successfully built, and the INSTALL option is used, tis directory
  8. # is then moved to the target.
  9. #
  10. # To ensure a pristine build, the directory must not exist.
  11. #
  12. # Reminder there's typically no $USER for GitHub actions, but:
  13. # ROOT_DIR="/mnt/c/Users/$USER/Documents/Arduino/libraries"
  14. #
  15. ROOT_DIR="/wolfSSL"
  16. # The Arduino Version will initially have a suffix appended during fine tuning stage.
  17. WOLFSSL_VERSION_ARUINO_SUFFIX="01"
  18. # For verbose copy, set CP_CMD="-v", otherwise clear it: CP_CMD="cp"
  19. # Do not set to empty string, as copy will fail with this: CP_CMD=""
  20. # CP_CMD="cp -v "
  21. CP_CMD="cp "
  22. # Specify the executable shell checker you want to use:
  23. MY_SHELLCHECK="shellcheck"
  24. # There are special circumstances to publish to GitHub repository.
  25. # Typically: https://github.com/wolfSSL/Arduino-wolfSSL
  26. #
  27. # Unlike a local Arduino library that requires a clean directory,
  28. # we'll allow extra files, overwrites, etc.
  29. #
  30. # Note in all cases, the local IDE/ARDUINO/wolfSSL must be empty.
  31. THIS_INSTALL_IS_GITHUB="false"
  32. # Check if the executable is available in the PATH
  33. if command -v "$MY_SHELLCHECK" >/dev/null 2>&1; then
  34. # Run your command here
  35. shellcheck "$0" || exit 1
  36. else
  37. echo "$MY_SHELLCHECK is not installed. Please install it if changes to this script have been made."
  38. fi
  39. if ! [ "$CP_CMD" = "cp " ]; then
  40. if [ "$CP_CMD" = "cp -v" ]; then
  41. echo "Copy verbose mode"
  42. else
  43. echo "ERROR: Copy mode not supported: $CP_CMD"
  44. exit 1
  45. fi
  46. fi
  47. # Check environment
  48. if [ -n "$WSL_DISTRO_NAME" ]; then
  49. # we found a non-blank WSL environment distro name
  50. current_path="$(pwd)"
  51. pattern="/mnt/?"
  52. if echo "$current_path" | grep -Eq "^$pattern"; then
  53. # if we are in WSL and shared Windows file system, 'ln' does not work.
  54. ARDUINO_ROOT="/mnt/c/Users/$USER/Documents/Arduino/libraries"
  55. else
  56. ARDUINO_ROOT="$HOME/Arduino/libraries"
  57. fi
  58. fi
  59. echo "The Arduino library root is: $ARDUINO_ROOT"
  60. if [ $# -gt 0 ]; then
  61. THIS_OPERATION="$1"
  62. if [ "$THIS_OPERATION" = "INSTALL" ]; then
  63. THIS_INSTALL_DIR=$2
  64. echo "Install is active."
  65. if [ "$THIS_INSTALL_DIR" = "" ]; then
  66. if [ -d "$ARDUINO_ROOT$ROOT_DIR" ]; then
  67. echo "Error: the installation directory already exists: $ARDUINO_ROOT$ROOT_DIR"
  68. echo "A new directory needs to be created to ensure there are no stray files"
  69. echo "Please delete or move the directory and try again."
  70. exit 1
  71. fi
  72. else
  73. echo "Installing to $THIS_INSTALL_DIR"
  74. if [ -d "$THIS_INSTALL_DIR/.git" ];then
  75. echo "Target is a GitHub repository."
  76. THIS_INSTALL_IS_GITHUB="true"
  77. else
  78. echo "Target is NOT a GitHub repository."
  79. fi
  80. fi
  81. else
  82. echo "Error: not a valid operation: $THIS_OPERATION"
  83. exit 1
  84. fi
  85. fi
  86. ROOT_SRC_DIR="${ROOT_DIR}/src"
  87. WOLFSSL_SRC="${ROOT_SRC_DIR}/src"
  88. WOLFSSL_HEADERS="${ROOT_SRC_DIR}/wolfssl"
  89. WOLFCRYPT_ROOT="${ROOT_SRC_DIR}/wolfcrypt"
  90. WOLFCRYPT_SRC="${WOLFCRYPT_ROOT}/src"
  91. WOLFCRYPT_HEADERS="${WOLFSSL_HEADERS}/wolfcrypt"
  92. OPENSSL_DIR="${WOLFSSL_HEADERS}/openssl"
  93. # TOP indicates the file directory for top level of the wolfssl repository.
  94. TOP_DIR="../.."
  95. WOLFSSL_SRC_TOP="${TOP_DIR}/src"
  96. WOLFSSL_HEADERS_TOP="${TOP_DIR}/wolfssl"
  97. WOLFCRYPT_ROOT_TOP="${TOP_DIR}/wolfcrypt"
  98. WOLFCRYPT_SRC_TOP="${WOLFCRYPT_ROOT_TOP}/src"
  99. WOLFCRYPT_HEADERS_TOP="${WOLFSSL_HEADERS_TOP}/wolfcrypt"
  100. OPENSSL_DIR_TOP="${WOLFSSL_HEADERS_TOP}/openssl"
  101. WOLFSSL_VERSION=$(grep -i "LIBWOLFSSL_VERSION_STRING" ${TOP_DIR}/wolfssl/version.h | cut -d '"' -f 2)
  102. if [ "$WOLFSSL_VERSION" = "" ]; then
  103. echo "ERROR: Could not find wolfSSL Version in ${TOP_DIR}/wolfssl/version.h"
  104. exit 1
  105. else
  106. echo "Found wolfSSL version $WOLFSSL_VERSION"
  107. fi
  108. THIS_DIR=${PWD##*/}
  109. if [ "$THIS_DIR" = "ARDUINO" ]; then
  110. # mkdir ./wolfSSL
  111. if [ -d ".${ROOT_DIR}" ]; then
  112. echo "ERROR: $(realpath ".${ROOT_DIR}") is not empty"
  113. exit 1
  114. else
  115. echo "Step 01: mkdir .${ROOT_DIR}"
  116. mkdir .${ROOT_DIR}
  117. fi
  118. # mkdir ./wolfSSL/src
  119. if [ ! -d ".${ROOT_SRC_DIR}" ]; then
  120. echo "Step 02: mkdir .${ROOT_SRC_DIR}"
  121. mkdir .${ROOT_SRC_DIR}
  122. fi
  123. # mkdir ./wolfSSL/src/wolfssl
  124. if [ ! -d ".${WOLFSSL_HEADERS}" ]; then
  125. echo "Step 03: mkdir .${WOLFSSL_HEADERS}"
  126. mkdir .${WOLFSSL_HEADERS}
  127. fi
  128. # cp ../../wolfssl/*.h ./wolfSSL/src/wolfssl
  129. echo "Step 04: cp ${WOLFSSL_HEADERS_TOP}/*.h .${WOLFSSL_HEADERS}"
  130. $CP_CMD ${WOLFSSL_HEADERS_TOP}/*.h .${WOLFSSL_HEADERS}
  131. if [ ! -d ".${WOLFCRYPT_HEADERS}" ]; then
  132. # mkdir ./wolfSSL/src/wolfssl/wolfcrypt
  133. echo "Step 05: mkdir .${WOLFCRYPT_HEADERS}"
  134. mkdir .${WOLFCRYPT_HEADERS}
  135. mkdir .${WOLFCRYPT_HEADERS}/port
  136. mkdir .${WOLFCRYPT_HEADERS}/port/atmel
  137. mkdir .${WOLFCRYPT_HEADERS}/port/Espressif
  138. fi
  139. # cp ../../wolfssl/wolfcrypt/*.h ./wolfSSL/src/wolfssl/wolfcrypt
  140. echo "Step 06: cp ${WOLFCRYPT_HEADERS_TOP}/*.h .${WOLFCRYPT_HEADERS}"
  141. $CP_CMD ${WOLFCRYPT_HEADERS_TOP}/*.h .${WOLFCRYPT_HEADERS} || exit 1
  142. $CP_CMD ${WOLFCRYPT_HEADERS_TOP}/port/atmel/*.h .${WOLFCRYPT_HEADERS}/port/atmel || exit 1
  143. $CP_CMD ${WOLFCRYPT_HEADERS_TOP}/port/Espressif/*.h .${WOLFCRYPT_HEADERS}/port/Espressif || exit 1
  144. # Add in source files to wolfcrypt/src
  145. if [ ! -d ".${WOLFCRYPT_ROOT}" ]; then
  146. # mkdir ./wolfSSL/src/wolfcrypt
  147. echo "Step 07: mkdir .${WOLFCRYPT_ROOT}"
  148. mkdir .${WOLFCRYPT_ROOT}
  149. fi
  150. # mkdir ./wolfSSL/src/wolfcrypt/src
  151. if [ ! -d ".${WOLFCRYPT_SRC}" ]; then
  152. echo "Step 08: mkdir .${WOLFCRYPT_SRC}"
  153. mkdir .${WOLFCRYPT_SRC}
  154. mkdir .${WOLFCRYPT_SRC}/port
  155. mkdir .${WOLFCRYPT_SRC}/port/atmel
  156. mkdir .${WOLFCRYPT_SRC}/port/Espressif
  157. fi
  158. # cp ../../wolfcrypt/src/*.c ./wolfSSL/src/wolfcrypt/src
  159. echo "Step 09: cp ${WOLFCRYPT_SRC_TOP}/*.c .${WOLFCRYPT_SRC}"
  160. $CP_CMD -r ${WOLFCRYPT_SRC_TOP}/*.c .${WOLFCRYPT_SRC} || exit 1
  161. $CP_CMD -r ${WOLFCRYPT_SRC_TOP}/port/atmel/*.c .${WOLFCRYPT_SRC}/port/atmel || exit 1
  162. $CP_CMD -r ${WOLFCRYPT_SRC_TOP}/port/Espressif/*.c .${WOLFCRYPT_SRC}/port/Espressif || exit 1
  163. # Add in source files to top level src folders
  164. if [ ! -d ".${WOLFSSL_SRC}" ]; then
  165. # mkdir ./wolfSSL/src/src
  166. echo "Step 10: mkdir .${WOLFSSL_SRC}"
  167. mkdir .${WOLFSSL_SRC}
  168. fi
  169. $CP_CMD ${WOLFSSL_SRC_TOP}/*.c .${WOLFSSL_SRC} || exit 1
  170. # put bio and evp as includes
  171. $CP_CMD .${WOLFSSL_SRC}/bio.c .${WOLFSSL_HEADERS} || exit 1
  172. $CP_CMD .${WOLFCRYPT_SRC}/evp.c .${WOLFSSL_HEADERS} || exit 1
  173. # make a copy of evp.c and bio.c for ssl.c to include inline
  174. $CP_CMD .${WOLFSSL_HEADERS}/evp.c .${WOLFCRYPT_SRC}/evp.c || exit 1
  175. $CP_CMD .${WOLFSSL_HEADERS}/bio.c .${WOLFCRYPT_SRC}/bio.c || exit 1
  176. # copy openssl compatibility headers to their appropriate location
  177. if [ ! -d ".${OPENSSL_DIR}" ]; then
  178. mkdir .${OPENSSL_DIR}
  179. fi
  180. $CP_CMD ${OPENSSL_DIR_TOP}/* .${OPENSSL_DIR} || exit 1
  181. cat > .${ROOT_SRC_DIR}/wolfssl.h <<EOF
  182. /* Generated wolfSSL header file for Arduino */
  183. #include <user_settings.h>
  184. #include <wolfssl/wolfcrypt/settings.h>
  185. #include <wolfssl/ssl.h>
  186. EOF
  187. else
  188. echo "ERROR: You must be in the IDE/ARDUINO directory to run this script"
  189. exit 1
  190. fi
  191. # At this point, the library is complete, but we need some additional files.
  192. #
  193. # optional diagnostics:
  194. # echo ".${ROOT_DIR}"
  195. # echo "${TOP_DIR}"
  196. # echo "cp ${TOP_DIR}/README.md .${ROOT_DIR}/"
  197. # Replace the `${WOLFSSL_VERSION}` text in Arduino_README_prepend.md,
  198. # saving it to a .tmp file. Prepend that file to the wolfSSL README.md
  199. # file as PREPENDED_README.md, then copy that to the publish directory
  200. # as an Arduino-specific README.md file.
  201. VERSION_PLACEHOLDER="\${WOLFSSL_VERSION}"
  202. ARDUINO_VERSION_SUFFIX_PLACEHOLDER="\${WOLFSSL_VERSION_ARUINO_SUFFIX}"
  203. PREPEND_FILE="Arduino_README_prepend.md"
  204. PROPERTIES_FILE_TEMPLATE="library.properties.template"
  205. sed s/"$VERSION_PLACEHOLDER"/"$WOLFSSL_VERSION"/ "$PREPEND_FILE" > "$PREPEND_FILE.tmp"
  206. cat "$PREPEND_FILE.tmp" ${TOP_DIR}/README.md > PREPENDED_README.md
  207. # Here we'll insert the wolfSSL version into the `library.properties.tmp` file, along with an Arduino version suffix.
  208. # The result should be something like version=5.6.601 (for the 1st incremental version on top of 5.6.6)
  209. sed s/"$VERSION_PLACEHOLDER"/"$WOLFSSL_VERSION"/ "$PROPERTIES_FILE_TEMPLATE" > "library.properties.tmp"
  210. sed -i.backup s/"$ARDUINO_VERSION_SUFFIX_PLACEHOLDER"/"$WOLFSSL_VERSION_ARUINO_SUFFIX"/ "library.properties.tmp"
  211. # cat library.properties.tmp
  212. # echo "${WOLFSSL_VERSION_ARUINO_SUFFIX}"
  213. echo "Step 11: Final root file copy"
  214. $CP_CMD PREPENDED_README.md .${ROOT_DIR}/README.md || exit 1
  215. $CP_CMD library.properties.tmp .${ROOT_DIR}/library.properties || exit 1
  216. $CP_CMD ${TOP_DIR}/"LICENSING" .${ROOT_DIR}/ || exit 1
  217. $CP_CMD ${TOP_DIR}/"README" .${ROOT_DIR}/ || exit 1
  218. $CP_CMD ${TOP_DIR}/"COPYING" .${ROOT_DIR}/ || exit 1
  219. $CP_CMD ${TOP_DIR}/"ChangeLog.md" .${ROOT_DIR}/ || exit 1
  220. $CP_CMD ${TOP_DIR}/".editorconfig" .${ROOT_DIR}/ || exit 1
  221. $CP_CMD ${TOP_DIR}/".gitignore" .${ROOT_DIR}/ || exit 1
  222. $CP_CMD "keywords.txt" .${ROOT_DIR}/ || exit 1
  223. echo "Step 12: workspace to publish:"
  224. echo ""
  225. head -n 3 PREPENDED_README.md
  226. echo ""
  227. ls ./wolfSSL -al
  228. echo ""
  229. # Optionally install to a separate directory.
  230. # Note we should have exited above if a problem was encountered,
  231. # as we'll never want to install a bad library.
  232. if [ "$THIS_OPERATION" = "INSTALL" ]; then
  233. if [ "$THIS_INSTALL_IS_GITHUB" = "true" ]; then
  234. echo "Installing to GitHub directory: $THIS_INSTALL_DIR"
  235. cp -r ".$ROOT_DIR"/* "$THIS_INSTALL_DIR" || exit 1
  236. else
  237. echo "Install:"
  238. echo "cp ../../examples/configs/user_settings_arduino.h .${ROOT_SRC_DIR}/user_settings.h"
  239. cp ../../examples/configs/user_settings_arduino.h ".${ROOT_SRC_DIR}/user_settings.h" || exit 1
  240. echo "mv $ROOT_DIR $ARDUINO_ROOT"
  241. mv ".$ROOT_DIR" "$ARDUINO_ROOT" || exit 1
  242. fi
  243. fi
  244. echo "Done!"