Generate bindings rather than using prebuilts

In order to pass some of the configuration that Android.bp has, we need
to pass in a function. Now that bindgen-as-a-library support has landed
in the build system, use it for libsqlite3-sys to avoid needing to
regenerate bindings.

Bug: 161816141
Test: mma
Change-Id: I53d4108f5e6ab0d6e47e8d6d1b24a93b1c66a7fe
diff --git a/Android.bp b/Android.bp
index ed10b09..a1da725 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,17 +1,17 @@
 // This file is generated by cargo2android.py --device --run --dependencies --test.
 
 // We renamed the shared_libs dependence from libsqlite3 to libsqlite.
+// srcs has been moved out of _defaults to work around b/163420435
 
 rust_library {
     name: "liblibsqlite3_sys",
     host_supported: true,
     crate_name: "libsqlite3_sys",
-    srcs: ["src/lib.rs"],
+    srcs: [
+        "src/lib.rs",
+        ":libsqlite3_bindgen",
+    ],
     edition: "2018",
-    // Note that the following features are default for this package,
-    // unrelated to the generation of bindings for external/sqlite.
-    // We might need to consider the non-default features for Android.
-    // If you add/change features, please check any impact to bindgen flags.
     features: [
         "default",
         "min_sqlite_version_3_6_8",
@@ -21,28 +21,11 @@
     shared_libs: [
         "libsqlite",
     ],
-    arch: {
-        // depend on external/sqlite/rust bindgen modules
-        // and include the output file from those modules
-        x86: {
-           srcs: [":sqlite3_bindgen_x86"],
-        },
-        x86_64: {
-           srcs: [":sqlite3_bindgen_x86_64"],
-        },
-        arm: {
-           srcs: [":sqlite3_bindgen_arm"],
-        },
-        arm64: {
-           srcs: [":sqlite3_bindgen_arm64"],
-        },
-    },
 }
 
 rust_defaults {
     name: "libsqlite3-sys_defaults",
     crate_name: "libsqlite3_sys",
-    srcs: ["src/lib.rs"],
     test_suites: ["general-tests"],
     auto_gen_config: true,
     edition: "2018",
@@ -55,146 +38,67 @@
     shared_libs: [
         "libsqlite",
     ],
-    arch: {
-        // depend on external/sqlite/rust bindgen modules
-        // and include the output file from those modules
-        x86: {
-           srcs: [":sqlite3_bindgen_x86"],
-        },
-        x86_64: {
-           srcs: [":sqlite3_bindgen_x86_64"],
-        },
-        arm: {
-           srcs: [":sqlite3_bindgen_arm"],
-        },
-        arm64: {
-           srcs: [":sqlite3_bindgen_arm64"],
-        },
-    },
 }
 
 rust_test_host {
     name: "libsqlite3-sys_host_test_src_lib",
+    srcs: [
+        "src/lib.rs",
+        ":libsqlite3_bindgen",
+    ],
     defaults: ["libsqlite3-sys_defaults"],
 }
 
 rust_test {
     name: "libsqlite3-sys_device_test_src_lib",
+    srcs: [
+        "src/lib.rs",
+        ":libsqlite3_bindgen",
+    ],
     defaults: ["libsqlite3-sys_defaults"],
 }
 
 // dependent_library ["feature_list"]
 //   pkg-config-0.3.18
 
-// Extra modules to generate bindings for external/sqlite.
-// Generated file is called bindgen.rs and saved into android/<arch>/bindings.rs.
-
-common_bindgen_flags = " -o $(out) $(in) -- -MD -MF $(depfile)"
-
-// Here is the place to add bindgen flags for sqlite3.
-// The following ignore options are copied from build.rs.
-sqlite3_bindgen_flags =
-      " --blacklist-function sqlite3_vmprintf"
-    + " --blacklist-function sqlite3_vsnprintf"
-    + " --blacklist-function sqlite3_str_vappendf"
-    + " --blacklist-type va_list"
-    + " --blacklist-type __builtin_va_list"
-    + " --blacklist-type __gnuc_va_list"
-    + " --blacklist-type __va_list_tag"
-    + " --blacklist-item __GNUC_VA_LIST"
-
-// The cflags defined in cc_defaults of external/sqlite/dist/Android.bp
-// should be passed to clang for bindgen.
-// The list of strings used in cflags cannot be easily
-// shared with the command string used in genrule yet,
-// since there is no string join function in .bp.
-sqlite3_bindgen_clang_flags = " -I external/sqlite/dist"
-    + " -DNDEBUG=1"
-    + " -DHAVE_USLEEP=1"
-    + " -DSQLITE_HAVE_ISNAN"
-    + " -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576"
-    + " -DSQLITE_THREADSAFE=2"
-    + " -DSQLITE_TEMP_STORE=3"
-    + " -DSQLITE_POWERSAFE_OVERWRITE=1"
-    + " -DSQLITE_DEFAULT_FILE_FORMAT=4"
-    + " -DSQLITE_DEFAULT_AUTOVACUUM=1"
-    + " -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1"
-    + " -DSQLITE_ENABLE_FTS3"
-    + " -DSQLITE_ENABLE_FTS3_BACKWARDS"
-    + " -DSQLITE_ENABLE_FTS4"
-    + " -DSQLITE_OMIT_BUILTIN_TEST"
-    + " -DSQLITE_OMIT_COMPILEOPTION_DIAGS"
-    + " -DSQLITE_OMIT_LOAD_EXTENSION"
-    + " -DSQLITE_DEFAULT_FILE_PERMISSIONS=0600"
-    + " -DSQLITE_SECURE_DELETE"
-    + " -DSQLITE_ENABLE_BATCH_ATOMIC_WRITE"
-    + " -DBIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD"
-    + " -DHAVE_POSIX_FALLOCATE=1"
+// Keep this array in sync with the cflags used in external/sqlite
+// After b/163598610 is resolved, use defaults instead.
+sqlite3_bindgen_clang_flags = [
+    " -DNDEBUG=1",
+    " -DHAVE_USLEEP=1",
+    " -DSQLITE_HAVE_ISNAN",
+    " -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576",
+    " -DSQLITE_THREADSAFE=2",
+    " -DSQLITE_TEMP_STORE=3",
+    " -DSQLITE_POWERSAFE_OVERWRITE=1",
+    " -DSQLITE_DEFAULT_FILE_FORMAT=4",
+    " -DSQLITE_DEFAULT_AUTOVACUUM=1",
+    " -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1",
+    " -DSQLITE_ENABLE_FTS3",
+    " -DSQLITE_ENABLE_FTS3_BACKWARDS",
+    " -DSQLITE_ENABLE_FTS4",
+    " -DSQLITE_OMIT_BUILTIN_TEST",
+    " -DSQLITE_OMIT_COMPILEOPTION_DIAGS",
+    " -DSQLITE_OMIT_LOAD_EXTENSION",
+    " -DSQLITE_DEFAULT_FILE_PERMISSIONS=0600",
+    " -DSQLITE_SECURE_DELETE",
+    " -DSQLITE_ENABLE_BATCH_ATOMIC_WRITE",
+    " -DBIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD",
+    " -DHAVE_POSIX_FALLOCATE=1",
     // extra -D flags for android targets
-    + " -DUSE_PREAD64"
-    + " -Dfdatasync=fdatasync"
-    + " -DHAVE_MALLOC_H=1"
-    + " -DHAVE_MALLOC_USABLE_SIZE"
-    + " -Wno-unused-parameter"
+    " -DUSE_PREAD64",
+    " -Dfdatasync=fdatasync",
+    " -DHAVE_MALLOC_H=1",
+    " -DHAVE_MALLOC_USABLE_SIZE",
+    " -Wno-unused-parameter",
+]
 
-genrule_defaults {
-    name: "sqlite3_bindgen_defaults",
-    tools: ["bindgen.sh", "bindgen"],
-    depfile: true,
-    srcs: ["android/bindgen.h"],
-    out: ["bindgen.rs"],
-}
-
-// This is a temporary hack to fix bindgen output files,
-// until a newer bindgen can do it with a flag.
-// The default int type for macros are changed
-// from u32 to i32 to work with other existing Rust.
-// Both u32 and i32 values should work with C libsqlite
-// as those macros have values in both i32 and u32 range.
-// See comments in later genrule modules about diff/patch
-// to detect and fix errors.
-bindgen_post_edit_cmd = " sed -i 's/: u32 /: i32 /' $(out) &&"
-
-genrule_default_cmd = "$(location bindgen.sh) $(location bindgen)"
-    + sqlite3_bindgen_flags
-    + common_bindgen_flags
-    + sqlite3_bindgen_clang_flags
-
-// In these genrule modules, diff -U3 is used to check if the
-// generated files are the same as the saved version.
-// This also catches any change made by bindgen_post_edit_cmd.
-// Any sed command failure or output difference will stop the genrule module.
-// To fix and resume the build, the diff -U3 output in stdout should
-// be saved and passed to the 'patch' command to fix the bindings.rs file.
-// The changes to bindgings.rs should be manually verified and tested.
-genrule {
-    name: "sqlite3_bindgen_x86",
-    defaults: ["sqlite3_bindgen_defaults"],
-    cmd: genrule_default_cmd + " --target=i386 &&"
-    + bindgen_post_edit_cmd
-    + " diff -U3 `dirname $(in)`/x86/bindings.rs $(out)",
-}
-
-genrule {
-    name: "sqlite3_bindgen_x86_64",
-    defaults: ["sqlite3_bindgen_defaults"],
-    cmd: genrule_default_cmd + " --target=x86_64 &&"
-    + bindgen_post_edit_cmd
-    + " diff -U3 `dirname $(in)`/x86_64/bindings.rs $(out)",
-}
-
-genrule {
-    name: "sqlite3_bindgen_arm",
-    defaults: ["sqlite3_bindgen_defaults"],
-    cmd: genrule_default_cmd + " --target=arm &&"
-    + bindgen_post_edit_cmd
-    + " diff -U3 `dirname $(in)`/arm/bindings.rs $(out)",
-}
-
-genrule {
-    name: "sqlite3_bindgen_arm64",
-    defaults: ["sqlite3_bindgen_defaults"],
-    cmd: genrule_default_cmd + " --target=arm64 &&"
-    + bindgen_post_edit_cmd
-    + " diff -U3 `dirname $(in)`/arm64/bindings.rs $(out)",
+rust_bindgen {
+    name: "libsqlite3_bindgen",
+    host_supported: true,
+    crate_name: "sqlite3_bindgen",
+    custom_bindgen: "sqlite3_bindgen_build",
+    wrapper_src: "wrapper.h",
+    source_stem: "bindgen",
+    cflags: ["-I external/sqlite/dist"] + sqlite3_bindgen_clang_flags,
 }