Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // Copyright 2015 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 main |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "fmt" |
| 20 | "os" |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame^] | 21 | "os/exec" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | "path/filepath" |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame^] | 23 | "strconv" |
| 24 | "strings" |
| 25 | "syscall" |
| 26 | "time" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 27 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 30 | "android/soong/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 33 | var ( |
| 34 | docFile string |
| 35 | ) |
| 36 | |
| 37 | func init() { |
| 38 | flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output") |
| 39 | } |
| 40 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 41 | func newNameResolver(config android.Config) *android.NameResolver { |
| 42 | namespacePathsToExport := make(map[string]bool) |
| 43 | |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 44 | for _, namespaceName := range config.ExportedNamespaces() { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 45 | namespacePathsToExport[namespaceName] = true |
| 46 | } |
| 47 | |
| 48 | namespacePathsToExport["."] = true // always export the root namespace |
| 49 | |
| 50 | exportFilter := func(namespace *android.Namespace) bool { |
| 51 | return namespacePathsToExport[namespace.Path] |
| 52 | } |
| 53 | |
| 54 | return android.NewNameResolver(exportFilter) |
| 55 | } |
| 56 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 57 | func main() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame^] | 58 | if android.SoongDelveListen != "" { |
| 59 | if android.SoongDelvePath == "" { |
| 60 | fmt.Fprintln(os.Stderr, "SOONG_DELVE is set but failed to find dlv") |
| 61 | os.Exit(1) |
| 62 | } |
| 63 | pid := strconv.Itoa(os.Getpid()) |
| 64 | cmd := []string{android.SoongDelvePath, |
| 65 | "attach", pid, |
| 66 | "--headless", |
| 67 | "-l", android.SoongDelveListen, |
| 68 | "--api-version=2", |
| 69 | "--accept-multiclient", |
| 70 | "--log", |
| 71 | } |
| 72 | |
| 73 | fmt.Println("Starting", strings.Join(cmd, " ")) |
| 74 | dlv := exec.Command(cmd[0], cmd[1:]...) |
| 75 | dlv.Stdout = os.Stdout |
| 76 | dlv.Stderr = os.Stderr |
| 77 | dlv.Stdin = nil |
| 78 | |
| 79 | // Put dlv into its own process group so we can kill it and the child process it starts. |
| 80 | dlv.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 81 | |
| 82 | err := dlv.Start() |
| 83 | if err != nil { |
| 84 | // Print the error starting dlv and continue. |
| 85 | fmt.Println(err) |
| 86 | } else { |
| 87 | // Kill the process group for dlv when soong_build exits. |
| 88 | defer syscall.Kill(-dlv.Process.Pid, syscall.SIGKILL) |
| 89 | // Wait to give dlv a chance to connect and pause the process. |
| 90 | time.Sleep(time.Second) |
| 91 | } |
| 92 | } |
| 93 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 94 | flag.Parse() |
| 95 | |
| 96 | // The top-level Blueprints file is passed as the first argument. |
| 97 | srcDir := filepath.Dir(flag.Arg(0)) |
| 98 | |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 99 | ctx := android.NewContext() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 100 | ctx.Register() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 101 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 102 | configuration, err := android.NewConfig(srcDir, bootstrap.BuildDir) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 103 | if err != nil { |
| 104 | fmt.Fprintf(os.Stderr, "%s", err) |
| 105 | os.Exit(1) |
| 106 | } |
| 107 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 108 | if docFile != "" { |
| 109 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 110 | } |
| 111 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 112 | ctx.SetNameInterface(newNameResolver(configuration)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 113 | |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 114 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 115 | |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame^] | 116 | extraNinjaDeps := []string{configuration.ConfigFileName, configuration.ProductVariablesFileName} |
| 117 | |
| 118 | // Read the SOONG_DELVE again through configuration so that there is a dependency on the environment variable |
| 119 | // and soong_build will rerun when it is set for the first time. |
| 120 | if listen := configuration.Getenv("SOONG_DELVE"); listen != "" { |
| 121 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 122 | // enabled even if it completed successfully. |
| 123 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve")) |
| 124 | } |
| 125 | |
| 126 | bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...) |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 127 | |
| 128 | if docFile != "" { |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame] | 129 | if err := writeDocs(ctx, docFile); err != nil { |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 130 | fmt.Fprintf(os.Stderr, "%s", err) |
| 131 | os.Exit(1) |
| 132 | } |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 133 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 134 | } |