aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/compiler_wrapper.go
blob: 3f94e185aa1c34c7ccde4ace4adaa8cbf3c29840 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main

import (
	"fmt"
	"io"
	"path/filepath"
	"strings"
	"syscall"
)

func callCompiler(env env, cfg *config, inputCmd *command) int {
	exitCode := 0
	var compilerErr error
	if shouldForwardToOldWrapper(env, inputCmd) {
		// TODO: Once this is only checking for bisect, create a command
		// that directly calls the bisect driver in calcCompilerCommand.
		exitCode, compilerErr = forwardToOldWrapper(env, cfg, inputCmd)
	} else if cfg.oldWrapperPath != "" {
		exitCode, compilerErr = callCompilerWithRunAndCompareToOldWrapper(env, cfg, inputCmd)
	} else {
		exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd)
	}
	if compilerErr != nil {
		printCompilerError(env.stderr(), compilerErr)
		exitCode = 1
	}
	return exitCode
}

func callCompilerWithRunAndCompareToOldWrapper(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
	recordingEnv := &commandRecordingEnv{
		env: env,
	}
	// Note: this won't do a real exec as recordingEnv redirects exec to run.
	if exitCode, err = callCompilerInternal(recordingEnv, cfg, inputCmd); err != nil {
		return 0, err
	}
	if err = compareToOldWrapper(env, cfg, inputCmd, recordingEnv.cmdResults, exitCode); err != nil {
		return exitCode, err
	}
	return exitCode, nil
}

func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
	if err := checkUnsupportedFlags(inputCmd); err != nil {
		return 0, err
	}
	mainBuilder, err := newCommandBuilder(env, cfg, inputCmd)
	if err != nil {
		return 0, err
	}
	var compilerCmd *command
	clangSyntax := processClangSyntaxFlag(mainBuilder)
	if mainBuilder.target.compilerType == clangType {
		cSrcFile, useClangTidy := processClangTidyFlags(mainBuilder)
		compilerCmd, err = calcClangCommand(useClangTidy, mainBuilder)
		if err != nil {
			return 0, err
		}
		if useClangTidy {
			if err := runClangTidy(env, compilerCmd, cSrcFile); err != nil {
				return 0, err
			}
		}
	} else {
		if clangSyntax {
			forceLocal := false
			clangCmd, err := calcClangCommand(forceLocal, mainBuilder.clone())
			if err != nil {
				return 0, err
			}
			exitCode, err = checkClangSyntax(env, clangCmd)
			if err != nil || exitCode != 0 {
				return exitCode, err
			}
		}
		compilerCmd = calcGccCommand(mainBuilder)
	}
	if shouldForceDisableWError(env) {
		return doubleBuildWithWNoError(env, cfg, compilerCmd)
	}
	if err := env.exec(compilerCmd); err != nil {
		if userErr, ok := getCCacheError(compilerCmd, err); ok {
			return 0, userErr
		}
		// Note: This case only happens when the underlying env is not
		// really doing an exec, e.g. commandRecordingEnv.
		if exitCode, ok := getExitCode(err); ok {
			return exitCode, nil
		}
		return exitCode, wrapErrorwithSourceLocf(err, "failed to execute %#v", compilerCmd)
	}
	return 0, err
}

func calcClangCommand(forceLocal bool, builder *commandBuilder) (*command, error) {
	sysroot := processSysrootFlag(builder)
	builder.addPreUserArgs(builder.cfg.clangFlags...)
	calcCommonPreUserArgs(builder)
	if err := processClangFlags(builder); err != nil {
		return nil, err
	}
	if !forceLocal {
		processGomaCCacheFlags(sysroot, builder)
	}
	return builder.build(), nil
}

func calcGccCommand(builder *commandBuilder) *command {
	sysroot := processSysrootFlag(builder)
	builder.addPreUserArgs(builder.cfg.gccFlags...)
	calcCommonPreUserArgs(builder)
	processGccFlags(builder)
	processGomaCCacheFlags(sysroot, builder)
	return builder.build()
}

func calcCommonPreUserArgs(builder *commandBuilder) {
	builder.addPreUserArgs(builder.cfg.commonFlags...)
	processPieFlags(builder)
	processStackProtectorFlags(builder)
	processThumbCodeFlags(builder)
	processX86Flags(builder)
	processSanitizerFlags(builder)
}

func processGomaCCacheFlags(sysroot string, builder *commandBuilder) {
	gomaccUsed := processGomaCccFlags(builder)
	if !gomaccUsed {
		processCCacheFlag(sysroot, builder)
	}
}

func getAbsWrapperDir(env env, wrapperPath string) (string, error) {
	if !filepath.IsAbs(wrapperPath) {
		wrapperPath = filepath.Join(env.getwd(), wrapperPath)
	}
	evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath)
	if err != nil {
		return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
	}
	return filepath.Dir(evaledCmdPath), nil
}

func getCCacheError(compilerCmd *command, compilerCmdErr error) (ccacheErr userError, ok bool) {
	if en, ok := compilerCmdErr.(syscall.Errno); ok && en == syscall.ENOENT &&
		strings.Contains(compilerCmd.path, "ccache") {
		ccacheErr =
			newUserErrorf("ccache not found under %s. Please install it",
				compilerCmd.path)
		return ccacheErr, ok
	}
	return ccacheErr, false
}

func printCompilerError(writer io.Writer, compilerErr error) {
	if _, ok := compilerErr.(userError); ok {
		fmt.Fprintf(writer, "%s\n", compilerErr)
	} else {
		fmt.Fprintf(writer,
			"Internal error. Please report to chromeos-toolchain@google.com.\n%s\n",
			compilerErr)
	}
}