Skip to content

builder: use ar-chives for linking big sketches (was: use the @ syntax to reduce command line length if needed) #961

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

Merged
merged 5 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion legacy/builder/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const BUILD_PROPERTIES_COMPILER_WARNING_FLAGS = "compiler.warning_flags"
const BUILD_PROPERTIES_FQBN = "build.fqbn"
const BUILD_PROPERTIES_INCLUDES = "includes"
const BUILD_PROPERTIES_OBJECT_FILE = "object_file"
const BUILD_PROPERTIES_OBJECT_FILES = "object_files"
const BUILD_PROPERTIES_PATTERN = "pattern"
const BUILD_PROPERTIES_PID = "pid"
const BUILD_PROPERTIES_PREPROCESSED_FILE_PATH = "preprocessed_file_path"
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/phases/libraries_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func compileLibraries(ctx *types.Context, libraries libraries.List, buildPath *p
if err != nil {
return nil, errors.WithStack(err)
}
objectFiles = append(objectFiles, libraryObjectFiles...)
objectFiles.AddAll(libraryObjectFiles)

ctx.Progress.CompleteStep()
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
Expand Down
41 changes: 37 additions & 4 deletions legacy/builder/phases/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,50 @@ func (s *Linker) Run(ctx *types.Context) error {
}

func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths.Path, coreArchiveFilePath *paths.Path, buildProperties *properties.Map) error {
quotedObjectFiles := utils.Map(objectFiles.AsStrings(), wrapWithDoubleQuotes)
objectFileList := strings.Join(quotedObjectFiles, constants.SPACE)
objectFileList := strings.Join(utils.Map(objectFiles.AsStrings(), wrapWithDoubleQuotes), " ")

// If command line length is too big (> 30000 chars), try to collect the object files into archives
// and use that archives to complete the build.
if len(objectFileList) > 30000 {

// We must create an object file for each visited directory: this is required becuase gcc-ar checks
// if an object file is already in the archive by looking ONLY at the filename WITHOUT the path, so
// it may happen that a subdir/spi.o inside the archive may be overwritten by a anotherdir/spi.o
// because thery are both named spi.o.

properties := buildProperties.Clone()
archives := map[string]bool{}
for _, object := range objectFiles {
archive := object.Parent().Join("objs.a")
if !archives[archive.String()] {
archives[archive.String()] = true
// Cleanup old archives
_ = archive.Remove()
}
properties.Set("archive_file", archive.Base())
properties.SetPath("archive_file_path", archive)
properties.SetPath("object_file", object)
_, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_AR_PATTERN, false, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if err != nil {
return err
}
}

objectFileList = ""
for archive := range archives {
objectFileList += " " + wrapWithDoubleQuotes(archive)
}
objectFileList = "-Wl,--whole-archive" + objectFileList + " -Wl,--no-whole-archive"
}

properties := buildProperties.Clone()
properties.Set(constants.BUILD_PROPERTIES_COMPILER_C_ELF_FLAGS, properties.Get(constants.BUILD_PROPERTIES_COMPILER_C_ELF_FLAGS))
properties.Set(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS, properties.Get(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+ctx.WarningsLevel))
properties.Set(constants.BUILD_PROPERTIES_ARCHIVE_FILE, coreDotARelPath.String())
properties.Set(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, coreArchiveFilePath.String())
properties.Set(constants.BUILD_PROPERTIES_OBJECT_FILES, objectFileList)
properties.Set("object_files", objectFileList)

_, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_C_COMBINE_PATTERN, false /* stdout */, utils.ShowIfVerbose /* stderr */, utils.Show)
_, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_C_COMBINE_PATTERN, false, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
return err
}

Expand Down