Skip to content

Commit

Permalink
Preserve profiling information in skiko.wasm with an extra build flag (
Browse files Browse the repository at this point in the history
…#985)

Adding a new flag `-Pskiko.wasm.withProfiling=true` which adds
`--profiling` flag to emcc, so it preserves the function names - makes
it easier to use the browser profiler.

By default, it's disabled.
  • Loading branch information
eymar authored Sep 9, 2024
1 parent c479fc9 commit e2bf12b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions skiko/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ project.tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile>().config

tasks.findByName("publishSkikoWasmRuntimePublicationToComposeRepoRepository")
?.dependsOn("publishWasmJsPublicationToComposeRepoRepository")
tasks.findByName("publishSkikoWasmRuntimePublicationToMavenLocal")
?.dependsOn("publishWasmJsPublicationToMavenLocal")


tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile>().configureEach {
Expand Down
6 changes: 5 additions & 1 deletion skiko/buildSrc/src/main/kotlin/properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class SkikoProperties(private val myProject: Project) {
val deployVersion: String
get() {
val main = if (isRelease) planeDeployVersion else "$planeDeployVersion-SNAPSHOT"
val metadata = if (buildType == SkiaBuildType.DEBUG) "+debug" else ""
var metadata = if (buildType == SkiaBuildType.DEBUG) "+debug" else ""
metadata += if (isWasmBuildWithProfiling) "+profiling" else ""
return main + metadata
}

Expand All @@ -136,6 +137,9 @@ class SkikoProperties(private val myProject: Project) {
val buildType: SkiaBuildType
get() = if (myProject.findProperty("skiko.debug") == "true") SkiaBuildType.DEBUG else SkiaBuildType.RELEASE

val isWasmBuildWithProfiling: Boolean
get() = myProject.findProperty("skiko.wasm.withProfiling") == "true"

val targetArch: Arch
get() = myProject.findProperty("skiko.arch")?.toString()?.let(Arch::byName) ?: hostArch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ fun skiaPreprocessorFlags(os: OS, buildType: SkiaBuildType): Array<String> {
"-DSK_BUILD_FOR_LINUX",
"-D_GLIBCXX_USE_CXX11_ABI=0"
)
OS.Wasm -> listOf(
"-DSKIKO_WASM"
)
OS.Wasm -> mutableListOf<String>().apply {
add("-DSKIKO_WASM")
// add("-sSUPPORT_LONGJMP=wasm") // TODO(o.karpovich): enable when skia is built with this flag (CMP-6628)
}
OS.Android -> listOf(
"-DSK_BUILD_FOR_ANDROID"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
includeHeadersNonRecursive(skiaHeadersDirs(skiaWasmDir.get()))

flags.set(
listOf(
*skiaPreprocessorFlags(OS.Wasm, buildType),
*buildType.clangFlags,
"-fno-rtti",
"-fno-exceptions",
)
mutableListOf<String?>().apply {
addAll(skiaPreprocessorFlags(OS.Wasm, buildType))
addAll(buildType.clangFlags)
add("-fno-rtti")
add("-fno-exceptions")
if (skiko.isWasmBuildWithProfiling) add("--profiling")
}
)
}

Expand Down Expand Up @@ -103,7 +104,7 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
)

@OptIn(kotlin.ExperimentalStdlibApi::class)
flags.set(buildList {
flags.set(mutableListOf<String?>().apply {
addAll(
listOf(
"-l", "GL",
Expand All @@ -117,6 +118,7 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
"-O2"
)
)
// addAll(listOf("-s", "SUPPORT_LONGJMP=wasm")) // TODO(o.karpovich): enable when skia is built with this flag (CMP-6628)
if (outputES6) {
addAll(
listOf(
Expand All @@ -128,6 +130,8 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
)
)
}

if (skiko.isWasmBuildWithProfiling) add("--profiling")
})

doLast {
Expand All @@ -136,11 +140,16 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
val jsFiles = outDir.asFile.get().walk()
.filter { it.isFile && (it.name.endsWith(".js") || it.name.endsWith(".mjs")) }

val isEnvironmentNodeCheckRegex = Regex(
// spaces are different in release and debug builds
"""if\s*\(ENVIRONMENT_IS_NODE\)\s*\{"""
)

for (jsFile in jsFiles) {
val originalContent = jsFile.readText()
val newContent = originalContent.replace("_org_jetbrains", "org_jetbrains")
.replace("skikomjs.wasm", "skiko.wasm")
.replace("if(ENVIRONMENT_IS_NODE){", "if (false) {") // to make webpack erase this part
.replace(isEnvironmentNodeCheckRegex, "if (false) {") // to make webpack erase this part
jsFile.writeText(newContent)

if (outputES6) {
Expand Down

0 comments on commit e2bf12b

Please sign in to comment.