fix: dynamic lib

This commit is contained in:
Richard Ramos 2020-05-12 17:48:04 -04:00
parent 95ca41016f
commit 9986a00dd5
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
8 changed files with 77 additions and 98 deletions

5
.gitignore vendored
View File

@ -1,10 +1,5 @@
nimcache/
nimblecache/
htmldocs/
libcrypto.a
sqlcipher_abi.nim
sqlite3.c
sqlite3.h
wrap
main
myDatabase

View File

@ -4,6 +4,18 @@
A low-level Nim wrapper for the [SQLCipher](https://github.com/sqlcipher/sqlcipher). Builds and exposes the raw C API of SQLCipher.
## Requirements
```
# Linux
sudo apt install libssl-dev
# MacOS
brew install openssl
# Windows
¯\_(ツ)_/¯
```
## Usage
TODO

View File

@ -1,27 +0,0 @@
import os
let OpenSSL = "openssl-1.1.1g"
let SQLCipher = "sqlcipher-4.3.0"
# libcripto ===========================================
exec "curl https://www.openssl.org/source/" & OpenSSL & ".tar.gz --output " & OpenSSL & ".tar.gz"
exec "tar -zxvf " & OpenSSL & ".tar.gz"
rmFile OpenSSL & ".tar.gz"
withDir OpenSSL:
exec "./config -shared"
exec "make -j`nproc`"
# Linux specific. Add `when` for different OS
cpFile(thisDir() / OpenSSL / "libcrypto.a", thisDir() / "libcrypto.a")
rmDir OpenSSL
# sqlite3.c ===========================================
exec "curl -LJO https://github.com/sqlcipher/sqlcipher/archive/v4.3.0.tar.gz --output " & SQLCipher & ".tar.gz"
exec "tar -zxvf " & SQLCipher & ".tar.gz"
rmFile SQLCipher & ".tar.gz"
withDir SQLCipher:
# Linux specific
exec """./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="../libcrypto.a""""
exec "make sqlite3.c"
cpFile(thisDir() / SQLCipher / "sqlite3.c", thisDir() / "sqlite3.c")
cpFile(thisDir() / SQLCipher / "sqlite3.h", thisDir() / "sqlite3.h")
rmDir SQLCipher

View File

@ -1,5 +0,0 @@
#!/bin/bash
[[ -v HAS_NIMTEROP ]] || nimble install -y nimterop@0.5.2
nim c --hints:off wrap.nim > sqlcipher_abi.nim
./wrap

36
sqlcipher_abi.nim Normal file
View File

@ -0,0 +1,36 @@
import nimterop/[cimport, build, paths]
import os, strutils
const
baseDir = getProjectCacheDir("sqlcipher_abi")
static:
# uses va_list which is undefined
cSkipSymbol(@[
# uses va_list which is undefined
"sqlite3_vmprintf",
"sqlite3_vsnprintf",
"sqlite3_str_vappendf",
"sqlite3_destructor_type"
])
gitPull("https://github.com/sqlcipher/sqlcipher", outdir = baseDir, checkout = "v4.4.0")
configure(baseDir, "Makefile", """--enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"""")
make(baseDir, "sqlite3.c", "sqlite3.c")
{.passC: "-DSQLITE_HAS_CODEC".}
# TODO: determine if these are OS specific
{.passL: "-lpthread".}
{.passL: "-lcrypto".}
cDefine("SQLITE_HAS_CODEC")
cCompile(baseDir / "sqlite3.c")
cImport(baseDir/"sqlite3.h", flags = "-f:ast2")
#TODO: flag for static linking?

View File

@ -10,10 +10,3 @@ skipDirs = @["test"]
# Dependencies
requires "nim >= 1.0.0"
requires "nimterop >= 0.5.2"
before install:
exec "nim e build_dependencies.nims"
exec "./gen-wrapper.sh"
# TODO: read nimterop documentation for remove the need for build_depenencies.nim
# TODO: see https://github.com/nimterop/nimterop/wiki/Wrappers

View File

@ -1,27 +1,42 @@
import sqlcipher_abi
{.passL: "-lpthread".}
# TODO: ask about this
{.passL: "../libcrypto.a".}
import times
import strformat
import os
when isMainModule:
var
dbConn: ptr sqlite3
errorMsg: cstring;
# TODO: add template to check if function results are SQLITE_OK
# TODO: create template to check if function results are SQLITE_OK
if sqlite3_open("./myDatabase", addr dbConn) != SQLITE_OK:
echo "ERROR!!!!"
echo "ERROR OPENING THE DB!!!!"
quit()
var passwd = "qwerty"
if sqlite3_key(dbConn, addr passwd, 6) != SQLITE_OK:
echo "ERROR!!!!"
var passwd = "qwerty!"
var res = sqlite3_key(dbConn, passwd.cstring, 7)
echo res
if res != SQLITE_OK:
echo "ERROR OPENING DB!!!!"
quit()
echo "TODO: create a table, and insert a value"
if sqlite3_exec(dbConn, "CREATE TABLE IF NOT EXISTS log (theTime TEXT PRIMARY KEY)".cstring, nil, nil, addr errorMsg) != SQLITE_OK:
echo "ERROR CREATING TABLE!!!", errorMsg
quit()
else:
echo "Table created or already exists"
let date = getDateStr(now())
let time = getClockStr(now())
if sqlite3_exec(dbConn, &"""INSERT INTO log VALUES("{date}:{time}")""", nil, nil, addr errorMsg) != SQLITE_OK:
echo "ERROR INSERTING DATA!!!", errorMsg
quit()
else:
echo "Record inserted"
if sqlite3_close(dbConn) != SQLITE_OK:
echo "ERROR CLOSING THE DB!!!!"
else:
echo "Fin!"

View File

@ -1,40 +0,0 @@
import nimterop/cimport
static:
cDebug()
# uses va_list which is undefined
cSkipSymbol(@[
"sqlite3_activate_see",
# uses va_list which is undefined
"sqlite3_vmprintf",
"sqlite3_vsnprintf",
"sqlite3_str_vappendf",
# can use native nim types instead
"sqlite_int64",
"sqlite_uint64",
"sqlite3_int64",
"sqlite3_uint64",
])
cDefine("SQLITE_HAS_CODEC")
cPlugin:
import strutils
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
if sym.kind == nskType:
case sym.name
of "sqlite_int64", "sqlite3_int64": sym.name = "int64"
of "sqlite_uint64", "sqlite3_uint64": sym.name = "uint64"
{.passC: "-DSQLITE_HAS_CODEC".}
# TODO: This is probably linux specific
{.passL: "-lpthread".}
{.passL: "libcrypto.a".}
cImport("sqlite3.h")
cCompile("sqlite3.c")
echo sqlite3_libversion()