blob: cfb9a83d9316067cc17e098f76f579dea4aa804f [file] [log] [blame]
From a00fdd7ed25829eb5489907fc1e4a43429e0b621 Mon Sep 17 00:00:00 2001
From: "tc@google.com" <tc@google.com>
Date: Tue, 6 Jan 2009 22:39:41 +0000
Subject: [PATCH 06/10] Custom shell.c helpers to load Chromium's ICU data.
History uses fts3 with an icu-based segmenter. These changes allow building a
sqlite3 binary for Linux or Windows which can read those files.
Original review URL: https://codereview.chromium.org/42250
---
third_party/sqlite/src/Makefile.linux-gcc | 7 ++++++
third_party/sqlite/src/main.mk | 2 +-
third_party/sqlite/src/src/shell.c | 10 +++++++++
third_party/sqlite/src/src/shell_icu_linux.c | 27 +++++++++++++++++++++++
third_party/sqlite/src/src/shell_icu_win.c | 32 ++++++++++++++++++++++++++++
5 files changed, 77 insertions(+), 1 deletion(-)
create mode 100644 third_party/sqlite/src/src/shell_icu_linux.c
create mode 100644 third_party/sqlite/src/src/shell_icu_win.c
diff --git a/third_party/sqlite/src/Makefile.linux-gcc b/third_party/sqlite/src/Makefile.linux-gcc
index f6291c0..17c9e00 100644
--- a/third_party/sqlite/src/Makefile.linux-gcc
+++ b/third_party/sqlite/src/Makefile.linux-gcc
@@ -85,6 +85,13 @@ OPTS += -DOS_UNIX=1
# purposes.
OPTS += -DDEFAULT_ENABLE_RECOVER=1
+# Support for loading Chromium ICU data in sqlite3.
+ifeq ($(shell uname -s),Darwin)
+SHELL_ICU =
+else
+SHELL_ICU = $(TOP)/src/shell_icu_linux.c -licuuc
+endif
+
#### The suffix to add to executable files. ".exe" for windows.
# Nothing for unix.
#
diff --git a/third_party/sqlite/src/main.mk b/third_party/sqlite/src/main.mk
index a8629aa..4a6d5d6 100644
--- a/third_party/sqlite/src/main.mk
+++ b/third_party/sqlite/src/main.mk
@@ -476,7 +476,7 @@ libsqlite3.a: $(LIBOBJ)
sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h
$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) $(SHELL_OPT) \
- $(TOP)/src/shell.c libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
+ $(TOP)/src/shell.c $(SHELL_ICU) libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
sqldiff$(EXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
$(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
diff --git a/third_party/sqlite/src/src/shell.c b/third_party/sqlite/src/src/shell.c
index b7a7abc..b1d5087 100644
--- a/third_party/sqlite/src/src/shell.c
+++ b/third_party/sqlite/src/src/shell.c
@@ -4645,6 +4645,16 @@ int SQLITE_CDECL main(int argc, char **argv){
}
#endif
+ /* Begin evanm patch. */
+#if !defined(__APPLE__)
+ extern int sqlite_shell_init_icu();
+ if( !sqlite_shell_init_icu() ){
+ fprintf(stderr, "%s: warning: couldn't find icudt38.dll; "
+ "queries against ICU FTS tables will fail.\n", argv[0]);
+ }
+#endif /* !defined(__APPLE__) */
+ /* End evanm patch. */
+
/* Do an initial pass through the command-line argument to locate
** the name of the database file, the name of the initialization file,
** the size of the alternative malloc heap,
diff --git a/third_party/sqlite/src/src/shell_icu_linux.c b/third_party/sqlite/src/src/shell_icu_linux.c
new file mode 100644
index 0000000..4ad0e42
--- /dev/null
+++ b/third_party/sqlite/src/src/shell_icu_linux.c
@@ -0,0 +1,27 @@
+/* Copyright 2007 Google Inc. All Rights Reserved.
+**/
+
+#include <limits.h>
+#include <unistd.h>
+#include "unicode/putil.h"
+#include "unicode/udata.h"
+
+/*
+** This function attempts to load the ICU data tables from a data file.
+** Returns 0 on failure, nonzero on success.
+** This a hack job of icu_utils.cc:Initialize(). It's Chrome-specific code.
+*/
+int sqlite_shell_init_icu() {
+ char bin_dir[PATH_MAX + 1];
+ int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
+ if (bin_dir_size < 0 || bin_dir_size > PATH_MAX)
+ return 0;
+ bin_dir[bin_dir_size] = 0;;
+
+ u_setDataDirectory(bin_dir);
+ // Only look for the packaged data file;
+ // the default behavior is to look for individual files.
+ UErrorCode err = U_ZERO_ERROR;
+ udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
+ return err == U_ZERO_ERROR;
+}
diff --git a/third_party/sqlite/src/src/shell_icu_win.c b/third_party/sqlite/src/src/shell_icu_win.c
new file mode 100644
index 0000000..67ebbf4
--- /dev/null
+++ b/third_party/sqlite/src/src/shell_icu_win.c
@@ -0,0 +1,32 @@
+/* Copyright 2011 Google Inc. All Rights Reserved.
+**/
+
+#include <windows.h>
+#include "unicode/udata.h"
+
+/*
+** This function attempts to load the ICU data tables from a DLL.
+** Returns 0 on failure, nonzero on success.
+** This a hack job of icu_utils.cc:Initialize(). It's Chrome-specific code.
+*/
+
+#define ICU_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
+int sqlite_shell_init_icu() {
+ HMODULE module;
+ FARPROC addr;
+ UErrorCode err;
+
+ // Chrome dropped U_ICU_VERSION_SHORT from the icu data dll name.
+ module = LoadLibrary(L"icudt.dll");
+ if (!module)
+ return 0;
+
+ addr = GetProcAddress(module, ICU_DATA_SYMBOL);
+ if (!addr)
+ return 0;
+
+ err = U_ZERO_ERROR;
+ udata_setCommonData(addr, &err);
+
+ return 1;
+}
--
2.7.0