blob: a32347317b93bfec5213e9edc5efb1ef57b256bb [file] [log] [blame]
Colin Crossca3e2872017-04-17 15:11:05 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// soong_javac_wrapper expects a javac command line and argments, executes
16// it, and produces an ANSI colorized version of the output on stdout.
17//
18// It also hides the unhelpful and unhideable "warning there is a warning"
19// messages.
20package main
21
22import (
23 "bufio"
24 "fmt"
25 "io"
26 "os"
27 "os/exec"
28 "regexp"
29 "syscall"
30)
31
32// Regular expressions are based on
33// https://chromium.googlesource.com/chromium/src/+/master/build/android/gyp/javac.py
34// Colors are based on clang's output
35var (
36 filelinePrefix = `^([-.\w/\\]+.java:[0-9]+: )`
37 warningRe = regexp.MustCompile(filelinePrefix + `?(warning:) .*$`)
38 errorRe = regexp.MustCompile(filelinePrefix + `(.*?:) .*$`)
39 markerRe = regexp.MustCompile(`()\s*(\^)\s*$`)
40
41 escape = "\x1b"
42 reset = escape + "[0m"
43 bold = escape + "[1m"
44 red = escape + "[31m"
45 green = escape + "[32m"
46 magenta = escape + "[35m"
47)
48
49func main() {
50 exitCode, err := Main(os.Args[0], os.Args[1:])
51 if err != nil {
52 fmt.Fprintln(os.Stderr, err.Error())
53 }
54 os.Exit(exitCode)
55}
56
57func Main(name string, args []string) (int, error) {
58 if len(args) < 1 {
59 return 1, fmt.Errorf("usage: %s javac ...", name)
60 }
61
62 pr, pw, err := os.Pipe()
63 if err != nil {
64 return 1, fmt.Errorf("creating output pipe: %s", err)
65 }
66
67 cmd := exec.Command(args[0], args[1:]...)
68 cmd.Stdin = os.Stdin
69 cmd.Stdout = pw
70 cmd.Stderr = pw
71 err = cmd.Start()
72 if err != nil {
73 return 1, fmt.Errorf("starting subprocess: %s", err)
74 }
75
76 pw.Close()
77
78 // Process subprocess stdout asynchronously
79 errCh := make(chan error)
80 go func() {
81 errCh <- process(pr, os.Stdout)
82 }()
83
84 // Wait for subprocess to finish
85 cmdErr := cmd.Wait()
86
87 // Wait for asynchronous stdout processing to finish
88 err = <-errCh
89
90 // Check for subprocess exit code
91 if cmdErr != nil {
92 if exitErr, ok := cmdErr.(*exec.ExitError); ok {
93 if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
94 if status.Exited() {
95 return status.ExitStatus(), nil
96 } else if status.Signaled() {
97 exitCode := 128 + int(status.Signal())
98 return exitCode, nil
99 } else {
100 return 1, exitErr
101 }
102 } else {
103 return 1, nil
104 }
105 }
106 }
107
108 if err != nil {
109 return 1, err
110 }
111
112 return 0, nil
113}
114
115func process(r io.Reader, w io.Writer) error {
116 scanner := bufio.NewScanner(r)
117 // Some javac wrappers output the entire list of java files being
118 // compiled on a single line, which can be very large, set the maximum
119 // buffer size to 2MB.
120 scanner.Buffer(nil, 2*1024*1024)
121 for scanner.Scan() {
122 processLine(w, scanner.Text())
123 }
124 err := scanner.Err()
125 if err != nil {
126 return fmt.Errorf("scanning input: %s", err)
127 }
128 return nil
129}
130
131func processLine(w io.Writer, line string) {
132 for _, f := range filters {
133 if f.MatchString(line) {
134 return
135 }
136 }
137 for _, p := range colorPatterns {
138 var matched bool
139 if line, matched = applyColor(line, p.color, p.re); matched {
140 break
141 }
142 }
143 fmt.Fprintln(w, line)
144}
145
146// If line matches re, make it bold and apply color to the first submatch
147// Returns line, modified if it matched, and true if it matched.
148func applyColor(line, color string, re *regexp.Regexp) (string, bool) {
149 if m := re.FindStringSubmatchIndex(line); m != nil {
150 tagStart, tagEnd := m[4], m[5]
151 line = bold + line[:tagStart] +
152 color + line[tagStart:tagEnd] + reset + bold +
153 line[tagEnd:] + reset
154 return line, true
155 }
156 return line, false
157}
158
159var colorPatterns = []struct {
160 re *regexp.Regexp
161 color string
162}{
163 {warningRe, magenta},
164 {errorRe, red},
165 {markerRe, green},
166}
167
168var filters = []*regexp.Regexp{
169 regexp.MustCompile(`Note: (Some input files|.*\.java) uses? or overrides? a deprecated API.`),
170 regexp.MustCompile(`Note: Recompile with -Xlint:deprecation for details.`),
171 regexp.MustCompile(`Note: (Some input files|.*\.java) uses? unchecked or unsafe operations.`),
172 regexp.MustCompile(`Note: Recompile with -Xlint:unchecked for details.`),
173}