From 3d5b450b8374f384278558f6c58c48473345a94b Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Fri, 8 Sep 2023 17:08:39 +0200 Subject: [PATCH] refactor CreateMakeRule in a function --- legacy/builder/builder.go | 56 ++++++++++++++++++------ legacy/builder/create_cmake_rule.go | 67 +++++++++++++++++------------ 2 files changed, 82 insertions(+), 41 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 1a94647f696..09c364ee1d4 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -20,9 +20,12 @@ import ( "time" "github.com/arduino/arduino-cli/arduino/builder/preprocessor" + "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/phases" "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" ) @@ -58,7 +61,7 @@ func (s *Builder) Run(ctx *types.Context) error { warnAboutArchIncompatibleLibraries(ctx), logIfVerbose(false, tr("Generating function prototypes...")), - types.BareCommand(PreprocessSketch), + preprocessSketchCommand(ctx), logIfVerbose(false, tr("Compiling sketch...")), @@ -249,7 +252,24 @@ func (s *Builder) Run(ctx *types.Context) error { return nil }), - &ExportProjectCMake{SketchError: mainErr != nil}, + types.BareCommand(func(ctx *types.Context) error { + normalOutput, verboseOutput, err := ExportProjectCMake( + mainErr != nil, + ctx.BuildPath, ctx.SketchBuildPath, + ctx.SketchLibrariesDetector.ImportedLibraries(), + ctx.BuildProperties, + ctx.Sketch, + ctx.SketchLibrariesDetector.IncludeFolders(), + ctx.LineOffset, + ctx.OnlyUpdateCompilationDatabase, + ) + if ctx.Verbose { + ctx.WriteStdout(verboseOutput) + } else { + ctx.WriteStdout(normalOutput) + } + return err + }), types.BareCommand(func(ctx *types.Context) error { executableSectionsSize, err := phases.Sizer( @@ -282,17 +302,27 @@ func (s *Builder) Run(ctx *types.Context) error { return otherErr } -func PreprocessSketch(ctx *types.Context) error { - preprocessorImpl := preprocessor.PreprocessSketchWithCtags - normalOutput, verboseOutput, err := preprocessorImpl( - ctx.Sketch, ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, - ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase) - if ctx.Verbose { - ctx.WriteStdout(verboseOutput) - } else { - ctx.WriteStdout(normalOutput) +func preprocessSketchCommand(ctx *types.Context) types.BareCommand { + return func(ctx *types.Context) error { + normalOutput, verboseOutput, err := PreprocessSketch( + ctx.Sketch, ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, + ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase) + if ctx.Verbose { + ctx.WriteStdout(verboseOutput) + } else { + ctx.WriteStdout(normalOutput) + } + return err } - return err +} + +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{} @@ -319,7 +349,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { warnAboutArchIncompatibleLibraries(ctx), - types.BareCommand(PreprocessSketch), + preprocessSketchCommand(ctx), } if err := runCommands(ctx, commands); err != nil { diff --git a/legacy/builder/create_cmake_rule.go b/legacy/builder/create_cmake_rule.go index 262218150f2..d24b31df8ad 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/legacy/builder/create_cmake_rule.go @@ -23,23 +23,29 @@ import ( "regexp" "strings" + "github.com/arduino/go-paths-helper" properties "github.com/arduino/go-properties-orderedmap" "golang.org/x/exp/slices" "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" - "github.com/arduino/arduino-cli/legacy/builder/types" ) -type ExportProjectCMake struct { - // Was there an error while compiling the sketch? - SketchError bool -} - var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`) -func (s *ExportProjectCMake) Run(ctx *types.Context) error { +func 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) { // 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 @@ -175,12 +181,13 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { } var validStaticLibExtensions = []string{".a"} - if s.SketchError || !canExportCmakeProject(ctx) { - return nil + // If sketch error or cannot export Cmake project + if sketchError || buildProperties.Get("compiler.export_cmake") == "" { + return nil, nil, nil } // Create new cmake subFolder - clean if the folder is already there - cmakeFolder := ctx.BuildPath.Join("_cmake") + cmakeFolder := buildPath.Join("_cmake") if _, err := cmakeFolder.Stat(); err == nil { cmakeFolder.RemoveAll() } @@ -197,10 +204,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { cmakeFile := cmakeFolder.Join("CMakeLists.txt") dynamicLibsFromPkgConfig := map[string]bool{} - for _, library := range ctx.SketchLibrariesDetector.ImportedLibraries() { + for _, library := range importedLibraries { // Copy used libraries in the correct folder libDir := libBaseFolder.Join(library.DirName) - mcu := ctx.BuildProperties.Get("build.mcu") + mcu := buildProperties.Get("build.mcu") copyDir(library.InstallDir.String(), libDir.String(), validExportExtensions) // Read cmake options if available @@ -231,20 +238,28 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { } // Copy core + variant in use + preprocessed sketch in the correct folders - err := copyDir(ctx.BuildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions) + err := copyDir(buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions) if err != nil { fmt.Println(err) } - err = copyDir(ctx.BuildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions) + err = copyDir(buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions) if err != nil { fmt.Println(err) } - if err := PreprocessSketch(ctx); err != nil { - return err + normalOutput, verboseOutput, err := PreprocessSketch( + sketch, + buildPath, + includeFolders, + lineOffset, + buildProperties, + onlyUpdateCompilationDatabase, + ) + if err != nil { + return normalOutput, verboseOutput, err } - err = copyDir(ctx.SketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions) + err = copyDir(sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions) if err != nil { fmt.Println(err) } @@ -279,9 +294,9 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { var dynamicLibsFromGccMinusL []string var linkDirectories []string - extractCompileFlags(ctx, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) - extractCompileFlags(ctx, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) - extractCompileFlags(ctx, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) + 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) // Extract folders with .h in them for adding in include list headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...) @@ -292,7 +307,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { // Generate the CMakeLists global file - projectName := ctx.Sketch.Name + projectName := sketch.Name cmakelist := "cmake_minimum_required(VERSION 3.5.0)\n" cmakelist += "INCLUDE(FindPkgConfig)\n" @@ -349,14 +364,10 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { cmakeFile.WriteFile([]byte(cmakelist)) - return nil -} - -func canExportCmakeProject(ctx *types.Context) bool { - return ctx.BuildProperties.Get("compiler.export_cmake") != "" + return normalOutput, verboseOutput, nil } -func extractCompileFlags(ctx *types.Context, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) { +func extractCompileFlags(buildProperties *properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) { appendIfNotPresent := func(target []string, elements ...string) []string { for _, element := range elements { if !slices.Contains(target, element) { @@ -366,7 +377,7 @@ func extractCompileFlags(ctx *types.Context, recipe string, defines, dynamicLibs return target } - command, _ := utils.PrepareCommandForRecipe(ctx.BuildProperties, recipe, true) + command, _ := utils.PrepareCommandForRecipe(buildProperties, recipe, true) for _, arg := range command.GetArgs() { if strings.HasPrefix(arg, "-D") {