1
0

wolfssl-arduino.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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=""
  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. if [ "$ROOT_DIR" = "" ]; then
  61. echo "ERROR: ROOT_DIR cannot be blank"
  62. exit 1
  63. fi
  64. # Check environment
  65. if [ -n "$WSL_DISTRO_NAME" ]; then
  66. # we found a non-blank WSL environment distro name
  67. current_path="$(pwd)"
  68. pattern="/mnt/?"
  69. if echo "$current_path" | grep -Eq "^$pattern"; then
  70. # if we are in WSL and shared Windows file system, 'ln' does not work.
  71. ARDUINO_ROOT="/mnt/c/Users/$USER/Documents/Arduino/libraries"
  72. else
  73. ARDUINO_ROOT="$HOME/Arduino/libraries"
  74. fi
  75. fi
  76. echo "The Arduino library root is: $ARDUINO_ROOT"
  77. if [ $# -gt 0 ]; then
  78. THIS_OPERATION="$1"
  79. if [ "$THIS_OPERATION" = "INSTALL" ]; then
  80. THIS_INSTALL_DIR=$2
  81. if [ "$THIS_INSTALL_DIR" = "/" ]; then
  82. echo "ERROR: THIS_INSTALL_DIR cannot be /"
  83. exit 1
  84. fi
  85. echo "Install is active."
  86. if [ "$THIS_INSTALL_DIR" = "" ]; then
  87. if [ -d "$ARDUINO_ROOT$ROOT_DIR" ]; then
  88. echo "Error: the installation directory already exists: $ARDUINO_ROOT$ROOT_DIR"
  89. echo "A new directory needs to be created to ensure there are no stray files"
  90. echo "Please delete or move the directory and try again."
  91. exit 1
  92. fi
  93. else
  94. echo "Installing to $THIS_INSTALL_DIR"
  95. if [ -d "$THIS_INSTALL_DIR/.git" ];then
  96. echo "Target is a GitHub root repository."
  97. THIS_INSTALL_IS_GITHUB="true"
  98. else
  99. echo "Target is NOT a GitHub root directory repository. (e.g. not wolfssl/Arduino-wolfssl)"
  100. fi
  101. fi
  102. else
  103. echo "Error: not a valid operation: $THIS_OPERATION"
  104. exit 1
  105. fi
  106. fi
  107. ROOT_SRC_DIR="${ROOT_DIR}/src"
  108. EXAMPLES_DIR="${ROOT_DIR}/examples"
  109. WOLFSSL_SRC="${ROOT_SRC_DIR}/src"
  110. WOLFSSL_HEADERS="${ROOT_SRC_DIR}/wolfssl"
  111. WOLFCRYPT_ROOT="${ROOT_SRC_DIR}/wolfcrypt"
  112. WOLFCRYPT_SRC="${WOLFCRYPT_ROOT}/src"
  113. WOLFCRYPT_HEADERS="${WOLFSSL_HEADERS}/wolfcrypt"
  114. OPENSSL_DIR="${WOLFSSL_HEADERS}/openssl"
  115. # TOP indicates the file directory for top level of the wolfssl repository.
  116. TOP_DIR="../.."
  117. WOLFSSL_SRC_TOP="${TOP_DIR}/src"
  118. WOLFSSL_HEADERS_TOP="${TOP_DIR}/wolfssl"
  119. WOLFCRYPT_ROOT_TOP="${TOP_DIR}/wolfcrypt"
  120. WOLFCRYPT_SRC_TOP="${WOLFCRYPT_ROOT_TOP}/src"
  121. WOLFCRYPT_HEADERS_TOP="${WOLFSSL_HEADERS_TOP}/wolfcrypt"
  122. OPENSSL_DIR_TOP="${WOLFSSL_HEADERS_TOP}/openssl"
  123. WOLFSSL_VERSION=$(grep -i "LIBWOLFSSL_VERSION_STRING" ${TOP_DIR}/wolfssl/version.h | cut -d '"' -f 2)
  124. if [ "$WOLFSSL_VERSION" = "" ]; then
  125. echo "ERROR: Could not find wolfSSL Version in ${TOP_DIR}/wolfssl/version.h"
  126. exit 1
  127. else
  128. echo "Found wolfSSL version $WOLFSSL_VERSION"
  129. echo "# WOLFSSL_VERSION_ARUINO_SUFFIX $WOLFSSL_VERSION_ARUINO_SUFFIX"
  130. fi
  131. echo ""
  132. THIS_DIR=${PWD##*/}
  133. if [ "$THIS_DIR" = "ARDUINO" ]; then
  134. # mkdir ./wolfssl
  135. if [ -d ".${ROOT_DIR}" ]; then
  136. echo "ERROR: $(realpath ".${ROOT_DIR}") is not empty"
  137. exit 1
  138. else
  139. echo "Step 01: mkdir .${ROOT_DIR}"
  140. mkdir ."${ROOT_DIR}"
  141. fi
  142. # mkdir ./wolfssl/src
  143. if [ ! -d ".${ROOT_SRC_DIR}" ]; then
  144. echo "Step 02: mkdir .${ROOT_SRC_DIR}"
  145. mkdir ."${ROOT_SRC_DIR}"
  146. fi
  147. # mkdir ./wolfssl/src/wolfssl
  148. if [ ! -d ".${WOLFSSL_HEADERS}" ]; then
  149. echo "Step 03: mkdir .${WOLFSSL_HEADERS}"
  150. mkdir ."${WOLFSSL_HEADERS}"
  151. fi
  152. # cp ../../wolfssl/*.h ./wolfssl/src/wolfssl
  153. echo "Step 04: cp ${WOLFSSL_HEADERS_TOP}/*.h .${WOLFSSL_HEADERS}"
  154. $CP_CMD "${WOLFSSL_HEADERS_TOP}"/*.h ."${WOLFSSL_HEADERS}"
  155. if [ ! -d ".${WOLFCRYPT_HEADERS}" ]; then
  156. # mkdir ./wolfssl/src/wolfssl/wolfcrypt
  157. echo "Step 05: mkdir .${WOLFCRYPT_HEADERS}"
  158. mkdir ."${WOLFCRYPT_HEADERS}"
  159. mkdir ."${WOLFCRYPT_HEADERS}/port"
  160. mkdir ."${WOLFCRYPT_HEADERS}/port/atmel"
  161. mkdir ."${WOLFCRYPT_HEADERS}/port/Espressif"
  162. fi
  163. # cp ../../wolfssl/wolfcrypt/*.h ./wolfssl/src/wolfssl/wolfcrypt
  164. echo "Step 06: cp ${WOLFCRYPT_HEADERS_TOP}/*.h .${WOLFCRYPT_HEADERS}"
  165. $CP_CMD "${WOLFCRYPT_HEADERS_TOP}"/*.h ."${WOLFCRYPT_HEADERS}" || exit 1
  166. $CP_CMD "${WOLFCRYPT_HEADERS_TOP}"/port/atmel/*.h ."${WOLFCRYPT_HEADERS}/port/atmel" || exit 1
  167. $CP_CMD "${WOLFCRYPT_HEADERS_TOP}"/port/Espressif/*.h ."${WOLFCRYPT_HEADERS}/port/Espressif" || exit 1
  168. # Add in source files to wolfcrypt/src
  169. if [ ! -d ".${WOLFCRYPT_ROOT}" ]; then
  170. # mkdir ./wolfssl/src/wolfcrypt
  171. echo "Step 07: mkdir .${WOLFCRYPT_ROOT}"
  172. mkdir ."${WOLFCRYPT_ROOT}"
  173. fi
  174. # mkdir ./wolfssl/src/wolfcrypt/src
  175. if [ ! -d ".${WOLFCRYPT_SRC}" ]; then
  176. echo "Step 08: mkdir .${WOLFCRYPT_SRC}"
  177. mkdir ."${WOLFCRYPT_SRC}"
  178. mkdir ."${WOLFCRYPT_SRC}"/port
  179. mkdir ."${WOLFCRYPT_SRC}"/port/atmel
  180. mkdir ."${WOLFCRYPT_SRC}"/port/Espressif
  181. fi
  182. # cp ../../wolfcrypt/src/*.c ./wolfssl/src/wolfcrypt/src
  183. echo "Step 09: cp ${WOLFCRYPT_SRC_TOP}/*.c .${WOLFCRYPT_SRC}"
  184. $CP_CMD -r "${WOLFCRYPT_SRC_TOP}"/*.c ."${WOLFCRYPT_SRC}" || exit 1
  185. $CP_CMD -r "${WOLFCRYPT_SRC_TOP}"/port/atmel/*.c ."${WOLFCRYPT_SRC}"/port/atmel || exit 1
  186. $CP_CMD -r "${WOLFCRYPT_SRC_TOP}"/port/Espressif/*.c ."${WOLFCRYPT_SRC}"/port/Espressif || exit 1
  187. # Add in source files to top level src folders
  188. if [ ! -d ".${WOLFSSL_SRC}" ]; then
  189. # mkdir ./wolfssl/src/src
  190. echo "Step 10: mkdir .${WOLFSSL_SRC}"
  191. mkdir ."${WOLFSSL_SRC}"
  192. fi
  193. $CP_CMD "${WOLFSSL_SRC_TOP}"/*.c ."${WOLFSSL_SRC}" || exit 1
  194. # put bio and evp as includes
  195. $CP_CMD ."${WOLFSSL_SRC}"/bio.c ."${WOLFSSL_HEADERS}" || exit 1
  196. $CP_CMD ."${WOLFCRYPT_SRC}"/evp.c ."${WOLFSSL_HEADERS}" || exit 1
  197. # make a copy of evp.c and bio.c for ssl.c to include inline
  198. $CP_CMD ."${WOLFSSL_HEADERS}"/evp.c ."${WOLFCRYPT_SRC}"/evp.c || exit 1
  199. $CP_CMD ."${WOLFSSL_HEADERS}"/bio.c ."${WOLFCRYPT_SRC}"/bio.c || exit 1
  200. # copy openssl compatibility headers to their appropriate location
  201. if [ ! -d ".${OPENSSL_DIR}" ]; then
  202. mkdir ."${OPENSSL_DIR}"
  203. fi
  204. $CP_CMD "${OPENSSL_DIR_TOP}"/* ."${OPENSSL_DIR}" || exit 1
  205. # Finally, copy the Arduino-specific wolfssl library files into place: [lib]/src
  206. $CP_CMD ./wolfssl.h ".${ROOT_SRC_DIR}"/wolfssl.h
  207. echo "Copy examples...."
  208. # Copy examples
  209. mkdir -p ".${ROOT_SRC_DIR}"/examples
  210. echo "Copy wolfssl_client example...."
  211. mkdir -p ".${EXAMPLES_DIR}"/wolfssl_client
  212. $CP_CMD ./sketches/wolfssl_client/wolfssl_client.ino ".${EXAMPLES_DIR}"/wolfssl_client/wolfssl_client.ino || exit 1
  213. $CP_CMD ./sketches/wolfssl_client/README.md ".${EXAMPLES_DIR}"/wolfssl_client/README.md || exit 1
  214. echo "Copy wolfssl_server example...."
  215. mkdir -p .${EXAMPLES_DIR}/wolfssl_server
  216. $CP_CMD ./sketches/wolfssl_server/wolfssl_server.ino ".${EXAMPLES_DIR}"/wolfssl_server/wolfssl_server.ino || exit 1
  217. $CP_CMD ./sketches/wolfssl_server/README.md ".${EXAMPLES_DIR}"/wolfssl_server/README.md || exit 1
  218. echo "Copy wolfssl_server example...."
  219. mkdir -p .${EXAMPLES_DIR}/wolfssl_version
  220. $CP_CMD ./sketches/wolfssl_version/wolfssl_version.ino ".${EXAMPLES_DIR}"/wolfssl_version/wolfssl_version.ino || exit 1
  221. $CP_CMD ./sketches/wolfssl_version/README.md ".${EXAMPLES_DIR}"/wolfssl_version/README.md || exit 1
  222. else
  223. echo "ERROR: You must be in the IDE/ARDUINO directory to run this script"
  224. exit 1
  225. fi
  226. # At this point, the library is complete, but we need some additional files.
  227. #
  228. # optional diagnostics:
  229. # echo ".${ROOT_DIR}"
  230. # echo "${TOP_DIR}"
  231. # echo "cp ${TOP_DIR}/README.md .${ROOT_DIR}/"
  232. # Replace the `${WOLFSSL_VERSION}` text in Arduino_README_prepend.md,
  233. # saving it to a .tmp file. Prepend that file to the wolfSSL README.md
  234. # file as PREPENDED_README.md, then copy that to the publish directory
  235. # as an Arduino-specific README.md file.
  236. VERSION_PLACEHOLDER="\${WOLFSSL_VERSION}"
  237. ARDUINO_VERSION_SUFFIX_PLACEHOLDER="\${WOLFSSL_VERSION_ARUINO_SUFFIX}"
  238. PREPEND_FILE="Arduino_README_prepend.md"
  239. PROPERTIES_FILE_TEMPLATE="library.properties.template"
  240. sed s/"$VERSION_PLACEHOLDER"/"$WOLFSSL_VERSION"/ "$PREPEND_FILE" > "$PREPEND_FILE.tmp"
  241. cat "$PREPEND_FILE.tmp" ${TOP_DIR}/README.md > PREPENDED_README.md
  242. # Here we'll insert the wolfSSL version into the `library.properties.tmp` file, along with an Arduino version suffix.
  243. # The result should be something like version=5.6.6.Arduino.1 (for the 1st incremental version on top of 5.6.6)
  244. sed s/"$VERSION_PLACEHOLDER"/"$WOLFSSL_VERSION"/ "$PROPERTIES_FILE_TEMPLATE" > "library.properties.tmp"
  245. sed -i.backup s/"$ARDUINO_VERSION_SUFFIX_PLACEHOLDER"/"$WOLFSSL_VERSION_ARUINO_SUFFIX"/ "library.properties.tmp"
  246. # cat library.properties.tmp
  247. # echo "${WOLFSSL_VERSION_ARUINO_SUFFIX}"
  248. echo "Step 11: Final root file copy"
  249. $CP_CMD PREPENDED_README.md ."${ROOT_DIR}"/README.md || exit 1
  250. $CP_CMD library.properties.tmp ."${ROOT_DIR}"/library.properties || exit 1
  251. $CP_CMD "${TOP_DIR}"/"LICENSING" ."${ROOT_DIR}"/ || exit 1
  252. $CP_CMD "${TOP_DIR}"/"README" ."${ROOT_DIR}"/ || exit 1
  253. $CP_CMD "${TOP_DIR}"/"COPYING" ."${ROOT_DIR}"/ || exit 1
  254. $CP_CMD "${TOP_DIR}"/"ChangeLog.md" ."${ROOT_DIR}"/ || exit 1
  255. $CP_CMD "${TOP_DIR}"/".editorconfig" ."${ROOT_DIR}"/ || exit 1
  256. $CP_CMD "${TOP_DIR}"/".gitignore" ."${ROOT_DIR}"/ || exit 1
  257. $CP_CMD "keywords.txt" ."${ROOT_DIR}"/ || exit 1
  258. echo "Step 12: Workspace to publish:"
  259. echo ""
  260. head -n 3 PREPENDED_README.md
  261. echo ""
  262. ls ./wolfssl -al
  263. echo ""
  264. # Optionally install to a separate directory.
  265. # Note we should have exited above if a problem was encountered,
  266. # as we'll never want to install a bad library.
  267. if [ "$THIS_OPERATION" = "INSTALL" ]; then
  268. echo "Config:"
  269. echo "cp ../../examples/configs/user_settings_arduino.h ".${ROOT_SRC_DIR}"/user_settings.h"
  270. # Nearly an ordinary copy, but we remove any lines with ">>" (typically edit with caution warning in comments)
  271. grep -v '>>' ../../examples/configs/user_settings_arduino.h > ".${ROOT_SRC_DIR}"/user_settings.h || exit 1
  272. # Show the user_settings.h revision string:
  273. grep "WOLFSSL_USER_SETTINGS_ID" ."${ROOT_SRC_DIR}/user_settings.h"
  274. echo ""
  275. if [ "$THIS_INSTALL_IS_GITHUB" = "true" ]; then
  276. echo "Installing to GitHub directory: $THIS_INSTALL_DIR"
  277. cp -r ."$ROOT_DIR"/* "$THIS_INSTALL_DIR" || exit 1
  278. echo "Removing workspace library directory: .$ROOT_DIR"
  279. rm -rf ".$ROOT_DIR"
  280. else
  281. echo "Installing to local directory:"
  282. if [ "$THIS_INSTALL_DIR" = "" ]; then
  283. echo "mv .$ROOT_DIR $ARDUINO_ROOT"
  284. mv ."$ROOT_DIR" "$ARDUINO_ROOT" || exit 1
  285. echo "Arduino wolfSSL Version: $WOLFSSL_VERSION$WOLFSSL_VERSION_ARUINO_SUFFIX"
  286. else
  287. echo "cp -r .\"$ROOT_DIR\"/* \"$THIS_INSTALL_DIR\""
  288. mkdir -p "$THIS_INSTALL_DIR" || exit 1
  289. cp -r ."$ROOT_DIR"/* "$THIS_INSTALL_DIR" || exit 1
  290. fi
  291. fi
  292. fi
  293. echo "Done!"