Skip to content

Instantly share code, notes, and snippets.

@aguynamedben
Last active October 28, 2018 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aguynamedben/c69e4ece4c3c7f057bcaac7ad7fc30bc to your computer and use it in GitHub Desktop.
Save aguynamedben/c69e4ece4c3c7f057bcaac7ad7fc30bc to your computer and use it in GitHub Desktop.
Rebuilding node-sqlite3 with support for statically linked SQLCipher
# This is a modified version of binding.gyp (from node-sqlite3) that allows
# SQLCipher to be *statically linked* for Electron distribution to machines that
# do not have SQLCipher development headers present. If you distribute
# node-sqlite3 with dynamically linked SQLite/SQLCipher bindings (the default),
# your app will error out at runtime on user's machines that do not have
# SQLCipher.
#
# This problem is tricky to test and catch because the app will work fine on
# your machine, because you've probably installed SQLCipher to custom-build
# node-sqlite3. To test that everything is working, package your Electron app,
# uninstall SQLCipher (i.e. `brew uninstall sqlcipher`), then run your app.
#
# This was very finnicky and tricky to get right. The final source of truth was
# this GitHub comment:
# https://github.com/fritx/win-sqlcipher/issues/2#issuecomment-326524839
#
# I run a script as part of my package.json `postinstall` script that replaces
# ~/node_modules/sqlite3/binding.gyp with this version of binding.gyp, then I
# run the appropriate `npm rebuild` command for my OS and Electron version.
#
# You will not need the compiler/linker flags (`export LDFLAGS="-L`brew --prefix`/opt/sqlcipher/lib"` and `export CPPFLAGS="-I`brew --prefix`/opt/sqlcipher/include"`) but you will need to additionally link the crypto library.
# Example command:
# export LDFLAGS="-lcrypto"
# npm rebuild sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix` --runtime=electron --target=2.0.11 --dist-url=https://atom.io/download/electron`
{
"includes": [ "deps/common-sqlite.gypi" ],
"variables": {
"sqlite%":"internal",
"sqlite_libname%":"sqlite3"
},
"targets": [
{
"target_name": "<(module_name)",
"include_dirs": ["<!(node -e \"require('nan')\")"],
"conditions": [
["sqlite != 'internal'", {
"include_dirs": [ "/usr/local/opt/sqlcipher/include/sqlcipher" ],
"libraries": [
"/usr/local/opt/sqlcipher/lib/libsqlcipher.a"
],
"conditions": [ [ "OS=='linux'", {"libraries+":["-Wl,-rpath=<@(sqlite)/lib/libsqlcipher.a"]} ] ],
"conditions": [ [ "OS!='win'", {"libraries+":["-L<@(sqlite)/lib/libsqlcipher.a"]} ] ],
'msvs_settings': {
'VCLinkerTool': {
'AdditionalLibraryDirectories': [
'<(sqlite)/lib'
],
},
}
},
{
"dependencies": [
"deps/sqlite3.gyp:sqlite3"
]
}
]
],
"sources": [
"src/database.cc",
"src/node_sqlite3.cc",
"src/statement.cc"
]
},
{
"target_name": "action_after_build",
"type": "none",
"dependencies": [ "<(module_name)" ],
"copies": [
{
"files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
"destination": "<(module_path)"
}
]
}
]
}
{
"name": "example",
"scripts": {
"postinstall": "yarn flow-typed && yarn build-dll && yarn limited-electron-rebuild && yarn rebuild-sqlite",
"limited-electron-rebuild": "electron-rebuild --only @sentry/electron,sequelize",
"rebuild-sqlite": "yarn install-sqlcipher && yarn copy-custom-node-sqlite3-binding && yarn npm-rebuild-sqlite",
"install-sqlcipher": "brew install sqlcipher --with-fts",
"copy-custom-node-sqlite3-binding": "cp ./internals/scripts/custom-node-sqlite3-binding.gyp ./node_modules/sqlite3/binding.gyp",
"npm-rebuild-sqlite": "LDFLAGS='-lcrypto' npm rebuild sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix` --runtime=electron --target=2.0.11 --dist-url=https://atom.io/download/electron &>/dev/null",
},
"build": {
"npmRebuild": false // We handle rebuilding in limited-electron-rebuild and rebuild-sqlcipher
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment