SQL: Compile SQLite with the built-in corruption recovery module

See https://www.sqlite.org/recovery.html

These build flags will be used to generate amalgamations for the next
release: https://crrev.com/c/4279142

Bug: 1385500, 1418203
Change-Id: I97326c290771c42658eb35435931c9d61f2ac68f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4278567
Commit-Queue: Austin Sullivan <asully@chromium.org>
Reviewed-by: Nathan Memmott <memmott@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1108580}
NOKEYCHECK=True
GitOrigin-RevId: 8e3853c50eea524d4c5bb5bebf9b5d413f1234cb
diff --git a/scripts/generate_amalgamation.py b/scripts/generate_amalgamation.py
index 495e34b..e4fb838 100755
--- a/scripts/generate_amalgamation.py
+++ b/scripts/generate_amalgamation.py
@@ -184,7 +184,11 @@
         os.chdir(_TEMP_CONFIG_DIR)
         _do_configure(config_name)
 
-        cmd = ['make', 'shell.c', 'sqlite3.h', 'sqlite3.c']
+        # Chromium compiles 'sqlite3r.c' and 'sqlite3r.h' to use the built-in
+        # corruption recovery module. These files are then mapped to the standard
+        # 'sqlite3.c' and 'sqlite3.h' files below. This mapping is required if
+        # the "SQLITE_HAVE_SQLITE3R" configuration option is specified.
+        cmd = ['make', 'shell.c', 'sqlite3r.h', 'sqlite3r.c']
         subprocess.check_call(cmd)
 
         amalgamation_dir = get_amalgamation_dir(config_name)
@@ -197,12 +201,10 @@
                                       'README_amalgamation.md')
             copyfile(readme_src, readme_dst)
 
-        copyfile(
-            os.path.join(_TEMP_CONFIG_DIR, 'sqlite3.c'),
-            os.path.join(amalgamation_dir, 'sqlite3.c'))
-        copyfile(
-            os.path.join(_TEMP_CONFIG_DIR, 'sqlite3.h'),
-            os.path.join(amalgamation_dir, 'sqlite3.h'))
+        copyfile(os.path.join(_TEMP_CONFIG_DIR, 'sqlite3r.c'),
+                 os.path.join(amalgamation_dir, 'sqlite3.c'))
+        copyfile(os.path.join(_TEMP_CONFIG_DIR, 'sqlite3r.h'),
+                 os.path.join(amalgamation_dir, 'sqlite3.h'))
 
         # shell.c must be placed in a different directory from sqlite3.h,
         # because it contains an '#include "sqlite3.h"' that we want to resolve
diff --git a/sqlite_common_configuration_flags.gni b/sqlite_common_configuration_flags.gni
index df9552f..c0f903b 100644
--- a/sqlite_common_configuration_flags.gni
+++ b/sqlite_common_configuration_flags.gni
@@ -86,4 +86,14 @@
 
   # Uses isnan() in the C99 standard library.
   "SQLITE_HAVE_ISNAN",
+
+  # Chrome uses SQLite's built-in corruption recovery module.
+  # See https://www.sqlite.org/recovery.html
+  # Use of this module requires compiling a separate "sqlite3r.c" target, which
+  # generates "sqlite3r.c" and "sqlite3r.h", versions of the amalgamation files
+  # that include the recover extension. We then map this back to "sqlite3.h"
+  # when generating amalgamations to hide this detail from the rest of the
+  # platform. If this changes, or if we ever remove this flag, we would need to
+  # update the mapping in scripts/generate_amalgamation.py.
+  "SQLITE_HAVE_SQLITE3R",
 ]