forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
175 lines (156 loc) · 5.88 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package builder
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
"github.com/arduino/arduino-cli/internal/buildcache"
"github.com/arduino/go-paths-helper"
)
// buildCore fixdoc
func (b *Builder) buildCore() error {
if err := b.coreBuildPath.MkdirAll(); err != nil {
return err
}
if b.coreBuildCachePath != nil {
if _, err := b.coreBuildCachePath.RelTo(b.buildPath); err != nil {
b.logger.Info(tr("Couldn't deeply cache core build: %[1]s", err))
b.logger.Info(tr("Running normal build of the core..."))
// TODO decide if we want to override this or not. (It's only used by the
// compileCore function).
b.coreBuildCachePath = nil
} else if err := b.coreBuildCachePath.MkdirAll(); err != nil {
return err
}
}
archiveFile, objectFiles, err := b.compileCore()
if err != nil {
return err
}
b.buildArtifacts.coreObjectsFiles = objectFiles
b.buildArtifacts.coreArchiveFilePath = archiveFile
return nil
}
func (b *Builder) compileCore() (*paths.Path, paths.PathList, error) {
coreFolder := b.buildProperties.GetPath("build.core.path")
variantFolder := b.buildProperties.GetPath("build.variant.path")
targetCoreFolder := b.buildProperties.GetPath("runtime.platform.path")
includes := []string{coreFolder.String()}
if variantFolder != nil && variantFolder.IsDir() {
includes = append(includes, variantFolder.String())
}
includes = f.Map(includes, cpp.WrapWithHyphenI)
var err error
variantObjectFiles := paths.NewPathList()
if variantFolder != nil && variantFolder.IsDir() {
variantObjectFiles, err = b.compileFiles(
variantFolder, b.coreBuildPath,
true, /** recursive **/
includes,
)
if err != nil {
return nil, nil, err
}
}
var targetArchivedCore *paths.Path
if b.coreBuildCachePath != nil {
realCoreFolder := coreFolder.Parent().Parent()
archivedCoreName := getCachedCoreArchiveDirName(
b.buildProperties.Get("build.fqbn"),
b.buildProperties.Get("compiler.optimization_flags"),
realCoreFolder,
)
targetArchivedCore = b.coreBuildCachePath.Join(archivedCoreName, "core.a")
if _, err := buildcache.New(b.coreBuildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) {
return nil, nil, fmt.Errorf(tr("creating core cache folder: %s", err))
}
var canUseArchivedCore bool
if b.onlyUpdateCompilationDatabase || b.clean {
canUseArchivedCore = false
} else if isOlder, err := utils.DirContentIsOlderThan(realCoreFolder, targetArchivedCore); err != nil || !isOlder {
// Recreate the archive if ANY of the core files (including platform.txt) has changed
canUseArchivedCore = false
} else if targetCoreFolder == nil || realCoreFolder.EquivalentTo(targetCoreFolder) {
canUseArchivedCore = true
} else if isOlder, err := utils.DirContentIsOlderThan(targetCoreFolder, targetArchivedCore); err != nil || !isOlder {
// Recreate the archive if ANY of the build core files (including platform.txt) has changed
canUseArchivedCore = false
} else {
canUseArchivedCore = true
}
if canUseArchivedCore {
// use archived core
if b.logger.Verbose() {
b.logger.Info(tr("Using precompiled core: %[1]s", targetArchivedCore))
}
return targetArchivedCore, variantObjectFiles, nil
}
}
coreObjectFiles, err := b.compileFiles(
coreFolder, b.coreBuildPath,
true, /** recursive **/
includes,
)
if err != nil {
return nil, nil, err
}
archiveFile, err := b.archiveCompiledFiles(b.coreBuildPath.Join("core.a"), coreObjectFiles)
if err != nil {
return nil, nil, err
}
// archive core.a
if targetArchivedCore != nil && !b.onlyUpdateCompilationDatabase {
err := archiveFile.CopyTo(targetArchivedCore)
if b.logger.Verbose() {
if err == nil {
b.logger.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore))
} else if os.IsNotExist(err) {
b.logger.Info(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s",
b.actualPlatform,
"https://arduino.github.io/arduino-cli/latest/platform-specification/#recipes-to-build-the-corea-archive-file"))
} else {
b.logger.Info(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err))
}
}
}
return archiveFile, variantObjectFiles, nil
}
// getCachedCoreArchiveDirName returns the directory name to be used to store
// the global cached core.a.
func getCachedCoreArchiveDirName(fqbn string, optimizationFlags string, coreFolder *paths.Path) string {
fqbnToUnderscore := strings.ReplaceAll(fqbn, ":", "_")
fqbnToUnderscore = strings.ReplaceAll(fqbnToUnderscore, "=", "_")
if absCoreFolder, err := coreFolder.Abs(); err == nil {
coreFolder = absCoreFolder
} // silently continue if absolute path can't be detected
md5Sum := func(data []byte) string {
md5sumBytes := md5.Sum(data)
return hex.EncodeToString(md5sumBytes[:])
}
hash := md5Sum([]byte(coreFolder.String() + optimizationFlags))
realName := fqbnToUnderscore + "_" + hash
if len(realName) > 100 {
// avoid really long names, simply hash the name again
realName = md5Sum([]byte(realName))
}
return realName
}