Search in sources :

Example 1 with DefaultSwiftSharedLibrary

use of org.gradle.language.swift.internal.DefaultSwiftSharedLibrary in project gradle by gradle.

the class SwiftLibraryPlugin method apply.

@Override
public void apply(final Project project) {
    project.getPluginManager().apply(SwiftBasePlugin.class);
    final ConfigurationContainer configurations = project.getConfigurations();
    final ObjectFactory objectFactory = project.getObjects();
    final ProviderFactory providers = project.getProviders();
    final DefaultSwiftLibrary library = componentFactory.newInstance(SwiftLibrary.class, DefaultSwiftLibrary.class, "main");
    project.getExtensions().add(SwiftLibrary.class, "library", library);
    project.getComponents().add(library);
    // Setup component
    final Property<String> module = library.getModule();
    module.set(GUtil.toCamelCase(project.getName()));
    library.getTargetMachines().convention(useHostAsDefaultTargetMachine(targetMachineFactory));
    library.getDevelopmentBinary().convention(project.provider(new Callable<SwiftBinary>() {

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

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

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

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

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

        private Stream<SwiftBinary> getDebugStaticHostStream() {
            return getDebugStaticStream().filter(binary -> Architectures.forInput(binary.getTargetMachine().getArchitecture().getName()).equals(DefaultNativePlatform.host().getArchitecture()));
        }
    }));
    project.afterEvaluate(p -> {
        // TODO: make build type configurable for components
        Dimensions.libraryVariants(library.getModule(), library.getLinkage(), library.getTargetMachines(), objectFactory, attributesFactory, providers.provider(() -> project.getGroup().toString()), providers.provider(() -> project.getVersion().toString()), variantIdentity -> {
            if (tryToBuildOnHost(variantIdentity)) {
                library.getSourceCompatibility().finalizeValue();
                ToolChainSelector.Result<SwiftPlatform> result = toolChainSelector.select(SwiftPlatform.class, new DefaultSwiftPlatform(variantIdentity.getTargetMachine(), library.getSourceCompatibility().getOrNull()));
                if (variantIdentity.getLinkage().equals(Linkage.SHARED)) {
                    library.addSharedLibrary(variantIdentity, variantIdentity.isDebuggable() && !variantIdentity.isOptimized(), result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                } else {
                    library.addStaticLibrary(variantIdentity, variantIdentity.isDebuggable() && !variantIdentity.isOptimized(), result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
                }
            }
        });
        library.getBinaries().whenElementKnown(SwiftSharedLibrary.class, sharedLibrary -> {
            Names names = ((ComponentWithNames) sharedLibrary).getNames();
            Configuration apiElements = configurations.create(names.withSuffix("SwiftApiElements"));
            // TODO This should actually extend from the api dependencies, but since Swift currently
            // requires all dependencies to be treated like api dependencies (with transitivity) we just
            // use the implementation dependencies here.  See https://bugs.swift.org/browse/SR-1393.
            apiElements.extendsFrom(((DefaultSwiftSharedLibrary) sharedLibrary).getImplementationDependencies());
            apiElements.setCanBeResolved(false);
            apiElements.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.SWIFT_API));
            apiElements.getAttributes().attribute(LINKAGE_ATTRIBUTE, Linkage.SHARED);
            apiElements.getAttributes().attribute(DEBUGGABLE_ATTRIBUTE, sharedLibrary.isDebuggable());
            apiElements.getAttributes().attribute(OPTIMIZED_ATTRIBUTE, sharedLibrary.isOptimized());
            apiElements.getAttributes().attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, sharedLibrary.getTargetMachine().getOperatingSystemFamily());
            apiElements.getOutgoing().artifact(sharedLibrary.getModuleFile());
        });
        library.getBinaries().whenElementKnown(SwiftStaticLibrary.class, staticLibrary -> {
            Names names = ((ComponentWithNames) staticLibrary).getNames();
            Configuration apiElements = configurations.create(names.withSuffix("SwiftApiElements"));
            // TODO This should actually extend from the api dependencies, but since Swift currently
            // requires all dependencies to be treated like api dependencies (with transitivity) we just
            // use the implementation dependencies here.  See https://bugs.swift.org/browse/SR-1393.
            apiElements.extendsFrom(((DefaultSwiftStaticLibrary) staticLibrary).getImplementationDependencies());
            apiElements.setCanBeResolved(false);
            apiElements.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.SWIFT_API));
            apiElements.getAttributes().attribute(LINKAGE_ATTRIBUTE, Linkage.STATIC);
            apiElements.getAttributes().attribute(DEBUGGABLE_ATTRIBUTE, staticLibrary.isDebuggable());
            apiElements.getAttributes().attribute(OPTIMIZED_ATTRIBUTE, staticLibrary.isOptimized());
            apiElements.getAttributes().attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, staticLibrary.getTargetMachine().getOperatingSystemFamily());
            apiElements.getOutgoing().artifact(staticLibrary.getModuleFile());
        });
        library.getBinaries().realizeNow();
    });
}
Also used : Usage(org.gradle.api.attributes.Usage) DefaultSwiftPlatform(org.gradle.language.swift.internal.DefaultSwiftPlatform) Configuration(org.gradle.api.artifacts.Configuration) DefaultSwiftLibrary(org.gradle.language.swift.internal.DefaultSwiftLibrary) SwiftStaticLibrary(org.gradle.language.swift.SwiftStaticLibrary) DefaultSwiftStaticLibrary(org.gradle.language.swift.internal.DefaultSwiftStaticLibrary) ComponentWithNames(org.gradle.language.nativeplatform.internal.ComponentWithNames) Callable(java.util.concurrent.Callable) ComponentWithNames(org.gradle.language.nativeplatform.internal.ComponentWithNames) Names(org.gradle.language.nativeplatform.internal.Names) ObjectFactory(org.gradle.api.model.ObjectFactory) SwiftBinary(org.gradle.language.swift.SwiftBinary) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) ProviderFactory(org.gradle.api.provider.ProviderFactory) SwiftSharedLibrary(org.gradle.language.swift.SwiftSharedLibrary) DefaultSwiftSharedLibrary(org.gradle.language.swift.internal.DefaultSwiftSharedLibrary) ToolChainSelector(org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector) SwiftPlatform(org.gradle.language.swift.SwiftPlatform) DefaultSwiftPlatform(org.gradle.language.swift.internal.DefaultSwiftPlatform)

Aggregations

Callable (java.util.concurrent.Callable)1 Configuration (org.gradle.api.artifacts.Configuration)1 ConfigurationContainer (org.gradle.api.artifacts.ConfigurationContainer)1 Usage (org.gradle.api.attributes.Usage)1 ObjectFactory (org.gradle.api.model.ObjectFactory)1 ProviderFactory (org.gradle.api.provider.ProviderFactory)1 ComponentWithNames (org.gradle.language.nativeplatform.internal.ComponentWithNames)1 Names (org.gradle.language.nativeplatform.internal.Names)1 ToolChainSelector (org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector)1 SwiftBinary (org.gradle.language.swift.SwiftBinary)1 SwiftPlatform (org.gradle.language.swift.SwiftPlatform)1 SwiftSharedLibrary (org.gradle.language.swift.SwiftSharedLibrary)1 SwiftStaticLibrary (org.gradle.language.swift.SwiftStaticLibrary)1 DefaultSwiftLibrary (org.gradle.language.swift.internal.DefaultSwiftLibrary)1 DefaultSwiftPlatform (org.gradle.language.swift.internal.DefaultSwiftPlatform)1 DefaultSwiftSharedLibrary (org.gradle.language.swift.internal.DefaultSwiftSharedLibrary)1 DefaultSwiftStaticLibrary (org.gradle.language.swift.internal.DefaultSwiftStaticLibrary)1