Search in sources :

Example 1 with CppPlatform

use of org.gradle.language.cpp.CppPlatform in project gradle by gradle.

the class CppLibraryPlugin method apply.

@Override
public void apply(final ProjectInternal project) {
    project.getPluginManager().apply(CppBasePlugin.class);
    final TaskContainer tasks = project.getTasks();
    final ObjectFactory objectFactory = project.getObjects();
    final ProviderFactory providers = project.getProviders();
    // Add the library and extension
    final DefaultCppLibrary library = componentFactory.newInstance(CppLibrary.class, DefaultCppLibrary.class, "main");
    project.getExtensions().add(CppLibrary.class, "library", library);
    project.getComponents().add(library);
    // Configure the component
    library.getBaseName().set(project.getName());
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(final Project project) {
            library.getOperatingSystems().lockNow();
            Set<OperatingSystemFamily> operatingSystemFamilies = library.getOperatingSystems().get();
            if (operatingSystemFamilies.isEmpty()) {
                throw new IllegalArgumentException("An operating system needs to be specified for the library.");
            }
            library.getLinkage().lockNow();
            Set<Linkage> linkages = library.getLinkage().get();
            if (linkages.isEmpty()) {
                throw new IllegalArgumentException("A linkage needs to be specified for the library.");
            }
            Usage runtimeUsage = objectFactory.named(Usage.class, Usage.NATIVE_RUNTIME);
            Usage linkUsage = objectFactory.named(Usage.class, Usage.NATIVE_LINK);
            for (BuildType buildType : BuildType.DEFAULT_BUILD_TYPES) {
                for (OperatingSystemFamily operatingSystem : operatingSystemFamilies) {
                    for (Linkage linkage : linkages) {
                        String operatingSystemSuffix = createDimensionSuffix(operatingSystem, operatingSystemFamilies);
                        String linkageSuffix = createDimensionSuffix(linkage, linkages);
                        String variantName = buildType.getName() + linkageSuffix + operatingSystemSuffix;
                        Provider<String> group = project.provider(new Callable<String>() {

                            @Override
                            public String call() throws Exception {
                                return project.getGroup().toString();
                            }
                        });
                        Provider<String> version = project.provider(new Callable<String>() {

                            @Override
                            public String call() throws Exception {
                                return project.getVersion().toString();
                            }
                        });
                        AttributeContainer runtimeAttributes = attributesFactory.mutable();
                        runtimeAttributes.attribute(Usage.USAGE_ATTRIBUTE, runtimeUsage);
                        runtimeAttributes.attribute(DEBUGGABLE_ATTRIBUTE, buildType.isDebuggable());
                        runtimeAttributes.attribute(OPTIMIZED_ATTRIBUTE, buildType.isOptimized());
                        runtimeAttributes.attribute(LINKAGE_ATTRIBUTE, linkage);
                        runtimeAttributes.attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, operatingSystem);
                        AttributeContainer linkAttributes = attributesFactory.mutable();
                        linkAttributes.attribute(Usage.USAGE_ATTRIBUTE, linkUsage);
                        linkAttributes.attribute(DEBUGGABLE_ATTRIBUTE, buildType.isDebuggable());
                        linkAttributes.attribute(OPTIMIZED_ATTRIBUTE, buildType.isOptimized());
                        linkAttributes.attribute(LINKAGE_ATTRIBUTE, linkage);
                        linkAttributes.attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, operatingSystem);
                        NativeVariantIdentity variantIdentity = new NativeVariantIdentity(variantName, library.getBaseName(), group, version, buildType.isDebuggable(), buildType.isOptimized(), operatingSystem, new DefaultUsageContext(variantName + "Link", linkUsage, linkAttributes), new DefaultUsageContext(variantName + "Runtime", runtimeUsage, runtimeAttributes));
                        if (DefaultNativePlatform.getCurrentOperatingSystem().toFamilyName().equals(operatingSystem.getName())) {
                            ToolChainSelector.Result<CppPlatform> result = toolChainSelector.select(CppPlatform.class);
                            if (linkage == Linkage.SHARED) {
                                CppSharedLibrary sharedLibrary = library.addSharedLibrary(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                                library.getMainPublication().addVariant(sharedLibrary);
                                // Use the debug shared library as the development binary
                                if (buildType == BuildType.DEBUG) {
                                    library.getDevelopmentBinary().set(sharedLibrary);
                                }
                            } else {
                                CppStaticLibrary staticLibrary = library.addStaticLibrary(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                                library.getMainPublication().addVariant(staticLibrary);
                                if (!linkages.contains(Linkage.SHARED) && buildType == BuildType.DEBUG) {
                                    // Use the debug static library as the development binary
                                    library.getDevelopmentBinary().set(staticLibrary);
                                }
                            }
                        } else {
                            // Known, but not buildable
                            library.getMainPublication().addVariant(variantIdentity);
                        }
                    }
                }
            }
            final MainLibraryVariant mainVariant = library.getMainPublication();
            final Configuration apiElements = library.getApiElements();
            // TODO - deal with more than one header dir, e.g. generated public headers
            Provider<File> publicHeaders = providers.provider(new Callable<File>() {

                @Override
                public File call() throws Exception {
                    Set<File> files = library.getPublicHeaderDirs().getFiles();
                    if (files.size() != 1) {
                        throw new UnsupportedOperationException(String.format("The C++ library plugin currently requires exactly one public header directory, however there are %d directories configured: %s", files.size(), files));
                    }
                    return files.iterator().next();
                }
            });
            apiElements.getOutgoing().artifact(publicHeaders);
            project.getPluginManager().withPlugin("maven-publish", new Action<AppliedPlugin>() {

                @Override
                public void execute(AppliedPlugin appliedPlugin) {
                    final Zip headersZip = tasks.create("cppHeaders", Zip.class);
                    headersZip.from(library.getPublicHeaderFiles());
                    // TODO - should track changes to build directory
                    headersZip.setDestinationDir(new File(project.getBuildDir(), "headers"));
                    headersZip.setClassifier("cpp-api-headers");
                    headersZip.setArchiveName("cpp-api-headers.zip");
                    mainVariant.addArtifact(new ArchivePublishArtifact(headersZip));
                }
            });
            library.getBinaries().realizeNow();
        }
    });
}
Also used : Zip(org.gradle.api.tasks.bundling.Zip) OperatingSystemFamily(org.gradle.nativeplatform.OperatingSystemFamily) Action(org.gradle.api.Action) Set(java.util.Set) Configuration(org.gradle.api.artifacts.Configuration) CppStaticLibrary(org.gradle.language.cpp.CppStaticLibrary) AttributeContainer(org.gradle.api.attributes.AttributeContainer) Callable(java.util.concurrent.Callable) CppPlatform(org.gradle.language.cpp.CppPlatform) ObjectFactory(org.gradle.api.model.ObjectFactory) ProviderFactory(org.gradle.api.provider.ProviderFactory) DefaultUsageContext(org.gradle.language.cpp.internal.DefaultUsageContext) MainLibraryVariant(org.gradle.language.cpp.internal.MainLibraryVariant) CppSharedLibrary(org.gradle.language.cpp.CppSharedLibrary) Usage(org.gradle.api.attributes.Usage) Linkage(org.gradle.nativeplatform.Linkage) NativeVariantIdentity(org.gradle.language.cpp.internal.NativeVariantIdentity) Provider(org.gradle.api.provider.Provider) Project(org.gradle.api.Project) TaskContainer(org.gradle.api.tasks.TaskContainer) ArchivePublishArtifact(org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact) DefaultCppLibrary(org.gradle.language.cpp.internal.DefaultCppLibrary) AppliedPlugin(org.gradle.api.plugins.AppliedPlugin) File(java.io.File)

Example 2 with CppPlatform

use of org.gradle.language.cpp.CppPlatform in project gradle by gradle.

the class CppLibraryPlugin method apply.

@Override
public void apply(final Project project) {
    project.getPluginManager().apply(CppBasePlugin.class);
    final TaskContainer tasks = project.getTasks();
    final ObjectFactory objectFactory = project.getObjects();
    final ProviderFactory providers = project.getProviders();
    // Add the library and extension
    final DefaultCppLibrary library = componentFactory.newInstance(CppLibrary.class, DefaultCppLibrary.class, "main");
    project.getExtensions().add(CppLibrary.class, "library", library);
    project.getComponents().add(library);
    // Configure the component
    library.getBaseName().convention(project.getName());
    library.getTargetMachines().convention(useHostAsDefaultTargetMachine(targetMachineFactory));
    library.getDevelopmentBinary().convention(project.provider(new Callable<CppBinary>() {

        @Override
        public CppBinary call() throws Exception {
            return getDebugSharedHostStream().findFirst().orElse(getDebugStaticHostStream().findFirst().orElse(getDebugSharedStream().findFirst().orElse(getDebugStaticStream().findFirst().orElse(null))));
        }

        private Stream<CppBinary> getDebugStream() {
            return library.getBinaries().get().stream().filter(binary -> !binary.isOptimized());
        }

        private Stream<CppBinary> getDebugSharedStream() {
            return getDebugStream().filter(CppSharedLibrary.class::isInstance);
        }

        private Stream<CppBinary> getDebugSharedHostStream() {
            return getDebugSharedStream().filter(binary -> Architectures.forInput(binary.getTargetMachine().getArchitecture().getName()).equals(DefaultNativePlatform.host().getArchitecture()));
        }

        private Stream<CppBinary> getDebugStaticStream() {
            return getDebugStream().filter(CppStaticLibrary.class::isInstance);
        }

        private Stream<CppBinary> getDebugStaticHostStream() {
            return getDebugStaticStream().filter(binary -> Architectures.forInput(binary.getTargetMachine().getArchitecture().getName()).equals(DefaultNativePlatform.host().getArchitecture()));
        }
    }));
    library.getBinaries().whenElementKnown(binary -> {
        library.getMainPublication().addVariant(binary);
    });
    project.afterEvaluate(p -> {
        // TODO: make build type configurable for components
        Dimensions.libraryVariants(library.getBaseName(), library.getLinkage(), library.getTargetMachines(), 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()));
                if (variantIdentity.getLinkage().equals(Linkage.SHARED)) {
                    library.addSharedLibrary(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                } else {
                    library.addStaticLibrary(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                }
            } else {
                // Known, but not buildable
                library.getMainPublication().addVariant(variantIdentity);
            }
        });
        // TODO - deal with more than one header dir, e.g. generated public headers
        final Configuration apiElements = library.getApiElements();
        Provider<File> publicHeaders = providers.provider(() -> {
            Set<File> files = library.getPublicHeaderDirs().getFiles();
            if (files.size() != 1) {
                throw new UnsupportedOperationException(String.format("The C++ library plugin currently requires exactly one public header directory, however there are %d directories configured: %s", files.size(), files));
            }
            return files.iterator().next();
        });
        apiElements.getOutgoing().artifact(publicHeaders);
        project.getPluginManager().withPlugin("maven-publish", appliedPlugin -> {
            final TaskProvider<Zip> headersZip = tasks.register("cppHeaders", Zip.class, task -> {
                task.from(library.getPublicHeaderFiles());
                task.getDestinationDirectory().set(project.getLayout().getBuildDirectory().dir("headers"));
                task.getArchiveClassifier().set("cpp-api-headers");
                task.getArchiveFileName().set("cpp-api-headers.zip");
            });
            library.getMainPublication().addArtifact(new LazyPublishArtifact(headersZip, ((ProjectInternal) project).getFileResolver()));
        });
        library.getBinaries().realizeNow();
    });
}
Also used : Zip(org.gradle.api.tasks.bundling.Zip) Configuration(org.gradle.api.artifacts.Configuration) CppBinary(org.gradle.language.cpp.CppBinary) ProjectInternal(org.gradle.api.internal.project.ProjectInternal) CppStaticLibrary(org.gradle.language.cpp.CppStaticLibrary) Callable(java.util.concurrent.Callable) CppPlatform(org.gradle.language.cpp.CppPlatform) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) TaskContainer(org.gradle.api.tasks.TaskContainer) LazyPublishArtifact(org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact) ObjectFactory(org.gradle.api.model.ObjectFactory) ProviderFactory(org.gradle.api.provider.ProviderFactory) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) DefaultCppLibrary(org.gradle.language.cpp.internal.DefaultCppLibrary) ToolChainSelector(org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector) File(java.io.File) CppSharedLibrary(org.gradle.language.cpp.CppSharedLibrary)

Example 3 with CppPlatform

use of org.gradle.language.cpp.CppPlatform in project gradle by gradle.

the class CppUnitTestPlugin method apply.

@Override
public void apply(final ProjectInternal project) {
    project.getPluginManager().apply(CppBasePlugin.class);
    project.getPluginManager().apply(NativeTestingBasePlugin.class);
    // 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().set(project.getName() + "Test");
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(final Project project) {
            testComponent.getOperatingSystems().lockNow();
            Set<OperatingSystemFamily> operatingSystemFamilies = testComponent.getOperatingSystems().get();
            if (operatingSystemFamilies.isEmpty()) {
                throw new IllegalArgumentException("An operating system needs to be specified for the unit test.");
            }
            boolean hasHostOperatingSystem = CollectionUtils.any(operatingSystemFamilies, new Spec<OperatingSystemFamily>() {

                @Override
                public boolean isSatisfiedBy(OperatingSystemFamily element) {
                    return DefaultNativePlatform.getCurrentOperatingSystem().toFamilyName().equals(element.getName());
                }
            });
            if (hasHostOperatingSystem) {
                String operatingSystemSuffix = "";
                OperatingSystemFamily operatingSystem = objectFactory.named(OperatingSystemFamily.class, DefaultNativePlatform.getCurrentOperatingSystem().toFamilyName());
                Usage runtimeUsage = objectFactory.named(Usage.class, Usage.NATIVE_RUNTIME);
                Provider<String> group = project.provider(new Callable<String>() {

                    @Override
                    public String call() throws Exception {
                        return project.getGroup().toString();
                    }
                });
                Provider<String> version = project.provider(new Callable<String>() {

                    @Override
                    public String call() throws Exception {
                        return project.getVersion().toString();
                    }
                });
                AttributeContainer attributesDebug = attributesFactory.mutable();
                attributesDebug.attribute(Usage.USAGE_ATTRIBUTE, runtimeUsage);
                attributesDebug.attribute(DEBUGGABLE_ATTRIBUTE, true);
                attributesDebug.attribute(OPTIMIZED_ATTRIBUTE, false);
                // TODO: Fix this naming convention to follow C++ executable/library
                NativeVariantIdentity debugVariant = new NativeVariantIdentity("debug" + operatingSystemSuffix, testComponent.getBaseName(), group, version, true, false, operatingSystem, null, new DefaultUsageContext("debug" + operatingSystemSuffix + "Runtime", runtimeUsage, attributesDebug));
                ToolChainSelector.Result<CppPlatform> result = toolChainSelector.select(CppPlatform.class);
                testComponent.addExecutable("executable", debugVariant, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                // TODO: Publishing for test executable?
                final TaskContainer tasks = project.getTasks();
                final ProductionCppComponent mainComponent = project.getComponents().withType(ProductionCppComponent.class).findByName("main");
                if (mainComponent != null) {
                    testComponent.getTestedComponent().set(mainComponent);
                }
                testComponent.getBinaries().whenElementKnown(DefaultCppTestExecutable.class, new Action<DefaultCppTestExecutable>() {

                    @Override
                    public void execute(final DefaultCppTestExecutable executable) {
                        if (mainComponent != null) {
                            // TODO: This should be modeled as a kind of dependency vs wiring binaries together directly.
                            mainComponent.getBinaries().whenElementFinalized(new Action<CppBinary>() {

                                @Override
                                public void execute(CppBinary cppBinary) {
                                    if (cppBinary == mainComponent.getDevelopmentBinary().get()) {
                                        AbstractLinkTask linkTest = executable.getLinkTask().get();
                                        linkTest.source(cppBinary.getObjects());
                                    }
                                }
                            });
                        }
                        // TODO: Replace with native test task
                        final RunTestExecutable testTask = tasks.create(executable.getNames().getTaskName("run"), RunTestExecutable.class);
                        testTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
                        testTask.setDescription("Executes C++ unit tests.");
                        final InstallExecutable installTask = executable.getInstallTask().get();
                        testTask.onlyIf(new Spec<Task>() {

                            @Override
                            public boolean isSatisfiedBy(Task element) {
                                return executable.getInstallDirectory().get().getAsFile().exists();
                            }
                        });
                        testTask.setExecutable(installTask.getRunScriptFile().get().getAsFile());
                        testTask.dependsOn(testComponent.getTestBinary().get().getInstallDirectory());
                        // TODO: Honor changes to build directory
                        testTask.setOutputDir(project.getLayout().getBuildDirectory().dir("test-results/" + executable.getNames().getDirName()).get().getAsFile());
                        executable.getRunTask().set(testTask);
                    }
                });
            }
            testComponent.getBinaries().realizeNow();
        }
    });
}
Also used : OperatingSystemFamily(org.gradle.nativeplatform.OperatingSystemFamily) Action(org.gradle.api.Action) Task(org.gradle.api.Task) AbstractLinkTask(org.gradle.nativeplatform.tasks.AbstractLinkTask) Set(java.util.Set) AttributeContainer(org.gradle.api.attributes.AttributeContainer) Callable(java.util.concurrent.Callable) CppPlatform(org.gradle.language.cpp.CppPlatform) DefaultCppTestExecutable(org.gradle.nativeplatform.test.cpp.internal.DefaultCppTestExecutable) InstallExecutable(org.gradle.nativeplatform.tasks.InstallExecutable) DefaultUsageContext(org.gradle.language.cpp.internal.DefaultUsageContext) Usage(org.gradle.api.attributes.Usage) CppBinary(org.gradle.language.cpp.CppBinary) NativeVariantIdentity(org.gradle.language.cpp.internal.NativeVariantIdentity) Provider(org.gradle.api.provider.Provider) ProductionCppComponent(org.gradle.language.cpp.ProductionCppComponent) RunTestExecutable(org.gradle.nativeplatform.test.tasks.RunTestExecutable) Project(org.gradle.api.Project) TaskContainer(org.gradle.api.tasks.TaskContainer) Spec(org.gradle.api.specs.Spec) AbstractLinkTask(org.gradle.nativeplatform.tasks.AbstractLinkTask) DefaultCppTestSuite(org.gradle.nativeplatform.test.cpp.internal.DefaultCppTestSuite)

Example 4 with CppPlatform

use of org.gradle.language.cpp.CppPlatform in project gradle by gradle.

the class CppApplicationPlugin method apply.

@Override
public void apply(final ProjectInternal project) {
    project.getPluginManager().apply(CppBasePlugin.class);
    final ObjectFactory objectFactory = project.getObjects();
    // Add the application and extension
    final DefaultCppApplication application = componentFactory.newInstance(CppApplication.class, DefaultCppApplication.class, "main");
    project.getExtensions().add(CppApplication.class, "application", application);
    project.getComponents().add(application);
    // Configure the component
    application.getBaseName().set(project.getName());
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(final Project project) {
            application.getOperatingSystems().lockNow();
            Set<OperatingSystemFamily> operatingSystemFamilies = application.getOperatingSystems().get();
            if (operatingSystemFamilies.isEmpty()) {
                throw new IllegalArgumentException("An operating system needs to be specified for the application.");
            }
            Usage runtimeUsage = objectFactory.named(Usage.class, Usage.NATIVE_RUNTIME);
            for (BuildType buildType : BuildType.DEFAULT_BUILD_TYPES) {
                for (OperatingSystemFamily operatingSystem : operatingSystemFamilies) {
                    String operatingSystemSuffix = createDimensionSuffix(operatingSystem, operatingSystemFamilies);
                    String variantName = buildType.getName() + operatingSystemSuffix;
                    Provider<String> group = project.provider(new Callable<String>() {

                        @Override
                        public String call() throws Exception {
                            return project.getGroup().toString();
                        }
                    });
                    Provider<String> version = project.provider(new Callable<String>() {

                        @Override
                        public String call() throws Exception {
                            return project.getVersion().toString();
                        }
                    });
                    AttributeContainer runtimeAttributes = attributesFactory.mutable();
                    runtimeAttributes.attribute(Usage.USAGE_ATTRIBUTE, runtimeUsage);
                    runtimeAttributes.attribute(DEBUGGABLE_ATTRIBUTE, buildType.isDebuggable());
                    runtimeAttributes.attribute(OPTIMIZED_ATTRIBUTE, buildType.isOptimized());
                    runtimeAttributes.attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, operatingSystem);
                    NativeVariantIdentity variantIdentity = new NativeVariantIdentity(variantName, application.getBaseName(), group, version, buildType.isDebuggable(), buildType.isOptimized(), operatingSystem, null, new DefaultUsageContext(variantName + "Runtime", runtimeUsage, runtimeAttributes));
                    if (DefaultNativePlatform.getCurrentOperatingSystem().toFamilyName().equals(operatingSystem.getName())) {
                        ToolChainSelector.Result<CppPlatform> result = toolChainSelector.select(CppPlatform.class);
                        CppExecutable executable = application.addExecutable(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                        // Use the debug variant as the development binary
                        if (buildType == BuildType.DEBUG) {
                            application.getDevelopmentBinary().set(executable);
                        }
                        application.getMainPublication().addVariant(executable);
                    } else {
                        // Known, but not buildable
                        application.getMainPublication().addVariant(variantIdentity);
                    }
                }
            }
            application.getBinaries().realizeNow();
        }
    });
}
Also used : OperatingSystemFamily(org.gradle.nativeplatform.OperatingSystemFamily) Usage(org.gradle.api.attributes.Usage) Set(java.util.Set) AttributeContainer(org.gradle.api.attributes.AttributeContainer) CppExecutable(org.gradle.language.cpp.CppExecutable) NativeVariantIdentity(org.gradle.language.cpp.internal.NativeVariantIdentity) Callable(java.util.concurrent.Callable) DefaultCppApplication(org.gradle.language.cpp.internal.DefaultCppApplication) Provider(org.gradle.api.provider.Provider) Project(org.gradle.api.Project) CppPlatform(org.gradle.language.cpp.CppPlatform) ObjectFactory(org.gradle.api.model.ObjectFactory) BuildType(org.gradle.language.nativeplatform.internal.BuildType) DefaultUsageContext(org.gradle.language.cpp.internal.DefaultUsageContext)

Example 5 with CppPlatform

use of org.gradle.language.cpp.CppPlatform 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)

Aggregations

CppPlatform (org.gradle.language.cpp.CppPlatform)7 Callable (java.util.concurrent.Callable)5 Project (org.gradle.api.Project)4 ObjectFactory (org.gradle.api.model.ObjectFactory)4 ProviderFactory (org.gradle.api.provider.ProviderFactory)4 TaskContainer (org.gradle.api.tasks.TaskContainer)4 DefaultCppPlatform (org.gradle.language.cpp.internal.DefaultCppPlatform)4 Set (java.util.Set)3 AttributeContainer (org.gradle.api.attributes.AttributeContainer)3 Usage (org.gradle.api.attributes.Usage)3 Provider (org.gradle.api.provider.Provider)3 DefaultUsageContext (org.gradle.language.cpp.internal.DefaultUsageContext)3 NativeVariantIdentity (org.gradle.language.cpp.internal.NativeVariantIdentity)3 ToolChainSelector (org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector)3 OperatingSystemFamily (org.gradle.nativeplatform.OperatingSystemFamily)3 DefaultNativePlatform (org.gradle.nativeplatform.platform.internal.DefaultNativePlatform)3 File (java.io.File)2 Action (org.gradle.api.Action)2 Configuration (org.gradle.api.artifacts.Configuration)2 Zip (org.gradle.api.tasks.bundling.Zip)2