diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt index b35f454b51b..79f40da6140 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt @@ -19,7 +19,7 @@ private class EnumDefImpl(spelling: String, type: PrimitiveType) : EnumDef(spell override val constants = mutableListOf() } -internal class NativeIndexImpl(val language: Language) : NativeIndex() { +internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() { private data class DeclarationID(val usr: String) @@ -275,6 +275,11 @@ internal class NativeIndexImpl(val language: Language) : NativeIndex() { val entityInfo = info.entityInfo.pointed!! val entityName = entityInfo.name.value?.toKString() val kind = entityInfo.kind.value + + if (!this.library.includesDeclaration(cursor)) { + return + } + when (kind) { CXIdxEntity_Struct, CXIdxEntity_Union -> { getStructDeclAt(cursor) @@ -291,7 +296,7 @@ internal class NativeIndexImpl(val language: Language) : NativeIndex() { Parameter(argName, type) } - val binaryName = when (language) { + val binaryName = when (library.language) { Language.C -> clang_Cursor_getMangling(cursor).convertAndDispose() } @@ -312,7 +317,7 @@ internal class NativeIndexImpl(val language: Language) : NativeIndex() { } fun buildNativeIndexImpl(library: NativeLibrary): NativeIndex { - val result = NativeIndexImpl(library.language) + val result = NativeIndexImpl(library) indexDeclarations(library, result) findMacroConstants(library, result) return result diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt index 9ebc889aad9..7eaf2084828 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt @@ -201,7 +201,10 @@ private fun collectMacroConstantsNames(library: NativeLibrary): List { val translationUnit = library.parse(index, options).ensureNoCompileErrors() try { visitChildren(translationUnit) { cursor, parent -> - if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition && canMacroBeConstant(cursor)) { + if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition && + library.includesDeclaration(cursor) && + canMacroBeConstant(cursor)) + { val spelling = getCursorSpelling(cursor) result.add(spelling) } diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt index ac043c26072..a95916cb4d9 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt @@ -4,7 +4,10 @@ enum class Language { C } -class NativeLibrary(val includes: List, val compilerArgs: List, val language: Language) +data class NativeLibrary(val includes: List, + val compilerArgs: List, + val language: Language, + val excludeSystemLibs: Boolean = true) /** * Retrieves the definitions from given C header file using given compiler arguments (e.g. defines). diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt index 3aa8a198552..b6ed261731a 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt @@ -173,9 +173,16 @@ internal fun NativeLibrary.precompileHeaders(): NativeLibrary { clang_disposeIndex(index) } - return NativeLibrary( + return this.copy( includes = emptyList(), - compilerArgs = this.compilerArgs + listOf("-include-pch", precompiledHeader.absolutePath), - language = this.language + compilerArgs = this.compilerArgs + listOf("-include-pch", precompiledHeader.absolutePath) ) +} + +internal fun NativeLibrary.includesDeclaration(cursor: CValue): Boolean { + return if (this.excludeSystemLibs) { + clang_Location_isInSystemHeader(clang_getCursorLocation(cursor)) == 0 + } else { + true + } } \ No newline at end of file diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt index 19ac77d7ac5..91b01b6e268 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt @@ -266,6 +266,8 @@ private fun processLib(konanHome: String, defaultOpts + additionalCompilerOpts val compiler = "clang" val language = Language.C + val excludeSystemLibs = config.getProperty("excludeSystemLibs")?.toBoolean() ?: false + val entryPoint = config.getSpaceSeparated("entryPoint").singleOrNull() val linkerOpts = config.getSpaceSeparated("linkerOpts").toTypedArray() + @@ -285,7 +287,7 @@ private fun processLib(konanHome: String, val libName = fqParts.joinToString("") + "stubs" - val library = NativeLibrary(headerFiles, compilerOpts, language) + val library = NativeLibrary(headerFiles, compilerOpts, language, excludeSystemLibs) val configuration = InteropConfiguration( library = library, pkgName = outKtPkg,