You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2023/04/13 08:02:42 UTC

[mynewt-newt] branch master updated: compiler: Add compiler flags $(...) patterns

This is an automated email from the ASF dual-hosted git repository.

janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git


The following commit(s) were added to refs/heads/master by this push:
     new c7b9664b compiler: Add compiler flags $(...) patterns
c7b9664b is described below

commit c7b9664b22afe5ba9d7b38968ac2bc9fd92ed234
Author: Michal Gorecki <mi...@codecoup.pl>
AuthorDate: Mon Feb 27 16:35:23 2023 +0100

    compiler: Add compiler flags $(...) patterns
    
    This adds the possibility to define and use $(...) patterns with
    compiler flags. To do so you have to define new value in compiler's syscfg.yml
    file, for example:
    
    EXAMPLE_FLAGS:
      value:"-my -compiler -flags"
    
    Expression "$(EXAMPLE_FLAGS)" used in any compiler flags definition in compiler.yml will
    be now replaced with the defined value (in this case whith "-my -compiler -flags").
    It is also possible to override or add new patterns from other syscfg.yml files, for example
    from some application's syscfg.yml
---
 newt/builder/targetbuild.go |  9 ++++++++-
 newt/mfg/emit.go            |  2 +-
 newt/toolchain/compiler.go  | 36 ++++++++++++++++++++++++++++--------
 3 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/newt/builder/targetbuild.go b/newt/builder/targetbuild.go
index 8f109306..1eefd800 100644
--- a/newt/builder/targetbuild.go
+++ b/newt/builder/targetbuild.go
@@ -113,8 +113,15 @@ func (t *TargetBuilder) NewCompiler(dstDir string, buildProfile string) (
 		buildProfile = t.target.BuildProfile
 	}
 
+	var cfg *cfgv.Settings
+	cfg = nil
+
+	if t.AppBuilder != nil {
+		cfg = t.AppBuilder.cfg.SettingValues()
+	}
+
 	c, err := toolchain.NewCompiler(
-		t.compilerPkg.BasePath(), dstDir, buildProfile)
+		t.compilerPkg.BasePath(), dstDir, buildProfile, cfg)
 
 	return c, err
 }
diff --git a/newt/mfg/emit.go b/newt/mfg/emit.go
index 6f4ee273..1b763ba0 100644
--- a/newt/mfg/emit.go
+++ b/newt/mfg/emit.go
@@ -167,7 +167,7 @@ func getCompilerFromBsp(bsp *pkg.BspPackage) (*toolchain.Compiler, error) {
 	}
 
 	c, err := toolchain.NewCompiler(compilerPkg.BasePath(), "",
-		target.DEFAULT_BUILD_PROFILE)
+		target.DEFAULT_BUILD_PROFILE, nil)
 	if err != nil {
 		return nil, err
 	}
diff --git a/newt/toolchain/compiler.go b/newt/toolchain/compiler.go
index 7c88655b..1a074c67 100644
--- a/newt/toolchain/compiler.go
+++ b/newt/toolchain/compiler.go
@@ -90,6 +90,7 @@ type Compiler struct {
 	baseDir               string
 	srcDir                string
 	dstDir                string
+	settings              *cfgv.Settings
 
 	// The info to be applied during compilation.
 	info CompilerInfo
@@ -250,7 +251,7 @@ func (ci *CompilerInfo) AddCompilerInfo(newCi *CompilerInfo) {
 }
 
 func NewCompiler(compilerDir string, dstDir string,
-	buildProfile string) (*Compiler, error) {
+	buildProfile string, cfg *cfgv.Settings) (*Compiler, error) {
 
 	c := &Compiler{
 		mutex:           &sync.Mutex{},
@@ -267,7 +268,7 @@ func NewCompiler(compilerDir string, dstDir string,
 	util.StatusMessage(util.VERBOSITY_VERBOSE,
 		"Loading compiler %s, buildProfile %s\n", compilerDir,
 		buildProfile)
-	err := c.load(compilerDir, buildProfile)
+	err := c.load(compilerDir, buildProfile, cfg)
 	if err != nil {
 		return nil, err
 	}
@@ -275,7 +276,19 @@ func NewCompiler(compilerDir string, dstDir string,
 	return c, nil
 }
 
-func loadFlags(yc ycfg.YCfg, settings *cfgv.Settings, key string) []string {
+func replaceVal(cfg *cfgv.Settings) func(string) string {
+
+	return func(m string) string {
+		name := m[2 : len(m)-1]
+		if cfg.Exists(name) {
+			return cfg.Get(name)
+		} else {
+			return ""
+		}
+	}
+}
+
+func loadFlags(yc ycfg.YCfg, settings *cfgv.Settings, key string, cfg *cfgv.Settings) []string {
 	flags := []string{}
 
 	rawFlags, err := yc.GetValStringSlice(key, settings)
@@ -292,10 +305,17 @@ func loadFlags(yc ycfg.YCfg, settings *cfgv.Settings, key string) []string {
 		}
 	}
 
+	if cfg != nil {
+		for i := range flags {
+			re := regexp.MustCompile(`\$\([A-Z0-9_]+\)`)
+			flags[i] = re.ReplaceAllStringFunc(flags[i], replaceVal(cfg))
+		}
+	}
+
 	return flags
 }
 
-func (c *Compiler) load(compilerDir string, buildProfile string) error {
+func (c *Compiler) load(compilerDir string, buildProfile string, cfg *cfgv.Settings) error {
 	yc, err := config.ReadFile(compilerDir + "/" + COMPILER_FILENAME)
 	if err != nil {
 		return err
@@ -327,10 +347,10 @@ func (c *Compiler) load(compilerDir string, buildProfile string) error {
 	c.ocPath, err = yc.GetValString("compiler.path.objcopy", settings)
 	util.OneTimeWarningError(err)
 
-	c.lclInfo.Cflags = loadFlags(yc, settings, "compiler.flags")
-	c.lclInfo.CXXflags = loadFlags(yc, settings, "compiler.cxx.flags")
-	c.lclInfo.Lflags = loadFlags(yc, settings, "compiler.ld.flags")
-	c.lclInfo.Aflags = loadFlags(yc, settings, "compiler.as.flags")
+	c.lclInfo.Cflags = loadFlags(yc, settings, "compiler.flags", cfg)
+	c.lclInfo.CXXflags = loadFlags(yc, settings, "compiler.cxx.flags", cfg)
+	c.lclInfo.Lflags = loadFlags(yc, settings, "compiler.ld.flags", cfg)
+	c.lclInfo.Aflags = loadFlags(yc, settings, "compiler.as.flags", cfg)
 
 	c.ldResolveCircularDeps, err = yc.GetValBool(
 		"compiler.ld.resolve_circular_deps", settings)