|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package preprocessor |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/executils" |
| 24 | + f "github.com/arduino/arduino-cli/internal/algorithms" |
| 25 | + "github.com/arduino/arduino-cli/legacy/builder/utils" |
| 26 | + "github.com/arduino/go-paths-helper" |
| 27 | + "github.com/arduino/go-properties-orderedmap" |
| 28 | + "github.com/pkg/errors" |
| 29 | +) |
| 30 | + |
| 31 | +// GCC performs a run of the gcc preprocess (macro/includes expansion). The function outputs the result |
| 32 | +// to targetFilePath. Returns the stdout/stderr of gcc if any. |
| 33 | +func GCC(sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList, buildProperties *properties.Map) ([]byte, []byte, error) { |
| 34 | + gccBuildProperties := properties.NewMap() |
| 35 | + gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC") |
| 36 | + gccBuildProperties.Merge(buildProperties) |
| 37 | + gccBuildProperties.Set("build.library_discovery_phase", "1") |
| 38 | + gccBuildProperties.SetPath("source_file", sourceFilePath) |
| 39 | + gccBuildProperties.SetPath("preprocessed_file_path", targetFilePath) |
| 40 | + |
| 41 | + includesStrings := f.Map(includes.AsStrings(), utils.WrapWithHyphenI) |
| 42 | + gccBuildProperties.Set("includes", strings.Join(includesStrings, " ")) |
| 43 | + |
| 44 | + const gccPreprocRecipeProperty = "recipe.preproc.macros" |
| 45 | + if gccBuildProperties.Get(gccPreprocRecipeProperty) == "" { |
| 46 | + // autogenerate preprocess macros recipe from compile recipe |
| 47 | + preprocPattern := gccBuildProperties.Get("recipe.cpp.o.pattern") |
| 48 | + // add {preproc.macros.flags} to {compiler.cpp.flags} |
| 49 | + preprocPattern = strings.Replace(preprocPattern, "{compiler.cpp.flags}", "{compiler.cpp.flags} {preproc.macros.flags}", 1) |
| 50 | + // replace "{object_file}" with "{preprocessed_file_path}" |
| 51 | + preprocPattern = strings.Replace(preprocPattern, "{object_file}", "{preprocessed_file_path}", 1) |
| 52 | + |
| 53 | + gccBuildProperties.Set(gccPreprocRecipeProperty, preprocPattern) |
| 54 | + } |
| 55 | + |
| 56 | + pattern := gccBuildProperties.Get(gccPreprocRecipeProperty) |
| 57 | + if pattern == "" { |
| 58 | + return nil, nil, errors.Errorf(tr("%s pattern is missing"), gccPreprocRecipeProperty) |
| 59 | + } |
| 60 | + |
| 61 | + commandLine := gccBuildProperties.ExpandPropsInString(pattern) |
| 62 | + args, err := properties.SplitQuotedString(commandLine, `"'`, false) |
| 63 | + if err != nil { |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + |
| 67 | + // Remove -MMD argument if present. Leaving it will make gcc try |
| 68 | + // to create a /dev/null.d dependency file, which won't work. |
| 69 | + args = f.Filter(args, f.NotEquals("-MMD")) |
| 70 | + |
| 71 | + proc, err := executils.NewProcess(nil, args...) |
| 72 | + if err != nil { |
| 73 | + return nil, nil, err |
| 74 | + } |
| 75 | + stdout, stderr, err := proc.RunAndCaptureOutput(context.Background()) |
| 76 | + |
| 77 | + // Append gcc arguments to stdout |
| 78 | + stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...) |
| 79 | + |
| 80 | + return stdout, stderr, err |
| 81 | +} |
0 commit comments