Reland "Migrate init.environ.rc module to Soong"

Makefile module still remains to handle post install commands. We're
going to move that to somewhere else as a followup.

Relanding with a fix for hwasan build.
HWADDRESS_SANITIZER_GLOBAL_OPTIONS is set by Soong's MakeVars, so we
can't refer the variable on soong config modules. Instead, as the
variable is hard-coded, this change hard-codes options in Android.bp.

Bug: 353429422
Test: boot cuttlefish w/ and w/o USE_SOONG_DEFINED_SYSTEM_IMAGE
Test: boot shiba with SANITIZE_TARGET=hwaddress
Change-Id: I4f61d7f800e0563c49dec0151d4a648ce9814bc9
diff --git a/rootdir/Android.bp b/rootdir/Android.bp
index 7105ed5..bbc5053 100644
--- a/rootdir/Android.bp
+++ b/rootdir/Android.bp
@@ -134,3 +134,58 @@
 sanitizer_libraries_txt {
     name: "sanitizer.libraries.txt",
 }
+
+EXPORT_GLOBAL_ASAN_OPTIONS = select(soong_config_variable("ANDROID", "ASAN_ENABLED"), {
+    true: "export ASAN_OPTIONS include=/system/asan.options",
+    default: "",
+})
+
+EXPORT_GLOBAL_HWASAN_OPTIONS = select(soong_config_variable("ANDROID", "HWASAN_ENABLED"), {
+    true: "export HWASAN_OPTIONS heap_history_size=1023,stack_history_size=512,export_memory_stats=0,max_malloc_fill_size=131072,malloc_fill_byte=0",
+    default: "",
+})
+
+EXPORT_GLOBAL_GCOV_OPTIONS = select(soong_config_variable("ANDROID", "GCOV_COVERAGE"), {
+    true: "export GCOV_PREFIX /data/misc/trace",
+    default: "",
+})
+
+EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS = select((soong_config_variable("ANDROID", "CLANG_COVERAGE"), soong_config_variable("ANDROID", "CLANG_COVERAGE_CONTINUOUS_MODE")), {
+    (true, true): "export LLVM_PROFILE_FILE /data/misc/trace/clang%c-%20m.profraw",
+    (true, default): "export LLVM_PROFILE_FILE /data/misc/trace/clang-%20m.profraw",
+    (default, default): "",
+})
+
+EXPORT_GLOBAL_SCUDO_ALLOCATION_RING_BUFFER_SIZE = select(soong_config_variable("ANDROID", "SCUDO_ALLOCATION_RING_BUFFER_SIZE"), {
+    "": "",
+    any @ size: "export SCUDO_ALLOCATION_RING_BUFFER_SIZE " + size,
+    default: "",
+})
+
+genrule {
+    name: "init.environ.rc.gen",
+    srcs: ["init.environ.rc.in"],
+    out: ["init.environ.rc"],
+    cmd: "cp -f $(in) $(out) && " +
+        "sed -i -e 's?%EXPORT_GLOBAL_ASAN_OPTIONS%?" + EXPORT_GLOBAL_ASAN_OPTIONS + "?g' $(out) && " +
+        "sed -i -e 's?%EXPORT_GLOBAL_GCOV_OPTIONS%?" + EXPORT_GLOBAL_GCOV_OPTIONS + "?g' $(out) && " +
+        "sed -i -e 's?%EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS%?" + EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS + "?g' $(out) && " +
+        "sed -i -e 's?%EXPORT_GLOBAL_HWASAN_OPTIONS%?" + EXPORT_GLOBAL_HWASAN_OPTIONS + "?g' $(out) && " +
+        "sed -i -e 's?%EXPORT_GLOBAL_SCUDO_ALLOCATION_RING_BUFFER_SIZE%?" + EXPORT_GLOBAL_SCUDO_ALLOCATION_RING_BUFFER_SIZE + "?g' $(out)",
+}
+
+prebuilt_root {
+    name: "init.environ.rc-soong",
+    src: ":init.environ.rc.gen",
+    filename: "init.environ.rc",
+    install_in_root: true,
+    no_full_install: true,
+    required: select((soong_config_variable("ANDROID", "ASAN_ENABLED"), soong_config_variable("ANDROID", "SANITIZE_TARGET_SYSTEM_ENABLED")), {
+        (true, true): [
+            "asan.options",
+            "asan_extract",
+        ],
+        (true, default): ["asan.options"],
+        (default, default): [],
+    }),
+}