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 2026/10/22 13:09:56 UTC

(mynewt-newt) branch master updated (9cf4db85 -> 972e5bcf)

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

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


    from 9cf4db85 ci: Report more CodeQL findings
     new 733af491 newt/toolchain: Add compiler.c.flags for C-only flags
     new 972e5bcf newt/toolchain: Add unit test for compiler.c.flags

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 newt/builder/cmake.go        |  2 ++
 newt/toolchain/compiler.go   | 16 ++++++++++++++--
 newt/toolchain/flags_test.go | 27 +++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)


(mynewt-newt) 01/02: newt/toolchain: Add compiler.c.flags for C-only flags

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 733af491ac082062648ee25ac1975453fc9da791
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Tue Oct 20 00:51:28 2026 +0200

    newt/toolchain: Add compiler.c.flags for C-only flags
    
    compiler.flags are passed to C, C++ and assembler invocations, so there
    is no way for a compiler package to set flags that are valid for C
    only. For example -std=c11 makes g++ fail with -Werror:
    
      cc1plus: error: command-line option '-std=c11' is valid for C/ObjC
      but not for C++ [-Werror]
    
    Add compiler.c.flags which are used only when compiling C files. They
    have the lowest priority: a C-only flag is dropped if the common flags
    already contain one with the same base, so a package setting e.g.
    -std=gnu99 in pkg.cflags keeps its own language standard.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/builder/cmake.go      |  2 ++
 newt/toolchain/compiler.go | 16 ++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/newt/builder/cmake.go b/newt/builder/cmake.go
index 05940e6d..af95634e 100644
--- a/newt/builder/cmake.go
+++ b/newt/builder/cmake.go
@@ -122,6 +122,8 @@ func CmakeSourceObjectWrite(w io.Writer, cj toolchain.CompilerJob,
 	case toolchain.COMPILER_TYPE_C:
 		flags = append(flags, c.GetCompilerInfo().Cflags...)
 		flags = append(flags, c.GetLocalCompilerInfo().Cflags...)
+		flags = append(flags, c.GetCompilerInfo().CCflags...)
+		flags = append(flags, c.GetLocalCompilerInfo().CCflags...)
 	case toolchain.COMPILER_TYPE_ASM:
 		flags = append(flags, c.GetCompilerInfo().Cflags...)
 		flags = append(flags, c.GetLocalCompilerInfo().Cflags...)
diff --git a/newt/toolchain/compiler.go b/newt/toolchain/compiler.go
index f920fc05..80855c04 100644
--- a/newt/toolchain/compiler.go
+++ b/newt/toolchain/compiler.go
@@ -58,6 +58,7 @@ const (
 type CompilerInfo struct {
 	Includes    []string
 	Cflags      []string
+	CCflags     []string
 	CXXflags    []string
 	Lflags      []string
 	Aflags      []string
@@ -243,6 +244,7 @@ func NewCompilerInfo() *CompilerInfo {
 	ci := &CompilerInfo{}
 	ci.Includes = []string{}
 	ci.Cflags = []string{}
+	ci.CCflags = []string{}
 	ci.CXXflags = []string{}
 	ci.Lflags = []string{}
 	ci.Aflags = []string{}
@@ -335,6 +337,7 @@ func (ci *CompilerInfo) AddCflags(cflags []string) {
 func (ci *CompilerInfo) AddCompilerInfo(newCi *CompilerInfo) {
 	ci.Includes = append(ci.Includes, newCi.Includes...)
 	ci.Cflags = addFlags("cflag", ci.Cflags, newCi.Cflags)
+	ci.CCflags = addFlags("ccflag", ci.CCflags, newCi.CCflags)
 	ci.CXXflags = addFlags("cxxflag", ci.CXXflags, newCi.CXXflags)
 	ci.Lflags = addFlags("lflag", ci.Lflags, newCi.Lflags)
 	ci.Aflags = addFlags("aflag", ci.Aflags, newCi.Aflags)
@@ -498,6 +501,7 @@ func (c *Compiler) load(compilerDir string, buildProfile string, cfg *cfgv.Setti
 	}
 
 	c.lclInfo.Cflags = loadFlags(yc, settings, "compiler.flags", cfg)
+	c.lclInfo.CCflags = loadFlags(yc, settings, "compiler.c.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)
@@ -603,6 +607,14 @@ func (c *Compiler) cflagsStrings() []string {
 	return cflags
 }
 
+// Returns flags for compiling C files: common flags followed by C-only flags.
+// A C-only flag is dropped if a common flag with the same base is already
+// present (e.g., -std from a package overrides -std from the compiler).
+func (c *Compiler) ccflagsStrings() []string {
+	return addFlags("ccflag", c.cflagsStrings(),
+		SortFlags(c.info.CCflags...))
+}
+
 func (c *Compiler) cxxflagsStrings() []string {
 	cxxflags := SortFlags(c.info.CXXflags...)
 	return cxxflags
@@ -653,7 +665,7 @@ func (c *Compiler) CompileFileCmd(file string, compilerType int) (
 	case COMPILER_TYPE_C:
 		cmdName = c.ccPath
 		toolKey = "compiler.path.cc"
-		flags = c.cflagsStrings()
+		flags = c.ccflagsStrings()
 	case COMPILER_TYPE_ASM:
 		cmdName = c.asPath
 		toolKey = "compiler.path.as"
@@ -704,7 +716,7 @@ func (c *Compiler) GenDepsForFile(file string, compilerType int) error {
 	case COMPILER_TYPE_C:
 		cmdName = c.ccPath
 		toolKey = "compiler.path.cc"
-		flags = c.cflagsStrings()
+		flags = c.ccflagsStrings()
 	case COMPILER_TYPE_ASM:
 		cmdName = c.asPath
 		toolKey = "compiler.path.as"


(mynewt-newt) 02/02: newt/toolchain: Add unit test for compiler.c.flags

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 972e5bcfc6689b2cd0528522746b45bec497aadc
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Tue Oct 20 15:55:31 2026 +0200

    newt/toolchain: Add unit test for compiler.c.flags
    
    Verify that compiler.c.flags are appended to C flags, that a C-only
    flag is dropped when the common flags already contain one with the
    same base, and that an option with a separate argument stays together
    with it.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/toolchain/flags_test.go | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/newt/toolchain/flags_test.go b/newt/toolchain/flags_test.go
index fb25da16..88d7a480 100644
--- a/newt/toolchain/flags_test.go
+++ b/newt/toolchain/flags_test.go
@@ -109,3 +109,30 @@ func TestCflagsStringsPairs(t *testing.T) {
 		t.Errorf("unexpected flags:\nexpected %q\ngot      %q", exp, got)
 	}
 }
+
+func TestCcflagsStrings(t *testing.T) {
+	tests := []struct {
+		cflags  []string
+		ccflags []string
+		exp     []string
+	}{
+		{[]string{"-Wall"}, []string{"-std=c11"},
+			[]string{"-Wall", "-std=c11"}},
+		{[]string{"-Wall", "-std=gnu99"}, []string{"-std=c11"},
+			[]string{"-Wall", "-std=gnu99"}},
+		{[]string{"-Wall"}, []string{}, []string{"-Wall"}},
+		{[]string{"-Wall"}, []string{"-include", "a.h", "-std=c11"},
+			[]string{"-Wall", "-std=c11", "-include", "a.h"}},
+	}
+
+	for _, tt := range tests {
+		c := &Compiler{}
+		c.info.Cflags = tt.cflags
+		c.info.CCflags = tt.ccflags
+		got := c.ccflagsStrings()
+		if !slices.Equal(got, tt.exp) {
+			t.Errorf("ccflagsStrings() with cflags %q ccflags %q = %q, "+
+				"expected %q", tt.cflags, tt.ccflags, got, tt.exp)
+		}
+	}
+}