diff --git a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt index 4b629d2d67b..80ac7d7889c 100644 --- a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt +++ b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt @@ -58,6 +58,34 @@ interface Compilation { val language: Language } +fun defaultCompilerArgs(language: Language): List = + listOf( + // We compile with -O2 because Clang may insert inline asm in bitcode at -O0. + // It is undesirable in case of watchos_arm64 since we target armv7k + // for this target instead of arm64_32 because it is not supported in LLVM 8. + // + // Note that PCH and the *.c file should be compiled with the same optimization level. + "-O2", + // Allow throwing exceptions through generated stubs. + "-fexceptions", + ) + when (language) { + Language.C -> emptyList() + Language.CPP -> emptyList() + Language.OBJECTIVE_C -> listOf( + // "Objective-C" within interop means "Objective-C with ARC": + "-fobjc-arc", + // Using this flag here has two effects: + // 1. The headers are parsed with ARC enabled, thus the API is visible correctly. + // 2. The generated Objective-C stubs are compiled with ARC enabled, so reference counting + // calls are inserted automatically. + + // Workaround for https://youtrack.jetbrains.com/issue/KT-48807. + // TODO(LLVM): Remove after update to a version with + // https://github.com/apple/llvm-project/commit/2de0a18a8949f0235fb3a08dcc55ff3aa7d969e7. + "-DNS_FORMAT_ARGUMENT(A)=" + ) + } + data class CompilationWithPCH( override val compilerArgs: List, override val language: Language diff --git a/kotlin-native/Interop/Indexer/src/test/kotlin/org/jetbrains/kotlin/native/interop/indexer/WorkaroundTests.kt b/kotlin-native/Interop/Indexer/src/test/kotlin/org/jetbrains/kotlin/native/interop/indexer/WorkaroundTests.kt index 00ab8985d0e..c08084bfd48 100644 --- a/kotlin-native/Interop/Indexer/src/test/kotlin/org/jetbrains/kotlin/native/interop/indexer/WorkaroundTests.kt +++ b/kotlin-native/Interop/Indexer/src/test/kotlin/org/jetbrains/kotlin/native/interop/indexer/WorkaroundTests.kt @@ -26,11 +26,12 @@ class WorkaroundTests : IndexerTests() { NS_FORMAT_ARGUMENT(1) int f(const char* c); """.trimIndent() + val language = Language.OBJECTIVE_C val compilation = CompilationImpl( includes = emptyList(), additionalPreambleLines = code.split("\n"), - compilerArgs = listOf(), - language = Language.C + compilerArgs = defaultCompilerArgs(language), + language = language ) withIndex { index -> val translationUnit = compilation.parse( diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt index a9ce7b525af..229864a10b4 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt @@ -477,34 +477,14 @@ internal fun buildNativeLibrary( val compilerOpts: List = mutableListOf().apply { addAll(def.config.compilerOpts) addAll(tool.getDefaultCompilerOptsForLanguage(language)) - // We compile with -O2 because Clang may insert inline asm in bitcode at -O0. - // It is undesirable in case of watchos_arm64 since we target armv7k - // for this target instead of arm64_32 because it is not supported in LLVM 8. - // - // Note that PCH and the *.c file should be compiled with the same optimization level. - add("-O2") - // Allow throwing exceptions through generated stubs. - add("-fexceptions") addAll(additionalCompilerOpts) addAll(getCompilerFlagsForVfsOverlay(arguments.headerFilterPrefix.toTypedArray(), def)) - addAll(when (language) { - Language.C -> emptyList() - Language.CPP -> emptyList() - Language.OBJECTIVE_C -> { - // "Objective-C" within interop means "Objective-C with ARC": - listOf("-fobjc-arc") - // Using this flag here has two effects: - // 1. The headers are parsed with ARC enabled, thus the API is visible correctly. - // 2. The generated Objective-C stubs are compiled with ARC enabled, so reference counting - // calls are inserted automatically. - } - }) } val compilation = CompilationImpl( includes = headerFiles, additionalPreambleLines = def.defHeaderLines, - compilerArgs = compilerOpts + tool.platformCompilerOpts, + compilerArgs = defaultCompilerArgs(language) + compilerOpts + tool.platformCompilerOpts, language = language )