Search in sources :

Example 1 with CppBinary

use of org.gradle.language.cpp.CppBinary 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 2 with CppBinary

use of org.gradle.language.cpp.CppBinary 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 3 with CppBinary

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

CppBinary (org.gradle.language.cpp.CppBinary)3 File (java.io.File)2 Callable (java.util.concurrent.Callable)2 TaskContainer (org.gradle.api.tasks.TaskContainer)2 CppPlatform (org.gradle.language.cpp.CppPlatform)2 CppSharedLibrary (org.gradle.language.cpp.CppSharedLibrary)2 CppStaticLibrary (org.gradle.language.cpp.CppStaticLibrary)2 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Action (org.gradle.api.Action)1 Project (org.gradle.api.Project)1 Task (org.gradle.api.Task)1 Configuration (org.gradle.api.artifacts.Configuration)1 AttributeContainer (org.gradle.api.attributes.AttributeContainer)1 Usage (org.gradle.api.attributes.Usage)1 LazyPublishArtifact (org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact)1 ProjectInternal (org.gradle.api.internal.project.ProjectInternal)1 ObjectFactory (org.gradle.api.model.ObjectFactory)1 Provider (org.gradle.api.provider.Provider)1 ProviderFactory (org.gradle.api.provider.ProviderFactory)1