This is an automated email from the ASF dual-hosted git repository. andk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git
commit 5cb641e33f065bd70d8ae0bc79e5135b08960b70 Author: Andrzej Kaczmarek <andrzej.kaczma...@codecoup.pl> AuthorDate: Mon Jan 30 14:25:23 2023 +0100 builder: Allow to specify source files for package This adds pkg.source_files to pkg.yml which allows to specify files which should be included in build. Once set, it overrides default src/ directory so it has to be added manually if required. --- newt/builder/build.go | 29 ++++++++++++++++++++++++++++- newt/builder/buildpackage.go | 5 +++++ newt/toolchain/compiler.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/newt/builder/build.go b/newt/builder/build.go index 8dc94379..5c0f323a 100644 --- a/newt/builder/build.go +++ b/newt/builder/build.go @@ -341,7 +341,8 @@ func (b *Builder) collectCompileEntriesBpkg(bpkg *BuildPackage) ( } srcDirs = append(srcDirs, dir) } - } else { + } else if len(bpkg.SourceFiles) == 0 { + // Add 'src/' automatically only if neither source dirs nor files are defined srcDir := bpkg.rpkg.Lpkg.BasePath() + "/src" if util.NodeNotExist(srcDir) { // Nothing to compile. @@ -362,6 +363,32 @@ func (b *Builder) collectCompileEntriesBpkg(bpkg *BuildPackage) ( entries = append(entries, subEntries...) } + for _, filename := range bpkg.SourceFiles { + var dir string + repo, path, err := newtutil.ParsePackageString(filename) + + if err != nil { + return nil, err + } + + if repo != "" { + filename = "repos/" + repo + "/" + path + } else { + filename = bpkg.rpkg.Lpkg.BasePath() + "/" + filename + } + + if util.NodeNotExist(filename) { + return nil, util.NewNewtError(fmt.Sprintf( + "Specified source directory %s, does not exist.", + dir)) + } + + entry, err := c.CollectSingleEntry(filename) + if err == nil { + entries = append(entries, *entry) + } + } + return entries, nil } diff --git a/newt/builder/buildpackage.go b/newt/builder/buildpackage.go index 94535e62..c731872e 100644 --- a/newt/builder/buildpackage.go +++ b/newt/builder/buildpackage.go @@ -35,6 +35,7 @@ import ( type BuildPackage struct { rpkg *resolve.ResolvePackage SourceDirectories []string + SourceFiles []string ci *toolchain.CompilerInfo } @@ -220,6 +221,10 @@ func (bpkg *BuildPackage) CompilerInfo( util.OneTimeWarningError(err) } + bpkg.SourceFiles, err = bpkg.rpkg.Lpkg.PkgY.GetValStringSlice( + "pkg.source_files", settings) + util.OneTimeWarningError(err) + includePaths, err := bpkg.recursiveIncludePaths(b) if err != nil { return nil, err diff --git a/newt/toolchain/compiler.go b/newt/toolchain/compiler.go index 5691f0cb..be9135ed 100644 --- a/newt/toolchain/compiler.go +++ b/newt/toolchain/compiler.go @@ -667,6 +667,21 @@ func compilerTypeToExts(compilerType int) ([]string, error) { } } +func fileNameToCompilerType(filename string) (int, error) { + switch filepath.Ext(filename) { + case ".c": + return COMPILER_TYPE_C, nil + case ".s", ".S": + return COMPILER_TYPE_ASM, nil + case ".cc", ".cpp", ".cxx": + return COMPILER_TYPE_CPP, nil + case ".a": + return COMPILER_TYPE_ARCHIVE, nil + default: + return -1, util.NewNewtError("Unknown file type for " + filename) + } +} + // Compiles all C files matching the specified file glob. func (c *Compiler) CompileC(filename string) error { filename = filepath.ToSlash(filename) @@ -815,6 +830,21 @@ func (c *Compiler) processEntry(node os.FileInfo, cType int, return entries, err } +func (c *Compiler) CollectSingleEntry(filename string) (*CompilerJob, error) { + file := filepath.ToSlash(filename) + ctype, err := fileNameToCompilerType(file) + + if err != nil { + return nil, err + } + + return &CompilerJob{ + Filename: file, + Compiler: c, + CompilerType: ctype, + }, nil +} + func (c *Compiler) RecursiveCollectEntries(cType int, ignDirs []string) ([]CompilerJob, error) {