All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.gradle.tasks.configuration.Kotlin2JsCompileConfig.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.gradle.tasks.configuration

import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompilerOptions
import org.jetbrains.kotlin.gradle.incremental.IncrementalModuleInfoBuildService
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationInfo
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.tcs
import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
import org.jetbrains.kotlin.gradle.targets.js.internal.LibraryFilterCachingService
import org.jetbrains.kotlin.gradle.targets.js.ir.*
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import org.jetbrains.kotlin.gradle.utils.klibModuleName
import org.jetbrains.kotlin.gradle.utils.providerWithLazyConvention

internal typealias Kotlin2JsCompileConfig = BaseKotlin2JsCompileConfig

internal open class BaseKotlin2JsCompileConfig(
    compilation: KotlinCompilationInfo,
) : AbstractKotlinCompileConfig(compilation) {

    init {
        val libraryFilterCachingService = LibraryFilterCachingService.registerIfAbsent(project)

        val incrementalModuleInfoProvider = IncrementalModuleInfoBuildService.registerIfAbsent(
            project,
            objectFactory.providerWithLazyConvention { GradleCompilerRunner.buildModulesInfo(project.gradle) },
        )

        configureTask { task ->
            task.incremental = propertiesProvider.incrementalJs ?: true
            task.incrementalJsKlib = propertiesProvider.incrementalJsKlib ?: true

            configureAdditionalFreeCompilerArguments(task, compilation)

            task.libraryFilterCacheService.value(libraryFilterCachingService).disallowChanges()
            task.incrementalModuleInfoProvider.value(incrementalModuleInfoProvider).disallowChanges()


            task.projectVersion.value(project.provider { project.version.toString() }).disallowChanges()
            if (!compilation.isMain && project.kotlinPropertiesProvider.enableKlibKt64115Workaround) {
                task.mainCompilationModuleName.value(
                    compilation.tcs.compilation
                        .target
                        .compilations
                        .getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
                        .compileTaskProvider
                        .flatMap { (it.compilerOptions as KotlinJsCompilerOptions).moduleName }
                )
            }
        }
    }

    protected open fun configureAdditionalFreeCompilerArguments(
        task: TASK,
        compilation: KotlinCompilationInfo,
    ) {
        task.enhancedFreeCompilerArgs.value(
            task.compilerOptions.freeCompilerArgs.map { freeArgs ->
                freeArgs.toMutableList().apply {
                    commonJsAdditionalCompilerFlags(compilation)
                }
            }
        ).disallowChanges()
    }

    protected fun MutableList.commonJsAdditionalCompilerFlags(
        compilation: KotlinCompilationInfo,
    ) {
        // Configure FQ module name to avoid cyclic dependencies in klib manifests (see KT-36721).
        val baseName = if (compilation.isMain) {
            project.name
        } else {
            "${project.name}_${compilation.compilationName}"
        }
        if (none { it.startsWith(KLIB_MODULE_NAME) }) {
            add("$KLIB_MODULE_NAME=${project.klibModuleName(baseName)}")
        }

        if (compilation.platformType == KotlinPlatformType.wasm) {
            add(WASM_BACKEND)
            val wasmTargetType = ((compilation.origin as KotlinJsIrCompilation).target as KotlinJsIrTarget).wasmTargetType!!
            val targetValue = if (wasmTargetType == KotlinWasmTargetType.WASI) "wasm-wasi" else "wasm-js"
            add("$WASM_TARGET=$targetValue")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy