Skip to content

Commit

Permalink
compiler: Add compiler flags $(...) patterns
Browse files Browse the repository at this point in the history
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
  • Loading branch information
m-gorecki authored and sjanc committed Apr 13, 2023
1 parent 0b46ad4 commit c7b9664
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
9 changes: 8 additions & 1 deletion newt/builder/targetbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion newt/mfg/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 28 additions & 8 deletions newt/toolchain/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{},
Expand All @@ -267,15 +268,27 @@ 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
}

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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c7b9664

Please sign in to comment.