From c719fde48fd5e28bfb5967613b30db6b69087731 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Tue, 20 Jun 2023 00:41:25 +0200 Subject: [PATCH] syscfg: Write defs for each APIs provided by included packages 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. --- newt/builder/targetbuild.go | 6 +++++- newt/syscfg/syscfg.go | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/newt/builder/targetbuild.go b/newt/builder/targetbuild.go index 1eefd8006..dc54b68e4 100644 --- a/newt/builder/targetbuild.go +++ b/newt/builder/targetbuild.go @@ -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 } diff --git a/newt/syscfg/syscfg.go b/newt/syscfg/syscfg.go index a9906685a..e7f62ff09 100644 --- a/newt/syscfg/syscfg.go +++ b/newt/syscfg/syscfg.go @@ -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") @@ -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 { @@ -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