Skip to content

linker: allow multi-step link recipe (c.combine) #2954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/platform-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ compiler.libraries.ldflags=
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} {compiler.libraries.ldflags} "{archive_file_path}" "-L{build.path}" -lm
```

If the linking process requires multiple steps, the recipe can be written using the **recipe.c.combine.NUMBER.pattern**
syntax. In this case, each step will be executed in the order specified by the number. When multiple steps are defined,
the **recipe.c.combine.pattern** property is ignored.

#### Recipes for extraction of executable files and other binary data

An arbitrary number of extra steps can be performed at the end of objects linking. These steps can be used to extract
Expand Down
2 changes: 1 addition & 1 deletion internal/arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (b *Builder) build() error {
}
b.Progress.CompleteStep()

if err := b.RunRecipe("recipe.objcopy.", ".pattern", true); err != nil {
if err := b.RunRecipe("recipe.objcopy", ".pattern", true); err != nil {
return err
}
b.Progress.CompleteStep()
Expand Down
7 changes: 1 addition & 6 deletions internal/arduino/builder/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,5 @@ func (b *Builder) link() error {
properties.Set("archive_file_path", b.buildArtifacts.coreArchiveFilePath.String())
properties.Set("object_files", objectFileList)

command, err := b.prepareCommandForRecipe(properties, "recipe.c.combine.pattern", false)
if err != nil {
return err
}

return b.execCommand(command)
return b.RunRecipeWithProps("recipe.c.combine", ".pattern", properties, false)
}
17 changes: 14 additions & 3 deletions internal/arduino/builder/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import (

// RunRecipe fixdoc
func (b *Builder) RunRecipe(prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase bool) error {
// TODO is it necessary to use Clone?
return b.RunRecipeWithProps(prefix, suffix, b.buildProperties.Clone(), skipIfOnlyUpdatingCompilationDatabase)
}

func (b *Builder) RunRecipeWithProps(prefix, suffix string, buildProperties *properties.Map, skipIfOnlyUpdatingCompilationDatabase bool) error {
logrus.Debugf("Looking for recipes like %s", prefix+"*"+suffix)

// TODO is it necessary to use Clone?
buildProperties := b.buildProperties.Clone()
recipes := findRecipes(buildProperties, prefix, suffix)

// TODO is it necessary to use Clone?
Expand Down Expand Up @@ -60,12 +63,20 @@ func (b *Builder) RunRecipe(prefix, suffix string, skipIfOnlyUpdatingCompilation

func findRecipes(buildProperties *properties.Map, patternPrefix string, patternSuffix string) []string {
var recipes []string

exactKey := patternPrefix + patternSuffix

for _, key := range buildProperties.Keys() {
if strings.HasPrefix(key, patternPrefix) && strings.HasSuffix(key, patternSuffix) && buildProperties.Get(key) != "" {
if key != exactKey && strings.HasPrefix(key, patternPrefix) && strings.HasSuffix(key, patternSuffix) && buildProperties.Get(key) != "" {
recipes = append(recipes, key)
}
}

// If no recipes were found, check if the exact key exists
if len(recipes) == 0 && buildProperties.Get(exactKey) != "" {
recipes = append(recipes, exactKey)
}

sort.Strings(recipes)

return recipes
Expand Down