aboutsummaryrefslogtreecommitdiff
path: root/cmd/release_config/build_flag_declarations/main.go
blob: f1716bb7eec03643776fd46bebaa0a0bf6571823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
	"flag"
	"fmt"
	"os"

	rc_lib "android/soong/cmd/release_config/release_config_lib"
	rc_proto "android/soong/cmd/release_config/release_config_proto"
)

type Flags struct {
	// The path to the top of the workspace.  Default: ".".
	top string

	// Output file.
	output string

	// Format for output file
	format string

	// List of flag_declaration files to add.
	decls rc_lib.StringList

	// List of flag_artifacts files to merge.
	intermediates rc_lib.StringList

	// Disable warning messages
	quiet bool

	// Panic on errors.
	debug bool
}

func main() {
	var flags Flags
	topDir, err := rc_lib.GetTopDir()

	// Handle the common arguments
	flag.StringVar(&flags.top, "top", topDir, "path to top of workspace")
	flag.Var(&flags.decls, "decl", "path to a flag_declaration file. May be repeated")
	flag.Var(&flags.intermediates, "intermediate", "path to a flag_artifacts file (output from a prior run). May be repeated")
	flag.StringVar(&flags.format, "format", "pb", "output file format")
	flag.StringVar(&flags.output, "output", "build_flags.pb", "output file")
	flag.BoolVar(&flags.debug, "debug", false, "turn on debugging output for errors")
	flag.BoolVar(&flags.quiet, "quiet", false, "disable warning messages")
	flag.Parse()

	errorExit := func(err error) {
		if flags.debug {
			panic(err)
		}
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}

	if flags.quiet {
		rc_lib.DisableWarnings()
	}

	if err = os.Chdir(flags.top); err != nil {
		errorExit(err)
	}

	flagArtifacts := rc_lib.FlagArtifactsFactory("")
	intermediates := []*rc_proto.FlagDeclarationArtifacts{}
	for _, intermediate := range flags.intermediates {
		fda := rc_lib.FlagDeclarationArtifactsFactory(intermediate)
		intermediates = append(intermediates, fda)
	}
	for _, decl := range flags.decls {
		fa, err := rc_lib.FlagArtifactFactory(decl)
		if err != nil {
			errorExit(err)
		}
		(*flagArtifacts)[*fa.FlagDeclaration.Name] = fa
	}

	message := flagArtifacts.GenerateFlagDeclarationArtifacts(intermediates)
	err = rc_lib.WriteFormattedMessage(flags.output, flags.format, message)
	if err != nil {
		errorExit(err)
	}
}