download.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env bash
  2. # ===
  3. # This script defines and generates the bundled SQLite unit (sqlite3.c).
  4. #
  5. # The following steps are taken:
  6. # 1. populate the shell environment with the defined compile-time options.
  7. # 2. download and extract the SQLite source code into a temporary directory.
  8. # 3. run "sh configure" and "make sqlite3.c" within the source directory.
  9. # 4. copy the generated amalgamation into the output directory (./sqlite3).
  10. # 5. export the defined compile-time options to a gyp file (./defines.gypi).
  11. # 6. update the docs (../docs/compilation.md) with details of this distribution.
  12. #
  13. # When a user builds better-sqlite3, the following steps are taken:
  14. # 1. node-gyp loads the previously exported compile-time options (defines.gypi).
  15. # 2. the copy.js script copies the bundled amalgamation into the build folder.
  16. # 3. node-gyp compiles the copied sqlite3.c along with better_sqlite3.cpp.
  17. # 4. node-gyp links the two resulting binaries to generate better_sqlite3.node.
  18. # ===
  19. YEAR="2025"
  20. VERSION="3510100"
  21. # Defines below are sorted alphabetically
  22. DEFINES="
  23. HAVE_INT16_T=1
  24. HAVE_INT32_T=1
  25. HAVE_INT8_T=1
  26. HAVE_STDINT_H=1
  27. HAVE_UINT16_T=1
  28. HAVE_UINT32_T=1
  29. HAVE_UINT8_T=1
  30. HAVE_USLEEP=1
  31. SQLITE_DEFAULT_CACHE_SIZE=-16000
  32. SQLITE_DEFAULT_FOREIGN_KEYS=1
  33. SQLITE_DEFAULT_MEMSTATUS=0
  34. SQLITE_DEFAULT_WAL_SYNCHRONOUS=1
  35. SQLITE_DQS=0
  36. SQLITE_ENABLE_COLUMN_METADATA
  37. SQLITE_ENABLE_DBSTAT_VTAB
  38. SQLITE_ENABLE_DESERIALIZE
  39. SQLITE_ENABLE_FTS3
  40. SQLITE_ENABLE_FTS3_PARENTHESIS
  41. SQLITE_ENABLE_FTS4
  42. SQLITE_ENABLE_FTS5
  43. SQLITE_ENABLE_GEOPOLY
  44. SQLITE_ENABLE_JSON1
  45. SQLITE_ENABLE_MATH_FUNCTIONS
  46. SQLITE_ENABLE_RTREE
  47. SQLITE_ENABLE_STAT4
  48. SQLITE_ENABLE_UPDATE_DELETE_LIMIT
  49. SQLITE_LIKE_DOESNT_MATCH_BLOBS
  50. SQLITE_OMIT_DEPRECATED
  51. SQLITE_OMIT_PROGRESS_CALLBACK
  52. SQLITE_OMIT_SHARED_CACHE
  53. SQLITE_OMIT_TCL_VARIABLE
  54. SQLITE_SOUNDEX
  55. SQLITE_THREADSAFE=2
  56. SQLITE_TRACE_SIZE_LIMIT=32
  57. SQLITE_USE_URI=0
  58. "
  59. # ========== START SCRIPT ========== #
  60. echo "setting up environment..."
  61. DEPS="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
  62. TEMP="$DEPS/temp"
  63. OUTPUT="$DEPS/sqlite3"
  64. rm -rf "$TEMP"
  65. rm -rf "$OUTPUT"
  66. mkdir -p "$TEMP"
  67. mkdir -p "$OUTPUT"
  68. export CFLAGS=`echo $(echo "$DEFINES" | sed -e "/^\s*$/d" -e "s/^/-D/")`
  69. echo "downloading source..."
  70. curl -#f "https://www.sqlite.org/$YEAR/sqlite-src-$VERSION.zip" > "$TEMP/source.zip" || exit 1
  71. echo "extracting source..."
  72. unzip "$TEMP/source.zip" -d "$TEMP" > /dev/null || exit 1
  73. cd "$TEMP/sqlite-src-$VERSION" || exit 1
  74. echo "configuring amalgamation..."
  75. sh configure > /dev/null || exit 1
  76. echo "building amalgamation..."
  77. make OPTIONS="$CFLAGS" sqlite3.c > /dev/null || exit 1
  78. echo "copying amalgamation..."
  79. cp sqlite3.c sqlite3.h sqlite3ext.h "$OUTPUT/" || exit 1
  80. echo "applying patches..."
  81. cd "$DEPS" || exit 1
  82. for patch in patches/*.patch; do
  83. # If a patch fails, just skip it an move on.
  84. # By default `patch` tries to be clever and reverse the patch, so we have to specify `--forward`.
  85. # If the patch fails, we # don't write .orig and .rej files, so we have to specify `--no-backup-if-mismatch` and `--reject-file=-`.
  86. patch --batch --forward --no-backup-if-mismatch --reject-file=- -p2 < "$patch"
  87. done
  88. echo "updating gyp configs..."
  89. GYP="$DEPS/defines.gypi"
  90. printf "# THIS FILE IS AUTOMATICALLY GENERATED BY deps/download.sh (DO NOT EDIT)\n\n{\n 'defines': [\n" > "$GYP"
  91. printf "$DEFINES" | sed -e "/^\s*$/d" -e "s/\(.*\)/ '\1',/" >> "$GYP"
  92. printf " ],\n}\n" >> "$GYP"
  93. echo "updating docs..."
  94. DOCS="$DEPS/../docs/compilation.md"
  95. MAJOR=`expr "${VERSION:0:1}" + 0`
  96. MINOR=`expr "${VERSION:1:2}" + 0`
  97. PATCH=`expr "${VERSION:3:2}" + 0`
  98. sed -Ei.bak -e "s/version [0-9]+\.[0-9]+\.[0-9]+/version $MAJOR.$MINOR.$PATCH/g" "$DOCS"
  99. sed -i.bak -e "/^SQLITE_/,\$d" "$DOCS"
  100. sed -i.bak -e "/^HAVE_/,\$d" "$DOCS"
  101. rm "$DOCS".bak
  102. printf "$DEFINES" | sed -e "/^\s*$/d" >> "$DOCS"
  103. printf "\`\`\`\n" >> "$DOCS"
  104. echo "cleaning up..."
  105. cd - > /dev/null || exit 1
  106. rm -rf "$TEMP"
  107. echo "done!"