Skip to content

Commit 0044ca5

Browse files
SvyatoslavScherbinaSpace Team
authored and
Space Team
committed
[K/N][tests] Stop using shared Clang module cache to improve stability
cinterop uses Clang and libclang. When getting `-fmodules` flag, Clang practices some caching, which is system-wide by default. Meaning, the cache directory can be used by other Clang invocations, even at the same time. Clang module cache machinery has sophisticated measures to avoid any problems with that, e.g. different compilation flags make it use different cache sub-directories, and there are locks in place to avoid data races. Still, sometimes we encounter problems seemingly triggered by re-using the same cache directory. This commit workarounds that by passing `-fmodules-cache-path=` flag to some of the relevant `cinterop` invocations in the test infrastructure. ^KT-68254
1 parent 39fef62 commit 0044ca5

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/NativeSimpleTestUtils.kt

+16-2
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,31 @@ internal fun AbstractNativeSimpleTest.cinteropToLibrary(
141141
outputDir: File,
142142
freeCompilerArgs: TestCompilerArgs
143143
): TestCompilationResult<out TestCompilationArtifact.KLIB> {
144-
val testCase: TestCase = generateCInteropTestCaseFromSingleDefFile(defFile, freeCompilerArgs)
144+
val args = freeCompilerArgs + cinteropModulesCachePathArguments(freeCompilerArgs.cinteropArgs, outputDir)
145+
val testCase: TestCase = generateCInteropTestCaseFromSingleDefFile(defFile, args)
145146
return CInteropCompilation(
146147
classLoader = testRunSettings.get(),
147148
targets = targets,
148-
freeCompilerArgs = freeCompilerArgs,
149+
freeCompilerArgs = args,
149150
defFile = testCase.modules.single().files.single().location,
150151
dependencies = emptyList(),
151152
expectedArtifact = getLibraryArtifact(testCase, outputDir)
152153
).result
153154
}
154155

156+
private fun cinteropModulesCachePathArguments(
157+
cinteropArgs: List<String>,
158+
outputDir: File,
159+
) = if (cinteropArgs.contains("-fmodules") && cinteropArgs.none { it.startsWith(FMODULES_CACHE_PATH_EQ) }) {
160+
// Don't reuse the system-wide module cache to make the test run more predictably.
161+
// See e.g. https://youtrack.jetbrains.com/issue/KT-68254.
162+
TestCInteropArgs("-compiler-option", "$FMODULES_CACHE_PATH_EQ$outputDir/modulesCachePath")
163+
} else {
164+
TestCompilerArgs.EMPTY
165+
}
166+
167+
private const val FMODULES_CACHE_PATH_EQ = "-fmodules-cache-path="
168+
155169
internal class CompiledExecutable(
156170
val testCase: TestCase,
157171
val compilationResult: TestCompilationResult.Success<out TestCompilationArtifact.Executable>

0 commit comments

Comments
 (0)