[K/N] cinterop: Do not expand non-constant macros

Expanding macros such as __FILE__ or __TIME__ exposes
arbitrary generated filenames and timestamps from the compiler
pipeline which are not useful for interop and makes the klib
generation non-deterministic. This patch instead redefines
the macros to just map to their name in the properties
available from Kotlin.

Co-authored-by: Johan Bay <jobay@google.com>
This commit is contained in:
Johan Bay
2022-10-12 16:54:00 +00:00
committed by Space
parent 49e343e08e
commit bc13173ea9
4 changed files with 37 additions and 1 deletions
@@ -60,6 +60,13 @@ private fun expandMacros(
// or function-like construction (e.g. #define FOO throw()) but such a function is undeclared.
compilerArgs += "-Werror=implicit-function-declaration"
// Some predefined macros expand to contextual values that won't make sense to expose in Kotlin properties.
// We instead redefined them to string values of the macro name.
compilerArgs += "-Wno-builtin-macro-redefined"
val predefinedMacros = listOf("__DATE__", "__TIME__", "__TIMESTAMP__", "__FILE__", "__FILE_NAME__", "__BASE_FILE__", "__LINE__")
predefinedMacros.forEach {
compilerArgs += "-D${it}=\"${it}\""
}
// Ensure libclang reports all errors:
compilerArgs += "-ferror-limit=0"
@@ -319,4 +326,4 @@ private fun canMacroBeConstant(cursor: CValue<CXCursor>): Boolean {
// Requires updating to 3.9.1 due to https://bugs.llvm.org//show_bug.cgi?id=9069
return true
}
}
@@ -4285,6 +4285,10 @@ createInterop("kt51925") {
it.defFile 'interop/kt51925/kt51925.def'
}
createInterop("kt54284") {
it.defFile 'interop/kt54284/kt54284.def'
}
createInterop("kt43502") {
it.defFile 'interop/kt43502/kt43502.def'
it.headers "$projectDir/interop/kt43502/kt43502.h"
@@ -4759,6 +4763,12 @@ interopTest("interop_kt44283") {
source = "interop/kt44283/main.kt"
}
interopTest("interop_kt54284") {
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
interop = 'kt54284'
source = "interop/kt54284/main.kt"
}
// TODO: This test should be run with caches on.
interopTest("interop_kt51925") {
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
@@ -0,0 +1,7 @@
---
#define KFILE "FILE:" __FILE__
#define KLINE "LINE:" __LINE__
#define KTIME "TIME:" __TIME__
#define KDATE "DATE:" __DATE__
#define KFILENAME "NAME:" __FILE_NAME__
#define KBASEFILE "BASE:" __BASE_FILE__
@@ -0,0 +1,12 @@
import kotlinx.cinterop.*
import kt54284.*
import kotlin.test.*
fun main() {
assertEquals(KFILE, "FILE:__FILE__")
assertEquals(KLINE, "LINE:__LINE__")
assertEquals(KTIME, "TIME:__TIME__")
assertEquals(KDATE, "DATE:__DATE__")
assertEquals(KFILENAME, "NAME:__FILE_NAME__")
assertEquals(KBASEFILE, "BASE:__BASE_FILE__")
}