forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocess_sketch.go
110 lines (90 loc) · 3.64 KB
/
preprocess_sketch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package builder
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/legacy/builder/utils"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)
// ArduinoPreprocessorProperties are the platform properties needed to run arduino-preprocessor
var ArduinoPreprocessorProperties = properties.NewFromHashmap(map[string]string{
// Ctags
"tools.arduino-preprocessor.path": "{runtime.tools.arduino-preprocessor.path}",
"tools.arduino-preprocessor.cmd.path": "{path}/arduino-preprocessor",
"tools.arduino-preprocessor.pattern": `"{cmd.path}" "{source_file}" -- -std=gnu++11`,
"preproc.macros.flags": "-w -x c++ -E -CC",
})
func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error {
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
commands := []types.Command{
&ArduinoPreprocessorRunner{},
}
if err := ctx.PreprocPath.MkdirAll(); err != nil {
return errors.WithStack(err)
}
GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp"), ctx.IncludeFolders)
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
return errors.WithStack(err)
}
}
return bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.Source), ctx.SketchBuildPath)
}
type ArduinoPreprocessorRunner struct{}
func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
buildProperties := ctx.BuildProperties
targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp")
preprocProperties := buildProperties.Clone()
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
preprocProperties.Merge(toolProps)
preprocProperties.SetPath("source_file", targetFilePath)
pattern := preprocProperties.Get("pattern")
if pattern == "" {
return errors.New(tr("arduino-preprocessor pattern is missing"))
}
commandLine := preprocProperties.ExpandPropsInString(pattern)
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
if err != nil {
return errors.WithStack(err)
}
command := exec.Command(parts[0], parts[1:]...)
command.Env = append(os.Environ(), ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)
if runtime.GOOS == "windows" {
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
command.Dir = filepath.VolumeName(command.Args[0]) + "/"
//command.Args[0], _ = filepath.Rel(command.Dir, command.Args[0])
}
verbose := ctx.Verbose
if verbose {
fmt.Println(commandLine)
}
buf, err := command.Output()
if err != nil {
return errors.New(errors.WithStack(err).Error() + string(err.(*exec.ExitError).Stderr))
}
result := utils.NormalizeUTF8(buf)
//fmt.Printf("PREPROCESSOR OUTPUT:\n%s\n", output)
ctx.Source = string(result)
return nil
}