Search in sources :

Example 1 with CppTestExecutable

use of org.gradle.nativeplatform.test.cpp.CppTestExecutable in project gradle by gradle.

the class DefaultCppTestSuite method addExecutable.

public CppTestExecutable addExecutable(String variantName, NativeVariantIdentity identity, CppPlatform targetPlatform, NativeToolChainInternal toolChain, PlatformToolProvider platformToolProvider) {
    Names executableNames = Names.of(getName() + variantName + "Executable", getName() + variantName);
    CppTestExecutable testBinary = objectFactory.newInstance(DefaultCppTestExecutable.class, executableNames, getBaseName(), getCppSource(), getPrivateHeaderDirs(), getImplementationDependencies(), getTestedComponent(), targetPlatform, toolChain, platformToolProvider, identity);
    getBinaries().add(testBinary);
    return testBinary;
}
Also used : Names(org.gradle.language.nativeplatform.internal.Names) CppTestExecutable(org.gradle.nativeplatform.test.cpp.CppTestExecutable)

Example 2 with CppTestExecutable

use of org.gradle.nativeplatform.test.cpp.CppTestExecutable 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();
    });
}
Also used : Callable(java.util.concurrent.Callable) RunTestExecutable(org.gradle.nativeplatform.test.tasks.RunTestExecutable) DefaultCppTestExecutable(org.gradle.nativeplatform.test.cpp.internal.DefaultCppTestExecutable) CppPlatform(org.gradle.language.cpp.CppPlatform) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) TaskContainer(org.gradle.api.tasks.TaskContainer) InstallExecutable(org.gradle.nativeplatform.tasks.InstallExecutable) CppComponent(org.gradle.language.cpp.CppComponent) ProductionCppComponent(org.gradle.language.cpp.ProductionCppComponent) TargetMachine(org.gradle.nativeplatform.TargetMachine) ProviderFactory(org.gradle.api.provider.ProviderFactory) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) NativePlatform(org.gradle.nativeplatform.platform.NativePlatform) DefaultNativePlatform(org.gradle.nativeplatform.platform.internal.DefaultNativePlatform) ToolChainSelector(org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector) DefaultCppTestExecutable(org.gradle.nativeplatform.test.cpp.internal.DefaultCppTestExecutable) CppTestExecutable(org.gradle.nativeplatform.test.cpp.CppTestExecutable) DefaultCppTestSuite(org.gradle.nativeplatform.test.cpp.internal.DefaultCppTestSuite)

Example 3 with CppTestExecutable

use of org.gradle.nativeplatform.test.cpp.CppTestExecutable in project gradle by gradle.

the class CppModelBuilder method binariesFor.

private List<DefaultCppBinaryModel> binariesFor(CppComponent component, Iterable<File> headerDirs, DefaultProjectIdentifier projectIdentifier, CompilerOutputFileNamingSchemeFactory namingSchemeFactory) {
    List<File> headerDirsCopy = ImmutableList.copyOf(headerDirs);
    List<DefaultCppBinaryModel> binaries = new ArrayList<DefaultCppBinaryModel>();
    for (CppBinary binary : component.getBinaries().get()) {
        DefaultCppBinary cppBinary = (DefaultCppBinary) binary;
        PlatformToolProvider platformToolProvider = cppBinary.getPlatformToolProvider();
        CppCompile compileTask = binary.getCompileTask().get();
        List<DefaultSourceFile> sourceFiles = sourceFiles(namingSchemeFactory, platformToolProvider, compileTask.getObjectFileDir().get().getAsFile(), binary.getCppSource().getFiles());
        List<File> systemIncludes = ImmutableList.copyOf(compileTask.getSystemIncludes().getFiles());
        List<File> userIncludes = ImmutableList.copyOf(compileTask.getIncludes().getFiles());
        List<DefaultMacroDirective> macroDefines = macroDefines(compileTask);
        List<String> additionalArgs = args(compileTask.getCompilerArgs().get());
        CommandLineToolSearchResult compilerLookup = platformToolProvider.locateTool(ToolType.CPP_COMPILER);
        File compilerExe = compilerLookup.isAvailable() ? compilerLookup.getTool() : null;
        LaunchableGradleTask compileTaskModel = ToolingModelBuilderSupport.buildFromTask(new LaunchableGradleTask(), projectIdentifier, compileTask);
        DefaultCompilationDetails compilationDetails = new DefaultCompilationDetails(compileTaskModel, compilerExe, compileTask.getObjectFileDir().get().getAsFile(), sourceFiles, headerDirsCopy, systemIncludes, userIncludes, macroDefines, additionalArgs);
        if (binary instanceof CppExecutable || binary instanceof CppTestExecutable) {
            ComponentWithExecutable componentWithExecutable = (ComponentWithExecutable) binary;
            LinkExecutable linkTask = componentWithExecutable.getLinkTask().get();
            LaunchableGradleTask linkTaskModel = ToolingModelBuilderSupport.buildFromTask(new LaunchableGradleTask(), projectIdentifier, componentWithExecutable.getExecutableFileProducer().get());
            DefaultLinkageDetails linkageDetails = new DefaultLinkageDetails(linkTaskModel, componentWithExecutable.getExecutableFile().get().getAsFile(), args(linkTask.getLinkerArgs().get()));
            binaries.add(new DefaultCppExecutableModel(binary.getName(), cppBinary.getIdentity().getName(), binary.getBaseName().get(), compilationDetails, linkageDetails));
        } else if (binary instanceof CppSharedLibrary) {
            CppSharedLibrary sharedLibrary = (CppSharedLibrary) binary;
            LinkSharedLibrary linkTask = sharedLibrary.getLinkTask().get();
            LaunchableGradleTask linkTaskModel = ToolingModelBuilderSupport.buildFromTask(new LaunchableGradleTask(), projectIdentifier, sharedLibrary.getLinkFileProducer().get());
            DefaultLinkageDetails linkageDetails = new DefaultLinkageDetails(linkTaskModel, sharedLibrary.getLinkFile().get().getAsFile(), args(linkTask.getLinkerArgs().get()));
            binaries.add(new DefaultCppSharedLibraryModel(binary.getName(), cppBinary.getIdentity().getName(), binary.getBaseName().get(), compilationDetails, linkageDetails));
        } else if (binary instanceof CppStaticLibrary) {
            CppStaticLibrary staticLibrary = (CppStaticLibrary) binary;
            LaunchableGradleTask createTaskModel = ToolingModelBuilderSupport.buildFromTask(new LaunchableGradleTask(), projectIdentifier, staticLibrary.getLinkFileProducer().get());
            DefaultLinkageDetails linkageDetails = new DefaultLinkageDetails(createTaskModel, staticLibrary.getLinkFile().get().getAsFile(), Collections.<String>emptyList());
            binaries.add(new DefaultCppStaticLibraryModel(binary.getName(), cppBinary.getIdentity().getName(), binary.getBaseName().get(), compilationDetails, linkageDetails));
        }
    }
    return binaries;
}
Also used : ArrayList(java.util.ArrayList) LinkSharedLibrary(org.gradle.nativeplatform.tasks.LinkSharedLibrary) CppStaticLibrary(org.gradle.language.cpp.CppStaticLibrary) CommandLineToolSearchResult(org.gradle.nativeplatform.toolchain.internal.tools.CommandLineToolSearchResult) CppSharedLibrary(org.gradle.language.cpp.CppSharedLibrary) ComponentWithExecutable(org.gradle.language.nativeplatform.ComponentWithExecutable) DefaultCppBinary(org.gradle.language.cpp.internal.DefaultCppBinary) CppBinary(org.gradle.language.cpp.CppBinary) CppExecutable(org.gradle.language.cpp.CppExecutable) DefaultCppBinary(org.gradle.language.cpp.internal.DefaultCppBinary) LaunchableGradleTask(org.gradle.plugins.ide.internal.tooling.model.LaunchableGradleTask) LinkExecutable(org.gradle.nativeplatform.tasks.LinkExecutable) PlatformToolProvider(org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider) CppCompile(org.gradle.language.cpp.tasks.CppCompile) File(java.io.File) CppTestExecutable(org.gradle.nativeplatform.test.cpp.CppTestExecutable)

Aggregations

CppTestExecutable (org.gradle.nativeplatform.test.cpp.CppTestExecutable)3 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ProviderFactory (org.gradle.api.provider.ProviderFactory)1 TaskContainer (org.gradle.api.tasks.TaskContainer)1 CppBinary (org.gradle.language.cpp.CppBinary)1 CppComponent (org.gradle.language.cpp.CppComponent)1 CppExecutable (org.gradle.language.cpp.CppExecutable)1 CppPlatform (org.gradle.language.cpp.CppPlatform)1 CppSharedLibrary (org.gradle.language.cpp.CppSharedLibrary)1 CppStaticLibrary (org.gradle.language.cpp.CppStaticLibrary)1 ProductionCppComponent (org.gradle.language.cpp.ProductionCppComponent)1 DefaultCppBinary (org.gradle.language.cpp.internal.DefaultCppBinary)1 DefaultCppPlatform (org.gradle.language.cpp.internal.DefaultCppPlatform)1 CppCompile (org.gradle.language.cpp.tasks.CppCompile)1 ComponentWithExecutable (org.gradle.language.nativeplatform.ComponentWithExecutable)1 Names (org.gradle.language.nativeplatform.internal.Names)1 ToolChainSelector (org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector)1 TargetMachine (org.gradle.nativeplatform.TargetMachine)1