kotlin multiplatformでmacosArm64向けビルドしたい

環境: M1 MacBookAir
下記記事を参考にしようとしたらkotlin何もわからないせいで前段階でつまづいているので、一旦macosArm64で動くバイナリを作れる状態にするのが目標。
まずIntelliJ IDEAで新しいプロジェクトを作ってみる。
サンプルコードを実行すると下記エラー
Inconsistent JVM-target compatibility detected for tasks 'compileJava' (23) and 'compileKotlin' (22)
build.gradle.ktsのkotlin("jvm") version "2.0.21"
をkotlin("jvm") version "2.1.0"
に修正したら動いた。
参考: https://discuss.gradle.org/t/inconsistent-jvm-target-compatibility-detected-for-tasks-compilejava-23-and-compilekotlin-20/49971/3

build.gradle.ktsを変更してみる。
plugins {
kotlin("multiplatform") version "2.1.0"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
macosArm64("macosArm64") {
binaries {
executable {
entryPoint = "org.example.main"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib"))
implementation("com.github.ajalt.clikt:clikt:5.0.2")
}
}
}
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
macosArm64
でUnresolved reference: macosArm64
というエラーがIDEに出ているが修正方法わからず。

一度./gradlew build
実行してみる。
build.gradle.kts:30:7: Unresolved reference: test
build.gradle.kts:31:5: Unresolved reference: useJUnitPlatform
…一度dependenciesとtasks.testをコメントアウトし、再度実行。
org.jetbrains.kotlin.konan.MissingXcodeException: An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
Failed command: /usr/bin/xcrun xcodebuild -version
Try running this command in Terminal and fix the errors by making Xcode (and its command line tools) configuration correct.
開発マシン(macbookAir)にxcodeがインストールされていなかったためAppStoreからダウンロードする。

e: Failed to build cache for /Users/user/.gradle/caches/modules-2/files-2.1/com.github.ajalt.clikt/clikt-core-macosarm64/5.0.2/7e622b4851d9408b15eb4326a7db537acafd626a/clikt-macosArm64Main.
As a workaround, please try to disable compiler caches (kotlin.native.cacheKind=none)
Also, consider filing an issue with full Gradle log here: https://kotl.in/issue
An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
Failed command: /usr/bin/xcrun -f ld
Try running this command in Terminal and fix the errors by making Xcode (and its command line tools) configuration correct.
org.jetbrains.kotlin.konan.MissingXcodeException: An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
Failed command: /usr/bin/xcrun -f ld
Try running this command in Terminal and fix the errors by making Xcode (and its command line tools) configuration correct.
xcodeのパスを変更する。
$ xcode-select -switch /Applications/Xcode.app
xcodebuildのライセンスに同意する。下記を実行してagreeを入力。
$ sudo xcodebuild -license
再度./gradlew build
実行したところビルド成功。
生成されたバイナリを下記コマンドで実行することができた。
$ ./build/bin/macosArm64/releaseExecutable/cli.kexe

バイナリは実行できたがIntelliJ IDEAでデバッグモードを起動することができないので修正する。
Cannot locate tasks that match ':classes' as task 'classes' not found in root project 'cli'.
build.gradle.ktsにjvm()
を追加。
...
kotlin {
jvm() // ←追加
macosArm64("macosArm64") {
...
}
sourceSets {
...
}
}
サイドバーのgradleプラグインからSyncアイコン🔄をクリック。
Syncが完了したら、IntelliJ IDEAを閉じて再度プロジェクトを開く。
main関数のデバッグができるようになった。

テストを実行できるようにする。
まずサンプルとして./src/commonMein/kotlin/Hello.ktと./src/commonTest/kotlin/TestHello.ktを作成する。
package org.example
class Hello(name: String) {
val value = "Hello, $name!"
}
import org.example.Hello
import kotlin.test.Test
import kotlin.test.assertEquals
class HelloTest {
@Test
fun testHello() {
val hello = Hello("Kotlin")
assertEquals("Hello, Kotlin!", hello.value)
}
}
build.gradle.ktにテスト用の設定を追加する。
...
kotlin {
...
sourceSets {
...
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.4")
}
}
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
テスト用クラスをIDEで実行 or $ ./gradlew jvmTest
で実行できればOK

なお$ ./gradlew macosArm64Test
を実行するとCould not resolve org.junit.jupiter:junit-jupiter-api:5.11.4.
というエラー。
JUnitがnativeプラットフォームに対応していないためエラーになる模様。
マルチプラットフォーム対応のテストフレームワークを使うのも手かも。