Only change behavior when actually embedded in make

The change to enable embedding in make change the default behavior to
produce an Android.mk file, change the ninja builddir, and add the
-soong suffix to the end of target names. These don't make sense if
we're not building inside make, so detect if we're running under make
by checking for a .soong.in_make file in the builddir.

Change-Id: I1a0f4c1becb327c769b8a130cafef11d83eb49f4
diff --git a/common/androidmk.go b/common/androidmk.go
index 06aa30c..9470b6b 100644
--- a/common/androidmk.go
+++ b/common/androidmk.go
@@ -55,6 +55,10 @@
 	hasBPDir := make(map[string]bool)
 	bpDirs := []string{}
 
+	if !ctx.Config().(Config).EmbeddedInMake() {
+		return
+	}
+
 	ctx.SetNinjaBuildDir(pctx, filepath.Join(ctx.Config().(Config).buildDir, ".."))
 
 	ctx.VisitAllModules(func(module blueprint.Module) {
diff --git a/common/config.go b/common/config.go
index 7f6ee65..971c4c1 100644
--- a/common/config.go
+++ b/common/config.go
@@ -58,6 +58,8 @@
 	envLock   sync.Mutex
 	envDeps   map[string]string
 	envFrozen bool
+
+	inMake bool
 }
 
 type jsonConfigurable interface {
@@ -163,6 +165,11 @@
 		return Config{}, err
 	}
 
+	inMakeFile := filepath.Join(buildDir, ".soong.in_make")
+	if _, err := os.Stat(inMakeFile); err == nil {
+		config.inMake = true
+	}
+
 	hostArches, deviceArches, err := decodeArchProductVariables(config.ProductVariables)
 	if err != nil {
 		return Config{}, err
@@ -228,6 +235,10 @@
 	return c.envDeps
 }
 
+func (c *config) EmbeddedInMake() bool {
+	return c.inMake
+}
+
 // DeviceName returns the name of the current device target
 // TODO: take an AndroidModuleContext to select the device name for multi-device builds
 func (c *config) DeviceName() string {
diff --git a/common/module.go b/common/module.go
index 36710c5..bdaeb7a 100644
--- a/common/module.go
+++ b/common/module.go
@@ -331,9 +331,14 @@
 	}
 
 	if len(deps) > 0 {
+		suffix := ""
+		if ctx.Config().(Config).EmbeddedInMake() {
+			suffix = "-soong"
+		}
+
 		ctx.Build(pctx, blueprint.BuildParams{
 			Rule:      blueprint.Phony,
-			Outputs:   []string{ctx.ModuleName() + "-soong"},
+			Outputs:   []string{ctx.ModuleName() + suffix},
 			Implicits: deps,
 			Optional:  true,
 		})
@@ -568,10 +573,15 @@
 		}
 	})
 
+	suffix := ""
+	if ctx.Config().(Config).EmbeddedInMake() {
+		suffix = "-soong"
+	}
+
 	// Create a top-level checkbuild target that depends on all modules
 	ctx.Build(pctx, blueprint.BuildParams{
 		Rule:      blueprint.Phony,
-		Outputs:   []string{"checkbuild-soong"},
+		Outputs:   []string{"checkbuild" + suffix},
 		Implicits: checkbuildDeps,
 		Optional:  true,
 	})
@@ -583,7 +593,9 @@
 			Rule:      blueprint.Phony,
 			Outputs:   []string{filepath.Join("mm", dir)},
 			Implicits: dirModules[dir],
-			Optional:  true,
+			// HACK: checkbuild should be an optional build, but force it
+			// enabled for now in standalone builds
+			Optional:  ctx.Config().(Config).EmbeddedInMake(),
 		})
 	}
 }