use of org.gradle.language.swift.tasks.SwiftCompile in project gradle by gradle.
the class XCTestConventionPlugin method configureTestSuiteBuildingTasks.
private void configureTestSuiteBuildingTasks(ProjectInternal project, final DefaultSwiftXCTestBinary binary) {
if (binary instanceof SwiftXCTestBundle) {
TaskContainer tasks = project.getTasks();
final Names names = binary.getNames();
SwiftCompile compile = binary.getCompileTask().get();
// TODO - creating a bundle should be done by some general purpose plugin
// TODO - make this lazy
DefaultNativePlatform currentPlatform = new DefaultNativePlatform("current");
final ModelRegistry modelRegistry = project.getModelRegistry();
NativeToolChain toolChain = modelRegistry.realize("toolChains", NativeToolChainRegistryInternal.class).getForPlatform(currentPlatform);
// Platform specific arguments
compile.getCompilerArgs().addAll(project.provider(new Callable<List<String>>() {
@Override
public List<String> call() {
File frameworkDir = new File(sdkPlatformPathLocator.find(), "Developer/Library/Frameworks");
return Arrays.asList("-parse-as-library", "-F" + frameworkDir.getAbsolutePath());
}
}));
// Add a link task
final LinkMachOBundle link = tasks.create(names.getTaskName("link"), LinkMachOBundle.class);
link.getLinkerArgs().set(project.provider(new Callable<List<String>>() {
@Override
public List<String> call() {
File frameworkDir = new File(sdkPlatformPathLocator.find(), "Developer/Library/Frameworks");
return Lists.newArrayList("-F" + frameworkDir.getAbsolutePath(), "-framework", "XCTest", "-Xlinker", "-rpath", "-Xlinker", "@executable_path/../Frameworks", "-Xlinker", "-rpath", "-Xlinker", "@loader_path/../Frameworks");
}
}));
InstallXCTestBundle install = tasks.create(names.getTaskName("install"), InstallXCTestBundle.class);
install.getBundleBinaryFile().set(link.getLinkedFile());
install.getInstallDirectory().set(project.getLayout().getBuildDirectory().dir("install/" + names.getDirName()));
binary.getInstallDirectory().set(install.getInstallDirectory());
link.source(binary.getObjects());
link.lib(binary.getLinkLibraries());
final PlatformToolProvider toolProvider = ((NativeToolChainInternal) toolChain).select(currentPlatform);
Provider<RegularFile> exeLocation = project.getLayout().getBuildDirectory().file(project.getProviders().provider(new Callable<String>() {
@Override
public String call() {
return toolProvider.getExecutableName("exe/" + names.getDirName() + binary.getBaseName().get());
}
}));
link.getLinkedFile().set(exeLocation);
link.getTargetPlatform().set(currentPlatform);
link.getToolChain().set(toolChain);
link.getDebuggable().set(binary.isDebuggable());
binary.getExecutableFile().set(link.getLinkedFile());
DefaultSwiftXCTestBundle bundle = (DefaultSwiftXCTestBundle) binary;
bundle.getLinkTask().set(link);
bundle.getRunScriptFile().set(install.getRunScriptFile());
} else {
DefaultSwiftXCTestExecutable executable = (DefaultSwiftXCTestExecutable) binary;
executable.getRunScriptFile().set(executable.getInstallTask().get().getRunScriptFile());
}
}
use of org.gradle.language.swift.tasks.SwiftCompile in project gradle by gradle.
the class XCTestConventionPlugin method configureTestSuiteBuildingTasks.
private void configureTestSuiteBuildingTasks(final Project project, final DefaultSwiftXCTestBinary binary) {
// Overwrite the source to exclude `LinuxMain.swift`
SwiftCompile compile = binary.getCompileTask().get();
compile.getSource().setFrom(binary.getSwiftSource().getAsFileTree().matching(patterns -> patterns.include("**/*").exclude("**/LinuxMain.swift")));
if (binary instanceof SwiftXCTestBundle) {
TaskContainer tasks = project.getTasks();
final Names names = binary.getNames();
// TODO - creating a bundle should be done by some general purpose plugin
// TODO - make this lazy
final DefaultNativePlatform currentPlatform = new DefaultNativePlatform("current");
final ModelRegistry modelRegistry = ((ProjectInternal) project).getModelRegistry();
final NativeToolChain toolChain = modelRegistry.realize("toolChains", NativeToolChainRegistryInternal.class).getForPlatform(currentPlatform);
// Platform specific arguments
// TODO: Need to lazily configure compile task
// TODO: Ultimately, this should be some kind of 3rd party dependency that's visible to dependency management.
compile.getCompilerArgs().addAll(project.provider(() -> {
File platformSdkPath = sdkPlatformPathLocator.find();
File frameworkDir = new File(platformSdkPath, "Developer/Library/Frameworks");
// Since Xcode 11/12, the XCTest framework is being replaced by a different library that's available in the sdk root
File extraInclude = new File(platformSdkPath, "Developer/usr/lib");
return Arrays.asList("-parse-as-library", "-F" + frameworkDir.getAbsolutePath(), "-I", extraInclude.getAbsolutePath(), "-v");
}));
// Add a link task
final TaskProvider<LinkMachOBundle> link = tasks.register(names.getTaskName("link"), LinkMachOBundle.class, task -> {
task.getLinkerArgs().set(project.provider(() -> {
File platformSdkPath = sdkPlatformPathLocator.find();
File frameworkDir = new File(platformSdkPath, "Developer/Library/Frameworks");
// Since Xcode 11/12, the XCTest framework is being replaced by a different library that's available in the sdk root
File extraInclude = new File(platformSdkPath, "Developer/usr/lib");
return Lists.newArrayList("-F" + frameworkDir.getAbsolutePath(), "-L", extraInclude.getAbsolutePath(), "-framework", "XCTest", "-Xlinker", "-rpath", "-Xlinker", "@executable_path/../Frameworks", "-Xlinker", "-rpath", "-Xlinker", "@loader_path/../Frameworks");
}));
task.source(binary.getObjects());
task.lib(binary.getLinkLibraries());
final PlatformToolProvider toolProvider = ((NativeToolChainInternal) toolChain).select(currentPlatform);
Provider<RegularFile> exeLocation = project.getLayout().getBuildDirectory().file(binary.getBaseName().map(baseName -> toolProvider.getExecutableName("exe/" + names.getDirName() + baseName)));
task.getLinkedFile().set(exeLocation);
task.getTargetPlatform().set(currentPlatform);
task.getToolChain().set(toolChain);
task.getDebuggable().set(binary.isDebuggable());
});
final TaskProvider<InstallXCTestBundle> install = tasks.register(names.getTaskName("install"), InstallXCTestBundle.class, task -> {
task.getBundleBinaryFile().set(link.get().getLinkedFile());
task.getInstallDirectory().set(project.getLayout().getBuildDirectory().dir("install/" + names.getDirName()));
});
binary.getInstallDirectory().set(install.flatMap(task -> task.getInstallDirectory()));
binary.getExecutableFile().set(link.flatMap(task -> task.getLinkedFile()));
DefaultSwiftXCTestBundle bundle = (DefaultSwiftXCTestBundle) binary;
bundle.getLinkTask().set(link);
bundle.getRunScriptFile().set(install.flatMap(task -> task.getRunScriptFile()));
} else {
DefaultSwiftXCTestExecutable executable = (DefaultSwiftXCTestExecutable) binary;
executable.getRunScriptFile().set(executable.getInstallTask().flatMap(task -> task.getRunScriptFile()));
// Rename `LinuxMain.swift` to `main.swift` so the entry point is correctly detected by swiftc
if (binary.getTargetMachine().getOperatingSystemFamily().isLinux()) {
TaskProvider<Sync> renameLinuxMainTask = project.getTasks().register("renameLinuxMain", Sync.class, task -> {
task.from(binary.getSwiftSource());
task.into(project.provider(() -> task.getTemporaryDir()));
task.include("LinuxMain.swift");
task.rename(it -> "main.swift");
});
compile.getSource().from(project.files(renameLinuxMainTask).getAsFileTree().matching(patterns -> patterns.include("**/*.swift")));
}
}
}
use of org.gradle.language.swift.tasks.SwiftCompile in project gradle by gradle.
the class SwiftBasePlugin method apply.
@Override
public void apply(final ProjectInternal project) {
project.getPluginManager().apply(NativeBasePlugin.class);
project.getPluginManager().apply(SwiftCompilerPlugin.class);
final TaskContainerInternal tasks = project.getTasks();
final DirectoryProperty buildDirectory = project.getLayout().getBuildDirectory();
final ProviderFactory providers = project.getProviders();
project.getDependencies().getAttributesSchema().attribute(Usage.USAGE_ATTRIBUTE).getCompatibilityRules().add(SwiftCppUsageCompatibilityRule.class);
project.getComponents().withType(DefaultSwiftBinary.class, new Action<DefaultSwiftBinary>() {
@Override
public void execute(final DefaultSwiftBinary binary) {
final Names names = binary.getNames();
SwiftCompile compile = tasks.create(names.getCompileTaskName("swift"), SwiftCompile.class);
compile.getModules().from(binary.getCompileModules());
compile.getSource().from(binary.getSwiftSource());
compile.getDebuggable().set(binary.isDebuggable());
compile.getOptimized().set(binary.isOptimized());
if (binary.isTestable()) {
compile.getCompilerArgs().add("-enable-testing");
}
if (binary.getTargetPlatform().getOperatingSystem().isMacOsX()) {
compile.getCompilerArgs().add("-sdk");
compile.getCompilerArgs().add(locator.find().getAbsolutePath());
}
compile.getModuleName().set(binary.getModule());
compile.getObjectFileDir().set(buildDirectory.dir("obj/" + names.getDirName()));
compile.getModuleFile().set(buildDirectory.file(providers.provider(new Callable<String>() {
@Override
public String call() {
return "modules/" + names.getDirName() + binary.getModule().get() + ".swiftmodule";
}
})));
compile.getSourceCompatibility().set(binary.getSourceCompatibility());
binary.getModuleFile().set(compile.getModuleFile());
compile.getTargetPlatform().set(binary.getTargetPlatform());
// TODO - make this lazy
compile.getToolChain().set(binary.getToolChain());
binary.getCompileTask().set(compile);
binary.getObjectsDir().set(compile.getObjectFileDir());
}
});
project.getComponents().withType(SwiftSharedLibrary.class, new Action<SwiftSharedLibrary>() {
@Override
public void execute(SwiftSharedLibrary library) {
// Specific compiler arguments
library.getCompileTask().get().getCompilerArgs().add("-parse-as-library");
}
});
project.getComponents().withType(SwiftStaticLibrary.class, new Action<SwiftStaticLibrary>() {
@Override
public void execute(SwiftStaticLibrary library) {
// Specific compiler arguments
library.getCompileTask().get().getCompilerArgs().add("-parse-as-library");
}
});
project.getComponents().withType(DefaultSwiftComponent.class, new Action<DefaultSwiftComponent>() {
@Override
public void execute(final DefaultSwiftComponent component) {
project.afterEvaluate(new Action<Project>() {
@Override
public void execute(Project project) {
component.getSourceCompatibility().lockNow();
}
});
component.getBinaries().whenElementKnown(DefaultSwiftBinary.class, new Action<DefaultSwiftBinary>() {
@Override
public void execute(final DefaultSwiftBinary binary) {
Provider<SwiftVersion> swiftLanguageVersionProvider = project.provider(new Callable<SwiftVersion>() {
@Override
public SwiftVersion call() throws Exception {
SwiftVersion swiftSourceCompatibility = component.getSourceCompatibility().getOrNull();
if (swiftSourceCompatibility == null) {
return toSwiftVersion(binary.getPlatformToolProvider().getCompilerMetadata(ToolType.SWIFT_COMPILER).getVersion());
}
return swiftSourceCompatibility;
}
});
binary.getSourceCompatibility().set(swiftLanguageVersionProvider);
}
});
}
});
project.getComponents().withType(ProductionSwiftComponent.class, new Action<ProductionSwiftComponent>() {
@Override
public void execute(final ProductionSwiftComponent component) {
project.afterEvaluate(new Action<Project>() {
@Override
public void execute(Project project) {
DefaultSwiftComponent componentInternal = (DefaultSwiftComponent) component;
publicationRegistry.registerPublication(project.getPath(), new DefaultProjectPublication(componentInternal.getDisplayName(), new SwiftPmTarget(component.getModule().get()), false));
}
});
}
});
}
use of org.gradle.language.swift.tasks.SwiftCompile in project gradle by gradle.
the class SwiftBasePlugin method apply.
@Override
public void apply(final Project project) {
project.getPluginManager().apply(NativeBasePlugin.class);
project.getPluginManager().apply(SwiftCompilerPlugin.class);
final TaskContainer tasks = project.getTasks();
final DirectoryProperty buildDirectory = project.getLayout().getBuildDirectory();
project.getDependencies().getAttributesSchema().attribute(Usage.USAGE_ATTRIBUTE).getCompatibilityRules().add(SwiftCppUsageCompatibilityRule.class);
project.getComponents().withType(DefaultSwiftBinary.class, binary -> {
final Names names = binary.getNames();
TaskProvider<SwiftCompile> compile = tasks.register(names.getCompileTaskName("swift"), SwiftCompile.class, task -> {
task.getModules().from(binary.getCompileModules());
task.getSource().from(binary.getSwiftSource());
task.getDebuggable().set(binary.isDebuggable());
task.getOptimized().set(binary.isOptimized());
if (binary.isTestable()) {
task.getCompilerArgs().add("-enable-testing");
}
if (binary.getTargetMachine().getOperatingSystemFamily().isMacOs()) {
task.getCompilerArgs().add("-sdk");
task.getCompilerArgs().add(locator.find().getAbsolutePath());
}
task.getModuleName().set(binary.getModule());
task.getObjectFileDir().set(buildDirectory.dir("obj/" + names.getDirName()));
task.getModuleFile().set(buildDirectory.file(binary.getModule().map(moduleName -> "modules/" + names.getDirName() + moduleName + ".swiftmodule")));
task.getSourceCompatibility().set(binary.getTargetPlatform().getSourceCompatibility());
task.getTargetPlatform().set(binary.getNativePlatform());
// TODO - make this lazy
task.getToolChain().set(binary.getToolChain());
if (binary instanceof SwiftSharedLibrary || binary instanceof SwiftStaticLibrary) {
task.getCompilerArgs().add("-parse-as-library");
}
});
binary.getModuleFile().set(compile.flatMap(task -> task.getModuleFile()));
binary.getCompileTask().set(compile);
binary.getObjectsDir().set(compile.flatMap(task -> task.getObjectFileDir()));
});
project.getComponents().withType(ProductionSwiftComponent.class, component -> {
project.afterEvaluate(p -> {
DefaultNativeComponent componentInternal = (DefaultNativeComponent) component;
publicationRegistry.registerPublication((ProjectInternal) project, new NativeProjectPublication(componentInternal.getDisplayName(), new SwiftPmTarget(component.getModule().get())));
});
});
}
Aggregations