Home | History | Annotate | Download | only in build
      1 // Copyright 2017 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package build
     16 
     17 import (
     18 	"os"
     19 	"path/filepath"
     20 	"strconv"
     21 	"time"
     22 
     23 	"github.com/google/blueprint/microfactory"
     24 )
     25 
     26 func runSoong(ctx Context, config Config) {
     27 	ctx.BeginTrace("soong")
     28 	defer ctx.EndTrace()
     29 
     30 	func() {
     31 		ctx.BeginTrace("blueprint bootstrap")
     32 		defer ctx.EndTrace()
     33 
     34 		cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t")
     35 		cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint")
     36 		cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash")
     37 		cmd.Environment.Set("BUILDDIR", config.SoongOutDir())
     38 		cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag()))
     39 		cmd.Environment.Set("BLUEPRINT_LIST_FILE", filepath.Join(config.FileListDir(), "Android.bp.list"))
     40 		cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir())
     41 		cmd.Environment.Set("SRCDIR", ".")
     42 		cmd.Environment.Set("TOPNAME", "Android.bp")
     43 		cmd.Sandbox = soongSandbox
     44 		cmd.Stdout = ctx.Stdout()
     45 		cmd.Stderr = ctx.Stderr()
     46 		cmd.RunOrFatal()
     47 	}()
     48 
     49 	func() {
     50 		ctx.BeginTrace("environment check")
     51 		defer ctx.EndTrace()
     52 
     53 		envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
     54 		envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env")
     55 		if _, err := os.Stat(envFile); err == nil {
     56 			if _, err := os.Stat(envTool); err == nil {
     57 				cmd := Command(ctx, config, "soong_env", envTool, envFile)
     58 				cmd.Sandbox = soongSandbox
     59 				cmd.Stdout = ctx.Stdout()
     60 				cmd.Stderr = ctx.Stderr()
     61 				if err := cmd.Run(); err != nil {
     62 					ctx.Verboseln("soong_env failed, forcing manifest regeneration")
     63 					os.Remove(envFile)
     64 				}
     65 			} else {
     66 				ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration")
     67 				os.Remove(envFile)
     68 			}
     69 		} else if !os.IsNotExist(err) {
     70 			ctx.Fatalf("Failed to stat %f: %v", envFile, err)
     71 		}
     72 	}()
     73 
     74 	func() {
     75 		ctx.BeginTrace("minibp")
     76 		defer ctx.EndTrace()
     77 
     78 		var cfg microfactory.Config
     79 		cfg.Map("github.com/google/blueprint", "build/blueprint")
     80 
     81 		cfg.TrimPath = absPath(ctx, ".")
     82 
     83 		minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
     84 		if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil {
     85 			ctx.Fatalln("Failed to build minibp:", err)
     86 		}
     87 	}()
     88 
     89 	ninja := func(name, file string) {
     90 		ctx.BeginTrace(name)
     91 		defer ctx.EndTrace()
     92 
     93 		cmd := Command(ctx, config, "soong "+name,
     94 			config.PrebuiltBuildTool("ninja"),
     95 			"-d", "keepdepfile",
     96 			"-w", "dupbuild=err",
     97 			"-j", strconv.Itoa(config.Parallel()),
     98 			"-f", filepath.Join(config.SoongOutDir(), file))
     99 		if config.IsVerbose() {
    100 			cmd.Args = append(cmd.Args, "-v")
    101 		}
    102 		cmd.Sandbox = soongSandbox
    103 		cmd.Stdin = ctx.Stdin()
    104 		cmd.Stdout = ctx.Stdout()
    105 		cmd.Stderr = ctx.Stderr()
    106 
    107 		defer ctx.ImportNinjaLog(filepath.Join(config.OutDir(), ".ninja_log"), time.Now())
    108 		cmd.RunOrFatal()
    109 	}
    110 
    111 	ninja("minibootstrap", ".minibootstrap/build.ninja")
    112 	ninja("bootstrap", ".bootstrap/build.ninja")
    113 }
    114