wolfssl-arduino.sh 13 KB

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