Skip to content

Commit

Permalink
refactor CreateMakeRule in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 8, 2023
1 parent ba0852c commit 90e539b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 41 deletions.
56 changes: 43 additions & 13 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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...")),

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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{}
Expand All @@ -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 {
Expand Down
67 changes: 39 additions & 28 deletions legacy/builder/create_cmake_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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...)
Expand All @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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") {
Expand Down

0 comments on commit 90e539b

Please sign in to comment.