Skip to content

Commit 275a1b5

Browse files
[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 (cherry picked from commit 0044ca5)
1 parent b47f431 commit 275a1b5

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
@@ -143,17 +143,31 @@ internal fun AbstractNativeSimpleTest.cinteropToLibrary(
143143
outputDir: File,
144144
freeCompilerArgs: TestCompilerArgs
145145
): TestCompilationResult<out TestCompilationArtifact.KLIB> {
146-
val testCase: TestCase = generateCInteropTestCaseFromSingleDefFile(defFile, freeCompilerArgs)
146+
val args = freeCompilerArgs + cinteropModulesCachePathArguments(freeCompilerArgs.cinteropArgs, outputDir)
147+
val testCase: TestCase = generateCInteropTestCaseFromSingleDefFile(defFile, args)
147148
return CInteropCompilation(
148149
classLoader = testRunSettings.get(),
149150
targets = targets,
150-
freeCompilerArgs = freeCompilerArgs,
151+
freeCompilerArgs = args,
151152
defFile = testCase.modules.single().files.single().location,
152153
dependencies = emptyList(),
153154
expectedArtifact = getLibraryArtifact(testCase, outputDir)
154155
).result
155156
}
156157

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

0 commit comments

Comments
 (0)