From ae555b45e2822de8d7fdcab8ed36d80479868c2c Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 18:16:07 +0200 Subject: [PATCH] make ExportCmake and PreprocessorSketch a method recevier of arduino/builder --- .../builder/export_cmake.go | 44 +++++++------------ arduino/builder/preprocess_sketch.go | 36 +++++++++++++++ legacy/builder/builder.go | 34 +------------- 3 files changed, 54 insertions(+), 60 deletions(-) rename legacy/builder/create_cmake_rule.go => arduino/builder/export_cmake.go (90%) create mode 100644 arduino/builder/preprocess_sketch.go diff --git a/legacy/builder/create_cmake_rule.go b/arduino/builder/export_cmake.go similarity index 90% rename from legacy/builder/create_cmake_rule.go rename to arduino/builder/export_cmake.go index d24b31df8ad..7bddd29456c 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/arduino/builder/export_cmake.go @@ -30,22 +30,18 @@ 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( +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 @@ -182,12 +178,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() } @@ -207,7 +203,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 @@ -238,28 +234,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) } @@ -294,9 +282,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...) @@ -307,7 +295,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" @@ -364,7 +352,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) { diff --git a/arduino/builder/preprocess_sketch.go b/arduino/builder/preprocess_sketch.go new file mode 100644 index 00000000000..5229feb3446 --- /dev/null +++ b/arduino/builder/preprocess_sketch.go @@ -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 license@arduino.cc. + +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 +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 5bfaa8ff9de..a901e5a112d 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -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" ) @@ -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 { @@ -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 {