Skip to content

Commit 316815b

Browse files
matthijskooijmanfacchinm
authored andcommitted
Fix removal of -MMD option when running the preprocessor
Usually, the `preproc.macros` recipe includes the C/C++ flags, and through that the `-MMD` flag to generate dependency files. However, since include detection passed an output file of `/dev/null` (or the equivalent on other operating systems), this causes gcc to try and generate a `/dev/null.d` file and fail. To prevent this, the `-MMD` flag was filtered out, but this filtering was applied to the `compiler.cpp.flags` variable, where it *usually* comes from. However, this is not necessarily true for all platforms. For example, the PIC32 platform used to have this flag in the `compiler.c.flags` variable and have `compiler.cpp.flags` include that. This prevented the flag from being filtered away and caused a failure. Due to previous changes, it is now possible for this filtering to happen after all variables have been replaced and the command to run was generated, but before actually running it. An extra advantage is that the filtering is more robust than the previous substring-based filtering. This fixes arduino#230. Signed-off-by: Matthijs Kooijman <[email protected]>
1 parent 1c7f985 commit 316815b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Diff for: builder_utils/utils.go

-4
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,6 @@ func PrepareCommandForRecipe(ctx *types.Context, buildProperties properties.Map,
430430
return command, nil
431431
}
432432

433-
func RemoveHyphenMDDFlagFromGCCCommandLine(buildProperties properties.Map) {
434-
buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS] = strings.Replace(buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS], "-MMD", "", -1)
435-
}
436-
437433
// CopyFile copies the contents of the file named src to the file named
438434
// by dst. The file will be created if it does not already exist. If the
439435
// destination file exists, all it's contents will be replaced by the contents

Diff for: gcc_preproc_runner.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package builder
3131

3232
import (
33+
"os/exec"
3334
"strings"
3435

3536
"github.com/arduino/arduino-builder/builder_utils"
@@ -41,12 +42,12 @@ import (
4142
)
4243

4344
func GCCPreprocRunner(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) error {
44-
properties, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
45+
cmd, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
4546
if err != nil {
4647
return i18n.WrapError(err)
4748
}
4849

49-
_, _, err = builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_PREPROC_MACROS, true, /* stdout */ utils.ShowIfVerbose, /* stderr */ utils.Show)
50+
_, _, err = utils.ExecCommand(ctx, cmd /* stdout */, utils.ShowIfVerbose /* stderr */, utils.Show)
5051
if err != nil {
5152
return i18n.WrapError(err)
5253
}
@@ -55,34 +56,42 @@ func GCCPreprocRunner(ctx *types.Context, sourceFilePath string, targetFilePath
5556
}
5657

5758
func GCCPreprocRunnerForDiscoveringIncludes(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) ([]byte, error) {
58-
properties, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
59+
cmd, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
5960
if err != nil {
6061
return nil, i18n.WrapError(err)
6162
}
6263

63-
_, stderr, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_PREPROC_MACROS, true, /* stdout */ utils.ShowIfVerbose, /* stderr */ utils.Capture)
64+
_, stderr, err := utils.ExecCommand(ctx, cmd /* stdout */, utils.ShowIfVerbose /* stderr */, utils.Capture)
6465
if err != nil {
6566
return stderr, i18n.WrapError(err)
6667
}
6768

6869
return stderr, nil
6970
}
7071

71-
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) (properties.Map, error) {
72+
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) (*exec.Cmd, error) {
7273
properties := ctx.BuildProperties.Clone()
7374
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = sourceFilePath
7475
properties[constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH] = targetFilePath
7576

7677
includes = utils.Map(includes, utils.WrapWithHyphenI)
7778
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
78-
builder_utils.RemoveHyphenMDDFlagFromGCCCommandLine(properties)
7979

8080
if properties[constants.RECIPE_PREPROC_MACROS] == constants.EMPTY_STRING {
8181
//generate PREPROC_MACROS from RECIPE_CPP_PATTERN
8282
properties[constants.RECIPE_PREPROC_MACROS] = GeneratePreprocPatternFromCompile(properties[constants.RECIPE_CPP_PATTERN])
8383
}
8484

85-
return properties, nil
85+
cmd, err := builder_utils.PrepareCommandForRecipe(ctx, properties, constants.RECIPE_PREPROC_MACROS, true)
86+
if err != nil {
87+
return nil, i18n.WrapError(err)
88+
}
89+
90+
// Remove -MMD argument if present. Leaving it will make gcc try
91+
// to create a /dev/null.d dependency file, which won't work.
92+
cmd.Args = utils.Filter(cmd.Args, func(a string) bool { return a != "-MMD" })
93+
94+
return cmd, nil
8695
}
8796

8897
func GeneratePreprocPatternFromCompile(compilePattern string) string {

0 commit comments

Comments
 (0)