summaryrefslogtreecommitdiff
path: root/gradle/native-targets.gradle
blob: 909fcec1bc553089a4a0437ce766dd7785e7d4ec (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
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

/**
 * Specifies what subset of Native targets to build
 *
 * ALL — all possible for compilation
 * HOST — host-specific ones, without cross compilation (e.g. do not build linuxX64 on macOS host)
 * SINGLE — only for current OS (to import into IDEA / quickly run tests)
 * DISABLED — disable all Native targets (useful with Kotlin compiler built from sources)
 *
 * For HOST mode, all targets are still listed in .module file, so HOST mode is useful for release process.
 */
enum NativeState { ALL, HOST, SINGLE, DISABLED }

def getNativeState(String description) {
    if (description == null) return NativeState.SINGLE
    switch(description.toLowerCase()) {
        case 'all':
        case 'true':
            return NativeState.ALL
        case 'host':
            return NativeState.HOST
        case 'disabled':
            return NativeState.DISABLED
        // 'single', 'false', etc
        default:
            return NativeState.SINGLE
    }
}

project.ext.ideaActive = System.getProperty('idea.active') == 'true'
project.ext.nativeState = getNativeState(property('native.deploy'))
project.ext.singleTargetMode = project.ext.ideaActive || (project.ext.nativeState == NativeState.SINGLE)

project.ext.nativeMainSets = []
project.ext.nativeTestSets = []

/**
 * Disables compilation but leaves the target in .module file
 */
def disableCompilation(targets) {
    configure(targets) {
        compilations.all {
            cinterops.all { project.tasks[interopProcessingTaskName].enabled = false }
            compileKotlinTask.enabled = false
        }
        binaries.all { linkTask.enabled = false }

        mavenPublication { publicationToDisable ->
            tasks.withType(AbstractPublishToMaven).all {
                onlyIf { publication != publicationToDisable }
            }
            tasks.withType(GenerateModuleMetadata).all {
                onlyIf { publication.get() != publicationToDisable }
            }
        }
    }
}

def getHostName() {
    def target = System.getProperty("os.name")
    if (target == 'Linux') return 'linux'
    if (target.startsWith('Windows')) return 'windows'
    if (target.startsWith('Mac')) return 'macos'
    return 'unknown'
}

kotlin {
    targets {
        def manager = project.ext.hostManager
        def linuxEnabled = manager.isEnabled(presets.linuxX64.konanTarget)
        def macosEnabled = manager.isEnabled(presets.macosX64.konanTarget)
        def winEnabled = manager.isEnabled(presets.mingwX64.konanTarget)

        def ideaPreset = presets.linuxX64
        if (macosEnabled) ideaPreset = presets.macosX64
        if (winEnabled) ideaPreset = presets.mingwX64
        project.ext.ideaPreset = ideaPreset
    }

    targets.metaClass.addTarget = { preset ->
        def target = delegate.fromPreset(preset, preset.name)
        project.ext.nativeMainSets.add(target.compilations['main'].kotlinSourceSets.first())
        project.ext.nativeTestSets.add(target.compilations['test'].kotlinSourceSets.first())
    }

    targets {
        if (project.ext.nativeState == NativeState.DISABLED) return
        if (project.ext.singleTargetMode) {
            fromPreset(project.ext.ideaPreset, 'native')
        } else {
            // Linux
            addTarget(presets.linuxX64)
            addTarget(presets.linuxArm32Hfp)
            addTarget(presets.linuxArm64)

            // Mac & iOS
            addTarget(presets.macosX64)

            addTarget(presets.iosArm64)
            addTarget(presets.iosArm32)
            addTarget(presets.iosX64)

            addTarget(presets.watchosX86)
            addTarget(presets.watchosX64)
            addTarget(presets.watchosArm32)
            addTarget(presets.watchosArm64)

            addTarget(presets.tvosArm64)
            addTarget(presets.tvosX64)

            // Apple Silicon
            addTarget(presets.iosSimulatorArm64)
            addTarget(presets.watchosSimulatorArm64)
            addTarget(presets.tvosSimulatorArm64)
            addTarget(presets.macosArm64)

            // Windows
            addTarget(presets.mingwX64)
            addTarget(presets.mingwX86)
        }

        if (project.ext.nativeState == NativeState.HOST) {
            // linux targets that can cross-compile on all three hosts
            def linuxCrossCompileTargets = [linuxX64, linuxArm32Hfp, linuxArm64]
            if (getHostName() != "linux") {
                disableCompilation(linuxCrossCompileTargets)
            }
        }
    }


    sourceSets {
        nativeMain { dependsOn commonMain }
        // Empty source set is required in order to have native tests task
        nativeTest { dependsOn commonTest }

        if (!project.ext.singleTargetMode) {
            configure(project.ext.nativeMainSets) {
                dependsOn nativeMain
            }

            configure(project.ext.nativeTestSets) {
                dependsOn nativeTest
            }
        }
    }
}