Merge "Fix incomplete LTO bug workaround."
diff --git a/Android.bp b/Android.bp
index 5396664..aa65209 100644
--- a/Android.bp
+++ b/Android.bp
@@ -145,6 +145,7 @@
         "cc/vndk_prebuilt.go",
 
         "cc/cmakelists.go",
+        "cc/compdb.go",
         "cc/compiler.go",
         "cc/installer.go",
         "cc/linker.go",
diff --git a/README.md b/README.md
index b234c71..b3239e9 100644
--- a/README.md
+++ b/README.md
@@ -175,6 +175,7 @@
 * [Best Practices](docs/best_practices.md)
 * [Build Performance](docs/perf.md)
 * [Generating CLion Projects](docs/clion.md)
+* [Generating YouCompleteMe/VSCode compile\_commands.json file](docs/compdb.md)
 * Make-specific documentation: [build/make/README.md](https://android.googlesource.com/platform/build/+/master/README.md)
 
 ## FAQ
diff --git a/android/androidmk.go b/android/androidmk.go
index a60520f..a24d7bc 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -169,7 +169,7 @@
 		data.Include = "$(BUILD_PREBUILT)"
 	}
 
-	data.Required = amod.commonProperties.Required
+	data.Required = append(data.Required, amod.commonProperties.Required...)
 
 	// Make does not understand LinuxBionic
 	if amod.Os() == LinuxBionic {
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 5824168..cdd4a5a 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -58,6 +58,8 @@
 
 	ret := android.AndroidMkData{
 		OutputFile: c.outputFile,
+		Required:   c.Properties.AndroidMkRuntimeLibs,
+
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
 				if len(c.Properties.Logtags) > 0 {
diff --git a/cc/cc.go b/cc/cc.go
index 9722cf0..76e6645 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -68,6 +68,7 @@
 	SharedLibs, LateSharedLibs                  []string
 	StaticLibs, LateStaticLibs, WholeStaticLibs []string
 	HeaderLibs                                  []string
+	RuntimeLibs                                 []string
 
 	ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
 
@@ -164,9 +165,10 @@
 	// Minimum sdk version supported when compiling against the ndk
 	Sdk_version *string
 
-	AndroidMkSharedLibs []string `blueprint:"mutated"`
-	HideFromMake        bool     `blueprint:"mutated"`
-	PreventInstall      bool     `blueprint:"mutated"`
+	AndroidMkSharedLibs  []string `blueprint:"mutated"`
+	AndroidMkRuntimeLibs []string `blueprint:"mutated"`
+	HideFromMake         bool     `blueprint:"mutated"`
+	PreventInstall       bool     `blueprint:"mutated"`
 
 	UseVndk bool `blueprint:"mutated"`
 
@@ -306,6 +308,7 @@
 	ndkStubDepTag         = dependencyTag{name: "ndk stub", library: true}
 	ndkLateStubDepTag     = dependencyTag{name: "ndk late stub", library: true}
 	vndkExtDepTag         = dependencyTag{name: "vndk extends", library: true}
+	runtimeDepTag         = dependencyTag{name: "runtime lib"}
 )
 
 // Module contains the properties and members used by all C/C++ module types, and implements
@@ -667,7 +670,6 @@
 }
 
 func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
-
 	ctx := &moduleContext{
 		ModuleContext: actx,
 		moduleContextImpl: moduleContextImpl{
@@ -846,6 +848,7 @@
 	deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
 	deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
 	deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
+	deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
 
 	for _, lib := range deps.ReexportSharedLibHeaders {
 		if !inList(lib, deps.SharedLibs) {
@@ -987,6 +990,9 @@
 	actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
 		deps.LateSharedLibs...)
 
+	actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, runtimeDepTag,
+		deps.RuntimeLibs...)
+
 	actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
 
 	for _, gen := range deps.GeneratedHeaders {
@@ -1346,28 +1352,34 @@
 			*depPtr = append(*depPtr, dep.Path())
 		}
 
-		// Export the shared libs to Make.
-		switch depTag {
-		case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
+		makeLibName := func(depName string) string {
 			libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
 			libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
 			libName = strings.TrimPrefix(libName, "prebuilt_")
 			isLLndk := inList(libName, llndkLibraries)
 			isVendorPublicLib := inList(libName, vendorPublicLibraries)
-			var makeLibName string
 			bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
 			if c.useVndk() && bothVendorAndCoreVariantsExist {
 				// The vendor module in Make will have been renamed to not conflict with the core
 				// module, so update the dependency name here accordingly.
-				makeLibName = libName + vendorSuffix
+				return libName + vendorSuffix
 			} else if (ctx.Platform() || ctx.ProductSpecific()) && isVendorPublicLib {
-				makeLibName = libName + vendorPublicLibrarySuffix
+				return libName + vendorPublicLibrarySuffix
 			} else {
-				makeLibName = libName
+				return libName
 			}
+		}
+
+		// Export the shared libs to Make.
+		switch depTag {
+		case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
 			// Note: the order of libs in this list is not important because
 			// they merely serve as Make dependencies and do not affect this lib itself.
-			c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, makeLibName)
+			c.Properties.AndroidMkSharedLibs = append(
+				c.Properties.AndroidMkSharedLibs, makeLibName(depName))
+		case runtimeDepTag:
+			c.Properties.AndroidMkRuntimeLibs = append(
+				c.Properties.AndroidMkRuntimeLibs, makeLibName(depName))
 		}
 	})
 
diff --git a/cc/cc_test.go b/cc/cc_test.go
index 9b4cc0f..4f26827 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -1388,6 +1388,120 @@
 	}
 }
 
+func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
+	actual := module.Properties.AndroidMkRuntimeLibs
+	if !reflect.DeepEqual(actual, expected) {
+		t.Errorf("incorrect runtime_libs for shared libs"+
+			"\nactual:   %v"+
+			"\nexpected: %v",
+			actual,
+			expected,
+		)
+	}
+}
+
+const runtimeLibAndroidBp = `
+	cc_library {
+		name: "libvendor_available1",
+		vendor_available: true,
+		no_libgcc : true,
+		nocrt : true,
+		system_shared_libs : [],
+	}
+	cc_library {
+		name: "libvendor_available2",
+		vendor_available: true,
+		runtime_libs: ["libvendor_available1"],
+		no_libgcc : true,
+		nocrt : true,
+		system_shared_libs : [],
+	}
+	cc_library {
+		name: "libvendor_available3",
+		vendor_available: true,
+		runtime_libs: ["libvendor_available1"],
+		target: {
+			vendor: {
+				exclude_runtime_libs: ["libvendor_available1"],
+			}
+		},
+		no_libgcc : true,
+		nocrt : true,
+		system_shared_libs : [],
+	}
+	cc_library {
+		name: "libcore",
+		runtime_libs: ["libvendor_available1"],
+		no_libgcc : true,
+		nocrt : true,
+		system_shared_libs : [],
+	}
+	cc_library {
+		name: "libvendor1",
+		vendor: true,
+		no_libgcc : true,
+		nocrt : true,
+		system_shared_libs : [],
+	}
+	cc_library {
+		name: "libvendor2",
+		vendor: true,
+		runtime_libs: ["libvendor_available1", "libvendor1"],
+		no_libgcc : true,
+		nocrt : true,
+		system_shared_libs : [],
+	}
+`
+
+func TestRuntimeLibs(t *testing.T) {
+	ctx := testCc(t, runtimeLibAndroidBp)
+
+	// runtime_libs for core variants use the module names without suffixes.
+	variant := "android_arm64_armv8-a_core_shared"
+
+	module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
+
+	module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
+
+	// runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
+	// and vendor variants.
+	variant = "android_arm64_armv8-a_vendor_shared"
+
+	module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)
+
+	module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)
+}
+
+func TestExcludeRuntimeLibs(t *testing.T) {
+	ctx := testCc(t, runtimeLibAndroidBp)
+
+	variant := "android_arm64_armv8-a_core_shared"
+	module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
+
+	variant = "android_arm64_armv8-a_vendor_shared"
+	module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)
+	checkRuntimeLibs(t, nil, module)
+}
+
+func TestRuntimeLibsNoVndk(t *testing.T) {
+	ctx := testCcNoVndk(t, runtimeLibAndroidBp)
+
+	// If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
+
+	variant := "android_arm64_armv8-a_core_shared"
+
+	module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1"}, module)
+
+	module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
+	checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)
+}
+
 var compilerFlagsTestCases = []struct {
 	in  string
 	out bool
diff --git a/cc/compdb.go b/cc/compdb.go
new file mode 100644
index 0000000..a9c5b5e
--- /dev/null
+++ b/cc/compdb.go
@@ -0,0 +1,192 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// 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 cc
+
+import (
+	"encoding/json"
+	"log"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"android/soong/android"
+)
+
+// This singleton generates a compile_commands.json file. It does so for each
+// blueprint Android.bp resulting in a cc.Module when either make, mm, mma, mmm
+// or mmma is called. It will only create a single compile_commands.json file
+// at out/development/ide/compdb/compile_commands.json. It will also symlink it
+// to ${SOONG_LINK_COMPDB_TO} if set. In general this should be created by running
+// make SOONG_GEN_COMPDB=1 nothing to get all targets.
+
+func init() {
+	android.RegisterSingletonType("compdb_generator", compDBGeneratorSingleton)
+}
+
+func compDBGeneratorSingleton() android.Singleton {
+	return &compdbGeneratorSingleton{}
+}
+
+type compdbGeneratorSingleton struct{}
+
+const (
+	compdbFilename                = "compile_commands.json"
+	compdbOutputProjectsDirectory = "out/development/ide/compdb"
+
+	// Environment variables used to modify behavior of this singleton.
+	envVariableGenerateCompdb          = "SOONG_GEN_COMPDB"
+	envVariableGenerateCompdbDebugInfo = "SOONG_GEN_COMPDB_DEBUG"
+	envVariableCompdbLink              = "SOONG_LINK_COMPDB_TO"
+)
+
+// A compdb entry. The compile_commands.json file is a list of these.
+type compDbEntry struct {
+	Directory string   `json:"directory"`
+	Arguments []string `json:"arguments"`
+	File      string   `json:"file"`
+	Output    string   `json:"output,omitempty"`
+}
+
+func (c *compdbGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
+	if !ctx.Config().IsEnvTrue(envVariableGenerateCompdb) {
+		return
+	}
+
+	// Instruct the generator to indent the json file for easier debugging.
+	outputCompdbDebugInfo := ctx.Config().IsEnvTrue(envVariableGenerateCompdbDebugInfo)
+
+	// We only want one entry per file. We don't care what module/isa it's from
+	m := make(map[string]compDbEntry)
+	ctx.VisitAllModules(func(module android.Module) {
+		if ccModule, ok := module.(*Module); ok {
+			if compiledModule, ok := ccModule.compiler.(CompiledInterface); ok {
+				generateCompdbProject(compiledModule, ctx, ccModule, m)
+			}
+		}
+	})
+
+	// Create the output file.
+	dir := filepath.Join(getCompdbAndroidSrcRootDirectory(ctx), compdbOutputProjectsDirectory)
+	os.MkdirAll(dir, 0777)
+	compDBFile := filepath.Join(dir, compdbFilename)
+	f, err := os.Create(compdbFilename)
+	if err != nil {
+		log.Fatalf("Could not create file %s: %s", filepath.Join(dir, compdbFilename), err)
+	}
+	defer f.Close()
+
+	v := make([]compDbEntry, 0, len(m))
+
+	for _, value := range m {
+		v = append(v, value)
+	}
+	var dat []byte
+	if outputCompdbDebugInfo {
+		dat, err = json.MarshalIndent(v, "", " ")
+	} else {
+		dat, err = json.Marshal(v)
+	}
+	if err != nil {
+		log.Fatalf("Failed to marshal: %s", err)
+	}
+	f.Write(dat)
+
+	finalLinkPath := filepath.Join(ctx.Config().Getenv(envVariableCompdbLink), compdbFilename)
+	if finalLinkPath != "" {
+		os.Remove(finalLinkPath)
+		if err := os.Symlink(compDBFile, finalLinkPath); err != nil {
+			log.Fatalf("Unable to symlink %s to %s: %s", compDBFile, finalLinkPath, err)
+		}
+	}
+}
+
+func expandAllVars(ctx android.SingletonContext, args []string) []string {
+	var out []string
+	for _, arg := range args {
+		if arg != "" {
+			if val, err := evalAndSplitVariable(ctx, arg); err == nil {
+				out = append(out, val...)
+			} else {
+				out = append(out, arg)
+			}
+		}
+	}
+	return out
+}
+
+func getArguments(src android.Path, ctx android.SingletonContext, ccModule *Module) []string {
+	var args []string
+	isCpp := false
+	isAsm := false
+	// TODO It would be better to ask soong for the types here.
+	switch src.Ext() {
+	case ".S", ".s", ".asm":
+		isAsm = true
+		isCpp = false
+	case ".c":
+		isAsm = false
+		isCpp = false
+	case ".cpp", ".cc", ".mm":
+		isAsm = false
+		isCpp = true
+	default:
+		log.Print("Unknown file extension " + src.Ext() + " on file " + src.String())
+		isAsm = true
+		isCpp = false
+	}
+	// The executable for the compilation doesn't matter but we need something there.
+	args = append(args, "/bin/false")
+	args = append(args, expandAllVars(ctx, ccModule.flags.GlobalFlags)...)
+	args = append(args, expandAllVars(ctx, ccModule.flags.CFlags)...)
+	if isCpp {
+		args = append(args, expandAllVars(ctx, ccModule.flags.CppFlags)...)
+	} else if !isAsm {
+		args = append(args, expandAllVars(ctx, ccModule.flags.ConlyFlags)...)
+	}
+	args = append(args, expandAllVars(ctx, ccModule.flags.SystemIncludeFlags)...)
+	args = append(args, src.String())
+	return args
+}
+
+func generateCompdbProject(compiledModule CompiledInterface, ctx android.SingletonContext, ccModule *Module, builds map[string]compDbEntry) {
+	srcs := compiledModule.Srcs()
+	if len(srcs) == 0 {
+		return
+	}
+
+	rootDir := getCompdbAndroidSrcRootDirectory(ctx)
+	for _, src := range srcs {
+		if _, ok := builds[src.String()]; !ok {
+			builds[src.String()] = compDbEntry{
+				Directory: rootDir,
+				Arguments: getArguments(src, ctx, ccModule),
+				File:      src.String(),
+			}
+		}
+	}
+}
+
+func evalAndSplitVariable(ctx android.SingletonContext, str string) ([]string, error) {
+	evaluated, err := ctx.Eval(pctx, str)
+	if err == nil {
+		return strings.Split(evaluated, " "), nil
+	}
+	return []string{""}, err
+}
+
+func getCompdbAndroidSrcRootDirectory(ctx android.SingletonContext) string {
+	srcPath, _ := filepath.Abs(android.PathForSource(ctx).String())
+	return srcPath
+}
diff --git a/cc/linker.go b/cc/linker.go
index d9da404..71da09e 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -88,15 +88,24 @@
 	// between static libraries, but it is generally better to order them correctly instead.
 	Group_static_libs *bool `android:"arch_variant"`
 
+	// list of modules that should be installed with this module.  This is similar to 'required'
+	// but '.vendor' suffix will be appended to the module names if the shared libraries have
+	// vendor variants and this module uses VNDK.
+	Runtime_libs []string `android:"arch_variant"`
+
 	Target struct {
 		Vendor struct {
-			// list of shared libs that should not be used to build
-			// the vendor variant of the C/C++ module.
+			// list of shared libs that should not be used to build the vendor variant
+			// of the C/C++ module.
 			Exclude_shared_libs []string
 
-			// list of static libs that should not be used to build
-			// the vendor variant of the C/C++ module.
+			// list of static libs that should not be used to build the vendor variant
+			// of the C/C++ module.
 			Exclude_static_libs []string
+
+			// list of runtime libs that should not be installed along with the vendor
+			// variant of the C/C++ module.
+			Exclude_runtime_libs []string
 		}
 	}
 
@@ -137,6 +146,7 @@
 	deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
 	deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
 	deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
+	deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
 
 	deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
 	deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
@@ -153,6 +163,7 @@
 		deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
 		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
 		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
+		deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
 	}
 
 	if ctx.ModuleName() != "libcompiler_rt-extras" {
@@ -205,6 +216,11 @@
 }
 
 func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
+	// Clang lld is not ready for for Darwin host executables yet.
+	// See https://lld.llvm.org/AtomLLD.html for status of lld for Mach-O.
+	if ctx.Darwin() {
+		return false
+	}
 	if linker.Properties.Use_clang_lld != nil {
 		return Bool(linker.Properties.Use_clang_lld)
 	}
diff --git a/cmd/pom2bp/Android.bp b/cmd/pom2bp/Android.bp
new file mode 100644
index 0000000..0b2b7b5
--- /dev/null
+++ b/cmd/pom2bp/Android.bp
@@ -0,0 +1,22 @@
+// Copyright 2017 Google Inc. All rights reserved.
+//
+// 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.
+
+blueprint_go_binary {
+    name: "pom2bp",
+    deps: [
+        "blueprint-proptools",
+        "bpfix-lib",
+    ],
+    srcs: ["pom2bp.go"],
+}
diff --git a/cmd/pom2bp/pom2bp.go b/cmd/pom2bp/pom2bp.go
new file mode 100644
index 0000000..078a07d
--- /dev/null
+++ b/cmd/pom2bp/pom2bp.go
@@ -0,0 +1,495 @@
+// Copyright 2017 Google Inc. All rights reserved.
+//
+// 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 main
+
+import (
+	"bufio"
+	"bytes"
+	"encoding/xml"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"regexp"
+	"sort"
+	"strings"
+	"text/template"
+
+	"github.com/google/blueprint/proptools"
+
+	"android/soong/bpfix/bpfix"
+)
+
+type RewriteNames []RewriteName
+type RewriteName struct {
+	regexp *regexp.Regexp
+	repl   string
+}
+
+func (r *RewriteNames) String() string {
+	return ""
+}
+
+func (r *RewriteNames) Set(v string) error {
+	split := strings.SplitN(v, "=", 2)
+	if len(split) != 2 {
+		return fmt.Errorf("Must be in the form of <regex>=<replace>")
+	}
+	regex, err := regexp.Compile(split[0])
+	if err != nil {
+		return nil
+	}
+	*r = append(*r, RewriteName{
+		regexp: regex,
+		repl:   split[1],
+	})
+	return nil
+}
+
+func (r *RewriteNames) MavenToBp(groupId string, artifactId string) string {
+	for _, r := range *r {
+		if r.regexp.MatchString(groupId + ":" + artifactId) {
+			return r.regexp.ReplaceAllString(groupId+":"+artifactId, r.repl)
+		} else if r.regexp.MatchString(artifactId) {
+			return r.regexp.ReplaceAllString(artifactId, r.repl)
+		}
+	}
+	return artifactId
+}
+
+var rewriteNames = RewriteNames{}
+
+type ExtraDeps map[string][]string
+
+func (d ExtraDeps) String() string {
+	return ""
+}
+
+func (d ExtraDeps) Set(v string) error {
+	split := strings.SplitN(v, "=", 2)
+	if len(split) != 2 {
+		return fmt.Errorf("Must be in the form of <module>=<module>[,<module>]")
+	}
+	d[split[0]] = strings.Split(split[1], ",")
+	return nil
+}
+
+var extraDeps = make(ExtraDeps)
+
+type Exclude map[string]bool
+
+func (e Exclude) String() string {
+	return ""
+}
+
+func (e Exclude) Set(v string) error {
+	e[v] = true
+	return nil
+}
+
+var excludes = make(Exclude)
+
+var sdkVersion string
+var useVersion string
+
+func InList(s string, list []string) bool {
+	for _, l := range list {
+		if l == s {
+			return true
+		}
+	}
+
+	return false
+}
+
+type Dependency struct {
+	XMLName xml.Name `xml:"dependency"`
+
+	BpTarget string `xml:"-"`
+
+	GroupId    string `xml:"groupId"`
+	ArtifactId string `xml:"artifactId"`
+	Version    string `xml:"version"`
+	Type       string `xml:"type"`
+	Scope      string `xml:"scope"`
+}
+
+func (d Dependency) BpName() string {
+	if d.BpTarget == "" {
+		d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
+	}
+	return d.BpTarget
+}
+
+type Pom struct {
+	XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
+
+	PomFile      string `xml:"-"`
+	ArtifactFile string `xml:"-"`
+	BpTarget     string `xml:"-"`
+
+	GroupId    string `xml:"groupId"`
+	ArtifactId string `xml:"artifactId"`
+	Version    string `xml:"version"`
+	Packaging  string `xml:"packaging"`
+
+	Dependencies []*Dependency `xml:"dependencies>dependency"`
+}
+
+func (p Pom) IsAar() bool {
+	return p.Packaging == "aar"
+}
+
+func (p Pom) IsJar() bool {
+	return p.Packaging == "jar"
+}
+
+func (p Pom) BpName() string {
+	if p.BpTarget == "" {
+		p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
+	}
+	return p.BpTarget
+}
+
+func (p Pom) BpJarDeps() []string {
+	return p.BpDeps("jar", []string{"compile", "runtime"})
+}
+
+func (p Pom) BpAarDeps() []string {
+	return p.BpDeps("aar", []string{"compile", "runtime"})
+}
+
+func (p Pom) BpExtraDeps() []string {
+	return extraDeps[p.BpName()]
+}
+
+// BpDeps obtains dependencies filtered by type and scope. The results of this
+// method are formatted as Android.bp targets, e.g. run through MavenToBp rules.
+func (p Pom) BpDeps(typeExt string, scopes []string) []string {
+	var ret []string
+	for _, d := range p.Dependencies {
+		if d.Type != typeExt || !InList(d.Scope, scopes) {
+			continue
+		}
+		name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
+		ret = append(ret, name)
+	}
+	return ret
+}
+
+func (p Pom) SdkVersion() string {
+	return sdkVersion
+}
+
+func (p *Pom) FixDeps(modules map[string]*Pom) {
+	for _, d := range p.Dependencies {
+		if d.Type == "" {
+			if depPom, ok := modules[d.BpName()]; ok {
+				// We've seen the POM for this dependency, use its packaging
+				// as the dependency type rather than Maven spec default.
+				d.Type = depPom.Packaging
+			} else {
+				// Dependency type was not specified and we don't have the POM
+				// for this artifact, use the default from Maven spec.
+				d.Type = "jar"
+			}
+		}
+		if d.Scope == "" {
+			// Scope was not specified, use the default from Maven spec.
+			d.Scope = "compile"
+		}
+	}
+}
+
+var bpTemplate = template.Must(template.New("bp").Parse(`
+{{if .IsAar}}android_library_import{{else}}java_import{{end}} {
+    name: "{{.BpName}}-nodeps",
+    {{if .IsAar}}aars{{else}}jars{{end}}: ["{{.ArtifactFile}}"],
+    sdk_version: "{{.SdkVersion}}",{{if .IsAar}}
+    static_libs: [{{range .BpAarDeps}}
+        "{{.}}",{{end}}{{range .BpExtraDeps}}
+        "{{.}}",{{end}}
+    ],{{end}}
+}
+
+{{if .IsAar}}android_library{{else}}java_library_static{{end}} {
+    name: "{{.BpName}}",
+    sdk_version: "{{.SdkVersion}}",{{if .IsAar}}
+    manifest: "manifests/{{.BpName}}/AndroidManifest.xml",{{end}}
+    static_libs: [
+        "{{.BpName}}-nodeps",{{range .BpJarDeps}}
+        "{{.}}",{{end}}{{range .BpAarDeps}}
+        "{{.}}",{{end}}{{range .BpExtraDeps}}
+        "{{.}}",{{end}}
+    ],
+    java_version: "1.7",
+}
+`))
+
+func parse(filename string) (*Pom, error) {
+	data, err := ioutil.ReadFile(filename)
+	if err != nil {
+		return nil, err
+	}
+
+	var pom Pom
+	err = xml.Unmarshal(data, &pom)
+	if err != nil {
+		return nil, err
+	}
+
+	if useVersion != "" && pom.Version != useVersion {
+		return nil, nil
+	}
+
+	if pom.Packaging == "" {
+		pom.Packaging = "jar"
+	}
+
+	pom.PomFile = filename
+	pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
+
+	return &pom, nil
+}
+
+func rerunForRegen(filename string) error {
+	buf, err := ioutil.ReadFile(filename)
+	if err != nil {
+		return err
+	}
+
+	scanner := bufio.NewScanner(bytes.NewBuffer(buf))
+
+	// Skip the first line in the file
+	for i := 0; i < 2; i++ {
+		if !scanner.Scan() {
+			if scanner.Err() != nil {
+				return scanner.Err()
+			} else {
+				return fmt.Errorf("unexpected EOF")
+			}
+		}
+	}
+
+	// Extract the old args from the file
+	line := scanner.Text()
+	if strings.HasPrefix(line, "// pom2bp ") {
+		line = strings.TrimPrefix(line, "// pom2bp ")
+	} else if strings.HasPrefix(line, "// pom2mk ") {
+		line = strings.TrimPrefix(line, "// pom2mk ")
+	} else if strings.HasPrefix(line, "# pom2mk ") {
+		line = strings.TrimPrefix(line, "# pom2mk ")
+	} else {
+		return fmt.Errorf("unexpected second line: %q", line)
+	}
+	args := strings.Split(line, " ")
+	lastArg := args[len(args)-1]
+	args = args[:len(args)-1]
+
+	// Append all current command line args except -regen <file> to the ones from the file
+	for i := 1; i < len(os.Args); i++ {
+		if os.Args[i] == "-regen" {
+			i++
+		} else {
+			args = append(args, os.Args[i])
+		}
+	}
+	args = append(args, lastArg)
+
+	cmd := os.Args[0] + " " + strings.Join(args, " ")
+	// Re-exec pom2bp with the new arguments
+	output, err := exec.Command("/bin/sh", "-c", cmd).Output()
+	if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
+		return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
+	} else if err != nil {
+		return err
+	}
+
+	// If the old file was a .mk file, replace it with a .bp file
+	if filepath.Ext(filename) == ".mk" {
+		os.Remove(filename)
+		filename = strings.TrimSuffix(filename, ".mk") + ".bp"
+	}
+
+	return ioutil.WriteFile(filename, output, 0666)
+}
+
+func main() {
+	flag.Usage = func() {
+		fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos
+
+The tool will extract the necessary information from *.pom files to create an Android.bp whose
+aar libraries can be linked against when using AAPT2.
+
+Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>]
+
+  -rewrite <regex>=<replace>
+     rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite
+     option can be specified multiple times. When determining the Android.bp module for a given Maven
+     project, mappings are searched in the order they were specified. The first <regex> matching
+     either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
+     the Android.bp module name using <replace>. If no matches are found, <artifactId> is used.
+  -exclude <module>
+     Don't put the specified module in the Android.bp file.
+  -extra-deps <module>=<module>[,<module>]
+     Some Android.bp modules have transitive dependencies that must be specified when they are
+     depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
+     This may be specified multiple times to declare these dependencies.
+  -sdk-version <version>
+     Sets LOCAL_SDK_VERSION := <version> for all modules.
+  -use-version <version>
+     If the maven directory contains multiple versions of artifacts and their pom files,
+     -use-version can be used to only write Android.bp files for a specific version of those artifacts.
+  <dir>
+     The directory to search for *.pom files under.
+     The contents are written to stdout, to be put in the current directory (often as Android.bp)
+  -regen <file>
+     Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it
+     ends with .mk).
+
+`, os.Args[0])
+	}
+
+	var regen string
+
+	flag.Var(&excludes, "exclude", "Exclude module")
+	flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
+	flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
+	flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
+	flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
+	flag.Bool("static-deps", false, "Ignored")
+	flag.StringVar(&regen, "regen", "", "Rewrite specified file")
+	flag.Parse()
+
+	if regen != "" {
+		err := rerunForRegen(regen)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, err)
+			os.Exit(1)
+		}
+		os.Exit(0)
+	}
+
+	if flag.NArg() == 0 {
+		fmt.Fprintln(os.Stderr, "Directory argument is required")
+		os.Exit(1)
+	} else if flag.NArg() > 1 {
+		fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
+		os.Exit(1)
+	}
+
+	dir := flag.Arg(0)
+	absDir, err := filepath.Abs(dir)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
+		os.Exit(1)
+	}
+
+	var filenames []string
+	err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
+		if err != nil {
+			return err
+		}
+
+		name := info.Name()
+		if info.IsDir() {
+			if strings.HasPrefix(name, ".") {
+				return filepath.SkipDir
+			}
+			return nil
+		}
+
+		if strings.HasPrefix(name, ".") {
+			return nil
+		}
+
+		if strings.HasSuffix(name, ".pom") {
+			path, err = filepath.Rel(absDir, path)
+			if err != nil {
+				return err
+			}
+			filenames = append(filenames, filepath.Join(dir, path))
+		}
+		return nil
+	})
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Error walking files:", err)
+		os.Exit(1)
+	}
+
+	if len(filenames) == 0 {
+		fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
+		os.Exit(1)
+	}
+
+	sort.Strings(filenames)
+
+	poms := []*Pom{}
+	modules := make(map[string]*Pom)
+	duplicate := false
+	for _, filename := range filenames {
+		pom, err := parse(filename)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, "Error converting", filename, err)
+			os.Exit(1)
+		}
+
+		if pom != nil {
+			key := pom.BpName()
+			if excludes[key] {
+				continue
+			}
+
+			if old, ok := modules[key]; ok {
+				fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
+				duplicate = true
+			}
+
+			poms = append(poms, pom)
+			modules[key] = pom
+		}
+	}
+	if duplicate {
+		os.Exit(1)
+	}
+
+	for _, pom := range poms {
+		pom.FixDeps(modules)
+	}
+
+	buf := &bytes.Buffer{}
+
+	fmt.Fprintln(buf, "// Automatically generated with:")
+	fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscape(os.Args[1:]), " "))
+
+	for _, pom := range poms {
+		var err error
+		err = bpTemplate.Execute(buf, pom)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err)
+			os.Exit(1)
+		}
+	}
+
+	out, err := bpfix.Reformat(buf.String())
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Error formatting output", err)
+		os.Exit(1)
+	}
+
+	os.Stdout.WriteString(out)
+}
diff --git a/cmd/pom2mk/pom2mk.go b/cmd/pom2mk/pom2mk.go
index b4659f7..fc83641 100644
--- a/cmd/pom2mk/pom2mk.go
+++ b/cmd/pom2mk/pom2mk.go
@@ -15,11 +15,14 @@
 package main
 
 import (
+	"bufio"
+	"bytes"
 	"encoding/xml"
 	"flag"
 	"fmt"
 	"io/ioutil"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"regexp"
 	"sort"
@@ -85,6 +88,19 @@
 
 var extraDeps = make(ExtraDeps)
 
+type Exclude map[string]bool
+
+func (e Exclude) String() string {
+	return ""
+}
+
+func (e Exclude) Set(v string) error {
+	e[v] = true
+	return nil
+}
+
+var excludes = make(Exclude)
+
 var sdkVersion string
 var useVersion string
 var staticDeps bool
@@ -160,6 +176,10 @@
 // method are formatted as Make targets, e.g. run through MavenToMk rules.
 func (p Pom) MkDeps(typeExt string, scopes []string) []string {
 	var ret []string
+	if typeExt == "jar" {
+		// all top-level extra deps are assumed to be of type "jar" until we add syntax to specify other types
+		ret = append(ret, extraDeps[p.MkName()]...)
+	}
 	for _, d := range p.Dependencies {
 		if d.Type != typeExt || !InList(d.Scope, scopes) {
 			continue
@@ -267,6 +287,58 @@
 	return &pom, nil
 }
 
+func rerunForRegen(filename string) error {
+	buf, err := ioutil.ReadFile(filename)
+	if err != nil {
+		return err
+	}
+
+	scanner := bufio.NewScanner(bytes.NewBuffer(buf))
+
+	// Skip the first line in the file
+	for i := 0; i < 2; i++ {
+		if !scanner.Scan() {
+			if scanner.Err() != nil {
+				return scanner.Err()
+			} else {
+				return fmt.Errorf("unexpected EOF")
+			}
+		}
+	}
+
+	// Extract the old args from the file
+	line := scanner.Text()
+	if strings.HasPrefix(line, "# pom2mk ") {
+		line = strings.TrimPrefix(line, "# pom2mk ")
+	} else {
+		return fmt.Errorf("unexpected second line: %q", line)
+	}
+	args := strings.Split(line, " ")
+	lastArg := args[len(args)-1]
+	args = args[:len(args)-1]
+
+	// Append all current command line args except -regen <file> to the ones from the file
+	for i := 1; i < len(os.Args); i++ {
+		if os.Args[i] == "-regen" {
+			i++
+		} else {
+			args = append(args, os.Args[i])
+		}
+	}
+	args = append(args, lastArg)
+
+	cmd := os.Args[0] + " " + strings.Join(args, " ")
+	// Re-exec pom2mk with the new arguments
+	output, err := exec.Command("/bin/sh", "-c", cmd).Output()
+	if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
+		return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
+	} else if err != nil {
+		return err
+	}
+
+	return ioutil.WriteFile(filename, output, 0666)
+}
+
 func main() {
 	flag.Usage = func() {
 		fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos
@@ -274,7 +346,7 @@
 The tool will extract the necessary information from *.pom files to create an Android.mk whose
 aar libraries can be linked against when using AAPT2.
 
-Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module>]] <dir>
+Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>]
 
   -rewrite <regex>=<replace>
      rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite
@@ -282,6 +354,8 @@
      project, mappings are searched in the order they were specified. The first <regex> matching
      either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
      the Make module name using <replace>. If no matches are found, <artifactId> is used.
+  -exclude <module>
+     Don't put the specified module in the makefile.
   -extra-deps <module>=<module>[,<module>]
      Some Android.mk modules have transitive dependencies that must be specified when they are
      depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
@@ -295,20 +369,37 @@
      Whether to statically include direct dependencies.
   <dir>
      The directory to search for *.pom files under.
-
-The makefile is written to stdout, to be put in the current directory (often as Android.mk)
+     The makefile is written to stdout, to be put in the current directory (often as Android.mk)
+  -regen <file>
+     Read arguments from <file> and overwrite it.
 `, os.Args[0])
 	}
 
+	var regen string
+
+	flag.Var(&excludes, "exclude", "Exclude module")
 	flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
 	flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
 	flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
 	flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
 	flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
+	flag.StringVar(&regen, "regen", "", "Rewrite specified file")
 	flag.Parse()
 
-	if flag.NArg() != 1 {
-		flag.Usage()
+	if regen != "" {
+		err := rerunForRegen(regen)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, err)
+			os.Exit(1)
+		}
+		os.Exit(0)
+	}
+
+	if flag.NArg() == 0 {
+		fmt.Fprintln(os.Stderr, "Directory argument is required")
+		os.Exit(1)
+	} else if flag.NArg() > 1 {
+		fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
 		os.Exit(1)
 	}
 
@@ -369,14 +460,17 @@
 		}
 
 		if pom != nil {
-			poms = append(poms, pom)
 			key := pom.MkName()
+			if excludes[key] {
+				continue
+			}
 
 			if old, ok := modules[key]; ok {
 				fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
 				duplicate = true
 			}
 
+			poms = append(poms, pom)
 			modules[key] = pom
 		}
 	}
diff --git a/docs/compdb.md b/docs/compdb.md
new file mode 100644
index 0000000..68927ca
--- /dev/null
+++ b/docs/compdb.md
@@ -0,0 +1,27 @@
+# Compdb (compile\_commands.json) Generator
+
+Soong can generate compdb files. This is intended for use with editing tools
+such as YouCompleteMe and other libclang based completers.
+
+compdb file generation is enabled via environment variable:
+
+```bash
+$ export SOONG_GEN_COMPDB=1
+$ export SOONG_GEN_COMPDB_DEBUG=1
+```
+
+One can make soong generate a symlink to the compdb file using an environment
+variable:
+
+```bash
+$ export SOONG_LINK_COMPDB_TO=$ANDROID_HOST_OUT
+```
+
+You can then trigger an empty build:
+
+```bash
+$ make nothing
+```
+
+Note that if you build using mm or other limited makes with these environment
+variables set the compdb will only include files in included modules.
diff --git a/java/aar.go b/java/aar.go
index 13c5369..16d82af 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -143,7 +143,16 @@
 	}
 
 	if !hasVersionName {
-		versionName := proptools.NinjaEscape([]string{ctx.Config().AppsDefaultVersionName()})[0]
+		var versionName string
+		if ctx.ModuleName() == "framework-res" {
+			// Some builds set AppsDefaultVersionName() to include the build number ("O-123456").  aapt2 copies the
+			// version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
+			// if it contains the build number.  Use the DefaultAppTargetSdk instead.
+			versionName = ctx.Config().DefaultAppTargetSdk()
+		} else {
+			versionName = ctx.Config().AppsDefaultVersionName()
+		}
+		versionName = proptools.NinjaEscape([]string{versionName})[0]
 		linkFlags = append(linkFlags, "--version-name ", versionName)
 	}
 
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 0700487..4dd5fbb 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -38,15 +38,13 @@
 				"${config.ZipSyncCmd}",
 				"${config.JavadocCmd}",
 				"${config.SoongZipCmd}",
-				"$JsilverJar",
-				"$DoclavaJar",
 			},
 			Rspfile:        "$out.rsp",
 			RspfileContent: "$in",
 			Restat:         true,
 		},
 		"outDir", "srcJarDir", "stubsDir", "srcJars", "opts",
-		"bootclasspathArgs", "classpathArgs", "sourcepath", "docZip", "JsilverJar", "DoclavaJar")
+		"bootclasspathArgs", "classpathArgs", "sourcepath", "docZip")
 )
 
 func init() {
@@ -256,7 +254,9 @@
 func (j *Javadoc) genWhitelistPathPrefixes(whitelistPathPrefixes map[string]bool) {
 	for _, dir := range j.properties.Srcs_lib_whitelist_dirs {
 		for _, pkg := range j.properties.Srcs_lib_whitelist_pkgs {
-			prefix := filepath.Join(dir, pkg)
+			// convert foo.bar.baz to foo/bar/baz
+			pkgAsPath := filepath.Join(strings.Split(pkg, ".")...)
+			prefix := filepath.Join(dir, pkgAsPath)
 			if _, found := whitelistPathPrefixes[prefix]; !found {
 				whitelistPathPrefixes[prefix] = true
 			}
@@ -575,8 +575,13 @@
 
 	implicits = append(implicits, d.Javadoc.srcJars...)
 
+	jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
+	doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
+	implicits = append(implicits, jsilver)
+	implicits = append(implicits, doclava)
+
 	opts := "-source 1.8 -J-Xmx1600m -J-XX:-OmitStackTraceInFastThrow -XDignore.symbol.file " +
-		"-doclet com.google.doclava.Doclava -docletpath ${config.JsilverJar}:${config.DoclavaJar} " +
+		"-doclet com.google.doclava.Doclava -docletpath " + jsilver.String() + ":" + doclava.String() + " " +
 		"-templatedir " + templateDir + " " + htmlDirArgs + " " + htmlDir2Args + " " +
 		"-hdf page.build " + ctx.Config().BuildId() + "-" + ctx.Config().BuildNumberFromFile() + " " +
 		"-hdf page.now " + `"$$(date -d @$$(cat ` + ctx.Config().Getenv("BUILD_DATETIME_FILE") + `) "+%d %b %Y %k:%M")"` +
@@ -607,8 +612,6 @@
 			"classpathArgs":     classpathArgs,
 			"sourcepath":        strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),
 			"docZip":            d.Javadoc.docZip.String(),
-			"JsilverJar":        "${config.JsilverJar}",
-			"DoclavaJar":        "${config.DoclavaJar}",
 		},
 	})
 }
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 301ec61..13a9275 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -18,8 +18,11 @@
 	"android/soong/android"
 	"android/soong/genrule"
 	"fmt"
+	"io"
 	"path"
+	"sort"
 	"strings"
+	"sync"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -43,6 +46,10 @@
 	systemApiStubsTag = dependencyTag{name: "system"}
 )
 
+var (
+	javaSdkLibrariesLock sync.Mutex
+)
+
 // java_sdk_library is to make a Java library that implements optional platform APIs to apps.
 // It is actually a wrapper of several modules: 1) stubs library that clients are linked against
 // to, 2) droiddoc module that internally generates API stubs source files, 3) the real runtime
@@ -62,6 +69,12 @@
 	android.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
 		ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
 	})
+
+	android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
+		javaSdkLibraries := javaSdkLibraries(ctx.Config())
+		sort.Strings(*javaSdkLibraries)
+		ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
+	})
 }
 
 type sdkLibraryProperties struct {
@@ -120,6 +133,20 @@
 	})
 }
 
+func (module *sdkLibrary) AndroidMk() android.AndroidMkData {
+	// Create a phony module that installs the impl library, for the case when this lib is
+	// in PRODUCT_PACKAGES.
+	return android.AndroidMkData{
+		Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
+			fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
+			fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
+			fmt.Fprintln(w, "LOCAL_MODULE :=", name)
+			fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+module.implName())
+			fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
+		},
+	}
+}
+
 // Module name of the stubs library
 func (module *sdkLibrary) stubsName(forSystemApi bool) string {
 	stubsName := module.BaseModuleName() + sdkStubsLibrarySuffix
@@ -210,6 +237,9 @@
 			Unbundled_build struct {
 				Enabled *bool
 			}
+			Pdk struct {
+				Enabled *bool
+			}
 		}
 	}{}
 
@@ -219,6 +249,7 @@
 	props.Sdk_version = proptools.StringPtr(module.sdkVersion(forSystemApi))
 	// Unbundled apps will use the prebult one from /prebuilts/sdk
 	props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
+	props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
 
 	if module.SocSpecific() {
 		props.Soc_specific = proptools.BoolPtr(true)
@@ -281,17 +312,25 @@
 	props.Api_filename = proptools.StringPtr(currentApiFileName)
 	props.Removed_api_filename = proptools.StringPtr(removedApiFileName)
 
-	// Includes the main framework source to ensure that doclava has access to the
-	// visibility information for the base classes of the mock classes. Without it
-	// otherwise hidden methods could be visible.
-	// TODO: remove the need for this
+	// Include the part of the framework source. This is required for the case when
+	// API class is extending from the framework class. In that case, doclava needs
+	// to know whether the base class is hidden or not. Since that information is
+	// encoded as @hide string in the comment, we need source files for the classes,
+	// not the compiled ones. Also there are rare cases where part of SDK library is
+	// implemented in the framework (e.g. org.apache.http.legacy). In that case,
+	// we need framework source to make API stubs, though the sources are not
+	// required to build the runtime library.
 	props.Srcs_lib = proptools.StringPtr("framework")
 	props.Srcs_lib_whitelist_dirs = []string{"core/java"}
-	props.Srcs_lib_whitelist_pkgs = []string{"android"}
-	// These libs are required by doclava to parse the sources from framework.
+	props.Srcs_lib_whitelist_pkgs = module.properties.Api_packages
+	// Add android.annotation package to give access to the framework-defined
+	// annotations such as SystemApi, NonNull, etc.
+	props.Srcs_lib_whitelist_pkgs = append(props.Srcs_lib_whitelist_pkgs, "android.annotation")
+	// These libs are required by doclava to parse the framework sources add via
+	// Src_lib and Src_lib_whitelist_* properties just above.
 	// If we don't add them to the classpath, errors messages are generated by doclava,
 	// though they don't break the build.
-	props.Libs = append(props.Libs, "conscrypt", "bouncycastle", "okhttp")
+	props.Libs = append(props.Libs, "conscrypt", "bouncycastle", "okhttp", "framework")
 
 	mctx.CreateModule(android.ModuleFactoryAdaptor(DroiddocFactory), &props)
 }
@@ -397,6 +436,12 @@
 	}
 }
 
+func javaSdkLibraries(config android.Config) *[]string {
+	return config.Once("javaSdkLibraries", func() interface{} {
+		return &[]string{}
+	}).(*[]string)
+}
+
 // For a java_sdk_library module, create internal modules for stubs, docs,
 // runtime libs and xml file. If requested, the stubs and docs are created twice
 // once for public API level and once for system API level
@@ -413,6 +458,12 @@
 		// for runtime
 		module.createXmlFile(mctx)
 		module.createImplLibrary(mctx)
+
+		// record java_sdk_library modules so that they are exported to make
+		javaSdkLibraries := javaSdkLibraries(mctx.Config())
+		javaSdkLibrariesLock.Lock()
+		defer javaSdkLibrariesLock.Unlock()
+		*javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
 	}
 }