Skip to content

Commit a5466d0

Browse files
authored
[skip-changelog] Added skip-libraries-discovery flag in Compile (#1777)
* Added skip-libraries-discovery flag in Compile * Always send compile response even in case of error
1 parent ffe4232 commit a5466d0

File tree

8 files changed

+142
-90
lines changed

8 files changed

+142
-90
lines changed

Diff for: cli/compile/compile.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ var (
7474
// library and libraries sound similar but they're actually different.
7575
// library expects a path to the root folder of one single library.
7676
// libraries expects a path to a directory containing multiple libraries, similarly to the <directories.user>/libraries path.
77-
library []string // List of paths to libraries root folders. Can be used multiple times for different libraries
78-
libraries []string // List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries paths.
79-
tr = i18n.Tr
77+
library []string // List of paths to libraries root folders. Can be used multiple times for different libraries
78+
libraries []string // List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries paths.
79+
skipLibrariesDiscovery bool
80+
tr = i18n.Tr
8081
)
8182

8283
// NewCommand created a new `compile` command
@@ -135,7 +136,8 @@ func NewCommand() *cobra.Command {
135136
compileCommand.Flags().BoolP("export-binaries", "e", false, tr("If set built binaries will be exported to the sketch folder."))
136137
compileCommand.Flags().StringVar(&sourceOverrides, "source-override", "", tr("Optional. Path to a .json file that contains a set of replacements of the sketch source code."))
137138
compileCommand.Flag("source-override").Hidden = true
138-
139+
compileCommand.Flags().BoolVar(&skipLibrariesDiscovery, "skip-libraries-discovery", false, "Skip libraries discovery. This flag is provided only for use in language server and other, very specific, use cases. Do not use for normal compiles")
140+
compileCommand.Flag("skip-libraries-discovery").Hidden = true
139141
configuration.Settings.BindPFlag("sketch.always_export_binaries", compileCommand.Flags().Lookup("export-binaries"))
140142

141143
compileCommand.Flags().MarkDeprecated("build-properties", tr("please use --build-property instead."))
@@ -220,6 +222,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
220222
KeysKeychain: keysKeychain,
221223
SignKey: signKey,
222224
EncryptKey: encryptKey,
225+
SkipLibrariesDiscovery: skipLibrariesDiscovery,
223226
}
224227
compileStdOut := new(bytes.Buffer)
225228
compileStdErr := new(bytes.Buffer)

Diff for: commands/compile/compile.go

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
114114
if pm.GetProfile() != nil {
115115
builderCtx.LibrariesManager = lm
116116
}
117+
builderCtx.UseCachedLibrariesResolution = req.GetSkipLibrariesDiscovery()
117118
builderCtx.FQBN = fqbn
118119
builderCtx.SketchLocation = sk.FullPath
119120
builderCtx.ProgressCB = progressCB

Diff for: commands/daemon/daemon.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,19 @@ func (s *ArduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSke
268268
func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.ArduinoCoreService_CompileServer) error {
269269
outStream, outCtx := utils.FeedStreamTo(func(data []byte) { stream.Send(&rpc.CompileResponse{OutStream: data}) })
270270
errStream, errCtx := utils.FeedStreamTo(func(data []byte) { stream.Send(&rpc.CompileResponse{ErrStream: data}) })
271-
resp, err := compile.Compile(
271+
compileResp, compileErr := compile.Compile(
272272
stream.Context(), req, outStream, errStream,
273273
func(p *rpc.TaskProgress) { stream.Send(&rpc.CompileResponse{Progress: p}) },
274274
false) // Set debug to false
275275
outStream.Close()
276276
errStream.Close()
277-
if err != nil {
278-
return convertErrorToRPCStatus(err)
279-
}
280277
<-outCtx.Done()
281278
<-errCtx.Done()
282-
return stream.Send(resp)
279+
compileRespSendErr := stream.Send(compileResp)
280+
if compileErr != nil {
281+
return convertErrorToRPCStatus(compileErr)
282+
}
283+
return compileRespSendErr
283284
}
284285

285286
// PlatformInstall FIXMEDOC

Diff for: legacy/builder/container_find_includes.go

+42-23
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
122122
}
123123

124124
func (s *ContainerFindIncludes) findIncludes(ctx *types.Context) error {
125+
librariesResolutionCache := ctx.BuildPath.Join("libraries.cache")
126+
if ctx.UseCachedLibrariesResolution && librariesResolutionCache.Exist() {
127+
if d, err := librariesResolutionCache.ReadFile(); err != nil {
128+
return err
129+
} else if err := json.Unmarshal(d, &ctx.IncludeFolders); err != nil {
130+
return err
131+
}
132+
if ctx.Verbose {
133+
ctx.Info("Using cached library discovery: " + librariesResolutionCache.String())
134+
}
135+
return nil
136+
}
137+
125138
cachePath := ctx.BuildPath.Join("includes.cache")
126139
cache := readCache(cachePath)
127140

@@ -130,38 +143,44 @@ func (s *ContainerFindIncludes) findIncludes(ctx *types.Context) error {
130143
appendIncludeFolder(ctx, cache, nil, "", ctx.BuildProperties.GetPath("build.variant.path"))
131144
}
132145

133-
sketch := ctx.Sketch
134-
mergedfile, err := types.MakeSourceFile(ctx, sketch, paths.New(sketch.MainFile.Base()+".cpp"))
135-
if err != nil {
136-
return errors.WithStack(err)
137-
}
138-
ctx.CollectedSourceFiles.Push(mergedfile)
146+
if !ctx.UseCachedLibrariesResolution {
147+
sketch := ctx.Sketch
148+
mergedfile, err := types.MakeSourceFile(ctx, sketch, paths.New(sketch.MainFile.Base()+".cpp"))
149+
if err != nil {
150+
return errors.WithStack(err)
151+
}
152+
ctx.CollectedSourceFiles.Push(mergedfile)
139153

140-
sourceFilePaths := ctx.CollectedSourceFiles
141-
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, ctx.SketchBuildPath, false /* recurse */)
142-
srcSubfolderPath := ctx.SketchBuildPath.Join("src")
143-
if srcSubfolderPath.IsDir() {
144-
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, srcSubfolderPath, true /* recurse */)
145-
}
154+
sourceFilePaths := ctx.CollectedSourceFiles
155+
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, ctx.SketchBuildPath, false /* recurse */)
156+
srcSubfolderPath := ctx.SketchBuildPath.Join("src")
157+
if srcSubfolderPath.IsDir() {
158+
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, srcSubfolderPath, true /* recurse */)
159+
}
146160

147-
for !sourceFilePaths.Empty() {
148-
err := findIncludesUntilDone(ctx, cache, sourceFilePaths.Pop())
149-
if err != nil {
150-
cachePath.Remove()
161+
for !sourceFilePaths.Empty() {
162+
err := findIncludesUntilDone(ctx, cache, sourceFilePaths.Pop())
163+
if err != nil {
164+
cachePath.Remove()
165+
return errors.WithStack(err)
166+
}
167+
}
168+
169+
// Finalize the cache
170+
cache.ExpectEnd()
171+
if err := writeCache(cache, cachePath); err != nil {
151172
return errors.WithStack(err)
152173
}
153174
}
154175

155-
// Finalize the cache
156-
cache.ExpectEnd()
157-
err = writeCache(cache, cachePath)
158-
if err != nil {
176+
if err := runCommand(ctx, &FailIfImportedLibraryIsWrong{}); err != nil {
159177
return errors.WithStack(err)
160178
}
161179

162-
err = runCommand(ctx, &FailIfImportedLibraryIsWrong{})
163-
if err != nil {
164-
return errors.WithStack(err)
180+
if d, err := json.Marshal(ctx.IncludeFolders); err != nil {
181+
return err
182+
} else if err := librariesResolutionCache.WriteFile(d); err != nil {
183+
return err
165184
}
166185

167186
return nil

Diff for: legacy/builder/libraries_loader.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import (
2626
type LibrariesLoader struct{}
2727

2828
func (s *LibrariesLoader) Run(ctx *types.Context) error {
29-
if ctx.LibrariesManager == nil {
29+
if ctx.UseCachedLibrariesResolution {
30+
// Since we are using the cached libraries resolution
31+
// the library manager is not needed.
32+
lm := librariesmanager.NewLibraryManager(nil, nil)
33+
ctx.LibrariesManager = lm
34+
} else if ctx.LibrariesManager == nil {
3035
lm := librariesmanager.NewLibraryManager(nil, nil)
3136
ctx.LibrariesManager = lm
3237

Diff for: legacy/builder/types/context.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ type Context struct {
120120
WarningsLevel string
121121

122122
// Libraries handling
123-
LibrariesManager *librariesmanager.LibrariesManager
124-
LibrariesResolver *librariesresolver.Cpp
125-
ImportedLibraries libraries.List
126-
LibrariesResolutionResults map[string]LibraryResolutionResult
127-
IncludeFolders paths.PathList
128-
//OutputGccMinusM string
123+
LibrariesManager *librariesmanager.LibrariesManager
124+
LibrariesResolver *librariesresolver.Cpp
125+
ImportedLibraries libraries.List
126+
LibrariesResolutionResults map[string]LibraryResolutionResult
127+
IncludeFolders paths.PathList
128+
UseCachedLibrariesResolution bool
129129

130130
// C++ Parsing
131131
CTagsOutput string

0 commit comments

Comments
 (0)