Skip to content

Commit 4764159

Browse files
committed
The build caching of core.a is now optional thorugh a command line option
Signed-off-by: Cristian Maglie <[email protected]>
1 parent 0448b73 commit 4764159

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

src/arduino.cc/arduino-builder/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const FLAG_FQBN = "fqbn"
6464
const FLAG_IDE_VERSION = "ide-version"
6565
const FLAG_CORE_API_VERSION = "core-api-version"
6666
const FLAG_BUILD_PATH = "build-path"
67+
const FLAG_BUILD_CACHE = "build-cache"
6768
const FLAG_VERBOSE = "verbose"
6869
const FLAG_QUIET = "quiet"
6970
const FLAG_DEBUG_LEVEL = "debug-level"
@@ -126,6 +127,7 @@ var fqbnFlag *string
126127
var coreAPIVersionFlag *string
127128
var ideVersionFlag *string
128129
var buildPathFlag *string
130+
var buildCachePathFlag *string
129131
var verboseFlag *bool
130132
var quietFlag *bool
131133
var debugLevelFlag *int
@@ -148,6 +150,7 @@ func init() {
148150
coreAPIVersionFlag = flag.String(FLAG_CORE_API_VERSION, "10600", "version of core APIs (used to populate ARDUINO #define)")
149151
ideVersionFlag = flag.String(FLAG_IDE_VERSION, "10600", "[deprecated] use '"+FLAG_CORE_API_VERSION+"' instead")
150152
buildPathFlag = flag.String(FLAG_BUILD_PATH, "", "build path")
153+
buildCachePathFlag = flag.String(FLAG_BUILD_CACHE, "", "builds of 'core.a' are saved into this folder to be cached and reused")
151154
verboseFlag = flag.Bool(FLAG_VERBOSE, false, "if 'true' prints lots of stuff")
152155
quietFlag = flag.Bool(FLAG_QUIET, false, "if 'true' doesn't print any warnings or progress or whatever")
153156
debugLevelFlag = flag.Int(FLAG_DEBUG_LEVEL, builder.DEFAULT_DEBUG_LEVEL, "Turns on debugging messages. The higher, the chattier")
@@ -256,6 +259,25 @@ func main() {
256259
}
257260
ctx.BuildPath = buildPath
258261

262+
// FLAG_BUILD_CACHE
263+
buildCachePath, err := gohasissues.Unquote(*buildCachePathFlag)
264+
if err != nil {
265+
printCompleteError(err)
266+
}
267+
if buildCachePath != "" {
268+
_, err := os.Stat(buildCachePath)
269+
if err != nil {
270+
fmt.Fprintln(os.Stderr, err)
271+
os.Exit(1)
272+
}
273+
274+
err = utils.EnsureFolderExists(buildCachePath)
275+
if err != nil {
276+
printCompleteError(err)
277+
}
278+
}
279+
ctx.BuildCachePath = buildCachePath
280+
259281
// FLAG_VID_PID
260282
if *vidPidFlag != "" {
261283
ctx.USBVidPid = *vidPidFlag

src/arduino.cc/builder/add_additional_entries_to_context.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
package builder
3131

3232
import (
33+
"path/filepath"
34+
3335
"arduino.cc/builder/constants"
3436
"arduino.cc/builder/i18n"
3537
"arduino.cc/builder/types"
36-
"path/filepath"
3738
)
3839

3940
type AddAdditionalEntriesToContext struct{}
@@ -64,6 +65,15 @@ func (s *AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
6465
ctx.CoreBuildPath = coreBuildPath
6566
}
6667

68+
if ctx.BuildCachePath != "" {
69+
coreBuildCachePath, err := filepath.Abs(filepath.Join(ctx.BuildCachePath, constants.FOLDER_CORE))
70+
if err != nil {
71+
return i18n.WrapError(err)
72+
}
73+
74+
ctx.CoreBuildCachePath = coreBuildCachePath
75+
}
76+
6777
if ctx.WarningsLevel == "" {
6878
ctx.WarningsLevel = DEFAULT_WARNINGS_LEVEL
6979
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,5 +471,5 @@ func GetCachedCoreArchiveFileName(fqbn, coreFolder string) string {
471471
coreFolder = absCoreFolder
472472
} // silently continue if absolute path can't be detected
473473
hash := utils.MD5Sum([]byte(coreFolder))
474-
return os.TempDir() + "/core_" + fqbnToUnderscore + "_" + hash + ".a"
474+
return "core_" + fqbnToUnderscore + "_" + hash + ".a"
475475
}

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
package phases
3131

3232
import (
33+
"path/filepath"
34+
3335
"arduino.cc/builder/builder_utils"
3436
"arduino.cc/builder/constants"
3537
"arduino.cc/builder/i18n"
@@ -42,6 +44,7 @@ type CoreBuilder struct{}
4244

4345
func (s *CoreBuilder) Run(ctx *types.Context) error {
4446
coreBuildPath := ctx.CoreBuildPath
47+
coreBuildCachePath := ctx.CoreBuildCachePath
4548
buildProperties := ctx.BuildProperties
4649
verbose := ctx.Verbose
4750
warningsLevel := ctx.WarningsLevel
@@ -52,7 +55,14 @@ func (s *CoreBuilder) Run(ctx *types.Context) error {
5255
return i18n.WrapError(err)
5356
}
5457

55-
archiveFile, objectFiles, err := compileCore(coreBuildPath, buildProperties, verbose, warningsLevel, logger)
58+
if coreBuildCachePath != "" {
59+
err := utils.EnsureFolderExists(coreBuildCachePath)
60+
if err != nil {
61+
return i18n.WrapError(err)
62+
}
63+
}
64+
65+
archiveFile, objectFiles, err := compileCore(coreBuildPath, coreBuildCachePath, buildProperties, verbose, warningsLevel, logger)
5666
if err != nil {
5767
return i18n.WrapError(err)
5868
}
@@ -63,7 +73,7 @@ func (s *CoreBuilder) Run(ctx *types.Context) error {
6373
return nil
6474
}
6575

66-
func compileCore(buildPath string, buildProperties properties.Map, verbose bool, warningsLevel string, logger i18n.Logger) (string, []string, error) {
76+
func compileCore(buildPath string, buildCachePath string, buildProperties properties.Map, verbose bool, warningsLevel string, logger i18n.Logger) (string, []string, error) {
6777
coreFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH]
6878
variantFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH]
6979

@@ -89,15 +99,19 @@ func compileCore(buildPath string, buildProperties properties.Map, verbose bool,
8999
// Recreate the archive if ANY of the core files (including platform.txt) has changed
90100
realCoreFolder := utils.GetParentFolder(coreFolder, 2)
91101

92-
targetArchivedCore := builder_utils.GetCachedCoreArchiveFileName(buildProperties[constants.BUILD_PROPERTIES_FQBN], realCoreFolder)
93-
canUseArchivedCore := !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore)
94-
95-
if canUseArchivedCore {
96-
// use archived core
97-
if verbose {
98-
logger.Println(constants.LOG_LEVEL_INFO, "Using precompiled core")
102+
var targetArchivedCore string
103+
if buildCachePath != "" {
104+
archivedCoreName := builder_utils.GetCachedCoreArchiveFileName(buildProperties[constants.BUILD_PROPERTIES_FQBN], realCoreFolder)
105+
targetArchivedCore = filepath.Join(buildCachePath, archivedCoreName)
106+
canUseArchivedCore := !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore)
107+
108+
if canUseArchivedCore {
109+
// use archived core
110+
if verbose {
111+
logger.Println(constants.LOG_LEVEL_INFO, "Using precompiled core")
112+
}
113+
return targetArchivedCore, variantObjectFiles, nil
99114
}
100-
return targetArchivedCore, variantObjectFiles, nil
101115
}
102116

103117
coreObjectFiles, err := builder_utils.CompileFiles([]string{}, coreFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
@@ -111,7 +125,10 @@ func compileCore(buildPath string, buildProperties properties.Map, verbose bool,
111125
}
112126

113127
// archive core.a
114-
builder_utils.CopyFile(archiveFile, targetArchivedCore)
128+
if targetArchivedCore != "" {
129+
logger.Println(constants.LOG_LEVEL_DEBUG, "Archiving built core (caching) in: "+targetArchivedCore)
130+
builder_utils.CopyFile(archiveFile, targetArchivedCore)
131+
}
115132

116133
return archiveFile, variantObjectFiles, nil
117134
}

src/arduino.cc/builder/types/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ type Context struct {
3737
BuildProperties properties.Map
3838
BuildCore string
3939
BuildPath string
40+
BuildCachePath string
4041
SketchBuildPath string
4142
CoreBuildPath string
43+
CoreBuildCachePath string
4244
CoreArchiveFilePath string
4345
CoreObjectsFiles []string
4446
LibrariesBuildPath string

0 commit comments

Comments
 (0)