Search in sources :

Example 1 with DefaultCppPlatform

use of org.gradle.language.cpp.internal.DefaultCppPlatform in project gradle by gradle.

the class DefaultToolChainSelector method select.

@Override
public <T extends NativePlatform> Result<T> select(Class<T> platformType) {
    DefaultNativePlatform targetMachine = host;
    // TODO - push all this stuff down to the tool chain and let it create the specific platform and provider
    NativeLanguage sourceLanguage = platformType == SwiftPlatform.class ? NativeLanguage.SWIFT : NativeLanguage.CPP;
    NativeToolChainRegistryInternal registry = modelRegistry.realize("toolChains", NativeToolChainRegistryInternal.class);
    NativeToolChainInternal toolChain = registry.getForPlatform(sourceLanguage, targetMachine);
    // TODO - don't select again here, as the selection is already performed to select the toolchain
    PlatformToolProvider toolProvider = toolChain.select(sourceLanguage, targetMachine);
    if (!toolProvider.isAvailable() && targetMachine.getOperatingSystem().isWindows() && targetMachine.getArchitecture().isAmd64()) {
        // Try building x86 on Windows. Don't do this for other operating systems (yet)
        DefaultNativePlatform x86platformRequest = targetMachine.withArchitecture(Architectures.of(Architectures.X86));
        NativeToolChainInternal x86ToolChain = registry.getForPlatform(sourceLanguage, x86platformRequest);
        // TODO - don't select again here, as the selection is already performed to select the toolchain
        PlatformToolProvider x86ToolProvider = x86ToolChain.select(sourceLanguage, x86platformRequest);
        if (x86ToolProvider.isAvailable()) {
            targetMachine = x86platformRequest;
            toolChain = x86ToolChain;
            toolProvider = x86ToolProvider;
        }
    }
    // TODO - use a better name for the platforms, rather than "current"
    T targetPlatform = null;
    if (CppPlatform.class.isAssignableFrom(platformType)) {
        targetPlatform = platformType.cast(new DefaultCppPlatform("host", targetMachine));
    } else if (SwiftPlatform.class.isAssignableFrom(platformType)) {
        targetPlatform = platformType.cast(new DefaultSwiftPlatform("host", targetMachine));
    }
    return new DefaultResult<T>(toolChain, toolProvider, targetPlatform);
}
Also used : NativeToolChainInternal(org.gradle.nativeplatform.toolchain.internal.NativeToolChainInternal) NativeLanguage(org.gradle.nativeplatform.toolchain.internal.NativeLanguage) DefaultSwiftPlatform(org.gradle.language.swift.internal.DefaultSwiftPlatform) NativeToolChainRegistryInternal(org.gradle.nativeplatform.toolchain.internal.NativeToolChainRegistryInternal) PlatformToolProvider(org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider) DefaultNativePlatform(org.gradle.nativeplatform.platform.internal.DefaultNativePlatform) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) SwiftPlatform(org.gradle.language.swift.SwiftPlatform) DefaultSwiftPlatform(org.gradle.language.swift.internal.DefaultSwiftPlatform)

Example 2 with DefaultCppPlatform

use of org.gradle.language.cpp.internal.DefaultCppPlatform 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 DefaultCppPlatform

use of org.gradle.language.cpp.internal.DefaultCppPlatform in project gradle by gradle.

the class CppApplicationPlugin method apply.

@Override
public void apply(final Project project) {
    project.getPluginManager().apply(CppBasePlugin.class);
    final ObjectFactory objectFactory = project.getObjects();
    final ProviderFactory providers = project.getProviders();
    // 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().convention(project.getName());
    application.getTargetMachines().convention(useHostAsDefaultTargetMachine(targetMachineFactory));
    application.getDevelopmentBinary().convention(project.provider(() -> {
        // Prefer the host architecture, if present, else use the first architecture specified
        return application.getBinaries().get().stream().filter(CppExecutable.class::isInstance).map(CppExecutable.class::cast).filter(binary -> !binary.isOptimized() && Architectures.forInput(binary.getTargetMachine().getArchitecture().getName()).equals(DefaultNativePlatform.host().getArchitecture())).findFirst().orElse(application.getBinaries().get().stream().filter(CppExecutable.class::isInstance).map(CppExecutable.class::cast).filter(binary -> !binary.isOptimized()).findFirst().orElse(null));
    }));
    application.getBinaries().whenElementKnown(binary -> {
        application.getMainPublication().addVariant(binary);
    });
    project.afterEvaluate(p -> {
        // TODO: make build type configurable for components
        Dimensions.applicationVariants(application.getBaseName(), application.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()));
                application.addExecutable(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
            } else {
                // Known, but not buildable
                application.getMainPublication().addVariant(variantIdentity);
            }
        });
        // Configure the binaries
        application.getBinaries().realizeNow();
    });
}
Also used : CppApplication(org.gradle.language.cpp.CppApplication) ImmutableAttributesFactory(org.gradle.api.internal.attributes.ImmutableAttributesFactory) NativeComponentFactory(org.gradle.language.internal.NativeComponentFactory) Dimensions.useHostAsDefaultTargetMachine(org.gradle.language.nativeplatform.internal.Dimensions.useHostAsDefaultTargetMachine) Project(org.gradle.api.Project) Dimensions(org.gradle.language.nativeplatform.internal.Dimensions) CppExecutable(org.gradle.language.cpp.CppExecutable) ToolChainSelector(org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector) Dimensions.tryToBuildOnHost(org.gradle.language.nativeplatform.internal.Dimensions.tryToBuildOnHost) CppPlatform(org.gradle.language.cpp.CppPlatform) Inject(javax.inject.Inject) ProviderFactory(org.gradle.api.provider.ProviderFactory) DefaultNativePlatform(org.gradle.nativeplatform.platform.internal.DefaultNativePlatform) ObjectFactory(org.gradle.api.model.ObjectFactory) Architectures(org.gradle.nativeplatform.platform.internal.Architectures) TargetMachineFactory(org.gradle.nativeplatform.TargetMachineFactory) DefaultCppApplication(org.gradle.language.cpp.internal.DefaultCppApplication) Plugin(org.gradle.api.Plugin) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) CppPlatform(org.gradle.language.cpp.CppPlatform) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) ObjectFactory(org.gradle.api.model.ObjectFactory) ProviderFactory(org.gradle.api.provider.ProviderFactory) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) CppExecutable(org.gradle.language.cpp.CppExecutable) ToolChainSelector(org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector) DefaultCppApplication(org.gradle.language.cpp.internal.DefaultCppApplication)

Example 4 with DefaultCppPlatform

use of org.gradle.language.cpp.internal.DefaultCppPlatform in project gradle by gradle.

the class DefaultToolChainSelector method select.

public Result<CppPlatform> select(CppPlatform requestPlatform) {
    DefaultNativePlatform targetNativePlatform = newNativePlatform(requestPlatform.getTargetMachine());
    // TODO - push all this stuff down to the tool chain and let it create the specific platform and provider
    NativeLanguage sourceLanguage = NativeLanguage.CPP;
    NativeToolChainInternal toolChain = getToolChain(sourceLanguage, targetNativePlatform);
    // TODO - don't select again here, as the selection is already performed to select the toolchain
    PlatformToolProvider toolProvider = toolChain.select(sourceLanguage, targetNativePlatform);
    CppPlatform targetPlatform = new DefaultCppPlatform(requestPlatform.getTargetMachine(), targetNativePlatform);
    return new DefaultResult<CppPlatform>(toolChain, toolProvider, targetPlatform);
}
Also used : NativeToolChainInternal(org.gradle.nativeplatform.toolchain.internal.NativeToolChainInternal) CppPlatform(org.gradle.language.cpp.CppPlatform) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform) NativeLanguage(org.gradle.nativeplatform.toolchain.internal.NativeLanguage) PlatformToolProvider(org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider) DefaultNativePlatform(org.gradle.nativeplatform.platform.internal.DefaultNativePlatform) DefaultCppPlatform(org.gradle.language.cpp.internal.DefaultCppPlatform)

Example 5 with DefaultCppPlatform

use of org.gradle.language.cpp.internal.DefaultCppPlatform 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

DefaultCppPlatform (org.gradle.language.cpp.internal.DefaultCppPlatform)5 CppPlatform (org.gradle.language.cpp.CppPlatform)4 DefaultNativePlatform (org.gradle.nativeplatform.platform.internal.DefaultNativePlatform)4 ProviderFactory (org.gradle.api.provider.ProviderFactory)3 ToolChainSelector (org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector)3 Callable (java.util.concurrent.Callable)2 ObjectFactory (org.gradle.api.model.ObjectFactory)2 TaskContainer (org.gradle.api.tasks.TaskContainer)2 NativeLanguage (org.gradle.nativeplatform.toolchain.internal.NativeLanguage)2 NativeToolChainInternal (org.gradle.nativeplatform.toolchain.internal.NativeToolChainInternal)2 PlatformToolProvider (org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider)2 File (java.io.File)1 Inject (javax.inject.Inject)1 Plugin (org.gradle.api.Plugin)1 Project (org.gradle.api.Project)1 Configuration (org.gradle.api.artifacts.Configuration)1 LazyPublishArtifact (org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact)1 ImmutableAttributesFactory (org.gradle.api.internal.attributes.ImmutableAttributesFactory)1 ProjectInternal (org.gradle.api.internal.project.ProjectInternal)1 Zip (org.gradle.api.tasks.bundling.Zip)1