main.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. package main
  15. import (
  16. "context"
  17. "encoding/json"
  18. "flag"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "android/soong/shared"
  27. "android/soong/ui/build"
  28. "android/soong/ui/logger"
  29. "android/soong/ui/metrics"
  30. "android/soong/ui/signal"
  31. "android/soong/ui/status"
  32. "android/soong/ui/terminal"
  33. "android/soong/ui/tracer"
  34. )
  35. const (
  36. configDir = "vendor/google/tools/soong_config"
  37. jsonSuffix = "json"
  38. )
  39. // A command represents an operation to be executed in the soong build
  40. // system.
  41. type command struct {
  42. // The flag name (must have double dashes).
  43. flag string
  44. // Description for the flag (to display when running help).
  45. description string
  46. // Stream the build status output into the simple terminal mode.
  47. simpleOutput bool
  48. // Sets a prefix string to use for filenames of log files.
  49. logsPrefix string
  50. // Creates the build configuration based on the args and build context.
  51. config func(ctx build.Context, args ...string) build.Config
  52. // Returns what type of IO redirection this Command requires.
  53. stdio func() terminal.StdioInterface
  54. // run the command
  55. run func(ctx build.Context, config build.Config, args []string, logsDir string)
  56. }
  57. // list of supported commands (flags) supported by soong ui
  58. var commands []command = []command{
  59. {
  60. flag: "--make-mode",
  61. description: "build the modules by the target name (i.e. soong_docs)",
  62. config: func(ctx build.Context, args ...string) build.Config {
  63. return build.NewConfig(ctx, args...)
  64. },
  65. stdio: stdio,
  66. run: runMake,
  67. }, {
  68. flag: "--dumpvar-mode",
  69. description: "print the value of the legacy make variable VAR to stdout",
  70. simpleOutput: true,
  71. logsPrefix: "dumpvars-",
  72. config: dumpVarConfig,
  73. stdio: customStdio,
  74. run: dumpVar,
  75. }, {
  76. flag: "--dumpvars-mode",
  77. description: "dump the values of one or more legacy make variables, in shell syntax",
  78. simpleOutput: true,
  79. logsPrefix: "dumpvars-",
  80. config: dumpVarConfig,
  81. stdio: customStdio,
  82. run: dumpVars,
  83. }, {
  84. flag: "--build-mode",
  85. description: "build modules based on the specified build action",
  86. config: buildActionConfig,
  87. stdio: stdio,
  88. run: runMake,
  89. },
  90. }
  91. // indexList returns the index of first found s. -1 is return if s is not
  92. // found.
  93. func indexList(s string, list []string) int {
  94. for i, l := range list {
  95. if l == s {
  96. return i
  97. }
  98. }
  99. return -1
  100. }
  101. // inList returns true if one or more of s is in the list.
  102. func inList(s string, list []string) bool {
  103. return indexList(s, list) != -1
  104. }
  105. func loadEnvConfig() error {
  106. bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
  107. if bc == "" {
  108. return nil
  109. }
  110. cfgFile := filepath.Join(os.Getenv("TOP"), configDir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
  111. envVarsJSON, err := ioutil.ReadFile(cfgFile)
  112. if err != nil {
  113. fmt.Fprintf(os.Stderr, "\033[33mWARNING:\033[0m failed to open config file %s: %s\n", cfgFile, err.Error())
  114. return nil
  115. }
  116. var envVars map[string]map[string]string
  117. if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
  118. return fmt.Errorf("env vars config file: %s did not parse correctly: %s", cfgFile, err.Error())
  119. }
  120. for k, v := range envVars["env"] {
  121. if os.Getenv(k) != "" {
  122. continue
  123. }
  124. if err := os.Setenv(k, v); err != nil {
  125. return err
  126. }
  127. }
  128. return nil
  129. }
  130. // Main execution of soong_ui. The command format is as follows:
  131. //
  132. // soong_ui <command> [<arg 1> <arg 2> ... <arg n>]
  133. //
  134. // Command is the type of soong_ui execution. Only one type of
  135. // execution is specified. The args are specific to the command.
  136. func main() {
  137. shared.ReexecWithDelveMaybe(os.Getenv("SOONG_UI_DELVE"), shared.ResolveDelveBinary())
  138. buildStarted := time.Now()
  139. c, args, err := getCommand(os.Args)
  140. if err != nil {
  141. fmt.Fprintf(os.Stderr, "Error parsing `soong` args: %s.\n", err)
  142. os.Exit(1)
  143. }
  144. // Create a terminal output that mimics Ninja's.
  145. output := terminal.NewStatusOutput(c.stdio().Stdout(), os.Getenv("NINJA_STATUS"), c.simpleOutput,
  146. build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD"))
  147. // Attach a new logger instance to the terminal output.
  148. log := logger.New(output)
  149. defer log.Cleanup()
  150. // Create a context to simplify the program termination process.
  151. ctx, cancel := context.WithCancel(context.Background())
  152. defer cancel()
  153. // Create a new trace file writer, making it log events to the log instance.
  154. trace := tracer.New(log)
  155. defer trace.Close()
  156. // Create and start a new metric record.
  157. met := metrics.New()
  158. met.SetBuildDateTime(buildStarted)
  159. met.SetBuildCommand(os.Args)
  160. // Create a new Status instance, which manages action counts and event output channels.
  161. stat := &status.Status{}
  162. defer stat.Finish()
  163. // Hook up the terminal output and tracer to Status.
  164. stat.AddOutput(output)
  165. stat.AddOutput(trace.StatusTracer())
  166. // Set up a cleanup procedure in case the normal termination process doesn't work.
  167. signal.SetupSignals(log, cancel, func() {
  168. trace.Close()
  169. log.Cleanup()
  170. stat.Finish()
  171. })
  172. buildCtx := build.Context{ContextImpl: &build.ContextImpl{
  173. Context: ctx,
  174. Logger: log,
  175. Metrics: met,
  176. Tracer: trace,
  177. Writer: output,
  178. Status: stat,
  179. }}
  180. if err := loadEnvConfig(); err != nil {
  181. fmt.Fprintf(os.Stderr, "failed to parse env config files: %v", err)
  182. os.Exit(1)
  183. }
  184. config := c.config(buildCtx, args...)
  185. build.SetupOutDir(buildCtx, config)
  186. if config.UseBazel() && config.Dist() {
  187. defer populateExternalDistDir(buildCtx, config)
  188. }
  189. // Set up files to be outputted in the log directory.
  190. logsDir := config.LogsDir()
  191. // Common list of metric file definition.
  192. buildErrorFile := filepath.Join(logsDir, c.logsPrefix+"build_error")
  193. rbeMetricsFile := filepath.Join(logsDir, c.logsPrefix+"rbe_metrics.pb")
  194. soongMetricsFile := filepath.Join(logsDir, c.logsPrefix+"soong_metrics")
  195. build.PrintOutDirWarning(buildCtx, config)
  196. os.MkdirAll(logsDir, 0777)
  197. log.SetOutput(filepath.Join(logsDir, c.logsPrefix+"soong.log"))
  198. trace.SetOutput(filepath.Join(logsDir, c.logsPrefix+"build.trace"))
  199. stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, c.logsPrefix+"verbose.log")))
  200. stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, c.logsPrefix+"error.log")))
  201. stat.AddOutput(status.NewProtoErrorLog(log, buildErrorFile))
  202. stat.AddOutput(status.NewCriticalPath(log))
  203. stat.AddOutput(status.NewBuildProgressLog(log, filepath.Join(logsDir, c.logsPrefix+"build_progress.pb")))
  204. buildCtx.Verbosef("Detected %.3v GB total RAM", float32(config.TotalRAM())/(1024*1024*1024))
  205. buildCtx.Verbosef("Parallelism (local/remote/highmem): %v/%v/%v",
  206. config.Parallel(), config.RemoteParallel(), config.HighmemParallel())
  207. {
  208. // The order of the function calls is important. The last defer function call
  209. // is the first one that is executed to save the rbe metrics to a protobuf
  210. // file. The soong metrics file is then next. Bazel profiles are written
  211. // before the uploadMetrics is invoked. The written files are then uploaded
  212. // if the uploading of the metrics is enabled.
  213. files := []string{
  214. buildErrorFile, // build error strings
  215. rbeMetricsFile, // high level metrics related to remote build execution.
  216. soongMetricsFile, // high level metrics related to this build system.
  217. config.BazelMetricsDir(), // directory that contains a set of bazel metrics.
  218. }
  219. defer build.UploadMetrics(buildCtx, config, c.simpleOutput, buildStarted, files...)
  220. defer met.Dump(soongMetricsFile)
  221. defer build.DumpRBEMetrics(buildCtx, config, rbeMetricsFile)
  222. }
  223. // Read the time at the starting point.
  224. if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
  225. // soong_ui.bash uses the date command's %N (nanosec) flag when getting the start time,
  226. // which Darwin doesn't support. Check if it was executed properly before parsing the value.
  227. if !strings.HasSuffix(start, "N") {
  228. if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
  229. log.Verbosef("Took %dms to start up.",
  230. time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
  231. buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
  232. }
  233. }
  234. if executable, err := os.Executable(); err == nil {
  235. trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
  236. }
  237. }
  238. // Fix up the source tree due to a repo bug where it doesn't remove
  239. // linkfiles that have been removed
  240. fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.bp")
  241. fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.mk")
  242. // Create a source finder.
  243. f := build.NewSourceFinder(buildCtx, config)
  244. defer f.Shutdown()
  245. build.FindSources(buildCtx, config, f)
  246. c.run(buildCtx, config, args, logsDir)
  247. }
  248. func fixBadDanglingLink(ctx build.Context, name string) {
  249. _, err := os.Lstat(name)
  250. if err != nil {
  251. return
  252. }
  253. _, err = os.Stat(name)
  254. if os.IsNotExist(err) {
  255. err = os.Remove(name)
  256. if err != nil {
  257. ctx.Fatalf("Failed to remove dangling link %q: %v", name, err)
  258. }
  259. }
  260. }
  261. func dumpVar(ctx build.Context, config build.Config, args []string, _ string) {
  262. flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
  263. flags.Usage = func() {
  264. fmt.Fprintf(ctx.Writer, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
  265. fmt.Fprintln(ctx.Writer, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
  266. fmt.Fprintln(ctx.Writer, "")
  267. fmt.Fprintln(ctx.Writer, "'report_config' is a special case that prints the human-readable config banner")
  268. fmt.Fprintln(ctx.Writer, "from the beginning of the build.")
  269. fmt.Fprintln(ctx.Writer, "")
  270. flags.PrintDefaults()
  271. }
  272. abs := flags.Bool("abs", false, "Print the absolute path of the value")
  273. flags.Parse(args)
  274. if flags.NArg() != 1 {
  275. flags.Usage()
  276. os.Exit(1)
  277. }
  278. varName := flags.Arg(0)
  279. if varName == "report_config" {
  280. varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
  281. if err != nil {
  282. ctx.Fatal(err)
  283. }
  284. fmt.Println(build.Banner(varData))
  285. } else {
  286. varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
  287. if err != nil {
  288. ctx.Fatal(err)
  289. }
  290. if *abs {
  291. var res []string
  292. for _, path := range strings.Fields(varData[varName]) {
  293. if abs, err := filepath.Abs(path); err == nil {
  294. res = append(res, abs)
  295. } else {
  296. ctx.Fatalln("Failed to get absolute path of", path, err)
  297. }
  298. }
  299. fmt.Println(strings.Join(res, " "))
  300. } else {
  301. fmt.Println(varData[varName])
  302. }
  303. }
  304. }
  305. func dumpVars(ctx build.Context, config build.Config, args []string, _ string) {
  306. flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
  307. flags.Usage = func() {
  308. fmt.Fprintf(ctx.Writer, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
  309. fmt.Fprintln(ctx.Writer, "In dumpvars mode, dump the values of one or more legacy make variables, in")
  310. fmt.Fprintln(ctx.Writer, "shell syntax. The resulting output may be sourced directly into a shell to")
  311. fmt.Fprintln(ctx.Writer, "set corresponding shell variables.")
  312. fmt.Fprintln(ctx.Writer, "")
  313. fmt.Fprintln(ctx.Writer, "'report_config' is a special case that dumps a variable containing the")
  314. fmt.Fprintln(ctx.Writer, "human-readable config banner from the beginning of the build.")
  315. fmt.Fprintln(ctx.Writer, "")
  316. flags.PrintDefaults()
  317. }
  318. varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
  319. absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
  320. varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
  321. absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
  322. flags.Parse(args)
  323. if flags.NArg() != 0 {
  324. flags.Usage()
  325. os.Exit(1)
  326. }
  327. vars := strings.Fields(*varsStr)
  328. absVars := strings.Fields(*absVarsStr)
  329. allVars := append([]string{}, vars...)
  330. allVars = append(allVars, absVars...)
  331. if i := indexList("report_config", allVars); i != -1 {
  332. allVars = append(allVars[:i], allVars[i+1:]...)
  333. allVars = append(allVars, build.BannerVars...)
  334. }
  335. if len(allVars) == 0 {
  336. return
  337. }
  338. varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
  339. if err != nil {
  340. ctx.Fatal(err)
  341. }
  342. for _, name := range vars {
  343. if name == "report_config" {
  344. fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
  345. } else {
  346. fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
  347. }
  348. }
  349. for _, name := range absVars {
  350. var res []string
  351. for _, path := range strings.Fields(varData[name]) {
  352. abs, err := filepath.Abs(path)
  353. if err != nil {
  354. ctx.Fatalln("Failed to get absolute path of", path, err)
  355. }
  356. res = append(res, abs)
  357. }
  358. fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
  359. }
  360. }
  361. func stdio() terminal.StdioInterface {
  362. return terminal.StdioImpl{}
  363. }
  364. // dumpvar and dumpvars use stdout to output variable values, so use stderr instead of stdout when
  365. // reporting events to keep stdout clean from noise.
  366. func customStdio() terminal.StdioInterface {
  367. return terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr)
  368. }
  369. // dumpVarConfig does not require any arguments to be parsed by the NewConfig.
  370. func dumpVarConfig(ctx build.Context, args ...string) build.Config {
  371. return build.NewConfig(ctx)
  372. }
  373. func buildActionConfig(ctx build.Context, args ...string) build.Config {
  374. flags := flag.NewFlagSet("build-mode", flag.ContinueOnError)
  375. flags.Usage = func() {
  376. fmt.Fprintf(ctx.Writer, "usage: %s --build-mode --dir=<path> <build action> [<build arg 1> <build arg 2> ...]\n\n", os.Args[0])
  377. fmt.Fprintln(ctx.Writer, "In build mode, build the set of modules based on the specified build")
  378. fmt.Fprintln(ctx.Writer, "action. The --dir flag is required to determine what is needed to")
  379. fmt.Fprintln(ctx.Writer, "build in the source tree based on the build action. See below for")
  380. fmt.Fprintln(ctx.Writer, "the list of acceptable build action flags.")
  381. fmt.Fprintln(ctx.Writer, "")
  382. flags.PrintDefaults()
  383. }
  384. buildActionFlags := []struct {
  385. name string
  386. description string
  387. action build.BuildAction
  388. set bool
  389. }{{
  390. name: "all-modules",
  391. description: "Build action: build from the top of the source tree.",
  392. action: build.BUILD_MODULES,
  393. }, {
  394. // This is redirecting to mma build command behaviour. Once it has soaked for a
  395. // while, the build command is deleted from here once it has been removed from the
  396. // envsetup.sh.
  397. name: "modules-in-a-dir-no-deps",
  398. description: "Build action: builds all of the modules in the current directory without their dependencies.",
  399. action: build.BUILD_MODULES_IN_A_DIRECTORY,
  400. }, {
  401. // This is redirecting to mmma build command behaviour. Once it has soaked for a
  402. // while, the build command is deleted from here once it has been removed from the
  403. // envsetup.sh.
  404. name: "modules-in-dirs-no-deps",
  405. description: "Build action: builds all of the modules in the supplied directories without their dependencies.",
  406. action: build.BUILD_MODULES_IN_DIRECTORIES,
  407. }, {
  408. name: "modules-in-a-dir",
  409. description: "Build action: builds all of the modules in the current directory and their dependencies.",
  410. action: build.BUILD_MODULES_IN_A_DIRECTORY,
  411. }, {
  412. name: "modules-in-dirs",
  413. description: "Build action: builds all of the modules in the supplied directories and their dependencies.",
  414. action: build.BUILD_MODULES_IN_DIRECTORIES,
  415. }}
  416. for i, flag := range buildActionFlags {
  417. flags.BoolVar(&buildActionFlags[i].set, flag.name, false, flag.description)
  418. }
  419. dir := flags.String("dir", "", "Directory of the executed build command.")
  420. // Only interested in the first two args which defines the build action and the directory.
  421. // The remaining arguments are passed down to the config.
  422. const numBuildActionFlags = 2
  423. if len(args) < numBuildActionFlags {
  424. flags.Usage()
  425. ctx.Fatalln("Improper build action arguments.")
  426. }
  427. flags.Parse(args[0:numBuildActionFlags])
  428. // The next block of code is to validate that exactly one build action is set and the dir flag
  429. // is specified.
  430. buildActionCount := 0
  431. var buildAction build.BuildAction
  432. for _, flag := range buildActionFlags {
  433. if flag.set {
  434. buildActionCount++
  435. buildAction = flag.action
  436. }
  437. }
  438. if buildActionCount != 1 {
  439. ctx.Fatalln("Build action not defined.")
  440. }
  441. if *dir == "" {
  442. ctx.Fatalln("-dir not specified.")
  443. }
  444. // Remove the build action flags from the args as they are not recognized by the config.
  445. args = args[numBuildActionFlags:]
  446. return build.NewBuildActionConfig(buildAction, *dir, ctx, args...)
  447. }
  448. func runMake(ctx build.Context, config build.Config, _ []string, logsDir string) {
  449. if config.IsVerbose() {
  450. writer := ctx.Writer
  451. fmt.Fprintln(writer, "! The argument `showcommands` is no longer supported.")
  452. fmt.Fprintln(writer, "! Instead, the verbose log is always written to a compressed file in the output dir:")
  453. fmt.Fprintln(writer, "!")
  454. fmt.Fprintf(writer, "! gzip -cd %s/verbose.log.gz | less -R\n", logsDir)
  455. fmt.Fprintln(writer, "!")
  456. fmt.Fprintln(writer, "! Older versions are saved in verbose.log.#.gz files")
  457. fmt.Fprintln(writer, "")
  458. select {
  459. case <-time.After(5 * time.Second):
  460. case <-ctx.Done():
  461. return
  462. }
  463. }
  464. if _, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok {
  465. writer := ctx.Writer
  466. fmt.Fprintln(writer, "! The variable `ONE_SHOT_MAKEFILE` is obsolete.")
  467. fmt.Fprintln(writer, "!")
  468. fmt.Fprintln(writer, "! If you're using `mm`, you'll need to run `source build/envsetup.sh` to update.")
  469. fmt.Fprintln(writer, "!")
  470. fmt.Fprintln(writer, "! Otherwise, either specify a module name with m, or use mma / MODULES-IN-...")
  471. fmt.Fprintln(writer, "")
  472. ctx.Fatal("done")
  473. }
  474. build.Build(ctx, config)
  475. }
  476. // getCommand finds the appropriate command based on args[1] flag. args[0]
  477. // is the soong_ui filename.
  478. func getCommand(args []string) (*command, []string, error) {
  479. if len(args) < 2 {
  480. return nil, nil, fmt.Errorf("Too few arguments: %q", args)
  481. }
  482. for _, c := range commands {
  483. if c.flag == args[1] {
  484. return &c, args[2:], nil
  485. }
  486. }
  487. // command not found
  488. return nil, nil, fmt.Errorf("Command not found: %q", args)
  489. }
  490. // For Bazel support, this moves files and directories from e.g. out/dist/$f to DIST_DIR/$f if necessary.
  491. func populateExternalDistDir(ctx build.Context, config build.Config) {
  492. // Make sure that internalDistDirPath and externalDistDirPath are both absolute paths, so we can compare them
  493. var err error
  494. var internalDistDirPath string
  495. var externalDistDirPath string
  496. if internalDistDirPath, err = filepath.Abs(config.DistDir()); err != nil {
  497. ctx.Fatalf("Unable to find absolute path of %s: %s", internalDistDirPath, err)
  498. }
  499. if externalDistDirPath, err = filepath.Abs(config.RealDistDir()); err != nil {
  500. ctx.Fatalf("Unable to find absolute path of %s: %s", externalDistDirPath, err)
  501. }
  502. if externalDistDirPath == internalDistDirPath {
  503. return
  504. }
  505. // Make sure the internal DIST_DIR actually exists before trying to read from it
  506. if _, err = os.Stat(internalDistDirPath); os.IsNotExist(err) {
  507. ctx.Println("Skipping Bazel dist dir migration - nothing to do!")
  508. return
  509. }
  510. // Make sure the external DIST_DIR actually exists before trying to write to it
  511. if err = os.MkdirAll(externalDistDirPath, 0755); err != nil {
  512. ctx.Fatalf("Unable to make directory %s: %s", externalDistDirPath, err)
  513. }
  514. ctx.Println("Populating external DIST_DIR...")
  515. populateExternalDistDirHelper(ctx, config, internalDistDirPath, externalDistDirPath)
  516. }
  517. func populateExternalDistDirHelper(ctx build.Context, config build.Config, internalDistDirPath string, externalDistDirPath string) {
  518. files, err := ioutil.ReadDir(internalDistDirPath)
  519. if err != nil {
  520. ctx.Fatalf("Can't read internal distdir %s: %s", internalDistDirPath, err)
  521. }
  522. for _, f := range files {
  523. internalFilePath := filepath.Join(internalDistDirPath, f.Name())
  524. externalFilePath := filepath.Join(externalDistDirPath, f.Name())
  525. if f.IsDir() {
  526. // Moving a directory - check if there is an existing directory to merge with
  527. externalLstat, err := os.Lstat(externalFilePath)
  528. if err != nil {
  529. if !os.IsNotExist(err) {
  530. ctx.Fatalf("Can't lstat external %s: %s", externalDistDirPath, err)
  531. }
  532. // Otherwise, if the error was os.IsNotExist, that's fine and we fall through to the rename at the bottom
  533. } else {
  534. if externalLstat.IsDir() {
  535. // Existing dir - try to merge the directories?
  536. populateExternalDistDirHelper(ctx, config, internalFilePath, externalFilePath)
  537. continue
  538. } else {
  539. // Existing file being replaced with a directory. Delete the existing file...
  540. if err := os.RemoveAll(externalFilePath); err != nil {
  541. ctx.Fatalf("Unable to remove existing %s: %s", externalFilePath, err)
  542. }
  543. }
  544. }
  545. } else {
  546. // Moving a file (not a dir) - delete any existing file or directory
  547. if err := os.RemoveAll(externalFilePath); err != nil {
  548. ctx.Fatalf("Unable to remove existing %s: %s", externalFilePath, err)
  549. }
  550. }
  551. // The actual move - do a rename instead of a copy in order to save disk space.
  552. if err := os.Rename(internalFilePath, externalFilePath); err != nil {
  553. ctx.Fatalf("Unable to rename %s -> %s due to error %s", internalFilePath, externalFilePath, err)
  554. }
  555. }
  556. }