Skip to content

Commit

Permalink
Add --cdef VAR=value
Browse files Browse the repository at this point in the history
CL: mos: Add --cdef VAR=value

PUBLISHED_FROM=db1c3bbeb4e621f18c3372390f114853bc68b3f1
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Mar 29, 2019
1 parent c55549e commit ea01910
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
57 changes: 29 additions & 28 deletions mos/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -57,7 +55,8 @@ var (

preferPrebuiltLibs = flag.Bool("prefer-prebuilt-libs", false, "if both sources and prebuilt binary of a lib exists, use the binary")

buildVarsSlice []string
buildVarsSlice = flag.StringSlice("build-var", []string{}, `Build variable in the format "NAME=VALUE". Can be used multiple times.`)
cdefsSlice = flag.StringSlice("cdef", []string{}, `C/C++ define in the format "NAME=VALUE". Can be used multiple times.`)

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)")
skipCleanLibs = flag.Bool("skip-clean-libs", true, "if false, then during the remote build all libs will be uploaded to the builder")
Expand Down Expand Up @@ -89,8 +88,6 @@ type buildParams struct {

func init() {
hiddenFlags = append(hiddenFlags, "docker_images")

flag.StringSliceVar(&buildVarsSlice, "build-var", []string{}, "build variable in the format \"NAME:VALUE\" Can be used multiple times.")
}

// Build {{{
Expand Down Expand Up @@ -125,6 +122,11 @@ func buildHandler(ctx context.Context, devConn dev.DevConn) error {
return errors.Trace(err)
}

cdefsFromCLI, err := getCdefsFromCLI()
if err != nil {
return errors.Trace(err)
}

libsFromCLI, err := getLibsFromCLI()
if err != nil {
return errors.Trace(err)
Expand All @@ -134,6 +136,7 @@ func buildHandler(ctx context.Context, devConn dev.DevConn) error {
ManifestAdjustments: manifest_parser.ManifestAdjustments{
Platform: flags.Platform(),
BuildVars: buildVarsFromCLI,
CDefs: cdefsFromCLI,
CFlags: *cflagsExtra,
CXXFlags: *cxxflagsExtra,
ExtraLibs: libsFromCLI,
Expand Down Expand Up @@ -269,13 +272,8 @@ func doBuild(ctx context.Context, bParams *buildParams) error {
return err
}

func getBuildVarsFromCLI() (map[string]string, error) {
m := map[string]string{
"BOARD": *flags.Board,
}

// Add build vars from CLI flags
for _, v := range buildVarsSlice {
func parseVarsSlice(varsSlice []string, vars map[string]string) error {
for _, v := range varsSlice {
pp1 := strings.SplitN(v, ":", 2)
pp2 := strings.SplitN(v, "=", 2)
var pp []string
Expand All @@ -291,11 +289,28 @@ func getBuildVarsFromCLI() (map[string]string, error) {
pp = pp2
}
default:
return nil, errors.Errorf("invalid --build-var spec: %q", v)
return errors.Errorf("invalid var specification: %q", v)
}
m[pp[0]] = pp[1]
vars[pp[0]] = pp[1]
}
return nil
}

func getBuildVarsFromCLI() (map[string]string, error) {
m := map[string]string{
"BOARD": *flags.Board,
}
if err := parseVarsSlice(*buildVarsSlice, m); err != nil {
return nil, errors.Annotatef(err, "invalid --build-var")
}
return m, nil
}

func getCdefsFromCLI() (map[string]string, error) {
m := map[string]string{}
if err := parseVarsSlice(*cdefsSlice, m); err != nil {
return nil, errors.Annotatef(err, "invalid --cdef")
}
return m, nil
}

Expand Down Expand Up @@ -368,20 +383,6 @@ func getCustomLibLocations() (map[string]string, error) {
return customLibLocations, nil
}

func generateCflags(cflags []string, cdefs map[string]string) string {
kk := []string{}
for k, _ := range cdefs {
kk = append(kk, k)
}
sort.Strings(kk)
for _, k := range kk {
v := cdefs[k]
cflags = append(cflags, fmt.Sprintf("-D%s=%s", k, v))
}

return strings.Join(append(cflags), " ")
}

func newMosVars() *interpreter.MosVars {
ret := interpreter.NewMosVars()
ret.SetVar(interpreter.GetMVarNameMosVersion(), version.GetMosVersion())
Expand Down
14 changes: 14 additions & 0 deletions mos/build_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ func buildLocal(ctx context.Context, bParams *buildParams) error {
return buildErr
}

func generateCflags(cflags []string, cdefs map[string]string) string {
kk := []string{}
for k, _ := range cdefs {
kk = append(kk, k)
}
sort.Strings(kk)
for _, k := range kk {
v := cdefs[k]
cflags = append(cflags, fmt.Sprintf("-D%s=%s", k, v))
}

return strings.Join(append(cflags), " ")
}

func buildLocal2(ctx context.Context, bParams *buildParams, clean bool) (err error) {
dockerAppPath := "/app"
dockerMgosPath := "/mongoose-os"
Expand Down
6 changes: 5 additions & 1 deletion mos/manifest_parser/manifest_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type ReadManifestCallbacks struct {
type ManifestAdjustments struct {
Platform string
BuildVars map[string]string
CDefs map[string]string
CFlags []string
CXXFlags []string
ExtraLibs []build.SWModule
Expand Down Expand Up @@ -573,7 +574,7 @@ func readManifestWithLibs2(parentNodeName, dir string, pc *manifestParseContext)

// Also, remove any build vars from adjustments, so that they won't be set on
// deps' manifest we're going to read as well
pc.adjustments.BuildVars = make(map[string]string)
pc.adjustments.BuildVars = nil

if !manifest.NoImplInitDeps {
found := false
Expand All @@ -595,6 +596,9 @@ func readManifestWithLibs2(parentNodeName, dir string, pc *manifestParseContext)
}
pc.adjustments.ExtraLibs = nil

for k, v := range pc.adjustments.CDefs {
manifest.CDefs[k] = v
}
manifest.CFlags = append(manifest.CFlags, pc.adjustments.CFlags...)
pc.adjustments.CFlags = nil
manifest.CXXFlags = append(manifest.CXXFlags, pc.adjustments.CXXFlags...)
Expand Down

0 comments on commit ea01910

Please sign in to comment.