This is an automated email from the ASF dual-hosted git repository.
kasjer 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 59d6a96d newt/toolchain: Use environment for empty $(NAME) flags
59d6a96d is described below
commit 59d6a96d731fd067571c2be9cf508f2412372e56
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Sun Oct 18 22:53:35 2026 +0200
newt/toolchain: Use environment for empty $(NAME) flags
Compiler flags in compiler.yml can contain $(NAME) patterns that are
replaced with value of syscfg setting NAME. There was no way to provide
machine specific values (e.g. path to Microchip device family pack
required by XC32 v5) without adding them to some package.
Now when setting does not exist or its value is empty, value of
environment variable with the same name is used. Non-empty syscfg value
still takes precedence.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jerzy Kasenberg <[email protected]>
---
newt/toolchain/compiler.go | 10 +++++++---
newt/toolchain/compiler_test.go | 28 ++++++++++++++++++++++++++++
2 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/newt/toolchain/compiler.go b/newt/toolchain/compiler.go
index 306818b1..e0e38250 100644
--- a/newt/toolchain/compiler.go
+++ b/newt/toolchain/compiler.go
@@ -369,15 +369,19 @@ func NewCompiler(compilerDir string, dstDir string,
return c, nil
}
+// Returns function that replaces $(NAME) pattern with value of syscfg
+// setting NAME. If setting does not exist or its value is empty, value of
+// environment variable NAME is used instead. This allows to provide
+// machine specific values (e.g. paths) without modifying any package.
func replaceVal(cfg *cfgv.Settings) func(string) string {
return func(m string) string {
name := m[2 : len(m)-1]
- if cfg.Exists(name) {
+ if cfg.Exists(name) && cfg.Get(name) != "" {
return cfg.Get(name)
- } else {
- return ""
}
+
+ return os.Getenv(name)
}
}
diff --git a/newt/toolchain/compiler_test.go b/newt/toolchain/compiler_test.go
index 33848322..5685f742 100644
--- a/newt/toolchain/compiler_test.go
+++ b/newt/toolchain/compiler_test.go
@@ -123,3 +123,31 @@ func TestCompilerSyscfgCond(t *testing.T) {
})
}
}
+
+func TestReplaceVal(t *testing.T) {
+ cfg := cfgv.NewSettingsFromMap(map[string]string{
+ "SET_FLAG": "-mset",
+ "EMPTY_FLAG": "",
+ })
+ replace := replaceVal(cfg)
+
+ t.Setenv("SET_FLAG", "-menv")
+ t.Setenv("EMPTY_FLAG", "-mdfp=/path/to/dfp")
+ t.Setenv("ENV_ONLY_FLAG", "-menv_only")
+
+ tests := []struct {
+ pattern string
+ want string
+ }{
+ {"$(SET_FLAG)", "-mset"},
+ {"$(EMPTY_FLAG)", "-mdfp=/path/to/dfp"},
+ {"$(ENV_ONLY_FLAG)", "-menv_only"},
+ {"$(UNDEFINED_FLAG)", ""},
+ }
+
+ for _, tc := range tests {
+ if got := replace(tc.pattern); got != tc.want {
+ t.Errorf("replaceVal(%q) = %q, want %q", tc.pattern,
got, tc.want)
+ }
+ }
+}