use of org.gradle.language.cpp.CppComponent in project gradle by gradle.
the class CppUnitTestPlugin method configureTestSuiteWithTestedComponentWhenAvailable.
private void configureTestSuiteWithTestedComponentWhenAvailable(Project project, DefaultCppTestSuite testSuite, DefaultCppTestExecutable testExecutable) {
CppComponent target = testSuite.getTestedComponent().getOrNull();
if (!(target instanceof ProductionCppComponent)) {
return;
}
final ProductionCppComponent testedComponent = (ProductionCppComponent) target;
final TaskContainer tasks = project.getTasks();
testedComponent.getBinaries().whenElementFinalized(testedBinary -> {
if (!isTestedBinary(testExecutable, testedComponent, testedBinary)) {
return;
}
// TODO - move this to a base plugin
// Setup the dependency on the main binary
// This should all be replaced by a single dependency that points at some "testable" variants of the main binary
// Inherit implementation dependencies
testExecutable.getImplementationDependencies().extendsFrom(((DefaultCppBinary) testedBinary).getImplementationDependencies());
// Configure test binary to link against tested component compiled objects
ConfigurableFileCollection testableObjects = project.files();
if (target instanceof CppApplication) {
// TODO - this should be an outgoing variant of the component under test
TaskProvider<UnexportMainSymbol> unexportMainSymbol = tasks.register(testExecutable.getNames().getTaskName("relocateMainFor"), UnexportMainSymbol.class, task -> {
String dirName = ((DefaultCppBinary) testedBinary).getNames().getDirName();
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("obj/for-test/" + dirName));
task.getObjects().from(testedBinary.getObjects());
});
testableObjects.from(unexportMainSymbol.map(task -> task.getRelocatedObjects()));
} else {
testableObjects.from(testedBinary.getObjects());
}
Dependency linkDependency = project.getDependencies().create(testableObjects);
testExecutable.getLinkConfiguration().getDependencies().add(linkDependency);
});
}
use of org.gradle.language.cpp.CppComponent in project gradle by gradle.
the class CppUnitTestPlugin method apply.
@Override
public void apply(final Project project) {
project.getPluginManager().apply(CppBasePlugin.class);
project.getPluginManager().apply(NativeTestingBasePlugin.class);
final ProviderFactory providers = project.getProviders();
final TaskContainer tasks = project.getTasks();
// Add the unit test and extension
final DefaultCppTestSuite testComponent = componentFactory.newInstance(CppTestSuite.class, DefaultCppTestSuite.class, "test");
project.getExtensions().add(CppTestSuite.class, "unitTest", testComponent);
project.getComponents().add(testComponent);
testComponent.getBaseName().convention(project.getName() + "Test");
testComponent.getTargetMachines().convention(Dimensions.useHostAsDefaultTargetMachine(targetMachineFactory));
final String mainComponentName = "main";
project.getComponents().withType(ProductionCppComponent.class, component -> {
if (mainComponentName.equals(component.getName())) {
testComponent.getTargetMachines().convention(component.getTargetMachines());
testComponent.getTestedComponent().convention(component);
}
});
testComponent.getTestBinary().convention(project.provider(new Callable<CppTestExecutable>() {
@Override
public CppTestExecutable call() throws Exception {
return getAllBuildableTestExecutable().filter(it -> isCurrentArchitecture(it.getNativePlatform())).findFirst().orElse(getAllBuildableTestExecutable().findFirst().orElse(getAllTestExecutable().findFirst().orElse(null)));
}
private boolean isCurrentArchitecture(NativePlatform targetPlatform) {
return targetPlatform.getArchitecture().equals(DefaultNativePlatform.getCurrentArchitecture());
}
private Stream<DefaultCppTestExecutable> getAllBuildableTestExecutable() {
return getAllTestExecutable().filter(it -> it.getPlatformToolProvider().isAvailable());
}
private Stream<DefaultCppTestExecutable> getAllTestExecutable() {
return testComponent.getBinaries().get().stream().filter(CppTestExecutable.class::isInstance).map(DefaultCppTestExecutable.class::cast);
}
}));
testComponent.getBinaries().whenElementKnown(DefaultCppTestExecutable.class, binary -> {
// TODO: Replace with native test task
final TaskProvider<RunTestExecutable> testTask = tasks.register(binary.getNames().getTaskName("run"), RunTestExecutable.class, task -> {
task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
task.setDescription("Executes C++ unit tests.");
final InstallExecutable installTask = binary.getInstallTask().get();
task.onlyIf(element -> binary.getInstallDirectory().get().getAsFile().exists());
task.getInputs().dir(binary.getInstallDirectory()).withPropertyName("installDirectory");
task.setExecutable(installTask.getRunScriptFile().get().getAsFile());
task.dependsOn(binary.getInstallDirectory());
// TODO: Honor changes to build directory
task.setOutputDir(project.getLayout().getBuildDirectory().dir("test-results/" + binary.getNames().getDirName()).get().getAsFile());
});
binary.getRunTask().set(testTask);
configureTestSuiteWithTestedComponentWhenAvailable(project, testComponent, binary);
});
project.afterEvaluate(p -> {
final CppComponent mainComponent = testComponent.getTestedComponent().getOrNull();
final SetProperty<TargetMachine> mainTargetMachines = mainComponent != null ? mainComponent.getTargetMachines() : null;
Dimensions.unitTestVariants(testComponent.getBaseName(), testComponent.getTargetMachines(), mainTargetMachines, objectFactory, attributesFactory, providers.provider(() -> project.getGroup().toString()), providers.provider(() -> project.getVersion().toString()), variantIdentity -> {
if (tryToBuildOnHost(variantIdentity)) {
ToolChainSelector.Result<CppPlatform> result = toolChainSelector.select(CppPlatform.class, new DefaultCppPlatform(variantIdentity.getTargetMachine()));
// TODO: Removing `debug` from variant name to keep parity with previous Gradle version in tooling models
CppTestExecutable testExecutable = testComponent.addExecutable(variantIdentity.getName().replace("debug", ""), variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
}
});
// TODO: Publishing for test executable?
testComponent.getBinaries().realizeNow();
});
}
Aggregations