2017-02-08 08:25:30 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-18 00:11:05 +02:00
|
|
|
// soong_javac_wrapper expects a javac command line and argments, executes
|
|
|
|
// it, and produces an ANSI colorized version of the output on stdout.
|
2017-02-08 08:25:30 +01:00
|
|
|
//
|
|
|
|
// It also hides the unhelpful and unhideable "warning there is a warning"
|
|
|
|
// messages.
|
2017-10-17 22:56:52 +02:00
|
|
|
//
|
|
|
|
// Each javac build statement has an order-only dependency on the
|
|
|
|
// soong_javac_wrapper tool, which means the javac command will not be rerun
|
|
|
|
// if soong_javac_wrapper changes. That means that soong_javac_wrapper must
|
|
|
|
// not do anything that will affect the results of the build.
|
2017-02-08 08:25:30 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2017-04-18 00:11:05 +02:00
|
|
|
"os/exec"
|
2017-02-08 08:25:30 +01:00
|
|
|
"regexp"
|
2019-11-11 22:07:38 +01:00
|
|
|
"strconv"
|
2017-04-18 00:11:05 +02:00
|
|
|
"syscall"
|
2017-02-08 08:25:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regular expressions are based on
|
|
|
|
// https://chromium.googlesource.com/chromium/src/+/master/build/android/gyp/javac.py
|
|
|
|
// Colors are based on clang's output
|
|
|
|
var (
|
2017-04-18 00:11:05 +02:00
|
|
|
filelinePrefix = `^([-.\w/\\]+.java:[0-9]+: )`
|
|
|
|
warningRe = regexp.MustCompile(filelinePrefix + `?(warning:) .*$`)
|
|
|
|
errorRe = regexp.MustCompile(filelinePrefix + `(.*?:) .*$`)
|
|
|
|
markerRe = regexp.MustCompile(`()\s*(\^)\s*$`)
|
2017-02-08 08:25:30 +01:00
|
|
|
|
|
|
|
escape = "\x1b"
|
|
|
|
reset = escape + "[0m"
|
|
|
|
bold = escape + "[1m"
|
|
|
|
red = escape + "[31m"
|
|
|
|
green = escape + "[32m"
|
|
|
|
magenta = escape + "[35m"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-04-20 19:39:38 +02:00
|
|
|
exitCode, err := Main(os.Stdout, os.Args[0], os.Args[1:])
|
2017-02-08 08:25:30 +01:00
|
|
|
if err != nil {
|
2017-04-18 00:11:05 +02:00
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
2017-02-08 08:25:30 +01:00
|
|
|
}
|
2017-04-18 00:11:05 +02:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2017-04-20 19:39:38 +02:00
|
|
|
func Main(out io.Writer, name string, args []string) (int, error) {
|
2017-04-18 00:11:05 +02:00
|
|
|
if len(args) < 1 {
|
|
|
|
return 1, fmt.Errorf("usage: %s javac ...", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return 1, fmt.Errorf("creating output pipe: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = pw
|
|
|
|
cmd.Stderr = pw
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return 1, fmt.Errorf("starting subprocess: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pw.Close()
|
|
|
|
|
2019-11-11 22:07:38 +01:00
|
|
|
proc := processor{}
|
2017-04-18 00:11:05 +02:00
|
|
|
// Process subprocess stdout asynchronously
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
2019-11-11 22:07:38 +01:00
|
|
|
errCh <- proc.process(pr, out)
|
2017-04-18 00:11:05 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for subprocess to finish
|
|
|
|
cmdErr := cmd.Wait()
|
|
|
|
|
|
|
|
// Wait for asynchronous stdout processing to finish
|
|
|
|
err = <-errCh
|
|
|
|
|
|
|
|
// Check for subprocess exit code
|
|
|
|
if cmdErr != nil {
|
|
|
|
if exitErr, ok := cmdErr.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
if status.Exited() {
|
|
|
|
return status.ExitStatus(), nil
|
|
|
|
} else if status.Signaled() {
|
|
|
|
exitCode := 128 + int(status.Signal())
|
|
|
|
return exitCode, nil
|
|
|
|
} else {
|
|
|
|
return 1, exitErr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
2017-02-08 08:25:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:07:38 +01:00
|
|
|
type processor struct {
|
|
|
|
silencedWarnings int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proc *processor) process(r io.Reader, w io.Writer) error {
|
2017-02-08 08:25:30 +01:00
|
|
|
scanner := bufio.NewScanner(r)
|
2017-02-20 04:51:45 +01:00
|
|
|
// Some javac wrappers output the entire list of java files being
|
|
|
|
// compiled on a single line, which can be very large, set the maximum
|
|
|
|
// buffer size to 2MB.
|
|
|
|
scanner.Buffer(nil, 2*1024*1024)
|
2017-02-08 08:25:30 +01:00
|
|
|
for scanner.Scan() {
|
2019-11-11 22:07:38 +01:00
|
|
|
proc.processLine(w, scanner.Text())
|
2017-02-08 08:25:30 +01:00
|
|
|
}
|
2017-04-18 00:11:05 +02:00
|
|
|
err := scanner.Err()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("scanning input: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-08 08:25:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:07:38 +01:00
|
|
|
func (proc *processor) processLine(w io.Writer, line string) {
|
|
|
|
for _, f := range warningFilters {
|
|
|
|
if f.MatchString(line) {
|
|
|
|
proc.silencedWarnings++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 08:25:30 +01:00
|
|
|
for _, f := range filters {
|
|
|
|
if f.MatchString(line) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 22:07:38 +01:00
|
|
|
if match := warningCount.FindStringSubmatch(line); match != nil {
|
|
|
|
c, err := strconv.Atoi(match[1])
|
|
|
|
if err == nil {
|
|
|
|
c -= proc.silencedWarnings
|
|
|
|
if c == 0 {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
line = fmt.Sprintf("%d warning", c)
|
|
|
|
if c > 1 {
|
|
|
|
line += "s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 08:25:30 +01:00
|
|
|
for _, p := range colorPatterns {
|
|
|
|
var matched bool
|
|
|
|
if line, matched = applyColor(line, p.color, p.re); matched {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If line matches re, make it bold and apply color to the first submatch
|
|
|
|
// Returns line, modified if it matched, and true if it matched.
|
|
|
|
func applyColor(line, color string, re *regexp.Regexp) (string, bool) {
|
|
|
|
if m := re.FindStringSubmatchIndex(line); m != nil {
|
2017-04-18 00:11:05 +02:00
|
|
|
tagStart, tagEnd := m[4], m[5]
|
2017-02-08 08:25:30 +01:00
|
|
|
line = bold + line[:tagStart] +
|
|
|
|
color + line[tagStart:tagEnd] + reset + bold +
|
|
|
|
line[tagEnd:] + reset
|
|
|
|
return line, true
|
|
|
|
}
|
|
|
|
return line, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var colorPatterns = []struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
color string
|
|
|
|
}{
|
|
|
|
{warningRe, magenta},
|
|
|
|
{errorRe, red},
|
|
|
|
{markerRe, green},
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:07:38 +01:00
|
|
|
var warningCount = regexp.MustCompile(`^([0-9]+) warning(s)?$`)
|
|
|
|
|
|
|
|
var warningFilters = []*regexp.Regexp{
|
|
|
|
regexp.MustCompile(`bootstrap class path not set in conjunction with -source`),
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:25:30 +01:00
|
|
|
var filters = []*regexp.Regexp{
|
|
|
|
regexp.MustCompile(`Note: (Some input files|.*\.java) uses? or overrides? a deprecated API.`),
|
|
|
|
regexp.MustCompile(`Note: Recompile with -Xlint:deprecation for details.`),
|
|
|
|
regexp.MustCompile(`Note: (Some input files|.*\.java) uses? unchecked or unsafe operations.`),
|
|
|
|
regexp.MustCompile(`Note: Recompile with -Xlint:unchecked for details.`),
|
2018-10-03 01:11:17 +02:00
|
|
|
|
|
|
|
regexp.MustCompile(`javadoc: warning - The old Doclet and Taglet APIs in the packages`),
|
|
|
|
regexp.MustCompile(`com.sun.javadoc, com.sun.tools.doclets and their implementations`),
|
|
|
|
regexp.MustCompile(`are planned to be removed in a future JDK release. These`),
|
|
|
|
regexp.MustCompile(`components have been superseded by the new APIs in jdk.javadoc.doclet.`),
|
|
|
|
regexp.MustCompile(`Users are strongly recommended to migrate to the new APIs.`),
|
|
|
|
|
|
|
|
regexp.MustCompile(`javadoc: option --boot-class-path not allowed with target 1.9`),
|
2017-02-08 08:25:30 +01:00
|
|
|
}
|