diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt index eae8cd26865..1dac84b2a79 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt @@ -29,6 +29,11 @@ import org.jetbrains.kotlin.utils.Maybe import java.io.File import javax.inject.Inject +private data class CompilerWorkUnit( + val input: File, + val output: File, +) : java.io.Serializable + private abstract class CompileToBitcodeJob : WorkAction { interface Parameters : WorkParameters { val workingDirectory: DirectoryProperty @@ -36,7 +41,7 @@ private abstract class CompileToBitcodeJob : WorkAction val compilerExecutable: Property - val compilerInputFiles: ConfigurableFileCollection + val compilerWorkUnits: ListProperty val compilerArgs: ListProperty val llvmLinkOutputFile: RegularFileProperty val llvmLinkArgs: ListProperty @@ -51,22 +56,23 @@ private abstract class CompileToBitcodeJob : WorkAction + workUnit.output.parentFile.mkdirs() + val inputRelativePath = baseDir.toPath().relativize(workUnit.input.toPath()) + execClang.execKonanClang(targetName.get()) { + workingDir = baseDir + executable = compilerExecutable.get() + args = compilerArgs.get() + listOf(inputRelativePath.toString(), "-o", workUnit.output.absolutePath) + } + } // TODO: Extract llvm-link out. This will allow parallelizing clang compilation. execOperations.execLlvmUtility(platformManager.get(), "llvm-link") { - args = listOf("-o", llvmLinkOutputFile.asFile.get().absolutePath) + llvmLinkArgs.get() + compilerOutputFiles.map { it.absolutePath } + args = listOf("-o", llvmLinkOutputFile.asFile.get().absolutePath) + llvmLinkArgs.get() + compilerWorkUnits.get().map { it.output.absolutePath } } } } @@ -102,7 +108,6 @@ abstract class CompileToBitcode @Inject constructor( /** * Where to put bitcode files generated by clang. */ - // TODO: Generate output files for this. @get:Internal abstract val outputDirectory: DirectoryProperty @@ -162,6 +167,37 @@ abstract class CompileToBitcode @Inject constructor( @get:InputFiles abstract val inputFiles: ConfigurableFileTree + /** + * Working directory for the compiler. + * + * All inputs will be passed to the compiler as relative paths to this directory. + */ + @get:Internal + abstract val compilerWorkingDirectory: DirectoryProperty + + @get:Input + protected val compilerWorkingDirectoryPath: Provider = project.provider { + compilerWorkingDirectory.get().asFile.absolutePath + } + + private val compilerWorkUnits: Provider> = project.provider { + val workUnits = mutableListOf() + inputFiles.visit { + if (!isDirectory) { + val output = outputDirectory.file(relativePath.parent.append(true, "${file.nameWithoutExtension}.bc").pathString) + workUnits.add(CompilerWorkUnit(file, output.get().asFile)) + } + } + workUnits + } + + /** + * Output files from the [compiler]. + */ + // TODO: Make it OutputFiles. Currently clashes with `kotlinNativeInterop` `linkOutputs` in kotlin-native/backend.native/build.gradle Can be fixed when the task is split into clang and llvm-link. + @get:Internal + val compilerOutputFiles = compilerWorkUnits.map { it.map { it.output } } + /** * Computed header files used for task dependencies tracking. */ @@ -206,11 +242,11 @@ abstract class CompileToBitcode @Inject constructor( val workQueue = workerExecutor.noIsolation() workQueue.submit(CompileToBitcodeJob::class.java) { - workingDirectory.set(this@CompileToBitcode.outputDirectory) + workingDirectory.set(this@CompileToBitcode.compilerWorkingDirectory) targetName.set(this@CompileToBitcode.target.name) compilerExecutable.set(this@CompileToBitcode.compiler) compilerArgs.set(this@CompileToBitcode.compilerFlags) - compilerInputFiles.from(this@CompileToBitcode.inputFiles) + compilerWorkUnits.set(this@CompileToBitcode.compilerWorkUnits) llvmLinkOutputFile.set(this@CompileToBitcode.outputFile) llvmLinkArgs.set(this@CompileToBitcode.linkerArgs) platformManager.set(this@CompileToBitcode.platformManager) diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcodePlugin.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcodePlugin.kt index e8bd7b6b799..2a72bd3ee2d 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcodePlugin.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcodePlugin.kt @@ -110,7 +110,7 @@ open class CompileToBitcodeExtension @Inject constructor(val project: Project) { compilationDatabase.target(konanTarget) { entry { val args = listOf(execClang.resolveExecutable(compileTask.compiler.get())) + compileTask.compilerFlags.get() + execClang.clangArgsForCppRuntime(konanTarget.name) - directory.set(compileTask.outputDirectory) + directory.set(compileTask.compilerWorkingDirectory) files.setFrom(compileTask.inputFiles) arguments.set(args) output.set(compileTask.outputFile.asFile.map { it.absolutePath }) @@ -135,6 +135,7 @@ open class CompileToBitcodeExtension @Inject constructor(val project: Project) { this.inputFiles.include("**/*.cpp", "**/*.mm") this.inputFiles.exclude("**/*Test.cpp", "**/*TestSupport.cpp", "**/*Test.mm", "**/*TestSupport.mm") this.headersDirs.from(this.inputFiles.dir) + this.compilerWorkingDirectory.set(project.layout.projectDirectory.dir("src")) when (outputGroup) { "test" -> this.group = VERIFICATION_BUILD_TASK_GROUP "main" -> this.group = BUILD_TASK_GROUP @@ -188,6 +189,7 @@ open class CompileToBitcodeExtension @Inject constructor(val project: Project) { this.inputFiles.include("**/*Test.cpp", "**/*TestSupport.cpp", "**/*Test.mm", "**/*TestSupport.mm") this.headersDirs.setFrom(it.headersDirs) this.headersDirs.from(googleTestExtension.headersDirs) + this.compilerWorkingDirectory.set(it.compilerWorkingDirectory) this.group = VERIFICATION_BUILD_TASK_GROUP this.description = "Compiles '${it.name}' tests to bitcode for $target${sanitizer.description}" diff --git a/kotlin-native/runtime/src/main/cpp/std_support/StdSupportMemoryTest.cpp b/kotlin-native/runtime/src/main/cpp/std_support/MemoryTest.cpp similarity index 100% rename from kotlin-native/runtime/src/main/cpp/std_support/StdSupportMemoryTest.cpp rename to kotlin-native/runtime/src/main/cpp/std_support/MemoryTest.cpp