diff --git a/kotlin-native/shared/build.gradle.kts b/kotlin-native/shared/build.gradle.kts index 8f473174477..300c889ad0a 100644 --- a/kotlin-native/shared/build.gradle.kts +++ b/kotlin-native/shared/build.gradle.kts @@ -48,6 +48,10 @@ tasks.jar { archiveFileName.set("shared.jar") } +projectTest(jUnitMode = JUnitMode.JUnit5) { + useJUnitPlatform() +} + dependencies { kotlinCompilerClasspath("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion") @@ -55,4 +59,5 @@ dependencies { implementation(commonDependency("org.jetbrains.kotlin:kotlin-reflect")) { isTransitive = false } api("org.jetbrains.kotlin:kotlin-native-utils:$kotlinVersion") api("org.jetbrains.kotlin:kotlin-util-klib:$kotlinVersion") + testApiJUnit5() } diff --git a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Xcode.kt b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Xcode.kt index fd834c90279..81ad2ebc654 100644 --- a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Xcode.kt +++ b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/Xcode.kt @@ -64,7 +64,7 @@ interface Xcode { } } -private class CurrentXcode : Xcode { +internal class CurrentXcode : Xcode { override val toolchain by lazy { val ldPath = xcrun("-f", "ld") // = $toolchain/usr/bin/ld @@ -87,10 +87,19 @@ private class CurrentXcode : Xcode { override val watchosSdk: String by lazy { getSdkPath("watchos") } override val watchsimulatorSdk: String by lazy { getSdkPath("watchsimulator") } + internal val xcodebuildVersion: String + get() = xcrun("xcodebuild", "-version") + .removePrefix("Xcode ") + + internal val bundleVersion: String + get() = bash("""/usr/libexec/PlistBuddy "$(xcode-select -print-path)/../Info.plist" -c "Print :CFBundleShortVersionString"""") override val version by lazy { - xcrun("xcodebuild", "-version") - .removePrefix("Xcode ") + try { + bundleVersion + } catch (e: KonanExternalToolFailure) { + xcodebuildVersion + } } private fun xcrun(vararg args: String): String = try { @@ -107,5 +116,7 @@ private class CurrentXcode : Xcode { throw MissingXcodeException(message, e) } + private fun bash(command: String): String = Command("/bin/bash", "-c", command).getOutputLines().joinToString("\n") + private fun getSdkPath(sdk: String) = xcrun("--sdk", sdk, "--show-sdk-path") } diff --git a/kotlin-native/shared/src/test/kotlin/org/jetbrains/kotlin/konan/target/CurrentXcodeTest.kt b/kotlin-native/shared/src/test/kotlin/org/jetbrains/kotlin/konan/target/CurrentXcodeTest.kt new file mode 100644 index 00000000000..31008263633 --- /dev/null +++ b/kotlin-native/shared/src/test/kotlin/org/jetbrains/kotlin/konan/target/CurrentXcodeTest.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2022 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.konan.target + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Assumptions.assumeTrue + +internal class CurrentXcodeTest { + companion object { + @BeforeAll + @JvmStatic + fun assumeMacOS() { + assumeTrue(HostManager.hostIsMac) + } + } + @Test + fun `Should be able to access Xcode bundle version`() { + val version = CurrentXcode().bundleVersion + assertNotEquals("", version) + } + + @Test + fun `Should be able to access xcodebuild version`() { + val version = CurrentXcode().xcodebuildVersion + assertNotEquals("", version) + } + + @Test + fun `Xcode bundle version version should match xcodebuild version`() { + val xcode = CurrentXcode() + + val xcodebuildVersion = xcode.xcodebuildVersion + val bundleVersion = xcode.bundleVersion + val version = xcode.version + + assertEquals(xcodebuildVersion, bundleVersion) + assertEquals(xcodebuildVersion, version) + } +} \ No newline at end of file