Fix asan crashes due to stack overflow

In debug mode, use -O1 for the validation layers.

MA-553 #comment

Change-Id: Ib010aab2b589b8b2a2c60d7cd1c578708a2889bb
diff --git a/layers/BUILD.gn b/layers/BUILD.gn
index 6dce979..82fe9a5 100644
--- a/layers/BUILD.gn
+++ b/layers/BUILD.gn
@@ -22,6 +22,18 @@
 import("//build/package.gni")
 import("//third_party/vulkan_loader_and_validation_layers/layers/layers.gni")
 
+# Some targets take over a minute to compile with -O2 or -O3 optimization
+# levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
+# drops the optimization level down to -O1, which is still slow but
+# bearable.
+# TODO(TC-298): Remove once the pathological compiler behavior is fixed.
+# We also need -O > 0 for debug to prevent stack overflows with static
+# constructors when asan is enabled.
+config("validation_layer_optimization") {
+  visibility = [ ":*" ]
+  cflags = [ "-O1" ]
+}
+
 package("vulkan_loader_and_layers_tests") {
   testonly = true
 
@@ -74,9 +86,7 @@
 # This source_set doesn't include the validation layer strings by default, though including some
 # files (e.g. vk_layer_logging.h or vk_layer_utils.h) might still bring them in to a target.
 source_set("micro_layer_common") {
-  public_configs = [
-    ":layer_include_config",
-  ]
+  public_configs = [ ":layer_include_config" ]
   sources = [
     "vk_layer_extension_utils.cpp",
     "vk_layer_table.cpp",
@@ -88,6 +98,7 @@
 }
 
 source_set("layer_common") {
+  visibility = [ ":*" ]
   public_configs = [
     ":layer_include_config",
     ":static_stdlib",
@@ -102,20 +113,20 @@
     "../:extra_vulkan_headers",
     "../:vulkan",
   ]
+  configs += [ ":validation_layer_optimization" ]
+}
 
-  if (!is_debug) {
-    # This target takes over a minute to compile with -O2 or -O3 optimization
-    # levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
-    # drops the optimization level down to -O1, which is still slow but
-    # bearable.
-    # TODO(TC-298): Remove once the pathological compiler behavior is fixed.
-    configs -= [ "//build/config:release" ]
-    cflags = [ "-O1" ]
-    ldflags = cflags
+template("validation_layer") {
+  loadable_module(target_name) {
+    forward_variables_from(invoker, "*", [ "configs" ])
+    if (defined(invoker.configs)) {
+      configs += invoker.configs
+    }
+    configs += [ ":validation_layer_optimization" ]
   }
 }
 
-loadable_module("libVkLayer_core_validation") {
+validation_layer("libVkLayer_core_validation") {
   cflags_cc = [ "-Wno-missing-field-initializers" ]
   include_dirs = [
     "//third_party/shaderc/third_party/spirv-tools/include",
@@ -134,25 +145,14 @@
     "//third_party/shaderc/third_party/glslang",
     "//third_party/shaderc/third_party/spirv-tools",
   ]
-
-  if (!is_debug) {
-    # This target takes over a minute to compile with -O2 or -O3 optimization
-    # levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
-    # drops the optimization level down to -O1, which is still slow but
-    # bearable.
-    # TODO(TC-298): Remove once the pathological compiler behavior is fixed.
-    configs -= [ "//build/config:release" ]
-    cflags = [ "-O1" ]
-    ldflags = cflags
-  }
 }
 
 config("VkLayer_parameter_validation_config") {
   cflags_cc = [ "-Wno-unused-const-variable" ]
 }
 
-loadable_module("libVkLayer_parameter_validation") {
-  configs += [ ":VkLayer_parameter_validation_config" ]
+validation_layer("libVkLayer_parameter_validation") {
+  configs = [ ":VkLayer_parameter_validation_config" ]
   sources = [
     "../build-fuchsia/generated/include/parameter_validation.cpp",
     "parameter_validation_utils.cpp",
@@ -161,60 +161,27 @@
   deps = [
     ":layer_common",
   ]
-
-  if (!is_debug) {
-    # This target takes over a minute to compile with -O2 or -O3 optimization
-    # levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
-    # drops the optimization level down to -O1, which is still slow but
-    # bearable.
-    # TODO(TC-298): Remove once the pathological compiler behavior is fixed.
-    configs -= [ "//build/config:release" ]
-    cflags = [ "-O1" ]
-    ldflags = cflags
-  }
 }
 
-loadable_module("libVkLayer_threading") {
+validation_layer("libVkLayer_threading") {
   sources = [
     "threading.cpp",
   ]
   deps = [
     ":layer_common",
   ]
-
-  if (!is_debug) {
-    # This target takes over a minute to compile with -O2 or -O3 optimization
-    # levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
-    # drops the optimization level down to -O1, which is still slow but
-    # bearable.
-    # TODO(TC-298): Remove once the pathological compiler behavior is fixed.
-    configs -= [ "//build/config:release" ]
-    cflags = [ "-O1" ]
-    ldflags = cflags
-  }
 }
 
-loadable_module("libVkLayer_unique_objects") {
+validation_layer("libVkLayer_unique_objects") {
   sources = [
     "unique_objects.cpp",
   ]
   deps = [
     ":layer_common",
   ]
-
-  if (!is_debug) {
-    # This target takes over a minute to compile with -O2 or -O3 optimization
-    # levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
-    # drops the optimization level down to -O1, which is still slow but
-    # bearable.
-    # TODO(TC-298): Remove once the pathological compiler behavior is fixed.
-    configs -= [ "//build/config:release" ]
-    cflags = [ "-O1" ]
-    ldflags = cflags
-  }
 }
 
-loadable_module("libVkLayer_object_tracker") {
+validation_layer("libVkLayer_object_tracker") {
   sources = [
     "../build-fuchsia/generated/include/object_tracker.cpp",
     "object_tracker_utils.cpp",
@@ -222,15 +189,4 @@
   deps = [
     ":layer_common",
   ]
-
-  if (!is_debug) {
-    # This target takes over a minute to compile with -O2 or -O3 optimization
-    # levels on an unloaded Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz. This
-    # drops the optimization level down to -O1, which is still slow but
-    # bearable.
-    # TODO(TC-298): Remove once the pathological compiler behavior is fixed.
-    configs -= [ "//build/config:release" ]
-    cflags = [ "-O1" ]
-    ldflags = cflags
-  }
 }