Skip to content

Commit 2c17e1e

Browse files
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 #230. Signed-off-by: Matthijs Kooijman <[email protected]>
1 parent 87068d2 commit 2c17e1e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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

-4
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,6 @@ func PrepareCommandForRecipe(ctx *types.Context, buildProperties properties.Map,
388388
return command, nil
389389
}
390390

391-
func RemoveHyphenMDDFlagFromGCCCommandLine(buildProperties properties.Map) {
392-
buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS] = strings.Replace(buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS], "-MMD", "", -1)
393-
}
394-
395391
// CopyFile copies the contents of the file named src to the file named
396392
// by dst. The file will be created if it does not already exist. If the
397393
// destination file exists, all it's contents will be replaced by the contents

src/arduino.cc/builder/gcc_preproc_runner.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ package builder
3131

3232
import (
3333
"strings"
34+
"os/exec"
3435

3536
"arduino.cc/builder/builder_utils"
3637
"arduino.cc/builder/constants"
3738
"arduino.cc/builder/i18n"
3839
"arduino.cc/builder/types"
3940
"arduino.cc/builder/utils"
40-
"arduino.cc/properties"
4141
)
4242

4343
func GCCPreprocRunner(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) error {
44-
properties, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
44+
cmd, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
4545
if err != nil {
4646
return i18n.WrapError(err)
4747
}
4848

49-
_, _, err = builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_PREPROC_MACROS, true, /* stdout */ utils.ShowIfVerbose, /* stderr */ utils.Show)
49+
_, _, err = utils.ExecCommand(ctx, cmd, /* stdout */ utils.ShowIfVerbose, /* stderr */ utils.Show)
5050
if err != nil {
5151
return i18n.WrapError(err)
5252
}
@@ -55,34 +55,42 @@ func GCCPreprocRunner(ctx *types.Context, sourceFilePath string, targetFilePath
5555
}
5656

5757
func GCCPreprocRunnerForDiscoveringIncludes(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) ([]byte, error) {
58-
properties, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
58+
cmd, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
5959
if err != nil {
6060
return nil, i18n.WrapError(err)
6161
}
6262

63-
_, stderr, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_PREPROC_MACROS, true, /* stdout */ utils.ShowIfVerbose, /* stderr */ utils.Capture)
63+
_, stderr, err := utils.ExecCommand(ctx, cmd, /* stdout */ utils.ShowIfVerbose, /* stderr */ utils.Capture)
6464
if err != nil {
6565
return stderr, i18n.WrapError(err)
6666
}
6767

6868
return stderr, nil
6969
}
7070

71-
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) (properties.Map, error) {
71+
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) (*exec.Cmd, error) {
7272
properties := ctx.BuildProperties.Clone()
7373
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = sourceFilePath
7474
properties[constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH] = targetFilePath
7575

7676
includes = utils.Map(includes, utils.WrapWithHyphenI)
7777
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
78-
builder_utils.RemoveHyphenMDDFlagFromGCCCommandLine(properties)
7978

8079
if properties[constants.RECIPE_PREPROC_MACROS] == constants.EMPTY_STRING {
8180
//generate PREPROC_MACROS from RECIPE_CPP_PATTERN
8281
properties[constants.RECIPE_PREPROC_MACROS] = GeneratePreprocPatternFromCompile(properties[constants.RECIPE_CPP_PATTERN])
8382
}
8483

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

8896
func GeneratePreprocPatternFromCompile(compilePattern string) string {

0 commit comments

Comments
 (0)