Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor KMP iOS build #796

Merged
merged 10 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app-ios-shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io.github.droidkaigi.confsched.primitive.activeArch
import org.jetbrains.compose.ComposePlugin.CommonComponentsDependencies
import org.jetbrains.kotlin.gradle.plugin.mpp.BitcodeEmbeddingMode
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
Expand Down Expand Up @@ -27,10 +28,11 @@ kotlin {
binaryOption("bundleVersion", version.toString())
binaryOption("bundleShortVersionString", version.toString())

val includeToXCF = if (project.properties["app.ios.shared.debug"] == "true") {
(this.target.name == "iosSimulatorArm64" && this.debuggable) || (this.target.name == "iosArm64" && this.debuggable)
} else {
true
val includeToXCF = when (project.activeArch) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the logic of activeArch

val Project.activeArch
    get() = Arch.findByArch(
        rootProject.layout.projectDirectory.file("local.properties").asFile.takeIf { it.exists() }
            ?.let {
                Properties().apply {
                    load(it.reader(Charsets.UTF_8))
                }.getProperty("arch")
            } ?: System.getenv("arch") ?: properties["app.ios.shared.arch"] as? String
    )

io.github.droidkaigi.confsched.primitive.Arch.ARM -> this.target.name.contains("iosArm64") || this.target.name.contains("iosSimulatorArm64")
io.github.droidkaigi.confsched.primitive.Arch.ARM_SIMULATOR -> this.target.name.contains("iosSimulatorArm64")
io.github.droidkaigi.confsched.primitive.Arch.X86 -> this.target.name.contains("iosX64")
io.github.droidkaigi.confsched.primitive.Arch.ALL -> true
}
if (includeToXCF) {
xcf.add(this)
Expand Down
10 changes: 7 additions & 3 deletions app-ios/App/App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
830BFA7C2C74EBF40017A600 /* compose-resources in Resources */ = {isa = PBXBuildFile; fileRef = 830BFA7B2C74EBF40017A600 /* compose-resources */; };
83CD07FE2C7B1F5400CDEFDB /* compose-resources in Resources */ = {isa = PBXBuildFile; fileRef = 83CD07FD2C7B1F5400CDEFDB /* compose-resources */; };
8C31F46B2BF6909A003F1BBA /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8C31F46A2BF6909A003F1BBA /* GoogleService-Info.plist */; };
8C7074772C7791BD00FD4F39 /* ci_post_clone.sh in Resources */ = {isa = PBXBuildFile; fileRef = 8C7074762C7791BD00FD4F39 /* ci_post_clone.sh */; };
8C7074792C7791DF00FD4F39 /* ci_pre_xcodebuild.sh in Resources */ = {isa = PBXBuildFile; fileRef = 8C7074782C7791DF00FD4F39 /* ci_pre_xcodebuild.sh */; };
Expand All @@ -19,6 +19,8 @@

/* Begin PBXFileReference section */
830BFA7B2C74EBF40017A600 /* compose-resources */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "compose-resources"; path = "../../app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/iosSimulatorArm64/compose-resources"; sourceTree = "<group>"; };
83CD07FB2C7B1A5E00CDEFDB /* compose-resources */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "compose-resources"; path = "../../app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/iosX64/compose-resources"; sourceTree = "<group>"; };
83CD07FD2C7B1F5400CDEFDB /* compose-resources */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "compose-resources"; path = "../../app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/compose-resources"; sourceTree = "<group>"; };
8C31F46A2BF6909A003F1BBA /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
8C7074762C7791BD00FD4F39 /* ci_post_clone.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = ci_post_clone.sh; sourceTree = "<group>"; };
8C7074782C7791DF00FD4F39 /* ci_pre_xcodebuild.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = ci_pre_xcodebuild.sh; sourceTree = "<group>"; };
Expand Down Expand Up @@ -57,6 +59,8 @@
8C772B0C2BCBCBCA00F2BADC = {
isa = PBXGroup;
children = (
83CD07FB2C7B1A5E00CDEFDB /* compose-resources */,
83CD07FD2C7B1F5400CDEFDB /* compose-resources */,
8C7074752C77919300FD4F39 /* ci_scripts */,
830BFA7B2C74EBF40017A600 /* compose-resources */,
C412816C2C149FB500B458D1 /* DroidKaigi2024App-Info.plist */,
Expand Down Expand Up @@ -171,7 +175,7 @@
8C7DACB72BCBCCB0002C298A /* Preview Assets.xcassets in Resources */,
8C31F46B2BF6909A003F1BBA /* GoogleService-Info.plist in Resources */,
8C7074772C7791BD00FD4F39 /* ci_post_clone.sh in Resources */,
830BFA7C2C74EBF40017A600 /* compose-resources in Resources */,
83CD07FE2C7B1F5400CDEFDB /* compose-resources in Resources */,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current resource is like this
image

);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -194,7 +198,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/zsh;
shellScript = "cd ${SRCROOT}/../..\n\n./gradlew assembleSharedXCFramework --no-configuration-cache\n./gradlew iosSimulatorArm64AggregateResources --no-configuration-cache\n./gradlew iosArm64AggregateResources --no-configuration-cache\n\nSIMULATOR_RESOURCE_DIR=\"./app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/iosSimulatorArm64\"\nDEVICE_RESOURCE_DIR=\"./app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/iosArm64\"\n\nmkdir -p ${SIMULATOR_RESOURCE_DIR}/composeResources\nmkdir -p ${DEVICE_RESOURCE_DIR}/composeResources\n\nmkdir -p ${SIMULATOR_RESOURCE_DIR}/compose-resources\nmkdir -p ${DEVICE_RESOURCE_DIR}/compose-resources\n\ncp -R ${SIMULATOR_RESOURCE_DIR}/composeResources ${SIMULATOR_RESOURCE_DIR}/compose-resources\ncp -R ${DEVICE_RESOURCE_DIR}/composeResources ${DEVICE_RESOURCE_DIR}/compose-resources\n";
shellScript = "cd ${SRCROOT}/../..\n\n./build_xcframework_with_xcode_environment_variable.sh\n./gradlew iosSimulatorArm64AggregateResources --no-configuration-cache\n./gradlew iosX64AggregateResources --no-configuration-cache\n\nARM_RESOURCE_DIR=\"./app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/iosSimulatorArm64\"\nX64_RESOURCE_DIR=\"./app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/iosX64\"\nRESOURCE_DIR=\"./app-ios-shared/build/kotlin-multiplatform-resources/aggregated-resources/compose-resources\"\n\nmkdir -p ${ARM_RESOURCE_DIR}/composeResources\nmkdir -p ${X64_RESOURCE_DIR}/composeResources\n\nmkdir -p ${RESOURCE_DIR}\n\ncp -R ${ARM_RESOURCE_DIR}/composeResources ${RESOURCE_DIR}\ncp -R ${X64_RESOURCE_DIR}/composeResources ${RESOURCE_DIR}\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down
9 changes: 7 additions & 2 deletions app-ios/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ build-kmp-module:
cd .. && \
./gradlew app-ios-shared:assembleSharedXCFramework --no-configuration-cache
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now build-kmp-module build all architectures.


.PHONY: build-kmp-module-debug
build-kmp-module-debug:
cd .. && \
./gradlew app-ios-shared:assembleSharedXCFramework -Papp.ios.shared.arch=arm64Simulator --no-configuration-cache

.PHONY: build-app-debug
build-app-debug: build-kmp-module
build-app-debug: build-kmp-module-debug
set -o pipefail && \
xcodebuild -project $(PROJECT_FILE) \
-scheme $(SCHEME_NAME_APP) \
Expand All @@ -39,7 +44,7 @@ test-app-debug:
.PHONY: bootstrap
bootstrap:
make setup && \
make build-kmp-module && \
make build-kmp-module-debug && \
make open

# Gradle Utility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.droidkaigi.confsched.primitive

import io.github.droidkaigi.confsched.primitive.Arch.ALL
import io.github.droidkaigi.confsched.primitive.Arch.ARM
import io.github.droidkaigi.confsched.primitive.Arch.ARM_SIMULATOR
import io.github.droidkaigi.confsched.primitive.Arch.X86
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -26,11 +27,21 @@ class KmpIosPlugin : Plugin<Project> {
"-linker-option", "-framework", "-linker-option", "CoreGraphics",
)
when (activeArch) {
ARM -> iosSimulatorArm64 {
ARM -> {
iosSimulatorArm64 {
binaries.forEach {
it.freeCompilerArgs += simulatorLinkerOptions
}
}
iosArm64()
}

ARM_SIMULATOR -> iosSimulatorArm64 {
binaries.forEach {
it.freeCompilerArgs += simulatorLinkerOptions
}
}

X86 -> iosX64()
ALL -> {
iosArm64()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.droidkaigi.confsched.primitive
import com.google.devtools.ksp.gradle.KspTaskNative
import io.github.droidkaigi.confsched.primitive.Arch.ALL
import io.github.droidkaigi.confsched.primitive.Arch.ARM
import io.github.droidkaigi.confsched.primitive.Arch.ARM_SIMULATOR
import io.github.droidkaigi.confsched.primitive.Arch.X86
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -35,7 +36,12 @@ class KmpKtorfitPlugin : Plugin<Project> {

dependencies {
val iosConfigs = when (activeArch) {
ARM -> listOf("IosSimulatorArm64")
ARM -> listOf(
"IosArm64",
"IosSimulatorArm64"
)

ARM_SIMULATOR -> listOf("IosSimulatorArm64")
X86 -> listOf("IosX64")
ALL -> listOf(
"IosArm64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.util.Properties

enum class Arch(val arch: String?) {
ARM("arm64"),
ARM_SIMULATOR("arm64Simulator"),
X86("x86_64"),
ALL(null),
;
Expand All @@ -23,5 +24,5 @@ val Project.activeArch
Properties().apply {
load(it.reader(Charsets.UTF_8))
}.getProperty("arch")
} ?: System.getenv("arch")
} ?: System.getenv("arch") ?: properties["app.ios.shared.arch"] as? String
)
33 changes: 33 additions & 0 deletions build_xcframework_with_xcode_environment_variable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Check if SDK_NAME and ARCHS environment variables are set
if [ -z "$SDK_NAME" ] || [ -z "$ARCHS" ]; then
echo "Error: SDK_NAME or ARCHS environment variable is not set"
exit 1
fi

# Initialize the target architecture
target_arch=""

# Determine the target architecture based on SDK_NAME and ARCHS
if [[ "$SDK_NAME" == *"simulator"* ]]; then
if [[ "$ARCHS" == *"arm64"* ]]; then
target_arch="arm64Simulator"
elif [[ "$ARCHS" == *"x86_64"* ]]; then
target_arch="x86_64"
fi
elif [[ "$SDK_NAME" == *"iphoneos"* ]] && [[ "$ARCHS" == *"arm64"* ]]; then
target_arch="arm64"
fi

# Check if a valid target architecture was determined
if [ -z "$target_arch" ]; then
echo "Error: Unable to determine target architecture from SDK_NAME=$SDK_NAME and ARCHS=$ARCHS"
exit 1
fi

# Execute Gradle command with the determined architecture
echo "Building for architecture: $target_arch"
./gradlew assembleSharedXCFramework --no-configuration-cache -Papp.ios.shared.arch="$target_arch"

echo "Build completed for $target_arch"
3 changes: 0 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# operation
app.ios.shared.debug=true

# gradle
org.gradle.jvmargs=-Xmx6048m -Dfile.encoding=UTF-8
org.gradle.configureondemand=true
Expand Down
Loading