Convert libart and dependencies to Android.bp

Re-landing I73839046a5a53eb34cd28eea53149911c568e411, with fixes for mac
build (only apply -Wl,--keep-unique to device x86 builds), typo in
checking for ART_HEAP_POISONING environment variable, and removing
-Wframe-larger-than for debug builds.

Test: mma -j, m -j test-art-host
Change-Id: If88492174cbcb0d9a8758176c006163a29eaaa63
diff --git a/build/Android.bp b/build/Android.bp
new file mode 100644
index 0000000..be7dafd
--- /dev/null
+++ b/build/Android.bp
@@ -0,0 +1,158 @@
+bootstrap_go_package {
+    name: "soong-art",
+    pkgPath: "android/soong/art",
+    deps: [
+        "blueprint",
+        "blueprint-pathtools",
+        "soong",
+        "soong-android",
+        "soong-cc",
+    ],
+    srcs: [
+        "art.go",
+        "codegen.go",
+        "makevars.go",
+    ],
+    pluginFor: ["soong_build"],
+}
+
+art_global_defaults {
+    // Additional flags are computed by art.go
+
+    name: "art_defaults",
+    clang: true,
+    cflags: [
+        "-O3",
+
+        // Base set of cflags used by all things ART.
+        "-fno-rtti",
+        "-ggdb3",
+        "-Wall",
+        "-Werror",
+        "-Wextra",
+        "-Wstrict-aliasing",
+        "-fstrict-aliasing",
+        "-Wunreachable-code",
+        "-Wredundant-decls",
+        "-Wshadow",
+        "-Wunused",
+        "-fvisibility=protected",
+
+        // Warn about thread safety violations with clang.
+        "-Wthread-safety",
+        "-Wthread-safety-negative",
+
+        // Warn if switch fallthroughs aren't annotated.
+        "-Wimplicit-fallthrough",
+
+        // Enable float equality warnings.
+        "-Wfloat-equal",
+
+        // Enable warning of converting ints to void*.
+        "-Wint-to-void-pointer-cast",
+
+        // Enable warning of wrong unused annotations.
+        "-Wused-but-marked-unused",
+
+        // Enable warning for deprecated language features.
+        "-Wdeprecated",
+
+        // Enable warning for unreachable break & return.
+        "-Wunreachable-code-break",
+        "-Wunreachable-code-return",
+
+        // Bug: http://b/29823425  Disable -Wconstant-conversion and
+        // -Wundefined-var-template for Clang update to r271374
+        "-Wno-constant-conversion",
+        "-Wno-undefined-var-template",
+
+        "-DART_STACK_OVERFLOW_GAP_arm=8192",
+        "-DART_STACK_OVERFLOW_GAP_arm64=8192",
+        "-DART_STACK_OVERFLOW_GAP_mips=16384",
+        "-DART_STACK_OVERFLOW_GAP_mips64=16384",
+        "-DART_STACK_OVERFLOW_GAP_x86=8192",
+        "-DART_STACK_OVERFLOW_GAP_x86_64=8192",
+    ],
+
+    target: {
+        android: {
+            cflags: [
+                "-DART_TARGET",
+
+                // Enable missing-noreturn only on non-Mac. As lots of things are not implemented
+                // for Apple, it's a pain.
+                "-Wmissing-noreturn",
+
+                // To use oprofile_android --callgraph, uncomment this and recompile with
+                //    mmma -j art
+                // "-fno-omit-frame-pointer",
+                // "-marm",
+                // "-mapcs",
+            ],
+            include_dirs: [
+                // We optimize Thread::Current() with a direct TLS access. This requires access to a
+                //  private Bionic header.
+                "bionic/libc/private",
+            ],
+        },
+        linux: {
+            cflags: [
+                // Enable missing-noreturn only on non-Mac. As lots of things are not implemented for
+                // Apple, it's a pain.
+                "-Wmissing-noreturn",
+            ],
+        },
+        host: {
+            cflags: [
+                // Bug: 15446488. We don't omit the frame pointer to work around
+                // clang/libunwind bugs that cause SEGVs in run-test-004-ThreadStress.
+                "-fno-omit-frame-pointer",
+            ],
+        },
+    },
+
+    codegen: {
+        arm: {
+            cflags: ["-DART_ENABLE_CODEGEN_arm"],
+        },
+        arm64: {
+            cflags: ["-DART_ENABLE_CODEGEN_arm64"],
+        },
+        mips: {
+            cflags: ["-DART_ENABLE_CODEGEN_mips"],
+        },
+        mips64: {
+            cflags: ["-DART_ENABLE_CODEGEN_mips64"],
+        },
+        x86: {
+            cflags: ["-DART_ENABLE_CODEGEN_x86"],
+        },
+        x86_64: {
+            cflags: ["-DART_ENABLE_CODEGEN_x86_64"],
+        },
+    },
+
+    include_dirs: [
+        "external/gtest/include",
+        "external/icu/icu4c/source/common",
+        "external/lz4/lib",
+        "external/valgrind/include",
+        "external/valgrind",
+        "external/vixl/src",
+        "external/zlib",
+    ],
+}
+
+cc_defaults {
+    name: "art_debug_defaults",
+    cflags: [
+        "-O2",
+        "-DDYNAMIC_ANNOTATIONS_ENABLED=1",
+        "-DVIXL_DEBUG",
+        "-UNDEBUG",
+        "-Wno-frame-larger-than=",
+    ],
+    asflags: [
+        "-UNDEBUG",
+    ],
+}
diff --git a/build/art.go b/build/art.go
new file mode 100644
index 0000000..4e64dcf
--- /dev/null
+++ b/build/art.go
@@ -0,0 +1,180 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package art
+
+import (
+	"android/soong"
+	"android/soong/android"
+	"android/soong/cc"
+	"fmt"
+
+	"github.com/google/blueprint"
+)
+
+var supportedArches = []string{"arm", "arm64", "mips", "mips64", "x86", "x86_64"}
+
+func globalFlags(ctx android.BaseContext) ([]string, []string) {
+	var cflags []string
+	var asflags []string
+
+	tlab := false
+
+	gcType := envDefault(ctx, "ART_DEFAULT_GC_TYPE", "CMS")
+
+	if envTrue(ctx, "ART_TEST_DEBUG_GC") {
+		gcType = "SS"
+		tlab = true
+	}
+
+	cflags = append(cflags, "-DART_DEFAULT_GC_TYPE_IS_"+gcType)
+	if tlab {
+		cflags = append(cflags, "-DART_USE_TLAB=1")
+	}
+
+	imtSize := envDefault(ctx, "ART_IMT_SIZE", "43")
+	cflags = append(cflags, "-DIMT_SIZE="+imtSize)
+
+	if envTrue(ctx, "ART_HEAP_POISONING") {
+		cflags = append(cflags, "-DART_HEAP_POISONING=1")
+		asflags = append(asflags, "-DART_HEAP_POISONING=1")
+	}
+
+	if envTrue(ctx, "ART_USE_READ_BARRIER") {
+		// Used to change the read barrier type. Valid values are BAKER, BROOKS, TABLELOOKUP.
+		// The default is BAKER.
+		barrierType := envDefault(ctx, "ART_READ_BARRIER_TYPE", "BAKER")
+		cflags = append(cflags,
+			"-DART_USE_READ_BARRIER=1",
+			"-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
+		asflags = append(asflags,
+			"-DART_USE_READ_BARRIER=1",
+			"-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
+
+		// Temporarily override -fstack-protector-strong with -fstack-protector to avoid a major
+		// slowdown with the read barrier config. b/26744236.
+		cflags = append(cflags, "-fstack-protector")
+	}
+
+	return cflags, asflags
+}
+
+func deviceFlags(ctx android.BaseContext) []string {
+	var cflags []string
+	deviceFrameSizeLimit := 1736
+	if len(ctx.AConfig().SanitizeDevice()) > 0 {
+		deviceFrameSizeLimit = 6400
+	}
+	cflags = append(cflags,
+		fmt.Sprintf("-Wframe-larger-than=%d", deviceFrameSizeLimit),
+		fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", deviceFrameSizeLimit),
+	)
+
+	cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgDeviceBaseAddress())
+	if envTrue(ctx, "ART_TARGET_LINUX") {
+		cflags = append(cflags, "-DART_TARGET_LINUX")
+	} else {
+		cflags = append(cflags, "-DART_TARGET_ANDROID")
+	}
+	minDelta := envDefault(ctx, "LIBART_IMG_TARGET_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
+	maxDelta := envDefault(ctx, "LIBART_IMG_TARGET_MAX_BASE_ADDRESS_DELTA", "0x1000000")
+	cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
+	cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
+
+	return cflags
+}
+
+func hostFlags(ctx android.BaseContext) []string {
+	var cflags []string
+	hostFrameSizeLimit := 1736
+	cflags = append(cflags,
+		fmt.Sprintf("-Wframe-larger-than=%d", hostFrameSizeLimit),
+		fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", hostFrameSizeLimit),
+	)
+
+	cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgHostBaseAddress())
+	minDelta := envDefault(ctx, "LIBART_IMG_HOST_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
+	maxDelta := envDefault(ctx, "LIBART_IMG_HOST_MAX_BASE_ADDRESS_DELTA", "0x1000000")
+	cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
+	cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
+
+	return cflags
+}
+
+func (a *artGlobalDefaults) CustomizeProperties(ctx android.CustomizePropertiesContext) {
+	type props struct {
+		Target struct {
+			Android struct {
+				Cflags []string
+			}
+			Host struct {
+				Cflags []string
+			}
+		}
+		Cflags  []string
+		Asflags []string
+	}
+
+	p := &props{}
+	p.Cflags, p.Asflags = globalFlags(ctx)
+	p.Target.Android.Cflags = deviceFlags(ctx)
+	p.Target.Host.Cflags = hostFlags(ctx)
+	ctx.AppendProperties(p)
+}
+
+type artGlobalDefaults struct{}
+
+func init() {
+	soong.RegisterModuleType("art_cc_library", artLibrary)
+	soong.RegisterModuleType("art_cc_defaults", artDefaultsFactory)
+	soong.RegisterModuleType("art_global_defaults", artGlobalDefaultsFactory)
+}
+
+func artGlobalDefaultsFactory() (blueprint.Module, []interface{}) {
+	c := &artGlobalDefaults{}
+	module, props := artDefaultsFactory()
+	android.AddCustomizer(module.(android.Module), c)
+
+	return module, props
+}
+
+func artDefaultsFactory() (blueprint.Module, []interface{}) {
+	c := &codegenCustomizer{}
+	module, props := cc.DefaultsFactory(&c.codegenProperties)
+	android.AddCustomizer(module.(android.Module), c)
+
+	return module, props
+}
+
+func artLibrary() (blueprint.Module, []interface{}) {
+	library, _ := cc.NewLibrary(android.HostAndDeviceSupported, true, true)
+	module, props := library.Init()
+
+	c := &codegenCustomizer{}
+	android.AddCustomizer(library, c)
+	props = append(props, &c.codegenProperties)
+	return module, props
+}
+
+func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
+	ret := ctx.AConfig().Getenv(key)
+	if ret == "" {
+		return defaultValue
+	}
+	return ret
+}
+
+func envTrue(ctx android.BaseContext, key string) bool {
+	return ctx.AConfig().Getenv(key) == "true"
+}
diff --git a/build/codegen.go b/build/codegen.go
new file mode 100644
index 0000000..eb2c37d
--- /dev/null
+++ b/build/codegen.go
@@ -0,0 +1,123 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package art
+
+// This file implements the "codegen" property to apply different properties based on the currently
+// selected codegen arches, which defaults to all arches on the host and the primary and secondary
+// arches on the device.
+
+import (
+	"android/soong/android"
+	"sort"
+	"strings"
+)
+
+func (a *codegenCustomizer) CustomizeProperties(ctx android.CustomizePropertiesContext) {
+	c := &a.codegenProperties.Codegen
+
+	var hostArches, deviceArches []string
+
+	e := envDefault(ctx, "ART_HOST_CODEGEN_ARCHS", "")
+	if e == "" {
+		hostArches = supportedArches
+	} else {
+		hostArches = strings.Split(e, " ")
+	}
+
+	e = envDefault(ctx, "ART_TARGET_CODEGEN_ARCHS", "")
+	if e == "" {
+		deviceArches = defaultDeviceCodegenArches(ctx)
+	} else {
+		deviceArches = strings.Split(e, " ")
+	}
+
+	type props struct {
+		Target struct {
+			Android *codegenArchProperties
+			Host    *codegenArchProperties
+		}
+	}
+
+	addCodegenArchProperties := func(p *props, hod **codegenArchProperties, arch string) {
+		switch arch {
+		case "arm":
+			*hod = &c.Arm
+		case "arm64":
+			*hod = &c.Arm64
+		case "mips":
+			*hod = &c.Mips
+		case "mips64":
+			*hod = &c.Mips64
+		case "x86":
+			*hod = &c.X86
+		case "x86_64":
+			*hod = &c.X86_64
+		default:
+			ctx.ModuleErrorf("Unknown codegen architecture %q", arch)
+			return
+		}
+		ctx.AppendProperties(p)
+	}
+
+	for _, a := range deviceArches {
+		p := &props{}
+		addCodegenArchProperties(p, &p.Target.Android, a)
+		if ctx.Failed() {
+			return
+		}
+	}
+
+	for _, a := range hostArches {
+		p := &props{}
+		addCodegenArchProperties(p, &p.Target.Host, a)
+		if ctx.Failed() {
+			return
+		}
+	}
+}
+
+type codegenArchProperties struct {
+	Srcs   []string
+	Cflags []string
+	Static struct {
+		Whole_static_libs []string
+	}
+	Shared struct {
+		Shared_libs []string
+	}
+}
+
+type codegenProperties struct {
+	Codegen struct {
+		Arm, Arm64, Mips, Mips64, X86, X86_64 codegenArchProperties
+	}
+}
+
+type codegenCustomizer struct {
+	codegenProperties codegenProperties
+}
+
+func defaultDeviceCodegenArches(ctx android.CustomizePropertiesContext) []string {
+	arches := make(map[string]bool)
+	for _, a := range ctx.DeviceConfig().Arches() {
+		arches[a.ArchType.String()] = true
+	}
+	ret := make([]string, 0, len(arches))
+	for a := range arches {
+		ret = append(ret, a)
+	}
+	sort.Strings(ret)
+	return ret
+}
diff --git a/build/makevars.go b/build/makevars.go
new file mode 100644
index 0000000..5655c55
--- /dev/null
+++ b/build/makevars.go
@@ -0,0 +1,30 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package art
+
+import "android/soong/android"
+
+var (
+	pctx = android.NewPackageContext("android/soong/art")
+)
+
+func init() {
+	android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
+}
+
+func makeVarsProvider(ctx android.MakeVarsContext) {
+	ctx.Strict("LIBART_IMG_HOST_BASE_ADDRESS", ctx.Config().LibartImgHostBaseAddress())
+	ctx.Strict("LIBART_IMG_TARGET_BASE_ADDRESS", ctx.Config().LibartImgDeviceBaseAddress())
+}