Merge "Fix mac sdk build"
diff --git a/cc/ndk_headers.go b/cc/ndk_headers.go
index 0ee0f05..1cd4829 100644
--- a/cc/ndk_headers.go
+++ b/cc/ndk_headers.go
@@ -34,6 +34,13 @@
 			CommandDeps: []string{"$versionerCmd"},
 		},
 		"depsPath", "srcDir", "outDir")
+
+	preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
+		blueprint.RuleParams{
+			Command:     "$preprocessor -o $out $in",
+			CommandDeps: []string{"$preprocessor"},
+		},
+		"preprocessor")
 )
 
 func init() {
@@ -279,3 +286,87 @@
 
 	return module
 }
+
+// preprocessed_ndk_header {
+//     name: "foo",
+//     preprocessor: "foo.sh",
+//     srcs: [...],
+//     to: "android",
+// }
+//
+// Will invoke the preprocessor as:
+//     $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
+// For each src in srcs.
+type preprocessedHeadersProperties struct {
+	// The preprocessor to run. Must be a program inside the source directory
+	// with no dependencies.
+	Preprocessor *string
+
+	// Source path to the files to be preprocessed.
+	Srcs []string
+
+	// Source paths that should be excluded from the srcs glob.
+	Exclude_srcs []string
+
+	// Install path within the sysroot. This is relative to usr/include.
+	To *string
+
+	// Path to the NOTICE file associated with the headers.
+	License *string
+}
+
+type preprocessedHeadersModule struct {
+	android.ModuleBase
+
+	properties preprocessedHeadersProperties
+
+	installPaths android.Paths
+	licensePath  android.ModuleSrcPath
+}
+
+func (m *preprocessedHeadersModule) DepsMutator(ctx android.BottomUpMutatorContext) {
+}
+
+func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	if String(m.properties.License) == "" {
+		ctx.PropertyErrorf("license", "field is required")
+	}
+
+	preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
+	m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
+
+	srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs)
+	installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
+	for _, src := range srcFiles {
+		installPath := installDir.Join(ctx, src.Base())
+		m.installPaths = append(m.installPaths, installPath)
+
+		ctx.Build(pctx, android.BuildParams{
+			Rule:        preprocessNdkHeader,
+			Description: "preprocess " + src.Rel(),
+			Input:       src,
+			Output:      installPath,
+			Args: map[string]string{
+				"preprocessor": preprocessor.String(),
+			},
+		})
+	}
+
+	if len(m.installPaths) == 0 {
+		ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
+	}
+}
+
+func preprocessedNdkHeadersFactory() android.Module {
+	module := &preprocessedHeadersModule{}
+
+	module.AddProperties(&module.properties)
+
+	// Host module rather than device module because device module install steps
+	// do not get run when embedded in make. We're not any of the existing
+	// module types that can be exposed via the Android.mk exporter, so just use
+	// a host module.
+	android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst)
+
+	return module
+}
diff --git a/cc/ndk_sysroot.go b/cc/ndk_sysroot.go
index 1a46702..144fc09 100644
--- a/cc/ndk_sysroot.go
+++ b/cc/ndk_sysroot.go
@@ -60,6 +60,7 @@
 	android.RegisterModuleType("ndk_headers", ndkHeadersFactory)
 	android.RegisterModuleType("ndk_library", ndkLibraryFactory)
 	android.RegisterModuleType("versioned_ndk_headers", versionedNdkHeadersFactory)
+	android.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
 	android.RegisterSingletonType("ndk", NdkSingleton)
 
 	pctx.Import("android/soong/common")
@@ -112,6 +113,11 @@
 			licensePaths = append(licensePaths, m.licensePath)
 		}
 
+		if m, ok := module.(*preprocessedHeadersModule); ok {
+			installPaths = append(installPaths, m.installPaths...)
+			licensePaths = append(licensePaths, m.licensePath)
+		}
+
 		if m, ok := module.(*Module); ok {
 			if installer, ok := m.installer.(*stubDecorator); ok {
 				installPaths = append(installPaths, installer.installPath)
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 6c76262..7cf1710 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -19,6 +19,7 @@
 	"android/soong/java/config"
 	"fmt"
 	"path/filepath"
+	"runtime"
 	"strings"
 
 	"github.com/google/blueprint"
@@ -733,12 +734,20 @@
 	implicits = append(implicits, jsilver)
 	implicits = append(implicits, doclava)
 
+	var date string
+	if runtime.GOOS == "darwin" {
+		date = `date -r`
+	} else {
+		date = `date -d`
+	}
+
 	opts := "-source 1.8 -J-Xmx1600m -J-XX:-OmitStackTraceInFastThrow -XDignore.symbol.file " +
 		"-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")"` +
+		`-hdf page.now "$$(` + date + ` @$$(cat ` + ctx.Config().Getenv("BUILD_DATETIME_FILE") + `) "+%d %b %Y %k:%M")" ` +
 		" " + args
+
 	if BoolDefault(d.properties.Create_stubs, true) {
 		opts += " -stubs " + android.PathForModuleOut(ctx, "docs", "stubsDir").String()
 	}
diff --git a/python/builder.go b/python/builder.go
index 969f9ef..ec4cb4e 100644
--- a/python/builder.go
+++ b/python/builder.go
@@ -96,11 +96,8 @@
 			Output:      binFile,
 			Implicits:   implicits,
 			Args: map[string]string{
-				"interp": strings.Replace(interpreter, "/", `\/`, -1),
-				// we need remove "runfiles/" suffix since stub script starts
-				// searching for main file in each sub-dir of "runfiles" directory tree.
-				"main": strings.Replace(strings.TrimPrefix(main, runFiles+"/"),
-					"/", `\/`, -1),
+				"interp":    strings.Replace(interpreter, "/", `\/`, -1),
+				"main":      strings.Replace(main, "/", `\/`, -1),
 				"template":  template.String(),
 				"stub":      stub,
 				"mergedZip": mergedZip.String(),
diff --git a/python/python.go b/python/python.go
index 6d6ac27..4b9111f 100644
--- a/python/python.go
+++ b/python/python.go
@@ -63,8 +63,7 @@
 	// files of the current module.
 	// eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
 	// (from a.b.c import ...) statement.
-	// if left unspecified, all the source/data files of current module are copied to
-	// "runfiles/" tree directory directly.
+	// if left unspecified, all the source/data files path is unchanged within zip file.
 	Pkg_path *string `android:"arch_variant"`
 
 	// true, if the Python module is used internally, eg, Python std libs.
@@ -215,7 +214,6 @@
 	mainFileName       = "__main__.py"
 	entryPointFile     = "entry_point.txt"
 	parFileExt         = ".zip"
-	runFiles           = "runfiles"
 	internal           = "internal"
 )
 
@@ -417,23 +415,11 @@
 			return
 		}
 		if p.properties.Is_internal != nil && *p.properties.Is_internal {
-			// pkg_path starts from "internal/" implicitly.
 			pkgPath = filepath.Join(internal, pkgPath)
-		} else {
-			if !p.isEmbeddedLauncherEnabled(p.properties.Actual_version) {
-				// pkg_path starts from "runfiles/" implicitly.
-				pkgPath = filepath.Join(runFiles, pkgPath)
-			}
 		}
 	} else {
 		if p.properties.Is_internal != nil && *p.properties.Is_internal {
-			// pkg_path starts from "runfiles/" implicitly.
 			pkgPath = internal
-		} else {
-			if !p.isEmbeddedLauncherEnabled(p.properties.Actual_version) {
-				// pkg_path starts from "runfiles/" implicitly.
-				pkgPath = runFiles
-			}
 		}
 	}
 
@@ -620,7 +606,7 @@
 func fillInMap(ctx android.ModuleContext, m map[string]string,
 	key, value, curModule, otherModule string) bool {
 	if oldValue, found := m[key]; found {
-		ctx.ModuleErrorf("found two files to be placed at the same runfiles location %q."+
+		ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
 			" First file: in module %s at path %q."+
 			" Second file: in module %s at path %q.",
 			key, curModule, oldValue, otherModule, value)
diff --git a/python/python_test.go b/python/python_test.go
index 60a1c82..e5fe126 100644
--- a/python/python_test.go
+++ b/python/python_test.go
@@ -44,7 +44,7 @@
 	badIdentifierErrTemplate = moduleVariantErrTemplate +
 		"srcs: the path %q contains invalid token %q."
 	dupRunfileErrTemplate = moduleVariantErrTemplate +
-		"found two files to be placed at the same runfiles location %q." +
+		"found two files to be placed at the same location within zip %q." +
 		" First file: in module %s at path %q." +
 		" Second file: in module %s at path %q."
 	noSrcFileErr      = moduleVariantErrTemplate + "doesn't have any source files!"
@@ -175,11 +175,11 @@
 			},
 			errors: []string{
 				fmt.Sprintf(badIdentifierErrTemplate, "dir/Blueprints:4:11",
-					"lib1", "PY3", "runfiles/a/b/c/-e/f/file1.py", "-e"),
+					"lib1", "PY3", "a/b/c/-e/f/file1.py", "-e"),
 				fmt.Sprintf(badIdentifierErrTemplate, "dir/Blueprints:4:11",
-					"lib1", "PY3", "runfiles/a/b/c/.file1.py", ".file1"),
+					"lib1", "PY3", "a/b/c/.file1.py", ".file1"),
 				fmt.Sprintf(badIdentifierErrTemplate, "dir/Blueprints:4:11",
-					"lib1", "PY3", "runfiles/a/b/c/123/file1.py", "123"),
+					"lib1", "PY3", "a/b/c/123/file1.py", "123"),
 			},
 		},
 		{
@@ -212,7 +212,7 @@
 			},
 			errors: []string{
 				fmt.Sprintf(dupRunfileErrTemplate, "dir/Blueprints:9:6",
-					"lib2", "PY3", "runfiles/a/b/c/file1.py", "lib2", "dir/file1.py",
+					"lib2", "PY3", "a/b/c/file1.py", "lib2", "dir/file1.py",
 					"lib1", "dir/c/file1.py"),
 			},
 		},
@@ -307,10 +307,10 @@
 					name:          "bin",
 					actualVersion: "PY3",
 					pyRunfiles: []string{
-						"runfiles/e/default.py",
-						"runfiles/e/bin.py",
-						"runfiles/e/default_py3.py",
-						"runfiles/e/file4.py",
+						"e/default.py",
+						"e/bin.py",
+						"e/default_py3.py",
+						"e/file4.py",
 					},
 					srcsZip: "@prefix@/.intermediates/dir/bin/PY3/bin.py.srcszip",
 					depsSrcsZips: []string{
diff --git a/python/scripts/stub_template_host.txt b/python/scripts/stub_template_host.txt
index 386298e..e686211 100644
--- a/python/scripts/stub_template_host.txt
+++ b/python/scripts/stub_template_host.txt
@@ -11,7 +11,6 @@
 PYTHON_BINARY = '%interpreter%'
 MAIN_FILE = '%main%'
 PYTHON_PATH = 'PYTHONPATH'
-ZIP_RUNFILES_DIRECTORY_NAME = 'runfiles'
 
 def SearchPathEnv(name):
   search_path = os.getenv('PATH', os.defpath).split(os.pathsep)
@@ -36,7 +35,7 @@
   temp_dir = tempfile.mkdtemp("", "Soong.python_")
   zf = zipfile.ZipFile(os.path.dirname(__file__))
   zf.extractall(temp_dir)
-  return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME)
+  return temp_dir
 
 def Main():
   args = sys.argv[1:]
@@ -83,7 +82,7 @@
   except:
     raise
   finally:
-    shutil.rmtree(os.path.dirname(runfiles_path), True)
+    shutil.rmtree(runfiles_path, True)
 
 if __name__ == '__main__':
   Main()