Skip to content

Commit e36af02

Browse files
committed
[WIP] Archive compiled core and use in any compatible project
1 parent b3303ce commit e36af02

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/arduino.cc/builder/builder_utils/utils.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package builder_utils
3232
import (
3333
"bytes"
3434
"fmt"
35+
"io"
3536
"os"
3637
"os/exec"
3738
"path/filepath"
@@ -116,6 +117,34 @@ func findFilesInFolder(sourcePath string, extension string, recurse bool) ([]str
116117
return sources, nil
117118
}
118119

120+
func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
121+
files, err := utils.ReadDirFiltered(sourcePath, utils.FilterFiles())
122+
if err != nil {
123+
return nil, i18n.WrapError(err)
124+
}
125+
var sources []string
126+
for _, file := range files {
127+
sources = append(sources, filepath.Join(sourcePath, file.Name()))
128+
}
129+
130+
if recurse {
131+
folders, err := utils.ReadDirFiltered(sourcePath, utils.FilterDirs)
132+
if err != nil {
133+
return nil, i18n.WrapError(err)
134+
}
135+
136+
for _, folder := range folders {
137+
otherSources, err := findAllFilesInFolder(filepath.Join(sourcePath, folder.Name()), recurse)
138+
if err != nil {
139+
return nil, i18n.WrapError(err)
140+
}
141+
sources = append(sources, otherSources...)
142+
}
143+
}
144+
145+
return sources, nil
146+
}
147+
119148
func compileFilesWithRecipe(objectFiles []string, sourcePath string, sources []string, buildPath string, buildProperties properties.Map, includes []string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
120149
for _, source := range sources {
121150
objectFile, err := compileFileWithRecipe(sourcePath, source, buildPath, buildProperties, includes, recipe, verbose, warningsLevel, logger)
@@ -258,6 +287,24 @@ func nonEmptyString(s string) bool {
258287
return s != constants.EMPTY_STRING
259288
}
260289

290+
func CheckIfRecompileIsAvoidable(corePath string, archiveFile string) bool {
291+
archiveFileStat, err := os.Stat(archiveFile)
292+
if err == nil {
293+
files, err := findAllFilesInFolder(corePath, true)
294+
if err != nil {
295+
return false
296+
}
297+
for _, file := range files {
298+
fileStat, err := os.Stat(file)
299+
if err != nil || fileStat.ModTime().After(archiveFileStat.ModTime()) {
300+
return false
301+
}
302+
}
303+
return true
304+
}
305+
return false
306+
}
307+
261308
func ArchiveCompiledFiles(buildPath string, archiveFile string, objectFiles []string, buildProperties properties.Map, verbose bool, logger i18n.Logger) (string, error) {
262309
archiveFilePath := filepath.Join(buildPath, archiveFile)
263310

@@ -367,6 +414,50 @@ func RemoveHyphenMDDFlagFromGCCCommandLine(buildProperties properties.Map) {
367414
buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS] = strings.Replace(buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS], "-MMD", "", -1)
368415
}
369416

417+
// CopyFile copies the contents of the file named src to the file named
418+
// by dst. The file will be created if it does not already exist. If the
419+
// destination file exists, all it's contents will be replaced by the contents
420+
// of the source file. The file mode will be copied from the source and
421+
// the copied data is synced/flushed to stable storage.
422+
func CopyFile(src, dst string) (err error) {
423+
in, err := os.Open(src)
424+
if err != nil {
425+
return
426+
}
427+
defer in.Close()
428+
429+
out, err := os.Create(dst)
430+
if err != nil {
431+
return
432+
}
433+
defer func() {
434+
if e := out.Close(); e != nil {
435+
err = e
436+
}
437+
}()
438+
439+
_, err = io.Copy(out, in)
440+
if err != nil {
441+
return
442+
}
443+
444+
err = out.Sync()
445+
if err != nil {
446+
return
447+
}
448+
449+
si, err := os.Stat(src)
450+
if err != nil {
451+
return
452+
}
453+
err = os.Chmod(dst, si.Mode())
454+
if err != nil {
455+
return
456+
}
457+
458+
return
459+
}
460+
370461
func GetCoreArchivePath(fqbn string) string {
371462
fqbnToUnderscore := strings.Replace(fqbn, ":", "_", -1)
372463
fqbnToUnderscore = strings.Replace(fqbnToUnderscore, "=", "_", -1)

src/arduino.cc/builder/phases/core_builder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ func compileCore(buildPath string, buildProperties properties.Map, verbose bool,
8484
}
8585
}
8686

87+
targetArchivedCore := builder_utils.GetCoreArchivePath(buildProperties[constants.BUILD_PROPERTIES_FQBN])
88+
noNeedToRecompile := builder_utils.CheckIfRecompileIsAvoidable(coreFolder, targetArchivedCore)
89+
90+
if noNeedToRecompile {
91+
// use archived core
92+
if verbose {
93+
logger.Println(constants.LOG_LEVEL_INFO, "Using precompiled core")
94+
}
95+
return targetArchivedCore, variantObjectFiles, nil
96+
}
97+
8798
coreObjectFiles, err := builder_utils.CompileFiles([]string{}, coreFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
8899
if err != nil {
89100
return "", nil, i18n.WrapError(err)
@@ -94,5 +105,8 @@ func compileCore(buildPath string, buildProperties properties.Map, verbose bool,
94105
return "", nil, i18n.WrapError(err)
95106
}
96107

108+
// archive core.a
109+
builder_utils.CopyFile(archiveFile, targetArchivedCore)
110+
97111
return archiveFile, variantObjectFiles, nil
98112
}

src/arduino.cc/builder/utils/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ func FilterFilesWithExtensions(extensions ...string) filterFiles {
150150
}
151151
}
152152

153+
func FilterFiles() filterFiles {
154+
return func(files []os.FileInfo) []os.FileInfo {
155+
var filtered []os.FileInfo
156+
for _, file := range files {
157+
if !file.IsDir() {
158+
filtered = append(filtered, file)
159+
}
160+
}
161+
return filtered
162+
}
163+
}
164+
153165
var SOURCE_CONTROL_FOLDERS = map[string]bool{"CVS": true, "RCS": true, ".git": true, ".github": true, ".svn": true, ".hg": true, ".bzr": true, ".vscode": true}
154166

155167
func IsSCCSOrHiddenFile(file os.FileInfo) bool {

0 commit comments

Comments
 (0)