aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/build_java_with_error_prone2.py
blob: 8d7330db9e7e81a98e9d7b10adc0290d84efb08e (plain) (blame)
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
import sys
import os
import re
import subprocess
import platform


ERROR_PRONE_FLAGS = [
    '-Xep:FunctionalInterfaceMethodChanged:WARN',
    '-Xep:ReturnValueIgnored:WARN',
]

JAVA10_EXPORTS = [
    '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
    '--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
]


def get_classpath(cmd):
    for i, part in enumerate(cmd):
        if part == '-classpath':
            i += 1
            if i < len(cmd):
                return cmd[i]
            else:
                return None
    return None


def parse_args(argv):
    parsed = []
    for i in range(len(argv)):
        if not argv[i].startswith('-'):
            parsed.append(argv[i])
            if len(parsed) >= 3:
                break
    return parsed + [argv[i + 1 :]]


def fix_cmd_line(error_prone_tool, cmd):
    if not error_prone_tool:
        return cmd
    for f in cmd:
        if f.startswith('-Xep'):
            ERROR_PRONE_FLAGS.append(f)
    for f in ERROR_PRONE_FLAGS:
        if f in cmd:
            cmd.remove(f)
    if '-processor' in cmd:
        classpath = get_classpath(cmd)
        if classpath:
            error_prone_tool = error_prone_tool + os.pathsep + classpath
    return (
        cmd
        + JAVA10_EXPORTS
        + ['-processorpath', error_prone_tool, '-XDcompilePolicy=byfile']
        + [(' '.join(['-Xplugin:ErrorProne'] + ERROR_PRONE_FLAGS))]
    )


# NOTE: legacy, only for "devtools/ya/jbuild"
def just_do_it(argv):
    java, javac, error_prone_tool, javac_cmd = parse_args(argv)
    for f in javac_cmd:
        if f.startswith('-Xep'):
            ERROR_PRONE_FLAGS.append(f)
    for f in ERROR_PRONE_FLAGS:
        if f in javac_cmd:
            javac_cmd.remove(f)
    if '-processor' in javac_cmd:
        classpath = get_classpath(javac_cmd)
        if classpath:
            error_prone_tool = error_prone_tool + os.pathsep + classpath
    cmd = (
        [javac]
        + JAVA10_EXPORTS
        + ['-processorpath', error_prone_tool, '-XDcompilePolicy=byfile']
        + [(' '.join(['-Xplugin:ErrorProne'] + ERROR_PRONE_FLAGS))]
        + javac_cmd
    )
    if platform.system() == 'Windows':
        sys.exit(subprocess.Popen(cmd).wait())
    else:
        os.execv(cmd[0], cmd)


if __name__ == '__main__':
    just_do_it(sys.argv[1:])