aboutsummaryrefslogtreecommitdiff
path: root/src/main/groovy/frege/gradle/tasks/FregeCompile.groovy
blob: ffd6aa35c1d5ffb04d685eec1c28594041c4e6b4 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package frege.gradle.tasks

import groovy.transform.TypeChecked
import org.gradle.api.Action
import org.gradle.api.file.Directory
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

/* Compiler flags as of 3.25.84

-d directory    target directory for *.java and *.class files
-fp classpath   where to find imported frege packages
-enc charset    charset for source code files, standard is UTF-8
-enc DEFAULT    platform default charset for source code files
-target n.m     generate code for java version n.m, also passed to javac
-nocp           exclude java classpath from -fp
-hints          print more detailed error messages and warnings
-inline         inline functions where possible
-strict-pats    check patterns in multi-argument functions strictly from left to right
-comments       generate commented code
-explain i[-j]  print some debugging output from type checker
               regarding line(s) i (to j). May help to understand
               inexplicable type errors better.
-nowarn         don't print warnings (not recommended)
-v              verbose mode on
-make           build outdated or missing imports
-sp srcpath     look for source files in srcpath, default is .
-target x.y     generate code for java version x.y, default is the
               version of the JVM the compiler is running in.
-j              do not run the java compiler
-ascii          do not use →, ⇒, ∀ and ∷ when presenting types,
               and use ascii characters for java generics variables
-greek          make greek type variables
-fraktur        make 𝖋𝖗𝖆𝖐𝖙𝖚𝖗 type variables
-latin          make latin type variables

*/



@TypeChecked
class FregeCompile extends AbstractCompile {

    FileCollection classpath

    @Input
    String stackSize = "4m"

    @Input
    boolean hints = false

    @Input
    boolean optimize = false

    @Input
    boolean strictPats = false

    @Input
    boolean excludeJavaClasspath = 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
    File destinationDir

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

    @Input
    List<String> allJvmArgs = []

    @Input
    String encoding = ""

    @Input
    String prefix = ""

    List<File> sourcePaths = []

    @Override
    @TaskAction
    protected void compile() {
        def jvmArgumentsToUse = allJvmArgs.empty ? ["-Xss$stackSize"] : new ArrayList<String>(allJvmArgs)
        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
        project.javaexec(new Action<JavaExecSpec>() {
            @Override
            void execute(JavaExecSpec javaExecSpec) {
                javaExecSpec.args = compilerArgs
                javaExecSpec.classpath = FregeCompile.this.classpath
                javaExecSpec.main = mainClass
                javaExecSpec.jvmArgs = jvmArgumentsToUse as List<String>
                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 (strictPats)
            args << "-strict-pats"
        if (excludeJavaClasspath)
            args << "-nocp"
        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
    }
}