Skip to content

Commit 27e01e8

Browse files
committed
Added compile flag to produce only the compilation database
1 parent 076de1e commit 27e01e8

File tree

11 files changed

+147
-97
lines changed

11 files changed

+147
-97
lines changed

cli/compile/compile.go

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,26 @@ import (
3535
)
3636

3737
var (
38-
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
39-
showProperties bool // Show all build preferences used instead of compiling.
40-
preprocess bool // Print preprocessed code to stdout.
41-
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
42-
buildPath string // Path where to save compiled files.
43-
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
44-
warnings string // Used to tell gcc which warning level to use.
45-
verbose bool // Turns on verbose mode.
46-
quiet bool // Suppresses almost every output.
47-
vidPid string // VID/PID specific build properties.
48-
uploadAfterCompile bool // Upload the binary after the compilation.
49-
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
50-
verify bool // Upload, verify uploaded binary after the upload.
51-
exportDir string // The compiled binary is written to this file
52-
libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
53-
optimizeForDebug bool // Optimize compile output for debug, not for release
54-
programmer string // Use the specified programmer to upload
55-
clean bool // Cleanup the build folder and do not use any cached build
56-
exportBinaries bool // Copies compiled binaries to sketch folder when true
38+
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
39+
showProperties bool // Show all build preferences used instead of compiling.
40+
preprocess bool // Print preprocessed code to stdout.
41+
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
42+
buildPath string // Path where to save compiled files.
43+
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
44+
warnings string // Used to tell gcc which warning level to use.
45+
verbose bool // Turns on verbose mode.
46+
quiet bool // Suppresses almost every output.
47+
vidPid string // VID/PID specific build properties.
48+
uploadAfterCompile bool // Upload the binary after the compilation.
49+
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
50+
verify bool // Upload, verify uploaded binary after the upload.
51+
exportDir string // The compiled binary is written to this file
52+
libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
53+
optimizeForDebug bool // Optimize compile output for debug, not for release
54+
programmer string // Use the specified programmer to upload
55+
clean bool // Cleanup the build folder and do not use any cached build
56+
exportBinaries bool // Copies compiled binaries to sketch folder when true
57+
compilationDatabaseOnly bool // Only create compilation database without actually compiling
5758
)
5859

5960
// NewCommand created a new `compile` command
@@ -94,6 +95,7 @@ func NewCommand() *cobra.Command {
9495
"List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.")
9596
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")
9697
command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.")
98+
command.Flags().BoolVar(&compilationDatabaseOnly, "only-compilation-database", false, "Just produce the compilation database, without actually compiling.")
9799
command.Flags().BoolVar(&clean, "clean", false, "Optional, cleanup the build folder and do not use any cached build.")
98100
// We must use the following syntax for this flag since it's also bound to settings, we could use the other one too
99101
// but it wouldn't make sense since we still must explicitly set the exportBinaries variable by reading from settings.
@@ -127,23 +129,24 @@ func run(cmd *cobra.Command, args []string) {
127129
exportBinaries = configuration.Settings.GetBool("sketch.always_export_binaries")
128130

129131
compileReq := &rpc.CompileReq{
130-
Instance: inst,
131-
Fqbn: fqbn,
132-
SketchPath: sketchPath.String(),
133-
ShowProperties: showProperties,
134-
Preprocess: preprocess,
135-
BuildCachePath: buildCachePath,
136-
BuildPath: buildPath,
137-
BuildProperties: buildProperties,
138-
Warnings: warnings,
139-
Verbose: verbose,
140-
Quiet: quiet,
141-
VidPid: vidPid,
142-
ExportDir: exportDir,
143-
Libraries: libraries,
144-
OptimizeForDebug: optimizeForDebug,
145-
Clean: clean,
146-
ExportBinaries: exportBinaries,
132+
Instance: inst,
133+
Fqbn: fqbn,
134+
SketchPath: sketchPath.String(),
135+
ShowProperties: showProperties,
136+
Preprocess: preprocess,
137+
BuildCachePath: buildCachePath,
138+
BuildPath: buildPath,
139+
BuildProperties: buildProperties,
140+
Warnings: warnings,
141+
Verbose: verbose,
142+
Quiet: quiet,
143+
VidPid: vidPid,
144+
ExportDir: exportDir,
145+
Libraries: libraries,
146+
OptimizeForDebug: optimizeForDebug,
147+
Clean: clean,
148+
ExportBinaries: exportBinaries,
149+
CreateCompilationDatabaseOnly: compilationDatabaseOnly,
147150
}
148151
compileOut := new(bytes.Buffer)
149152
compileErr := new(bytes.Buffer)

commands/compile/compile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
191191
builderCtx.ExecStderr = errStream
192192
builderCtx.SetLogger(i18n.LoggerToCustomStreams{Stdout: outStream, Stderr: errStream})
193193
builderCtx.Clean = req.GetClean()
194+
builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly()
194195

195196
// Use defer() to create an rpc.CompileResp with all the information available at the
196197
// moment of return.

legacy/builder/builder_utils/utils.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,24 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
247247
if err != nil {
248248
return nil, errors.WithStack(err)
249249
}
250-
if !objIsUpToDate {
251-
command, err := PrepareCommandForRecipe(properties, recipe, false)
252-
if err != nil {
253-
return nil, errors.WithStack(err)
254-
}
255-
250+
command, err := PrepareCommandForRecipe(properties, recipe, false)
251+
if err != nil {
252+
return nil, errors.WithStack(err)
253+
}
254+
if ctx.CompilationDatabase != nil {
255+
ctx.CompilationDatabase.ReplaceEntry(source, command)
256+
}
257+
if !objIsUpToDate && !ctx.OnlyUpdateCompilationDatabase {
256258
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
257259
if err != nil {
258260
return nil, errors.WithStack(err)
259261
}
260262
} else if ctx.Verbose {
261-
if ctx.CompilationDatabase != nil {
262-
ctx.CompilationDatabase.KeepEntry(source)
263+
if objIsUpToDate {
264+
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_PREVIOUS_COMPILED_FILE, objectFile)
265+
} else {
266+
logger.Println("info", "Skipping compile of: {0}", objectFile)
263267
}
264-
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_PREVIOUS_COMPILED_FILE, objectFile)
265268
}
266269

267270
return objectFile, nil
@@ -455,10 +458,15 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
455458
logger := ctx.GetLogger()
456459
archiveFilePath := buildPath.JoinPath(archiveFile)
457460

458-
rebuildArchive := false
461+
if ctx.OnlyUpdateCompilationDatabase {
462+
if ctx.Verbose {
463+
logger.Println("info", "Skipping archive creation of: {0}", archiveFilePath)
464+
}
465+
return archiveFilePath, nil
466+
}
459467

460468
if archiveFileStat, err := archiveFilePath.Stat(); err == nil {
461-
469+
rebuildArchive := false
462470
for _, objectFile := range objectFilesToArchive {
463471
objectFileStat, err := objectFile.Stat()
464472
if err != nil || objectFileStat.ModTime().After(archiveFileStat.ModTime()) {

legacy/builder/merge_sketch_with_bootloader.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import (
3131
type MergeSketchWithBootloader struct{}
3232

3333
func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error {
34+
if ctx.OnlyUpdateCompilationDatabase {
35+
return nil
36+
}
37+
3438
buildProperties := ctx.BuildProperties
3539
if !buildProperties.ContainsKey(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK) && !buildProperties.ContainsKey(constants.BUILD_PROPERTIES_BOOTLOADER_FILE) {
3640
return nil

legacy/builder/phases/core_builder.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
9494
archivedCoreName := GetCachedCoreArchiveFileName(buildProperties.Get(constants.BUILD_PROPERTIES_FQBN),
9595
buildProperties.Get("compiler.optimization_flags"), realCoreFolder)
9696
targetArchivedCore = buildCachePath.Join(archivedCoreName)
97-
canUseArchivedCore := !ctx.Clean && !builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore)
97+
canUseArchivedCore := !ctx.OnlyUpdateCompilationDatabase &&
98+
!ctx.Clean &&
99+
!builder_utils.CoreOrReferencedCoreHasChanged(realCoreFolder, targetCoreFolder, targetArchivedCore)
98100

99101
if canUseArchivedCore {
100102
// use archived core
@@ -116,7 +118,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
116118
}
117119

118120
// archive core.a
119-
if targetArchivedCore != nil {
121+
if targetArchivedCore != nil && !ctx.OnlyUpdateCompilationDatabase {
120122
err := archiveFile.CopyTo(targetArchivedCore)
121123
if ctx.Verbose {
122124
if err == nil {

legacy/builder/phases/linker.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ import (
3030
type Linker struct{}
3131

3232
func (s *Linker) Run(ctx *types.Context) error {
33+
if ctx.OnlyUpdateCompilationDatabase {
34+
if ctx.Verbose {
35+
ctx.GetLogger().Println("info", "Skip linking of final executable.")
36+
}
37+
return nil
38+
}
39+
3340
objectFilesSketch := ctx.SketchObjectFiles
3441
objectFilesLibraries := ctx.LibrariesObjectFiles
3542
objectFilesCore := ctx.CoreObjectsFiles

legacy/builder/phases/sizer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ type Sizer struct {
3232
}
3333

3434
func (s *Sizer) Run(ctx *types.Context) error {
35-
35+
if ctx.OnlyUpdateCompilationDatabase {
36+
return nil
37+
}
3638
if s.SketchError {
3739
return nil
3840
}

legacy/builder/recipe_runner.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
5353
return errors.WithStack(err)
5454
}
5555

56+
if ctx.OnlyUpdateCompilationDatabase {
57+
if ctx.Verbose {
58+
ctx.GetLogger().Println("info", "Skipping: {0}", strings.Join(command.Args, " "))
59+
}
60+
return nil
61+
}
62+
5663
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
5764
if err != nil {
5865
return errors.WithStack(err)

legacy/builder/types/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ type Context struct {
167167

168168
// Compilation Database to build/update
169169
CompilationDatabase *builder.CompilationDatabase
170+
// Set to true to skip build and produce only Compilation Database
171+
OnlyUpdateCompilationDatabase bool
170172
}
171173

172174
// ExecutableSectionSize represents a section of the executable output file

0 commit comments

Comments
 (0)