Skip to content

Commit

Permalink
make ExportCmake and PreprocessorSketch a method recevier of arduino/…
Browse files Browse the repository at this point in the history
…builder
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent 8da2287 commit 7748942
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ import (
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder/constants"
)

var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`)

func ExportProjectCMake(
// ExportProjectCMake fixdoc
func (b *Builder) ExportProjectCMake(
sketchError bool, // Was there an error while compiling the sketch?
buildPath, sketchBuildPath *paths.Path,
importedLibraries libraries.List,
buildProperties *properties.Map,
sketch *sketch.Sketch,
includeFolders paths.PathList,
lineOffset int,
onlyUpdateCompilationDatabase bool,
) ([]byte, []byte, error) {
) error {
// copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
Expand Down Expand Up @@ -182,12 +179,12 @@ func ExportProjectCMake(
var validStaticLibExtensions = []string{".a"}

// If sketch error or cannot export Cmake project
if sketchError || buildProperties.Get("compiler.export_cmake") == "" {
return nil, nil, nil
if sketchError || b.buildProperties.Get("compiler.export_cmake") == "" {
return nil
}

// Create new cmake subFolder - clean if the folder is already there
cmakeFolder := buildPath.Join("_cmake")
cmakeFolder := b.buildPath.Join("_cmake")
if _, err := cmakeFolder.Stat(); err == nil {
cmakeFolder.RemoveAll()
}
Expand All @@ -207,7 +204,7 @@ func ExportProjectCMake(
for _, library := range importedLibraries {
// Copy used libraries in the correct folder
libDir := libBaseFolder.Join(library.DirName)
mcu := buildProperties.Get("build.mcu")
mcu := b.buildProperties.Get("build.mcu")
copyDir(library.InstallDir.String(), libDir.String(), validExportExtensions)

// Read cmake options if available
Expand Down Expand Up @@ -238,28 +235,20 @@ func ExportProjectCMake(
}

// Copy core + variant in use + preprocessed sketch in the correct folders
err := copyDir(buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions)
err := copyDir(b.buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions)
if err != nil {
fmt.Println(err)
}
err = copyDir(buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions)
err = copyDir(b.buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions)
if err != nil {
fmt.Println(err)
}

normalOutput, verboseOutput, err := PreprocessSketch(
sketch,
buildPath,
includeFolders,
lineOffset,
buildProperties,
onlyUpdateCompilationDatabase,
)
if err != nil {
return normalOutput, verboseOutput, err
if err := b.PreprocessSketch(includeFolders, lineOffset, onlyUpdateCompilationDatabase); err != nil {
return err
}

err = copyDir(sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions)
err = copyDir(b.sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -294,9 +283,9 @@ func ExportProjectCMake(
var dynamicLibsFromGccMinusL []string
var linkDirectories []string

extractCompileFlags(buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
extractCompileFlags(buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
extractCompileFlags(buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
extractCompileFlags(b.buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
extractCompileFlags(b.buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
extractCompileFlags(b.buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)

// Extract folders with .h in them for adding in include list
headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...)
Expand All @@ -307,7 +296,7 @@ func ExportProjectCMake(

// Generate the CMakeLists global file

projectName := sketch.Name
projectName := b.sketch.Name

cmakelist := "cmake_minimum_required(VERSION 3.5.0)\n"
cmakelist += "INCLUDE(FindPkgConfig)\n"
Expand Down Expand Up @@ -364,7 +353,7 @@ func ExportProjectCMake(

cmakeFile.WriteFile([]byte(cmakelist))

return normalOutput, verboseOutput, nil
return nil
}

func extractCompileFlags(buildProperties *properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {
Expand Down
36 changes: 36 additions & 0 deletions arduino/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of arduino-cli.
//
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package builder

import (
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
"github.com/arduino/go-paths-helper"
)

// PreprocessSketch fixdoc
func (b *Builder) PreprocessSketch(includes paths.PathList, lineOffset int, onlyUpdateCompilationDatabase bool) error {
// In the future we might change the preprocessor
normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags(
b.sketch, b.buildPath, includes, lineOffset,
b.buildProperties, onlyUpdateCompilationDatabase,
)
if b.logger.Verbose() {
b.logger.WriteStdout(verboseOutput)
}
b.logger.WriteStdout(normalOutput)

return err
}
34 changes: 2 additions & 32 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ import (
"time"

"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
"github.com/arduino/arduino-cli/arduino/builder/sizer"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -216,22 +212,13 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
normalOutput, verboseOutput, err := ExportProjectCMake(
return ctx.Builder.ExportProjectCMake(
mainErr != nil,
ctx.Builder.GetBuildPath(), ctx.Builder.GetSketchBuildPath(),
ctx.SketchLibrariesDetector.ImportedLibraries(),
ctx.Builder.GetBuildProperties(),
ctx.Builder.Sketch(),
ctx.SketchLibrariesDetector.IncludeFolders(),
ctx.LineOffset,
ctx.OnlyUpdateCompilationDatabase,
)
if ctx.BuilderLogger.Verbose() {
ctx.BuilderLogger.WriteStdout(verboseOutput)
} else {
ctx.BuilderLogger.WriteStdout(normalOutput)
}
return err
}),

types.BareCommand(func(ctx *types.Context) error {
Expand Down Expand Up @@ -264,27 +251,10 @@ func (s *Builder) Run(ctx *types.Context) error {

func preprocessSketchCommand(ctx *types.Context) types.BareCommand {
return func(ctx *types.Context) error {
normalOutput, verboseOutput, err := PreprocessSketch(
ctx.Builder.Sketch(), ctx.Builder.GetBuildPath(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset,
ctx.Builder.GetBuildProperties(), ctx.OnlyUpdateCompilationDatabase)
if ctx.BuilderLogger.Verbose() {
ctx.BuilderLogger.WriteStdout(verboseOutput)
} else {
ctx.BuilderLogger.WriteStdout(normalOutput)
}
return err
return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.OnlyUpdateCompilationDatabase)
}
}

func PreprocessSketch(
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int,
buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
) ([]byte, []byte, error) {
// In the future we might change the preprocessor
preprocessorImpl := preprocessor.PreprocessSketchWithCtags
return preprocessorImpl(sketch, buildPath, includes, lineOffset, buildProperties, onlyUpdateCompilationDatabase)
}

type Preprocess struct{}

func (s *Preprocess) Run(ctx *types.Context) error {
Expand Down

0 comments on commit 7748942

Please sign in to comment.