File: build.gradle

package info (click to toggle)
kotlin 1.3.31%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 109,908 kB
  • sloc: java: 454,756; xml: 18,599; javascript: 10,452; sh: 513; python: 97; makefile: 69; ansic: 4
file content (402 lines) | stat: -rw-r--r-- 12,555 bytes parent folder | download | duplicates (2)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
plugins {
    id "com.moowork.node" version "1.2.0"
}

description = 'Kotlin Standard Library for JS'

apply plugin: 'kotlin-platform-js'
apply plugin: 'idea'

configureDist(project)
configurePublishing(project)


def builtinsSrcDir = "${buildDir}/builtin-sources"
def builtinsSrcDir2 = "${buildDir}/builtin-sources-for-builtins"
def commonSrcDir = "${projectDir}/../src/kotlin"
def commonSrcDir2 = "${projectDir}/../common/src"
def coroutinesExpJsSrcDir = "${rootDir}/libraries/stdlib/coroutines-experimental/js/src"

def builtinsDir = "${rootDir}/core/builtins"
def unsignedCommonSrcDir = "${rootDir}/libraries/stdlib/unsigned/src"
def experimentalSrcDir = "${rootDir}/libraries/stdlib/experimental"
def experimentalJsModuleName = 'kotlin-experimental'
def coroutinesJsModuleName = 'kotlin-stdlib-coroutines'
def jsSrcDir = "src"
def jsTestSrcDir = "test"
def jsSrcJsDir = "${jsSrcDir}/js"
def jsOutputFile = "${buildDir}/classes/kotlin.js"
def jsOutputMetaFile = "${buildDir}/classes/kotlin.meta.js"

def kotlinTestJsOutputFile = "${project(':kotlin-test:kotlin-test-js').buildDir}/classes/main/kotlin-test.js"

// TODO: take from sourcesets' outputs
def jsTestOutputFile = "${buildDir}/classes/kotlin/test/kotlin-stdlib-js_test.js"
def kotlinTestJsTestOutputFile = "${project(':kotlin-test:kotlin-test-js').buildDir}/classes/kotlin/test/kotlin-test-js_test.js"


sourceSets {
    builtins {
        kotlin {
            srcDir builtinsSrcDir2
            srcDir 'runtime'
        }
    }

    main {
        kotlin {
            srcDir builtinsSrcDir
            srcDir jsSrcDir
        }
    }

    experimental {
        if(!System.properties.'idea.active')
        kotlin {
            srcDir experimentalSrcDir
        }
    }

    test {
        kotlin {
            srcDir jsTestSrcDir
        }
    }

    coroutinesExperimental {
        kotlin {
            srcDir coroutinesExpJsSrcDir
        }
    }
}

configurations {
    commonSources
}
dependencies {
    expectedBy project(":kotlin-stdlib-common")
    commonSources project(path: ":kotlin-stdlib-common", configuration: "sources")
    testCompile project(':kotlin-test:kotlin-test-js')
    coroutinesExperimentalCompile project.files { sourceSets.main.output.files }.builtBy(compileKotlin2Js)
}

task prepareComparableSource(type: Copy) {
    doFirst {
        delete builtinsSrcDir2
    }
    from("${builtinsDir}/native/kotlin") {
        include "Comparable.kt"
    }
    into builtinsSrcDir2
}

task prepareBuiltinsSources(type: Copy) {
    doFirst {
        delete builtinsSrcDir
    }
    from("${builtinsDir}/native/kotlin") {
        include "Iterator.kt"
        include "Collections.kt"
        include "CharSequence.kt"
        include "Annotation.kt"
    }
    from("${builtinsDir}/src/kotlin/") {
        include "annotation/Annotations.kt"
        include "Function.kt"
        include "Iterators.kt"
        include "Range.kt"
        include "Progressions.kt"
        include "ProgressionIterators.kt"
        include "Ranges.kt"
        include "internal/InternalAnnotations.kt"
        include "internal/progressionUtil.kt"
        include "reflect/**/*.kt"
        include "Unit.kt"
    }
    into builtinsSrcDir
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile) {
    kotlinOptions {
        main = "noCall"
        moduleKind = "commonjs"
        freeCompilerArgs = [
                "-version",
                "-Xallow-kotlin-package",
                "-Xallow-result-return-type"
        ]
    }
}

compileBuiltinsKotlin2Js {
    dependsOn prepareComparableSource
    kotlinOptions {
        metaInfo = false
        outputFile = "${buildDir}/classes/builtins/kotlin.js"
        sourceMap = true
        sourceMapPrefix = "./"
    }
}

compileKotlin2Js {
    dependsOn prepareBuiltinsSources
    kotlinOptions {
        outputFile = "${buildDir}/classes/main/kotlin.js"
        sourceMap = true
        sourceMapPrefix = "./"
        freeCompilerArgs += [
            "-source-map-base-dirs", [builtinsSrcDir, jsSrcDir, commonSrcDir, commonSrcDir2, unsignedCommonSrcDir].collect { file(it).absoluteFile }.join(File.pathSeparator),
            "-Xuse-experimental=kotlin.Experimental",
            "-Xuse-experimental=kotlin.ExperimentalMultiplatform",
            "-Xuse-experimental=kotlin.contracts.ExperimentalContracts",
            "-XXLanguage:+InlineClasses"
        ]
    }
}

compileExperimentalKotlin2Js {
    dependsOn compileKotlin2Js
    kotlinOptions {
        languageVersion = "1.3"
        apiVersion = "1.3"
        outputFile = "${buildDir}/classes/experimental/${experimentalJsModuleName}.js"
        sourceMap = true
        sourceMapPrefix = "./"
        freeCompilerArgs += ["-source-map-base-dirs", [experimentalSrcDir].join(File.pathSeparator)]
    }
}

compileCoroutinesExperimentalKotlin2Js {
    kotlinOptions {
        languageVersion = "1.3"
        apiVersion = "1.3"
        outputFile = "${buildDir}/classes/coroutinesExperimental/kotlin.js"
        sourceMap = true
        sourceMapPrefix = "./"
        freeCompilerArgs += [
            "-Xuse-experimental=kotlin.contracts.ExperimentalContracts",
            "-Xuse-experimental=kotlin.Experimental",
            "-Xcoroutines=enable",
            "-XXLanguage:-ReleaseCoroutines"
        ]
    }
}

compileTestKotlin2Js {
    kotlinOptions {
        moduleKind = "umd"
        freeCompilerArgs += [
            "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"
        ]
    }
}

task compileJs(type: NoDebugJavaExec) {
    dependsOn compileBuiltinsKotlin2Js, compileKotlin2Js, compileExperimentalKotlin2Js, compileCoroutinesExperimentalKotlin2Js
    inputs.files(compileBuiltinsKotlin2Js.outputs.files)
    inputs.files(compileKotlin2Js.outputs.files)
    inputs.files(compileExperimentalKotlin2Js.outputs.files)
    inputs.files(compileCoroutinesExperimentalKotlin2Js.outputs.files)
    inputs.dir(jsSrcDir)
    outputs.file(jsOutputFile)
    outputs.file("${jsOutputFile}.map")

    def inputFiles = fileTree(jsSrcJsDir) {
        include '**/*.js'
    }

    main = "org.jetbrains.kotlin.cli.js.internal.JSStdlibLinker"
    doFirst {
        args = [jsOutputFile, rootDir, "$jsSrcDir/wrapper.js"] + inputFiles.collect { it.path }.sort() +
               (compileBuiltinsKotlin2Js.outputs.files.collect { it.path }.sort() +
                compileKotlin2Js.outputs.files.collect { it.path }.sort() +
                compileCoroutinesExperimentalKotlin2Js.outputs.files.collect { it.path }.sort() /* +
                compileExperimentalKotlin2Js.outputs.files.collect { it.path }.sort() */).findAll {
                   it.endsWith(".js") && !it.endsWith(".meta.js")
               }
    }
    classpath = configurations.kotlinCompilerClasspath

    doLast {
        ant.replaceregexp(
                file: jsOutputFile,
                match: "module.exports,\\s*require\\([^)]+\\)",
                replace: "",
                byline: "true", encoding: "UTF-8")
        ant.replaceregexp(
                file: jsOutputFile,
                match: "function\\s*\\(_,\\s*Kotlin\\)",
                replace: "function()",
                byline: "true", encoding: "UTF-8")
        ant.replaceregexp(
                file: jsOutputFile,
                match: "return\\s+_;",
                replace: "",
                byline: "true", encoding: "UTF-8")

        def sourceMapFile = file("${jsOutputFile}.map")
        def sourceMap = new groovy.json.JsonSlurper().parseText(sourceMapFile.text)

        def sourceMapBasePaths = [
                "./",
                "libraries/stdlib/js/src/js/",
                "libraries/stdlib/js/src/",
        ]
        sourceMap.sources = sourceMap.sources.collect { sourcePath ->
            def prefixToRemove = sourceMapBasePaths.find { basePath -> sourcePath.startsWith(basePath) }
            if (prefixToRemove != null) sourcePath.substring(prefixToRemove.length()) else sourcePath
        }

        def sourceMapSourcesBaseDirs = [jsSrcDir, jsSrcJsDir, builtinsSrcDir, commonSrcDir, commonSrcDir2, projectDir, experimentalSrcDir,  unsignedCommonSrcDir]

        sourceMap.sourcesContent = sourceMap.sources.collect { sourceName ->
            def text = sourceMapSourcesBaseDirs.collect { file("$it/$sourceName") }.find { it.exists() }?.text
            if (text == null) logger.warn("Sources missing for file $sourceName")
            text
        }

        sourceMapFile.text = groovy.json.JsonOutput.toJson(sourceMap)

        file(jsOutputMetaFile).text = file(compileKotlin2Js.outputFile.path.replaceAll('\\.js$', '.meta.js')).text +
                                      file(compileCoroutinesExperimentalKotlin2Js.outputFile.path.replaceAll('\\.js$', '.meta.js')).text /* +
                                      file(compileExperimentalKotlin2Js.outputFile.path.replaceAll('\\.js$', '.meta.js')).text
                                              .replaceFirst(experimentalJsModuleName, 'kotlin') */
    }
}

classes.dependsOn compileJs


jar {
    enabled false
}

task mergedJar(type: Jar, dependsOn: compileJs) {
    classifier = null
    manifestAttributes(manifest, project, 'Main')

    // TODO: Use standard implementation title after js stdlib detector becomes more flexible (KT-17655)
    Properties properties = new Properties()
    new File("${rootDir}/resources/kotlinManifest.properties").withInputStream {
        properties.load(it)
    }
    manifest.attributes 'Implementation-Title': properties."manifest.impl.title.kotlin.javascript.stdlib"

    includeEmptyDirs false
    duplicatesStrategy DuplicatesStrategy.EXCLUDE
    from jsOutputFile
    from jsOutputMetaFile
    from "${jsOutputFile}.map"
    from sourceSets.main.output
    from sourceSets.experimental.output
    from("${buildDir}/classes/coroutinesExperimental/kotlin") {
        into coroutinesJsModuleName
    }
    exclude "${experimentalJsModuleName}.*"
}

task sourcesJar(type: Jar, dependsOn: compileJs) {
    classifier = 'sources'
    includeEmptyDirs false
    from(sourceSets.builtins.allSource) {
        into 'kotlin'
    }
    from(sourceSets.main.allSource) {
        into 'kotlin'
        exclude '**/*.java'
        exclude 'org.w3c/**'
        exclude 'js/**'
    }
    from(sourceSets.main.allSource) {
        include 'org.w3c/**'
    }
    from(sourceSets.experimental.allSource) {
        into 'kotlin'
    }
    from(sourceSets.coroutinesExperimental.allSource) {
        into 'kotlin'
    }
}

task distSourcesJar(type: Jar) {
    dependsOn(sourcesJar, configurations.commonSources)
    baseName = 'dist-kotlin-stdlib-js'
    version = null
    classifier = 'sources'
    duplicatesStrategy = DuplicatesStrategy.FAIL
    from zipTree(sourcesJar.outputs.files.singleFile)

    from(zipTree(configurations.commonSources.singleFile)) {
        it.includeEmptyDirs = false
        exclude 'META-INF/*'
        into 'common'
    }

    from(project(":kotlin-stdlib-common").sourceSets.coroutinesExperimental.allSource) {
        into 'kotlin'
    }
}



artifacts {
    runtime mergedJar
    archives mergedJar
    archives sourcesJar
}

javadocJar()

task distJs(type: Copy) {
    from(compileJs)
    into "$distDir/js"
}

dist {
    dependsOn distJs
    [mergedJar, distSourcesJar].forEach {
        rename("dist-", "")
        from(it)
    }
}

node {
    download = true
    version = '8.9.4' // The default 6.9.1 has buggy hyperbolic functions implementation
    nodeModulesDir = buildDir
}

// Otherwise Node ignores nodeModulesDir
task deleteLegacyNodeModules(type: Delete) {
    delete "$projectDir/node_modules"
}

task installMocha(type: NpmTask, dependsOn: [deleteLegacyNodeModules]) {
    args = ['install', 'mocha']
}

task installTeamcityReporter(type: NpmTask, dependsOn: [deleteLegacyNodeModules]) {
    args = ['install', 'mocha-teamcity-reporter']
}

task runMocha(type: NodeTask, dependsOn: [testClasses, installMocha, ':kotlin-test:kotlin-test-js:testClasses']) {
    script = file("${buildDir}/node_modules/mocha/bin/mocha")

    if (project.hasProperty("teamcity")) {
        dependsOn installTeamcityReporter
        args = ['--reporter', 'mocha-teamcity-reporter']
    }
    else {
        args = ['--reporter', 'min']
    }

    args += [jsTestOutputFile, kotlinTestJsTestOutputFile]

    execOverrides {
        it.ignoreExitValue = rootProject.ignoreTestFailures
        it.environment('NODE_PATH', [file(jsOutputFile).parent, file(kotlinTestJsOutputFile).parent].join(File.pathSeparator))
        it.workingDir = buildDir
    }
}

test.dependsOn runMocha