This is an automated email from the ASF dual-hosted git repository.

naraj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git


The following commit(s) were added to refs/heads/master by this push:
     new b4d77a8  cmake: Fix processing archive files
b4d77a8 is described below

commit b4d77a8eacb454d0ed8fef13b3510407f65b2787
Author: MichaƂ Narajowski <[email protected]>
AuthorDate: Thu Jun 25 15:07:33 2020 +0200

    cmake: Fix processing archive files
    
    Libraries were treated like source files and were not properly
    handled by cmake.
---
 newt/builder/cmake.go | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)

diff --git a/newt/builder/cmake.go b/newt/builder/cmake.go
index 7eeaeb1..84517a3 100644
--- a/newt/builder/cmake.go
+++ b/newt/builder/cmake.go
@@ -25,6 +25,7 @@ import (
        "fmt"
        "io"
        "os"
+       "path"
        "path/filepath"
        "strings"
 
@@ -47,6 +48,13 @@ func EscapeName(name string) string {
        return strings.Replace(name, "/", "_", -1)
 }
 
+func ExtractLibraryName(filepath string) string {
+       _, name := path.Split(filepath)
+       name = strings.TrimPrefix(name, "lib")
+       name = strings.TrimSuffix(name, ".a")
+       return name
+}
+
 func replaceBackslashes(path string) string {
        return strings.Replace(path, "\\", "/", -1)
 }
@@ -136,7 +144,7 @@ func CmakeSourceObjectWrite(w io.Writer, cj 
toolchain.CompilerJob,
 }
 
 func (b *Builder) CMakeBuildPackageWrite(w io.Writer, bpkg *BuildPackage,
-       linkFlags *[]string) (*BuildPackage, error) {
+       linkFlags *[]string, libraries *[]string) (*BuildPackage, error) {
        entries, err := b.collectCompileEntriesBpkg(bpkg)
        if err != nil {
                return nil, err
@@ -148,6 +156,7 @@ func (b *Builder) CMakeBuildPackageWrite(w io.Writer, bpkg 
*BuildPackage,
 
        otherIncludes := []string{}
        files := []string{}
+       linkDirs := []string{}
 
        for _, s := range entries {
                filename := filepath.ToSlash(s.Filename)
@@ -156,9 +165,19 @@ func (b *Builder) CMakeBuildPackageWrite(w io.Writer, bpkg 
*BuildPackage,
                        continue
                }
 
-               CmakeSourceObjectWrite(w, s, &otherIncludes, linkFlags)
-               s.Filename = trimProjectPath(s.Filename)
-               files = append(files, s.Filename)
+               if s.CompilerType == toolchain.COMPILER_TYPE_ARCHIVE {
+                       arFile := trimProjectPath(s.Filename)
+                       linkDirs = append(linkDirs, filepath.Dir(arFile))
+                       *libraries = append(*libraries, arFile)
+               } else {
+                       CmakeSourceObjectWrite(w, s, &otherIncludes, linkFlags)
+                       s.Filename = trimProjectPath(s.Filename)
+                       files = append(files, s.Filename)
+               }
+       }
+
+       if len(linkDirs) > 0 {
+               fmt.Fprintf(w, "link_directories(%s)\n", 
strings.Join(util.SortFields(linkDirs...), " "))
        }
 
        if len(files) <= 0 {
@@ -169,10 +188,9 @@ func (b *Builder) CMakeBuildPackageWrite(w io.Writer, bpkg 
*BuildPackage,
 
        util.StatusMessage(util.VERBOSITY_DEFAULT, "Generating CMakeLists.txt 
for %s\n", pkgName)
        fmt.Fprintf(w, "\n# Generating CMakeLists.txt for %s\n", pkgName)
-       fmt.Fprintf(w, "add_library(%s %s)\n\n",
+       fmt.Fprintf(w, "add_library(%s %s)\n",
                EscapeName(pkgName),
                strings.Join(files, " "))
-
        archivePath := 
replaceBackslashes(trimProjectPath(filepath.Dir(b.ArchivePath(bpkg))))
        CmakeCompilerInfoWrite(w, archivePath, bpkg, entries[0], otherIncludes)
 
@@ -183,13 +201,15 @@ func (b *Builder) CMakeTargetWrite(w io.Writer, 
targetCompiler *toolchain.Compil
        bpkgs := b.sortedBuildPackages()
        var compileFlags []string
        var linkFlags []string
+       var libraries []string
 
        c := targetCompiler
        c.AddInfo(b.GetCompilerInfo())
 
        builtPackages := []*BuildPackage{}
        for _, bpkg := range bpkgs {
-               builtPackage, err := b.CMakeBuildPackageWrite(w, bpkg, 
&linkFlags)
+               builtPackage, err := b.CMakeBuildPackageWrite(w, bpkg,
+                       &linkFlags, &libraries)
                if err != nil {
                        return err
                }
@@ -209,6 +229,11 @@ func (b *Builder) CMakeTargetWrite(w io.Writer, 
targetCompiler *toolchain.Compil
                        EscapeName(bpkg.rpkg.Lpkg.Name())))
        }
 
+       for _, filename := range libraries {
+               targetObjectsBuffer.WriteString(fmt.Sprintf("%s ",
+                       ExtractLibraryName(filename)))
+       }
+
        elfOutputDir := 
replaceBackslashes(trimProjectPath(filepath.Dir(b.AppElfPath())))
        fmt.Fprintf(w, "file(WRITE %s \"\")\n", 
replaceBackslashes(filepath.Join(elfOutputDir, "null.c")))
        fmt.Fprintf(w, "add_executable(%s %s)\n\n", elfName,

Reply via email to