[K/N][KT-39120] Build platform libraries with -fmodules

Merge-request: KT-MR-8175
Merged-by: Vladimir Sukharev <Vladimir.Sukharev@jetbrains.com>
This commit is contained in:
Vladimir Sukharev
2023-01-13 07:33:42 +00:00
committed by Space Team
parent d85b23ebb9
commit 45de88abae
35 changed files with 302 additions and 58 deletions
@@ -318,18 +318,13 @@ public open class NativeIndexImpl(val library: NativeLibrary, val verbose: Boole
if (clang_isCursorDefinition(definitionCursor) != 0) {
return getEnumDefAt(definitionCursor)
} else {
TODO("support enum forward declarations: " +
clang_getTypeSpelling(clang_getCursorType(cursor)).convertAndDispose())
// FIXME("enum declaration without constants might be not a typedef, but a forward declaration instead")
return enumRegistry.getOrPut(cursor) { createEnumDefImpl(cursor) }
}
}
return enumRegistry.getOrPut(cursor) {
val cursorType = clang_getCursorType(cursor)
val typeSpelling = clang_getTypeSpelling(cursorType).convertAndDispose()
val baseType = convertType(clang_getEnumDeclIntegerType(cursor))
val enumDef = EnumDefImpl(typeSpelling, baseType, getLocation(cursor))
val enumDef = createEnumDefImpl(cursor)
visitChildren(cursor) { childCursor, _ ->
if (clang_getCursorKind(childCursor) == CXCursorKind.CXCursor_EnumConstantDecl) {
@@ -347,6 +342,13 @@ public open class NativeIndexImpl(val library: NativeLibrary, val verbose: Boole
}
}
private fun createEnumDefImpl(cursor: CValue<CXCursor>): EnumDefImpl {
val cursorType = clang_getCursorType(cursor)
val typeSpelling = clang_getTypeSpelling(cursorType).convertAndDispose()
val baseType = convertType(clang_getEnumDeclIntegerType(cursor))
return EnumDefImpl(typeSpelling, baseType, getLocation(cursor))
}
private fun getObjCCategoryClassCursor(cursor: CValue<CXCursor>): CValue<CXCursor> {
assert(cursor.kind == CXCursorKind.CXCursor_ObjCCategoryDecl)
var classRef: CValue<CXCursor>? = null
@@ -4,7 +4,7 @@ import clang.*
import kotlinx.cinterop.*
import java.nio.file.Files
data class ModulesInfo(val topLevelHeaders: List<String>, val ownHeaders: Set<String>, val modules: List<String>)
data class ModulesInfo(val topLevelHeaders: List<IncludeInfo>, val ownHeaders: Set<String>, val modules: List<String>)
fun getModulesInfo(compilation: Compilation, modules: List<String>): ModulesInfo {
if (modules.isEmpty()) return ModulesInfo(emptyList(), emptySet(), emptyList())
@@ -17,9 +17,11 @@ fun getModulesInfo(compilation: Compilation, modules: List<String>): ModulesInfo
}
}
data class IncludeInfo(val headerPath: String, val moduleName: String?)
private fun buildModulesInfo(index: CXIndex, modules: List<String>, modulesASTFiles: List<String>): ModulesInfo {
val ownHeaders = mutableSetOf<String>()
val topLevelHeaders = linkedSetOf<String>()
val topLevelHeaders = linkedSetOf<IncludeInfo>()
modulesASTFiles.forEach {
val moduleTranslationUnit = clang_createTranslationUnit(index, it)!!
try {
@@ -89,7 +91,7 @@ private fun getModulesHeaders(
index: CXIndex,
translationUnit: CXTranslationUnit,
modules: Set<String>,
topLevelHeaders: LinkedHashSet<String>
topLevelHeaders: LinkedHashSet<IncludeInfo>
): Set<CXFile> {
val nonModularIncludes = mutableMapOf<CXFile, MutableSet<CXFile>>()
val result = mutableSetOf<CXFile>()
@@ -99,13 +101,13 @@ private fun getModulesHeaders(
val file = info.file!!
val includer = clang_indexLoc_getCXSourceLocation(info.hashLoc.readValue()).getContainingFile()
val module = clang_getModuleForFile(translationUnit, file)
if (includer == null) {
// i.e. the header is included by the module itself.
topLevelHeaders += file.path
topLevelHeaders += IncludeInfo(file.path, clang_Module_getFullName(module).convertAndDispose())
}
val module = clang_getModuleForFile(translationUnit, file)
if (module != null) {
val moduleWithParents = generateSequence(module, { clang_Module_getParent(it) }).map {
clang_Module_getFullName(it).convertAndDispose()
@@ -52,7 +52,7 @@ sealed class NativeLibraryHeaderFilter {
}
interface Compilation {
val includes: List<String>
val includes: List<IncludeInfo>
val additionalPreambleLines: List<String>
val compilerArgs: List<String>
val language: Language
@@ -93,7 +93,7 @@ data class CompilationWithPCH(
constructor(compilerArgs: List<String>, precompiledHeader: String, language: Language)
: this(compilerArgs + listOf("-include-pch", precompiledHeader), language)
override val includes: List<String>
override val includes: List<IncludeInfo>
get() = emptyList()
override val additionalPreambleLines: List<String>
@@ -102,7 +102,7 @@ data class CompilationWithPCH(
// TODO: Compilation hierarchy seems to require some refactoring.
data class NativeLibrary(override val includes: List<String>,
data class NativeLibrary(override val includes: List<IncludeInfo>,
override val additionalPreambleLines: List<String>,
override val compilerArgs: List<String>,
val headerToIdMapper: HeaderToIdMapper,
@@ -354,7 +354,13 @@ internal fun List<String>.toNativeStringArray(scope: AutofreeScope): CArrayPoint
}
val Compilation.preambleLines: List<String>
get() = this.includes.map { "#include <$it>" } + this.additionalPreambleLines
get() = this.includes.map {
if (it.moduleName != null && it.moduleName != "" && "-fmodules" in this.compilerArgs) {
"@import ${it.moduleName};"
} else {
"#include <${it.headerPath}>"
}
} + this.additionalPreambleLines
internal fun Appendable.appendPreamble(compilation: Compilation) = this.apply {
compilation.preambleLines.forEach {
@@ -377,7 +383,7 @@ internal fun Compilation.createTempSource(): File {
}
fun Compilation.copy(
includes: List<String> = this.includes,
includes: List<IncludeInfo> = this.includes,
additionalPreambleLines: List<String> = this.additionalPreambleLines,
compilerArgs: List<String> = this.compilerArgs,
language: Language = this.language
@@ -394,7 +400,7 @@ fun Compilation.copyWithArgsForPCH(): Compilation =
copy(compilerArgs = compilerArgs.filterNot { it.startsWith("-fmodule-map-file") })
data class CompilationImpl(
override val includes: List<String>,
override val includes: List<IncludeInfo>,
override val additionalPreambleLines: List<String>,
override val compilerArgs: List<String>,
override val language: Language
@@ -153,7 +153,7 @@ class ModuleTests : IndexerTests() {
assertContains(error.message.orEmpty(), "testModuleWithBadCode/Foo.h:1:1: error: unknown type name 'bad'")
}
private fun List<String>.canonicalize(): List<String> = this.map { File(it).canonicalPath }
private fun List<IncludeInfo>.canonicalize(): List<String> = this.map { File(it.headerPath).canonicalPath }
private fun compilationIncluding(includeDirectory: File) = compilation("-I$includeDirectory")