Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to define global cflags in packages #534

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion newt/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,40 @@ func buildWorker(
}
}

func (b *Builder) appendAppCflags(bpkgs []*BuildPackage) error {
for _, bpkg := range bpkgs {
settings := b.cfg.AllSettingsForLpkg(bpkg.rpkg.Lpkg)
globalAppCflags, err := bpkg.rpkg.Lpkg.PkgY.Get("app.cflags", settings)
if err != nil {
return err
}
for _, f := range globalAppCflags {
if itfVals, ok := f.Value.([]interface{}); ok {
for _, itfVal := range itfVals {
if strVal, ok := itfVal.(string); ok {
b.compilerInfo.Cflags = append(b.compilerInfo.Cflags, strVal)
}
}
}
}
}

return nil
}

func (b *Builder) Build() error {
var err error

b.CleanArtifacts()

// Build the packages alphabetically to ensure a consistent order.
bpkgs := b.sortedBuildPackages()

err = b.appendAppCflags(bpkgs)
if err != nil {
return err
}

// Calculate the list of jobs. Each record represents a single file that
// needs to be compiled.
entries := []toolchain.CompilerJob{}
Expand Down Expand Up @@ -675,7 +703,6 @@ func (b *Builder) Build() error {
go buildWorker(i, jobs, stop, errors)
}

var err error
for i := 0; i < newtutil.NewtNumJobs; i++ {
subErr := <-errors
if err == nil && subErr != nil {
Expand Down
5 changes: 5 additions & 0 deletions newt/builder/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ func (b *Builder) CMakeTargetWrite(w io.Writer, targetCompiler *toolchain.Compil
var linkFlags []string
var libraries []string

err := b.appendAppCflags(bpkgs)
if err != nil {
return err
}

c := targetCompiler
c.AddInfo(b.GetCompilerInfo())

Expand Down
60 changes: 60 additions & 0 deletions newt/cli/target_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"mynewt.apache.org/newt/newt/ycfg"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -249,6 +250,50 @@ func targetShowCmd(cmd *cobra.Command, args []string) {
}
}

func printCflags(appCflags []ycfg.YCfgEntry) {
for _, f := range appCflags {
if itfVals, ok := f.Value.([]interface{}); ok {
for _, itfVal := range itfVals {
if strVal, ok := itfVal.(string); ok {
fmt.Println(strVal)
}
}
}
}
}

func targetInfoCmd(cmd *cobra.Command, args []string) {
if len(args) < 1 {
NewtUsage(cmd,
util.NewNewtError("Must specify target name"))
}

TryGetProject()

b, err := TargetBuilderForTargetOrUnittest(args[0])
if err != nil {
NewtUsage(cmd, err)
}

if err := b.PrepBuild(); err != nil {
NewtUsage(nil, err)
}

fmt.Println("Packages containing app.cflags:")

for _, rpkg := range b.AppBuilder.SortedRpkgs() {
appCflags, err := rpkg.Lpkg.PkgY.Get("app.cflags", nil)
if err != nil {
NewtUsage(nil, err)
}
if appCflags != nil {
fmt.Println(rpkg.Lpkg.Name())
printCflags(appCflags)
fmt.Println("")
}
}
}

func targetListCmd(cmd *cobra.Command, args []string) {
TryGetProject()
targetNames := []string{}
Expand Down Expand Up @@ -937,6 +982,21 @@ func AddTargetCommands(cmd *cobra.Command) {
return append(targetList(), unittestList()...)
})

infoHelpText := "Shows which packages contain app cflags in the target specified " +
"by <target-name>."
infoHelpEx := " newt target info <target-name>\n"
infoHelpEx += " newt target info my_target1"

infoCmd := &cobra.Command{
Use: "info",
Short: "Show packages with global cflags",
Long: infoHelpText,
Example: infoHelpEx,
Run: targetInfoCmd,
}
targetCmd.AddCommand(infoCmd)
AddTabCompleteFn(infoCmd, targetList)

for _, cmd := range targetCfgCmdAll() {
targetCmd.AddCommand(cmd)
}
Expand Down
Loading