-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImporter.SQLite.pas
More file actions
274 lines (250 loc) · 12.4 KB
/
Copy pathImporter.SQLite.pas
File metadata and controls
274 lines (250 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
namespace RemObjects.Elements.HI2;
uses
RemObjects.Elements.RTL,
System.Text.RegularExpressions;
type
Importer = public partial class
private
method ResolveSQLiteSourceFolder: not nullable String;
begin
var lRoot := RequireIslandSDKFolder(SQLiteSourceFolder, "SQLite amalgamation folder");
if Path.Combine(lRoot, "sqlite3.c").FileExists and Path.Combine(lRoot, "sqlite3.h").FileExists then
exit lRoot;
var lCandidates := Folder.GetSubfolders(lRoot)
.Where(aFolder -> Path.Combine(aFolder, "sqlite3.c").FileExists and Path.Combine(aFolder, "sqlite3.h").FileExists)
.ToList;
if lCandidates.Count <> 1 then
raise new Exception($"SQLite amalgamation files sqlite3.c and sqlite3.h were not found directly below '{lRoot}' or one unambiguous child folder.");
result := lCandidates[0] as not nullable;
end;
method SQLiteVersion(aSQLiteFolder: not nullable String): not nullable String;
begin
var lHeader := RequireIslandSDKFile(Path.Combine(aSQLiteFolder, "sqlite3.h"), "SQLite amalgamation header");
var lMatch := Regex.Match(File.ReadText(lHeader), '#define\s+SQLITE_VERSION\s+"([^"]+)"');
if not lMatch.Success then
raise new Exception($"SQLITE_VERSION was not found in '{lHeader}'.");
result := lMatch.Groups[1].Value as not nullable;
end;
method ResolveSQLiteClangCL: not nullable String;
begin
var lPath := SQLiteClang;
if length(lPath) > 0 then begin
lPath := Path.GetFullPath(lPath as not nullable);
if lPath.FolderExists then
lPath := Path.Combine(lPath, "clang-cl");
exit RequireIslandSDKFile(lPath, "Windows SQLite clang-cl executable");
end;
for each lCandidate in ["/opt/homebrew/opt/llvm/bin/clang-cl", "/usr/local/opt/llvm/bin/clang-cl"] do
if lCandidate.FileExists then
exit lCandidate as not nullable;
raise new Exception("Windows SQLite clang-cl was not found. Pass --clang=<absolute-path-or-LLVM-bin-folder>.");
end;
method ResolveSQLiteLLVMAr(aClangCL: not nullable String): not nullable String;
begin
var lPath := SQLiteLLVMAr;
if length(lPath) > 0 then begin
lPath := Path.GetFullPath(lPath as not nullable);
if lPath.FolderExists then
lPath := Path.Combine(lPath, "llvm-ar");
exit RequireIslandSDKFile(lPath, "Windows SQLite llvm-ar executable");
end;
var lSibling := Path.Combine(Path.GetParentDirectory(aClangCL), "llvm-ar");
if lSibling.FileExists then
exit lSibling as not nullable;
for each lCandidate in ["/opt/homebrew/opt/llvm/bin/llvm-ar", "/usr/local/opt/llvm/bin/llvm-ar"] do
if lCandidate.FileExists then
exit lCandidate as not nullable;
raise new Exception("Windows SQLite llvm-ar was not found beside clang-cl. Pass --llvm-ar=<absolute-path-or-LLVM-bin-folder>.");
end;
method ValidateWindowsCOFFObject(aPath: not nullable String;
aArchitecture: not nullable WindowsSDKArchitecture);
begin
RequireIslandSDKFile(aPath, $"Windows {aArchitecture.Name} SQLite COFF object");
using lStream := new System.IO.FileStream(aPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read) do begin
var lLow := lStream.ReadByte;
var lHigh := lStream.ReadByte;
if (lLow < 0) or (lHigh < 0) then
raise new Exception($"Windows {aArchitecture.Name} SQLite COFF object '{aPath}' is truncated.");
var lMachine := UInt16(lLow or (lHigh shl 8));
if lMachine <> aArchitecture.COFFMachine then
raise new Exception($"Windows {aArchitecture.Name} SQLite COFF object '{aPath}' has machine 0x{lMachine.ToString('x4')}, expected 0x{aArchitecture.COFFMachine.ToString('x4')}.");
end;
end;
method WindowsSQLiteShimSource: not nullable String;
begin
result := String.Join(Environment.LineBreak, new List<String>(
"typedef __SIZE_TYPE__ sqlite3_island_size_t;",
"",
"void *sqlite3_island_memchr(const void *aBuffer, int aValue, sqlite3_island_size_t aLength)",
"{",
" const unsigned char *lBuffer = (const unsigned char *)aBuffer;",
" unsigned char lValue = (unsigned char)aValue;",
" for (sqlite3_island_size_t i = 0; i < aLength; ++i)",
" if (lBuffer[i] == lValue)",
" return (void *)(lBuffer+i);",
" return (void *)0;",
"}",
"",
"sqlite3_island_size_t sqlite3_island_strspn(const char *aString, const char *aAccepted)",
"{",
" sqlite3_island_size_t lLength = 0;",
" for (; aString[lLength] != 0; ++lLength) {",
" const char *lCandidate = aAccepted;",
" while ((*lCandidate != 0) && (*lCandidate != aString[lLength]))",
" ++lCandidate;",
" if (*lCandidate == 0)",
" break;",
" }",
" return lLength;",
"}",
"",
"sqlite3_island_size_t sqlite3_island_strcspn(const char *aString, const char *aRejected)",
"{",
" sqlite3_island_size_t lLength = 0;",
" for (; aString[lLength] != 0; ++lLength) {",
" const char *lCandidate = aRejected;",
" while ((*lCandidate != 0) && (*lCandidate != aString[lLength]))",
" ++lCandidate;",
" if (*lCandidate != 0)",
" break;",
" }",
" return lLength;",
"}",
""
));
end;
method BuildWindowsSQLiteLibrary(aArchitecture: not nullable WindowsSDKArchitecture;
aSQLiteFolder: not nullable String;
aSQLiteVersion: not nullable String;
aClangCL: not nullable String;
aLLVMAr: not nullable String;
aIncludeFolders: not nullable ImmutableList<String>;
aIntermediateFolder: not nullable String;
aOutputFolder: not nullable String);
begin
var lBuildFolder := Path.Combine(aIntermediateFolder, "Windows", aSQLiteVersion, aArchitecture.Name);
Folder.Create(lBuildFolder);
var lObject := Path.Combine(lBuildFolder, "sqlite3.obj");
var lShimSource := Path.Combine(lBuildFolder, "sqlite3_island_shims.c");
var lShimObject := Path.Combine(lBuildFolder, "sqlite3_island_shims.obj");
var lLibrary := Path.Combine(aOutputFolder, "sqlite3.lib");
if lObject.FileExists then
File.Delete(lObject);
if lShimObject.FileExists then
File.Delete(lShimObject);
if lLibrary.FileExists then
File.Delete(lLibrary);
File.WriteText(lShimSource, WindowsSQLiteShimSource);
var lCompilerArguments := new List<String>(
$"--target={aArchitecture.TargetString}",
"/nologo",
"/c",
"/Fo"+lObject,
"/O2",
"/Brepro",
"/Zl",
"/DNDEBUG",
"/DSQLITE_THREADSAFE=1",
"/DSQLITE_OMIT_SEH",
"/D_WIN32_WINNT=0x0A00",
"/Dmemchr=sqlite3_island_memchr",
"/Dstrspn=sqlite3_island_strspn",
"/Dstrcspn=sqlite3_island_strcspn",
"-ffile-reproducible",
$"/clang:-ffile-prefix-map={aSQLiteFolder}=.",
"/clang:-fno-builtin-memchr",
"-fms-compatibility-version=19.44"
);
for each lIncludeFolder in aIncludeFolders do
lCompilerArguments.Add("-imsvc", lIncludeFolder);
lCompilerArguments.Add("--", Path.Combine(aSQLiteFolder, "sqlite3.c"));
Log($"Building SQLite {aSQLiteVersion} for Windows {aArchitecture.Name}.");
RunIslandSDKTool(aClangCL, lCompilerArguments, aSQLiteFolder);
ValidateWindowsCOFFObject(lObject, aArchitecture);
var lShimArguments := new List<String>(
$"--target={aArchitecture.TargetString}",
"/nologo",
"/c",
"/Fo"+lShimObject,
"/O2",
"/Brepro",
"/Zl",
"/GS-",
"-ffile-reproducible",
$"/clang:-ffile-prefix-map={lBuildFolder}=.",
"/clang:-fno-builtin"
);
lShimArguments.Add("--", lShimSource);
RunIslandSDKTool(aClangCL, lShimArguments, lBuildFolder);
ValidateWindowsCOFFObject(lShimObject, aArchitecture);
RunIslandSDKTool(aLLVMAr, new List<String>("rcsD", lLibrary, lObject, lShimObject), lBuildFolder);
RequireIslandSDKFile(lLibrary, $"Windows {aArchitecture.Name} SQLite static library");
end;
public
property SQLiteSourceFolder: nullable String;
property SQLiteOutputFolder: nullable String;
property SQLiteWindowsDeclarationsFolder: nullable String;
property SQLiteIntermediateFolder: nullable String;
property SQLiteClang: nullable String;
property SQLiteLLVMAr: nullable String;
method BuildSQLitePackage;
begin
SQLiteOutputFolder := Path.GetFullPath(SQLiteOutputFolder);
SQLiteIntermediateFolder := if length(SQLiteIntermediateFolder) > 0 then Path.GetFullPath(SQLiteIntermediateFolder) else Path.Combine(SQLiteOutputFolder, "Import Configurations", "SQLite");
Folder.Create(SQLiteOutputFolder);
Folder.Create(SQLiteIntermediateFolder);
var lSQLiteFolder := ResolveSQLiteSourceFolder;
var lSQLiteVersion := SQLiteVersion(lSQLiteFolder);
var lClangCL := ResolveSQLiteClangCL;
var lLLVMAr := ResolveSQLiteLLVMAr(lClangCL);
var lLayout := ResolveWindowsSDKLayout;
var lMSVCIncludeFolder := ResolveWindowsMSVCIncludeFolder;
var lIncludeFolders := WindowsIncludeFolders(lLayout, lMSVCIncludeFolder, nil);
var lArchitectures := ResolveWindowsArchitectures;
var lDeclarationsFolder := RequireIslandSDKFolder(SQLiteWindowsDeclarationsFolder, "Windows SQLite declaration .fx folder");
var lDeclarationStage := Path.Combine(SQLiteIntermediateFolder, "Declarations", "Windows");
if lDeclarationStage.FolderExists then
System.IO.Directory.Delete(lDeclarationStage, true);
Folder.Create(lDeclarationStage);
for each lArchitecture in lArchitectures do begin
var lStagedDeclaration := Path.Combine(lDeclarationStage, lArchitecture.Name, "sqlite3.fx");
CopyIslandSDKFile(Path.Combine(lDeclarationsFolder, lArchitecture.Name, "sqlite3.fx"), lStagedDeclaration);
ValidateWindowsFx(lStagedDeclaration, "sqlite3", lArchitecture);
end;
var lPackageFolder := Path.Combine(SQLiteOutputFolder, "SQLite");
var lIslandFolder := Path.Combine(lPackageFolder, "Island");
var lWindowsFolder := Path.Combine(lIslandFolder, "Windows");
if lWindowsFolder.FolderExists then
System.IO.Directory.Delete(lWindowsFolder, true);
Folder.Create(lWindowsFolder);
Log($"Building standalone SQLite {lSQLiteVersion} package for Windows {String.Join(", ", lArchitectures.Select(aArchitecture -> aArchitecture.Name).ToList)}.");
Log($"SQLite source SHA-256: {IslandSDKSHA256(Path.Combine(lSQLiteFolder, "sqlite3.c"))}");
for each lArchitecture in lArchitectures do begin
var lArchitectureFolder := Path.Combine(lWindowsFolder, lArchitecture.Name);
Folder.Create(lArchitectureFolder);
CopyIslandSDKFile(Path.Combine(lDeclarationStage, lArchitecture.Name, "sqlite3.fx"),
Path.Combine(lArchitectureFolder, "sqlite3.fx"));
ValidateWindowsFx(Path.Combine(lArchitectureFolder, "sqlite3.fx"), "sqlite3", lArchitecture);
BuildWindowsSQLiteLibrary(lArchitecture,
lSQLiteFolder,
lSQLiteVersion,
lClangCL,
lLLVMAr,
lIncludeFolders,
SQLiteIntermediateFolder as not nullable,
lArchitectureFolder);
end;
if CreateZips then begin
var lPublicFolder := Path.Combine(SQLiteOutputFolder, "__Public");
var lLegacyZip := Path.Combine(lPublicFolder, "SQLite.zip");
if lLegacyZip.FileExists then
File.Delete(lLegacyZip);
CreateIslandPlatformLibraryZip(SQLiteOutputFolder,
SQLiteIntermediateFolder,
"SQLite",
"Windows",
"Island-Windows-sqlite.zip");
end;
end;
end;
end.