1 From c7020c4c8f2e786436804819d11ca2e4b9878a5f Mon Sep 17 00:00:00 2001
2 From: "tc@google.com" <tc@google.com>
3 Date: Tue, 6 Jan 2009 22:39:41 +0000
4 Subject: [PATCH 07/16] Custom shell.c helpers to load Chromium's ICU data.
6 History uses fts3 with an icu-based segmenter. These changes allow building a
7 sqlite3 binary for Linux or Windows which can read those files.
9 Original review URL: https://codereview.chromium.org/42250
11 third_party/sqlite/src/Makefile.linux-gcc | 7 ++++++
12 third_party/sqlite/src/main.mk | 2 +-
13 third_party/sqlite/src/src/shell.c | 10 +++++++++
14 third_party/sqlite/src/src/shell_icu_linux.c | 27 +++++++++++++++++++++++
15 third_party/sqlite/src/src/shell_icu_win.c | 32 ++++++++++++++++++++++++++++
16 5 files changed, 77 insertions(+), 1 deletion(-)
17 create mode 100644 third_party/sqlite/src/src/shell_icu_linux.c
18 create mode 100644 third_party/sqlite/src/src/shell_icu_win.c
20 diff --git a/third_party/sqlite/src/Makefile.linux-gcc b/third_party/sqlite/src/Makefile.linux-gcc
21 index e631816..f60f1a1 100644
22 --- a/third_party/sqlite/src/Makefile.linux-gcc
23 +++ b/third_party/sqlite/src/Makefile.linux-gcc
24 @@ -86,6 +86,13 @@ OPTS += -DOS_UNIX=1
26 OPTS += -DDEFAULT_ENABLE_RECOVER=1
28 +# Support for loading Chromium ICU data in sqlite3.
29 +ifeq ($(shell uname -s),Darwin)
32 +SHELL_ICU = $(TOP)/src/shell_icu_linux.c -licuuc
35 #### The suffix to add to executable files. ".exe" for windows.
38 diff --git a/third_party/sqlite/src/main.mk b/third_party/sqlite/src/main.mk
39 index 2189fd6..54b5b35 100644
40 --- a/third_party/sqlite/src/main.mk
41 +++ b/third_party/sqlite/src/main.mk
42 @@ -395,7 +395,7 @@ libsqlite3.a: $(LIBOBJ)
44 sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h
45 $(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) \
46 - $(TOP)/src/shell.c \
47 + $(TOP)/src/shell.c $(SHELL_ICU) \
48 libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
50 mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c
51 diff --git a/third_party/sqlite/src/src/shell.c b/third_party/sqlite/src/src/shell.c
52 index 3ca4b09..0951481 100644
53 --- a/third_party/sqlite/src/src/shell.c
54 +++ b/third_party/sqlite/src/src/shell.c
55 @@ -3963,6 +3963,16 @@ int main(int argc, char **argv){
56 signal(SIGINT, interrupt_handler);
59 + /* Begin evanm patch. */
60 +#if !defined(__APPLE__)
61 + extern int sqlite_shell_init_icu();
62 + if( !sqlite_shell_init_icu() ){
63 + fprintf(stderr, "%s: warning: couldn't find icudt38.dll; "
64 + "queries against ICU FTS tables will fail.\n", argv[0]);
66 +#endif /* !defined(__APPLE__) */
67 + /* End evanm patch. */
69 /* Do an initial pass through the command-line argument to locate
70 ** the name of the database file, the name of the initialization file,
71 ** the size of the alternative malloc heap,
72 diff --git a/third_party/sqlite/src/src/shell_icu_linux.c b/third_party/sqlite/src/src/shell_icu_linux.c
74 index 0000000..4ad0e42
76 +++ b/third_party/sqlite/src/src/shell_icu_linux.c
78 +/* Copyright 2007 Google Inc. All Rights Reserved.
83 +#include "unicode/putil.h"
84 +#include "unicode/udata.h"
87 +** This function attempts to load the ICU data tables from a data file.
88 +** Returns 0 on failure, nonzero on success.
89 +** This a hack job of icu_utils.cc:Initialize(). It's Chrome-specific code.
91 +int sqlite_shell_init_icu() {
92 + char bin_dir[PATH_MAX + 1];
93 + int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
94 + if (bin_dir_size < 0 || bin_dir_size > PATH_MAX)
96 + bin_dir[bin_dir_size] = 0;;
98 + u_setDataDirectory(bin_dir);
99 + // Only look for the packaged data file;
100 + // the default behavior is to look for individual files.
101 + UErrorCode err = U_ZERO_ERROR;
102 + udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
103 + return err == U_ZERO_ERROR;
105 diff --git a/third_party/sqlite/src/src/shell_icu_win.c b/third_party/sqlite/src/src/shell_icu_win.c
107 index 0000000..67ebbf4
109 +++ b/third_party/sqlite/src/src/shell_icu_win.c
111 +/* Copyright 2011 Google Inc. All Rights Reserved.
114 +#include <windows.h>
115 +#include "unicode/udata.h"
118 +** This function attempts to load the ICU data tables from a DLL.
119 +** Returns 0 on failure, nonzero on success.
120 +** This a hack job of icu_utils.cc:Initialize(). It's Chrome-specific code.
123 +#define ICU_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
124 +int sqlite_shell_init_icu() {
129 + // Chrome dropped U_ICU_VERSION_SHORT from the icu data dll name.
130 + module = LoadLibrary(L"icudt.dll");
134 + addr = GetProcAddress(module, ICU_DATA_SYMBOL);
138 + err = U_ZERO_ERROR;
139 + udata_setCommonData(addr, &err);