Implement excludeSystemLibs interop option (disabled by default)

This commit is contained in:
Svyatoslav Scherbina
2017-03-28 11:11:20 +03:00
committed by SvyatoslavScherbina
parent 30bab17c21
commit 4a54fe793e
5 changed files with 29 additions and 9 deletions
@@ -19,7 +19,7 @@ private class EnumDefImpl(spelling: String, type: PrimitiveType) : EnumDef(spell
override val constants = mutableListOf<EnumConstant>()
}
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
@@ -201,7 +201,10 @@ private fun collectMacroConstantsNames(library: NativeLibrary): List<String> {
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)
}
@@ -4,7 +4,10 @@ enum class Language {
C
}
class NativeLibrary(val includes: List<String>, val compilerArgs: List<String>, val language: Language)
data class NativeLibrary(val includes: List<String>,
val compilerArgs: List<String>,
val language: Language,
val excludeSystemLibs: Boolean = true)
/**
* Retrieves the definitions from given C header file using given compiler arguments (e.g. defines).
@@ -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<CXCursor>): Boolean {
return if (this.excludeSystemLibs) {
clang_Location_isInSystemHeader(clang_getCursorLocation(cursor)) == 0
} else {
true
}
}
@@ -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,