Skip to content

Commit eeeb9d7

Browse files
committed
Move build flags to the flags package and pass via BuildParams
1 parent 098c1d8 commit eeeb9d7

File tree

7 files changed

+94
-83
lines changed

7 files changed

+94
-83
lines changed

cli/build.go

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,8 @@ import (
5252

5353
// mos build specific advanced flags
5454
var (
55-
cleanBuildFlag = flag.Bool("clean", false, "perform a clean build, wipe the previous build state")
56-
buildDryRunFlag = flag.Bool("build-dry-run", false, "do not actually run the build, only prepare")
57-
buildParamsFlag = flag.String("build-params", "", "build params file")
58-
buildTarget = flag.String("build-target", moscommon.BuildTargetDefault, "target to build with make")
59-
modules = flag.StringArray("module", []string{}, "location of the module from mos.yaml, in the format: \"module_name:/path/to/location\". Can be used multiple times.")
60-
libs = flag.StringArray("lib", []string{}, "location of the lib from mos.yaml, in the format: \"lib_name:/path/to/location\". Can be used multiple times.")
61-
libsUpdateInterval = flag.Duration("libs-update-interval", time.Hour*1, "how often to update already fetched libs")
62-
55+
// Don't want to move this to BuildParams to avoid trivial command line injection.
6356
buildCmdExtra = flag.StringArray("build-cmd-extra", []string{}, "extra make flags, added at the end of the make command. Can be used multiple times.")
64-
cflagsExtra = flag.StringArray("cflags-extra", []string{}, "extra C flag, appended to the \"cflags\" in the manifest. Can be used multiple times.")
65-
cxxflagsExtra = flag.StringArray("cxxflags-extra", []string{}, "extra C++ flag, appended to the \"cxxflags\" in the manifest. Can be used multiple times.")
66-
libsExtraFlag = flag.StringArray("lib-extra", []string{}, "Extra libs to add to the app being built. Value should be a YAML string. Can be used multiple times.")
67-
saveBuildStat = flag.Bool("save-build-stat", true, "save build statistics")
68-
69-
noPlatformCheckFlag = flag.Bool("no-platform-check", false, "override platform support check")
70-
71-
preferPrebuiltLibs = flag.Bool("prefer-prebuilt-libs", false, "if both sources and prebuilt binary of a lib exists, use the binary")
72-
73-
buildVarsSlice = flag.StringSlice("build-var", []string{}, `Build variable in the format "NAME=VALUE". Can be used multiple times.`)
74-
cdefsSlice = flag.StringSlice("cdef", []string{}, `C/C++ define in the format "NAME=VALUE". Can be used multiple times.`)
75-
76-
noLibsUpdate = flag.Bool("no-libs-update", false, "if true, never try to pull existing libs (treat existing default locations as if they were given in --lib)")
77-
skipCleanLibs = flag.Bool("skip-clean-libs", true, "if false, then during the remote build all libs will be uploaded to the builder")
7857

7958
// In-memory buffer containing all the log messages. It has to be
8059
// thread-safe, because it's used in compProviderReal, which is an
@@ -153,8 +132,8 @@ func getCredentialsFromCLI() (map[string]build.Credentials, error) {
153132
// Build command handler {{{
154133
func buildHandler(ctx context.Context, devConn dev.DevConn) error {
155134
var bParams build.BuildParams
156-
if *buildParamsFlag != "" {
157-
buildParamsBytes, err := ioutil.ReadFile(*buildParamsFlag)
135+
if *flags.BuildParams != "" {
136+
buildParamsBytes, err := ioutil.ReadFile(*flags.BuildParams)
158137
if err != nil {
159138
return errors.Annotatef(err, "error reading --build-params file")
160139
}
@@ -163,18 +142,18 @@ func buildHandler(ctx context.Context, devConn dev.DevConn) error {
163142
}
164143
} else {
165144
// Create map of given lib locations, via --lib flag(s)
166-
cll, err := getCustomLocations(*libs)
145+
cll, err := getCustomLocations(*flags.Libs)
167146
if err != nil {
168147
return errors.Trace(err)
169148
}
170149

171150
// Create map of given module locations, via --module flag(s)
172-
cml, err := getCustomLocations(*modules)
151+
cml, err := getCustomLocations(*flags.Modules)
173152
if err != nil {
174153
return errors.Trace(err)
175154
}
176-
if *mosRepo != "" {
177-
cml[build.MosModuleName] = *mosRepo
155+
if *flags.MosRepo != "" {
156+
cml[build.MosModuleName] = *flags.MosRepo
178157
}
179158

180159
buildVarsFromCLI, err := getBuildVarsFromCLI()
@@ -197,19 +176,30 @@ func buildHandler(ctx context.Context, devConn dev.DevConn) error {
197176
return errors.Annotatef(err, "error parsing --credentials")
198177
}
199178

179+
libsUpdateIntvl := *flags.LibsUpdateInterval
180+
if *flags.NoLibsUpdate {
181+
libsUpdateIntvl = 0
182+
}
183+
200184
bParams = build.BuildParams{
201185
ManifestAdjustments: build.ManifestAdjustments{
202186
Platform: flags.Platform(),
203187
BuildVars: buildVarsFromCLI,
204188
CDefs: cdefsFromCLI,
205-
CFlags: *cflagsExtra,
206-
CXXFlags: *cxxflagsExtra,
189+
CFlags: *flags.CFlagsExtra,
190+
CXXFlags: *flags.CXXFlagsExtra,
207191
ExtraLibs: libsFromCLI,
208192
},
209-
BuildTarget: *buildTarget,
193+
Clean: *flags.Clean,
194+
DryRun: *flags.BuildDryRun,
195+
Verbose: *flags.Verbose,
196+
BuildTarget: *flags.BuildTarget,
210197
CustomLibLocations: cll,
211198
CustomModuleLocations: cml,
212-
NoPlatformCheck: *noPlatformCheckFlag,
199+
LibsUpdateInterval: libsUpdateIntvl,
200+
NoPlatformCheck: *flags.NoPlatformCheck,
201+
SaveBuildStat: *flags.SaveBuildStat,
202+
PreferPrebuiltLibs: *flags.PreferPrebuiltLibs,
213203
Credentials: credentials,
214204
}
215205
}
@@ -229,7 +219,7 @@ func doBuild(ctx context.Context, bParams *build.BuildParams) error {
229219

230220
// Request server version in parallel
231221
serverVersionCh := make(chan *version.VersionJson, 1)
232-
if true || !*local {
222+
if true || !*flags.Local {
233223
go func() {
234224
v, err := update.GetServerMosVersion(string(update.GetUpdateChannel()), bParams.Platform, bParams.BuildVars["BOARD"])
235225
if err != nil {
@@ -256,7 +246,7 @@ func doBuild(ctx context.Context, bParams *build.BuildParams) error {
256246
logWriterStderr = io.MultiWriter(logFile, &logBuf, os.Stderr)
257247
logWriter = io.MultiWriter(logFile, &logBuf)
258248

259-
if *verbose {
249+
if bParams.Verbose {
260250
logWriter = logWriterStderr
261251
}
262252

@@ -265,15 +255,15 @@ func doBuild(ctx context.Context, bParams *build.BuildParams) error {
265255
return errors.Errorf("No mos.yml file")
266256
}
267257

268-
if *local {
258+
if *flags.Local {
269259
err = buildLocal(ctx, bParams)
270260
} else {
271261
err = buildRemote(bParams)
272262
}
273263
if err != nil {
274264
return errors.Trace(err)
275265
}
276-
if *buildDryRunFlag {
266+
if bParams.DryRun {
277267
return nil
278268
}
279269

@@ -289,7 +279,7 @@ func doBuild(ctx context.Context, bParams *build.BuildParams) error {
289279

290280
end := time.Now()
291281

292-
if *saveBuildStat {
282+
if bParams.SaveBuildStat {
293283
bstat := moscommon.BuildStat{
294284
ArchOld: fw.Platform,
295285
Platform: fw.Platform,
@@ -305,7 +295,7 @@ func doBuild(ctx context.Context, bParams *build.BuildParams) error {
305295
ioutil.WriteFile(moscommon.GetBuildStatFilePath(buildDir), data, 0666)
306296
}
307297

308-
if *local || !*verbose {
298+
if *flags.Local || !bParams.Verbose {
309299
if err == nil {
310300
freportf(logWriter, "Success, built %s/%s version %s (%s).", fw.Name, fw.Platform, fw.Version, fw.BuildID)
311301
}
@@ -366,23 +356,23 @@ func getBuildVarsFromCLI() (map[string]string, error) {
366356
m := map[string]string{
367357
"BOARD": *flags.Board,
368358
}
369-
if err := parseVarsSlice(*buildVarsSlice, m); err != nil {
359+
if err := parseVarsSlice(*flags.BuildVars, m); err != nil {
370360
return nil, errors.Annotatef(err, "invalid --build-var")
371361
}
372362
return m, nil
373363
}
374364

375365
func getCdefsFromCLI() (map[string]string, error) {
376366
m := map[string]string{}
377-
if err := parseVarsSlice(*cdefsSlice, m); err != nil {
367+
if err := parseVarsSlice(*flags.CDefs, m); err != nil {
378368
return nil, errors.Annotatef(err, "invalid --cdef")
379369
}
380370
return m, nil
381371
}
382372

383373
func getLibsFromCLI() ([]build.SWModule, error) {
384374
var res []build.SWModule
385-
for _, v := range *libsExtraFlag {
375+
for _, v := range *flags.LibsExtra {
386376
var m build.SWModule
387377
if err := yaml.Unmarshal([]byte(v), &m); err != nil {
388378
return nil, errors.Annotatef(err, "invalid --libs-extra value %q", v)
@@ -537,10 +527,7 @@ func (lpr *compProviderReal) GetLibLocalPath(
537527
return "", errors.Trace(err)
538528
}
539529

540-
updateIntvl := *libsUpdateInterval
541-
if *noLibsUpdate {
542-
updateIntvl = 0
543-
}
530+
updateIntvl := lpr.bParams.LibsUpdateInterval
544531

545532
// Try to get current hash, ignoring errors
546533
curHash := ""
@@ -644,10 +631,7 @@ func (lpr *compProviderReal) GetModuleLocalPath(
644631
return "", errors.Trace(err)
645632
}
646633

647-
updateIntvl := *libsUpdateInterval
648-
if *noLibsUpdate {
649-
updateIntvl = 0
650-
}
634+
updateIntvl := lpr.bParams.LibsUpdateInterval
651635

652636
targetDir, err := m.PrepareLocalDir(paths.GetModulesDir(appDir), logWriter, true, modulesDefVersion, updateIntvl, 0)
653637
if err != nil {

cli/build/params.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package build
22

3+
import "time"
4+
35
// Last-minute adjustments for the manifest, typically constructed from command line
46
type ManifestAdjustments struct {
57
Platform string
@@ -13,10 +15,16 @@ type ManifestAdjustments struct {
1315
// Note: this struct gets transmitted to the server
1416
type BuildParams struct {
1517
ManifestAdjustments
18+
Clean bool
19+
DryRun bool
20+
Verbose bool
1621
BuildTarget string
1722
CustomLibLocations map[string]string
1823
CustomModuleLocations map[string]string
24+
LibsUpdateInterval time.Duration
1925
NoPlatformCheck bool
26+
SaveBuildStat bool
27+
PreferPrebuiltLibs bool
2028
// host -> credentials, used for authentication when fetching libs.
2129
Credentials map[string]Credentials
2230
}

cli/build_local.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ func buildLocal(ctx context.Context, bParams *build.BuildParams) error {
5555

5656
buildDir := moscommon.GetBuildDir(projectDir)
5757

58-
buildErr := buildLocal2(ctx, bParams, *cleanBuildFlag)
58+
buildErr := buildLocal2(ctx, bParams)
5959

60-
if !*verbose && buildErr != nil {
60+
if !bParams.Verbose && buildErr != nil {
6161
log, err := os.Open(moscommon.GetBuildLogFilePath(buildDir))
6262
if err != nil {
6363
glog.Errorf("can't read build log: %s", err)
@@ -83,7 +83,7 @@ func generateCflags(cflags []string, cdefs map[string]string) string {
8383
return strings.Join(append(cflags), " ")
8484
}
8585

86-
func buildLocal2(ctx context.Context, bParams *build.BuildParams, clean bool) (err error) {
86+
func buildLocal2(ctx context.Context, bParams *build.BuildParams) (err error) {
8787
gitinst := mosgit.NewOurGit(nil)
8888

8989
buildDir := moscommon.GetBuildDir(projectDir)
@@ -104,7 +104,7 @@ func buildLocal2(ctx context.Context, bParams *build.BuildParams, clean bool) (e
104104
fwFilename := moscommon.GetFirmwareZipFilePath(buildDir)
105105

106106
// Perform cleanup before the build {{{
107-
if clean {
107+
if bParams.Clean {
108108
// Cleanup build dir, but leave build log intact, because we're already
109109
// writing to it.
110110
if err := ourio.RemoveFromDir(buildDir, []string{moscommon.GetBuildLogFilePath("")}); err != nil {
@@ -134,14 +134,10 @@ func buildLocal2(ctx context.Context, bParams *build.BuildParams, clean bool) (e
134134
return errors.Trace(err)
135135
}
136136

137-
libsUpdateIntvl := *libsUpdateInterval
138-
if *noLibsUpdate {
139-
libsUpdateIntvl = 0
140-
}
141137
manifest, fp, err := manifest_parser.ReadManifestFinal(
142138
appDir, &bParams.ManifestAdjustments, logWriter, interp,
143139
&manifest_parser.ReadManifestCallbacks{ComponentProvider: &compProvider},
144-
true /* requireArch */, *preferPrebuiltLibs, libsUpdateIntvl)
140+
true /* requireArch */, bParams.PreferPrebuiltLibs, bParams.LibsUpdateInterval)
145141
if err != nil {
146142
return errors.Annotatef(err, "error parsing manifest")
147143
}
@@ -152,9 +148,11 @@ func buildLocal2(ctx context.Context, bParams *build.BuildParams, clean bool) (e
152148
return errors.Trace(err)
153149
}
154150
// Force clean rebuild if manifest was updated
155-
if manifestUpdated && !clean {
151+
if manifestUpdated && !bParams.Clean {
156152
freportf(logWriter, "== Manifest has changed, forcing a clean rebuild...")
157-
return buildLocal2(ctx, bParams, true /* clean */)
153+
bParams2 := *bParams
154+
bParams2.Clean = true
155+
return buildLocal2(ctx, bParams)
158156
}
159157

160158
switch manifest.Type {
@@ -445,10 +443,10 @@ func buildLocal2(ctx context.Context, bParams *build.BuildParams, clean bool) (e
445443
"/bin/bash", "-c", "nice make '"+strings.Join(makeArgs, "' '")+"'",
446444
)
447445

448-
if err := runDockerBuild(dockerRunArgs); err != nil {
446+
if err := runDockerBuild(dockerRunArgs, bParams.DryRun); err != nil {
449447
return errors.Trace(err)
450448
}
451-
if *buildDryRunFlag {
449+
if bParams.DryRun {
452450
return nil
453451
}
454452
} else {
@@ -470,7 +468,7 @@ func buildLocal2(ctx context.Context, bParams *build.BuildParams, clean bool) (e
470468

471469
freportf(logWriter, "Make arguments: %s", strings.Join(makeArgs, " "))
472470

473-
if *buildDryRunFlag {
471+
if bParams.DryRun {
474472
return nil
475473
}
476474

@@ -634,7 +632,7 @@ func getPathsForDocker(p []string) []string {
634632
return ret
635633
}
636634

637-
func runDockerBuild(dockerRunArgs []string) error {
635+
func runDockerBuild(dockerRunArgs []string, dryRun bool) error {
638636
containerName := fmt.Sprintf(
639637
"mos_build_%s_%d", time.Now().Format("2006-01-02T15-04-05-00"), rand.Int(),
640638
)
@@ -645,7 +643,7 @@ func runDockerBuild(dockerRunArgs []string) error {
645643

646644
freportf(logWriter, "Docker arguments: %s", strings.Join(dockerArgs, " "))
647645

648-
if *buildDryRunFlag {
646+
if dryRun {
649647
return nil
650648
}
651649

0 commit comments

Comments
 (0)