aboutsummaryrefslogtreecommitdiff
path: root/src/main/groovy/frege/gradle/tasks/FregeCompile.groovy
blob: 290f7502c4b0f964fce5d18c222ae649bfccb4de (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
165
166
167
168
169
170
171
172
package frege.gradle.tasks

import groovy.transform.TypeChecked
import org.gradle.api.Action
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.process.JavaExecSpec

@TypeChecked
class FregeCompile extends AbstractCompile {

    FileCollection classpath

    @Input
    String stackSize = "4m"

    @Input
    boolean hints = false

    @Input
    boolean optimize = false

    boolean verbose = false

    @Input
    boolean inline = true

    @Input
    boolean make = true

    @Input
    boolean compileGeneratedJava = true

    @Input
    String target = ""

    @Input
    boolean comments = false

    @Input
    boolean suppressWarnings = false

    @Input
    String explain = ""

    @Input
    String extraArgs = ""

    @Input
    String allArgs = "" // this is an option to overrule all other settings

    @Input
    String module = ""

    @Optional @InputFiles
    FileCollection fregepath

    @Input
    String mainClass = "frege.compiler.Main"

    @Optional
    @Input
    List<String> allJvmArgs = []

    @Input
    String encoding = ""

    @Input
    String prefix = ""

    List<File> sourcePaths = []

    @Override
    @TaskAction
    protected void compile() {
        def jvmArgs = allJvmArgs
        if (jvmArgs.isEmpty()) {
            jvmArgs << "-Xss$stackSize".toString()
        }
        def compilerArgs = allArgs ? allArgs.split().toList() : assembleArguments()

        logger.info("Calling Frege compiler with compilerArgs: '$compilerArgs'")

        //TODO integrate with gradle compiler daemon infrastructure and skip internal execution

        def errOutputStream = new ByteArrayOutputStream();
        def outOutputStream = new ByteArrayOutputStream();
        project.javaexec(new Action<JavaExecSpec>() {
            @Override
            void execute(JavaExecSpec javaExecSpec) {
                javaExecSpec.args = compilerArgs
                javaExecSpec.classpath = FregeCompile.this.classpath
                javaExecSpec.main = mainClass
                javaExecSpec.errorOutput = System.err;
                javaExecSpec.standardOutput = System.out;
            }
        });

    }

    public FregeCompile source(Object... sources) {
        super.source(sources);
        // track directory roots
        for (Object source : sources) {
            sourcePaths.add(project.file(source))
        }
        return this;
    }

    protected List<String> assembleArguments() {
        List args = []
        if (hints)
            args << "-hints"
        if (optimize) {
            args << "-O"
            args << "-inline"
        }
        if (inline & !optimize)
            args << "-inline"
        if (make)
            args << "-make"
        if (!compileGeneratedJava) args << "-j"
        if (target != "") {
            args << "-target"
            args << target
        }
        if (comments) args << "-comments"
        if (suppressWarnings) args << "-nowarn"
        if (explain != "") {
            args << "-explain"
            args << explain
        }
        if (verbose)
            args << "-v"


        if (fregepath != null && !fregepath.isEmpty()) {
            args << "-fp"
            args << fregepath.files.collect { f -> f.absolutePath }.join(File.pathSeparator)
        }

        if (sourcePaths != null && !sourcePaths.isEmpty()) {
            args << "-sp"
            args << sourcePaths.collect { d -> d.absolutePath }.join(File.pathSeparator)
        }

        if (encoding != "") {
            args << "-enc"
            args << encoding
        }

        if (prefix != "") {
            args << "-prefix"
            args << prefix
        }

        args << "-d"
        args << getDestinationDir().absolutePath

        if (!module.isEmpty()) {
            logger.info "compiling module '$module'"
            args << module
        } else {
            args = (args + extraArgs.split().toList()).toList()
        }
        args
    }
}