From 6f9b84aa4af4d364882b4f35c7f82d42ef730af9 Mon Sep 17 00:00:00 2001 From: AJ Date: Mon, 2 Sep 2024 15:09:28 -0700 Subject: [PATCH] Use gradle instead of manual copy-pasting --- .github/workflows/build.yml | 8 ++- .../mordant-native-conventions.gradle.kts | 12 ++++ ...mordant-native-core-conventions.gradle.kts | 14 +++- .../TerminalInterface.native.macosArm64.kt | 72 ------------------- .../mordant/internal/MppInternal.macosX64.kt | 8 --- .../TerminalInterface.native.macosX64.kt | 72 ------------------- .../MppInternal.native.posixshared.kt} | 4 +- .../TerminalInterface.native.shared.kt} | 2 +- .../mordant/internal/MppInternal.tvos.kt | 8 --- .../mordant/internal/MppInternal.watchos.kt | 8 --- .../TerminalInterface.native.watchos.kt | 72 ------------------- 11 files changed, 34 insertions(+), 246 deletions(-) delete mode 100644 mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosArm64.kt delete mode 100644 mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosX64.kt delete mode 100644 mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosX64.kt rename mordant/src/{macosArm64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosArm64.kt => posixSharedMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.native.posixshared.kt} (76%) rename mordant/src/{tvosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.tvos.kt => posixSharedMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.shared.kt} (97%) delete mode 100644 mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.tvos.kt delete mode 100644 mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.watchos.kt delete mode 100644 mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.watchos.kt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c802cf4a..6549b93f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,7 +22,13 @@ jobs: - os: ubuntu-latest EXTRA_GRADLE_ARGS: apiCheck :test:proguard:r8jar - os: macos-latest - EXTRA_GRADLE_ARGS: :mordant:compileKotlinMetadata + EXTRA_GRADLE_ARGS: >- + :mordant:compileNativeMainKotlinMetadata + :mordant:compilePosixMainKotlinMetadata + :mordant:compileAppleMainKotlinMetadata + :mordant:compileMacosMainKotlinMetadata + :mordant:compileWatchosMainKotlinMetadata + :mordant:compileTvosMainKotlinMetadata runs-on: ${{matrix.os}} steps: - uses: actions/checkout@v4 diff --git a/buildSrc/src/main/kotlin/mordant-native-conventions.gradle.kts b/buildSrc/src/main/kotlin/mordant-native-conventions.gradle.kts index da143de6..932fb06d 100644 --- a/buildSrc/src/main/kotlin/mordant-native-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/mordant-native-conventions.gradle.kts @@ -8,6 +8,18 @@ kotlin { tvosX64() tvosArm64() tvosSimulatorArm64() + watchosArm32() watchosArm64() + watchosDeviceArm64() watchosX64() + watchosSimulatorArm64() + + sourceSets { + for (target in listOf( + "tvosX64", "tvosArm64", "tvosSimulatorArm64", + "watchosArm32", "watchosArm64", "watchosDeviceArm64", "watchosX64", "watchosSimulatorArm64", + )) { + sourceSets.getByName(target + "Main").kotlin.srcDirs("src/posixSharedMain/kotlin") + } + } } diff --git a/buildSrc/src/main/kotlin/mordant-native-core-conventions.gradle.kts b/buildSrc/src/main/kotlin/mordant-native-core-conventions.gradle.kts index 9e1e99ca..780a1107 100644 --- a/buildSrc/src/main/kotlin/mordant-native-core-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/mordant-native-core-conventions.gradle.kts @@ -15,12 +15,12 @@ kotlin { // Not all targets are supported by the markdown library // tvosX64() // tvosArm64() - tvosSimulatorArm64() +// tvosSimulatorArm64() // watchosArm32() // watchosArm64() // watchosDeviceArm64() // watchosX64() - watchosSimulatorArm64() +// watchosSimulatorArm64() applyDefaultHierarchyTemplate() @@ -33,5 +33,15 @@ kotlin { for (target in listOf(iosMain, tvosMain, watchosMain)) { target.get().dependsOn(appleNonDesktopMain) } + // Kotlin 2.0 changed the way MPP is compiled, so instead of copying shared sources to each + // target, it compiles intermediate sources separately. That means that code that previously + // compiled is broken due to errors like "declaration is using numbers with different bit + // widths". So we copy the shared sources to each target manually. + for (target in listOf( + "linuxX64", "linuxArm64", + "macosX64", "macosArm64", + )) { + sourceSets.getByName(target + "Main").kotlin.srcDirs("src/posixSharedMain/kotlin") + } } } diff --git a/mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosArm64.kt b/mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosArm64.kt deleted file mode 100644 index a1ed5922..00000000 --- a/mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosArm64.kt +++ /dev/null @@ -1,72 +0,0 @@ -package com.github.ajalt.mordant.terminal.terminalinterface - -import com.github.ajalt.mordant.rendering.Size -import kotlinx.cinterop.* -import platform.posix.* - -// XXX: The source code for this file is identical between linux and the various apple targets, but -// they have different bit widths for some fields, so the compileMetadata task fails if we don't use -// separate files. Hopefully some day there will be solution that doesn't require copy-pasting. - -internal class TerminalInterfaceNativeCopyPasted : TerminalInterfaceNativePosix() { - override val termiosConstants: TermiosConstants = TermiosConstants( - VTIME = VTIME, - VMIN = VMIN, - INPCK = INPCK.convert(), - ISTRIP = ISTRIP.convert(), - INLCR = INLCR.convert(), - IGNCR = IGNCR.convert(), - ICRNL = ICRNL.convert(), - IXON = IXON.convert(), - OPOST = OPOST.convert(), - CS8 = CS8.convert(), - ISIG = ISIG.convert(), - ICANON = ICANON.convert(), - ECHO = ECHO.convert(), - IEXTEN = IEXTEN.convert(), - ) - - override fun readIntoBuffer(c: ByteVar): Long { - return read(platform.posix.STDIN_FILENO, c.ptr, 1u).convert() - } - - override fun getTerminalSize(): Size? = memScoped { - val size = alloc() - if (ioctl(STDIN_FILENO, TIOCGWINSZ.convert(), size) < 0) { - null - } else { - Size(width = size.ws_col.toInt(), height = size.ws_row.toInt()) - } - } - - override fun getStdinTermios(): Termios = memScoped { - val termios = alloc() - if (tcgetattr(platform.posix.STDIN_FILENO, termios.ptr) != 0) { - throw RuntimeException("Error reading terminal attributes") - } - return Termios( - iflag = termios.c_iflag.convert(), - oflag = termios.c_oflag.convert(), - cflag = termios.c_cflag.convert(), - lflag = termios.c_lflag.convert(), - cc = ByteArray(NCCS) { termios.c_cc[it].convert() }, - ) - } - - override fun setStdinTermios(termios: Termios) = memScoped { - val nativeTermios = alloc() - // different platforms have different fields in termios, so we need to read the current - // struct before we set the fields we care about. - if (tcgetattr(platform.posix.STDIN_FILENO, nativeTermios.ptr) != 0) { - throw RuntimeException("Error reading terminal attributes") - } - nativeTermios.c_iflag = termios.iflag.convert() - nativeTermios.c_oflag = termios.oflag.convert() - nativeTermios.c_cflag = termios.cflag.convert() - nativeTermios.c_lflag = termios.lflag.convert() - repeat(NCCS) { nativeTermios.c_cc[it] = termios.cc[it].convert() } - if (tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr) != 0) { - throw RuntimeException("Error setting terminal attributes") - } - } -} diff --git a/mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosX64.kt b/mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosX64.kt deleted file mode 100644 index 256b039a..00000000 --- a/mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosX64.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.github.ajalt.mordant.internal - -import com.github.ajalt.mordant.terminal.TerminalInterface -import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceNativeCopyPasted - -internal actual fun getStandardTerminalInterface(): TerminalInterface { - return TerminalInterfaceNativeCopyPasted() -} diff --git a/mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosX64.kt b/mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosX64.kt deleted file mode 100644 index a1ed5922..00000000 --- a/mordant/src/macosX64Main/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.macosX64.kt +++ /dev/null @@ -1,72 +0,0 @@ -package com.github.ajalt.mordant.terminal.terminalinterface - -import com.github.ajalt.mordant.rendering.Size -import kotlinx.cinterop.* -import platform.posix.* - -// XXX: The source code for this file is identical between linux and the various apple targets, but -// they have different bit widths for some fields, so the compileMetadata task fails if we don't use -// separate files. Hopefully some day there will be solution that doesn't require copy-pasting. - -internal class TerminalInterfaceNativeCopyPasted : TerminalInterfaceNativePosix() { - override val termiosConstants: TermiosConstants = TermiosConstants( - VTIME = VTIME, - VMIN = VMIN, - INPCK = INPCK.convert(), - ISTRIP = ISTRIP.convert(), - INLCR = INLCR.convert(), - IGNCR = IGNCR.convert(), - ICRNL = ICRNL.convert(), - IXON = IXON.convert(), - OPOST = OPOST.convert(), - CS8 = CS8.convert(), - ISIG = ISIG.convert(), - ICANON = ICANON.convert(), - ECHO = ECHO.convert(), - IEXTEN = IEXTEN.convert(), - ) - - override fun readIntoBuffer(c: ByteVar): Long { - return read(platform.posix.STDIN_FILENO, c.ptr, 1u).convert() - } - - override fun getTerminalSize(): Size? = memScoped { - val size = alloc() - if (ioctl(STDIN_FILENO, TIOCGWINSZ.convert(), size) < 0) { - null - } else { - Size(width = size.ws_col.toInt(), height = size.ws_row.toInt()) - } - } - - override fun getStdinTermios(): Termios = memScoped { - val termios = alloc() - if (tcgetattr(platform.posix.STDIN_FILENO, termios.ptr) != 0) { - throw RuntimeException("Error reading terminal attributes") - } - return Termios( - iflag = termios.c_iflag.convert(), - oflag = termios.c_oflag.convert(), - cflag = termios.c_cflag.convert(), - lflag = termios.c_lflag.convert(), - cc = ByteArray(NCCS) { termios.c_cc[it].convert() }, - ) - } - - override fun setStdinTermios(termios: Termios) = memScoped { - val nativeTermios = alloc() - // different platforms have different fields in termios, so we need to read the current - // struct before we set the fields we care about. - if (tcgetattr(platform.posix.STDIN_FILENO, nativeTermios.ptr) != 0) { - throw RuntimeException("Error reading terminal attributes") - } - nativeTermios.c_iflag = termios.iflag.convert() - nativeTermios.c_oflag = termios.oflag.convert() - nativeTermios.c_cflag = termios.cflag.convert() - nativeTermios.c_lflag = termios.lflag.convert() - repeat(NCCS) { nativeTermios.c_cc[it] = termios.cc[it].convert() } - if (tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr) != 0) { - throw RuntimeException("Error setting terminal attributes") - } - } -} diff --git a/mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosArm64.kt b/mordant/src/posixSharedMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.native.posixshared.kt similarity index 76% rename from mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosArm64.kt rename to mordant/src/posixSharedMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.native.posixshared.kt index 256b039a..2c7f038b 100644 --- a/mordant/src/macosArm64Main/kotlin/com/github/ajalt/mordant/internal/MppInternal.macosArm64.kt +++ b/mordant/src/posixSharedMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.native.posixshared.kt @@ -1,8 +1,8 @@ package com.github.ajalt.mordant.internal import com.github.ajalt.mordant.terminal.TerminalInterface -import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceNativeCopyPasted +import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceNativeShared internal actual fun getStandardTerminalInterface(): TerminalInterface { - return TerminalInterfaceNativeCopyPasted() + return TerminalInterfaceNativeShared() } diff --git a/mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.tvos.kt b/mordant/src/posixSharedMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.shared.kt similarity index 97% rename from mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.tvos.kt rename to mordant/src/posixSharedMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.shared.kt index a1ed5922..9d9b0678 100644 --- a/mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.tvos.kt +++ b/mordant/src/posixSharedMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.shared.kt @@ -8,7 +8,7 @@ import platform.posix.* // they have different bit widths for some fields, so the compileMetadata task fails if we don't use // separate files. Hopefully some day there will be solution that doesn't require copy-pasting. -internal class TerminalInterfaceNativeCopyPasted : TerminalInterfaceNativePosix() { +internal class TerminalInterfaceNativeShared : TerminalInterfaceNativePosix() { override val termiosConstants: TermiosConstants = TermiosConstants( VTIME = VTIME, VMIN = VMIN, diff --git a/mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.tvos.kt b/mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.tvos.kt deleted file mode 100644 index 256b039a..00000000 --- a/mordant/src/tvosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.tvos.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.github.ajalt.mordant.internal - -import com.github.ajalt.mordant.terminal.TerminalInterface -import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceNativeCopyPasted - -internal actual fun getStandardTerminalInterface(): TerminalInterface { - return TerminalInterfaceNativeCopyPasted() -} diff --git a/mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.watchos.kt b/mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.watchos.kt deleted file mode 100644 index 256b039a..00000000 --- a/mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.watchos.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.github.ajalt.mordant.internal - -import com.github.ajalt.mordant.terminal.TerminalInterface -import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceNativeCopyPasted - -internal actual fun getStandardTerminalInterface(): TerminalInterface { - return TerminalInterfaceNativeCopyPasted() -} diff --git a/mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.watchos.kt b/mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.watchos.kt deleted file mode 100644 index a1ed5922..00000000 --- a/mordant/src/watchosMain/kotlin/com/github/ajalt/mordant/terminal/terminalinterface/TerminalInterface.native.watchos.kt +++ /dev/null @@ -1,72 +0,0 @@ -package com.github.ajalt.mordant.terminal.terminalinterface - -import com.github.ajalt.mordant.rendering.Size -import kotlinx.cinterop.* -import platform.posix.* - -// XXX: The source code for this file is identical between linux and the various apple targets, but -// they have different bit widths for some fields, so the compileMetadata task fails if we don't use -// separate files. Hopefully some day there will be solution that doesn't require copy-pasting. - -internal class TerminalInterfaceNativeCopyPasted : TerminalInterfaceNativePosix() { - override val termiosConstants: TermiosConstants = TermiosConstants( - VTIME = VTIME, - VMIN = VMIN, - INPCK = INPCK.convert(), - ISTRIP = ISTRIP.convert(), - INLCR = INLCR.convert(), - IGNCR = IGNCR.convert(), - ICRNL = ICRNL.convert(), - IXON = IXON.convert(), - OPOST = OPOST.convert(), - CS8 = CS8.convert(), - ISIG = ISIG.convert(), - ICANON = ICANON.convert(), - ECHO = ECHO.convert(), - IEXTEN = IEXTEN.convert(), - ) - - override fun readIntoBuffer(c: ByteVar): Long { - return read(platform.posix.STDIN_FILENO, c.ptr, 1u).convert() - } - - override fun getTerminalSize(): Size? = memScoped { - val size = alloc() - if (ioctl(STDIN_FILENO, TIOCGWINSZ.convert(), size) < 0) { - null - } else { - Size(width = size.ws_col.toInt(), height = size.ws_row.toInt()) - } - } - - override fun getStdinTermios(): Termios = memScoped { - val termios = alloc() - if (tcgetattr(platform.posix.STDIN_FILENO, termios.ptr) != 0) { - throw RuntimeException("Error reading terminal attributes") - } - return Termios( - iflag = termios.c_iflag.convert(), - oflag = termios.c_oflag.convert(), - cflag = termios.c_cflag.convert(), - lflag = termios.c_lflag.convert(), - cc = ByteArray(NCCS) { termios.c_cc[it].convert() }, - ) - } - - override fun setStdinTermios(termios: Termios) = memScoped { - val nativeTermios = alloc() - // different platforms have different fields in termios, so we need to read the current - // struct before we set the fields we care about. - if (tcgetattr(platform.posix.STDIN_FILENO, nativeTermios.ptr) != 0) { - throw RuntimeException("Error reading terminal attributes") - } - nativeTermios.c_iflag = termios.iflag.convert() - nativeTermios.c_oflag = termios.oflag.convert() - nativeTermios.c_cflag = termios.cflag.convert() - nativeTermios.c_lflag = termios.lflag.convert() - repeat(NCCS) { nativeTermios.c_cc[it] = termios.cc[it].convert() } - if (tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr) != 0) { - throw RuntimeException("Error setting terminal attributes") - } - } -}