Skip to content

Commit

Permalink
syscfg: Write defs for each APIs provided by included packages
Browse files Browse the repository at this point in the history
This adds symbols in syscfg.h for each API provided by packages included
in build. The symbol name has "MYNEWT_API_" prefix followed with sanitized
API name, all illegal characters in resulting name are replaced with "_".

This allows to check if any package included in build provides specific
API. Could be useful if some API is not strictly required, but we can
use it if provided by some package.
  • Loading branch information
andrzej-kaczmarek committed Jul 10, 2023
1 parent 9dbd3da commit c719fde
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 5 additions & 1 deletion newt/builder/targetbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ func (t *TargetBuilder) validateAndWriteCfg() error {
srcDir := GeneratedSrcDir(t.target.FullName())

lpkgs := resolve.RpkgSliceToLpkgSlice(t.res.MasterSet.Rpkgs)
if err := syscfg.EnsureWritten(t.res.Cfg, incDir, lpkgs); err != nil {
apis := []string{}
for api := range t.res.ApiMap {
apis = append(apis, api)
}
if err := syscfg.EnsureWritten(t.res.Cfg, incDir, lpkgs, apis); err != nil {
return err
}

Expand Down
21 changes: 18 additions & 3 deletions newt/syscfg/syscfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,19 @@ func writePackages(lpkgs []*pkg.LocalPackage, w io.Writer) {
}
}

func write(cfg Cfg, lpkgs []*pkg.LocalPackage, w io.Writer) {
func writeApis(apis []string, w io.Writer) {
for i, api := range apis {
apis[i] = cfgPkgIllegalChar.ReplaceAllLiteralString(api, "_")
}
sort.Strings(apis)

fmt.Fprintf(w, "/*** Included APIs */\n")
for _, name := range apis {
fmt.Fprintf(w, "#define MYNEWT_API_%s 1\n", name)
}
}

func write(cfg Cfg, lpkgs []*pkg.LocalPackage, apis []string, w io.Writer) {
fmt.Fprintf(w, newtutil.GeneratedPreamble())

fmt.Fprintf(w, "#ifndef H_MYNEWT_SYSCFG_\n")
Expand All @@ -1619,10 +1631,13 @@ func write(cfg Cfg, lpkgs []*pkg.LocalPackage, w io.Writer) {
writePackages(lpkgs, w)
fmt.Fprintf(w, "\n")

writeApis(apis, w)
fmt.Fprintf(w, "\n")

fmt.Fprintf(w, "#endif\n")
}

func EnsureWritten(cfg Cfg, includeDir string, lpkgs []*pkg.LocalPackage) error {
func EnsureWritten(cfg Cfg, includeDir string, lpkgs []*pkg.LocalPackage, apis []string) error {
// XXX: Detect these problems at error text generation time.
if err := calcPriorities(cfg, CFG_SETTING_TYPE_TASK_PRIO,
SYSCFG_TASK_PRIO_MAX, false); err != nil {
Expand All @@ -1631,7 +1646,7 @@ func EnsureWritten(cfg Cfg, includeDir string, lpkgs []*pkg.LocalPackage) error
}

buf := bytes.Buffer{}
write(cfg, lpkgs, &buf)
write(cfg, lpkgs, apis, &buf)

path := includeDir + "/" + HEADER_PATH

Expand Down

0 comments on commit c719fde

Please sign in to comment.