Search in sources :

Example 1 with ProductionSwiftComponent

use of org.gradle.language.swift.ProductionSwiftComponent in project gradle by gradle.

the class XCTestConventionPlugin method apply.

@Override
public void apply(ProjectInternal project) {
    project.getPluginManager().apply(SwiftBasePlugin.class);
    project.getPluginManager().apply(NativeTestingBasePlugin.class);
    // Create test suite component
    final DefaultSwiftXCTestSuite testComponent = createTestSuite(project);
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(final Project project) {
            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);
            NativeVariantIdentity debugVariant = new NativeVariantIdentity("debug" + operatingSystemSuffix, testComponent.getModule(), group, version, true, false, operatingSystem, null, new DefaultUsageContext("debug" + operatingSystemSuffix + "-runtime", runtimeUsage, attributesDebug));
            ToolChainSelector.Result<SwiftPlatform> result = toolChainSelector.select(SwiftPlatform.class);
            // Create test suite executable
            DefaultSwiftXCTestBinary binary;
            if (result.getTargetPlatform().getOperatingSystem().isMacOsX()) {
                binary = (DefaultSwiftXCTestBinary) testComponent.addBundle("executable", debugVariant, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
            } else {
                binary = (DefaultSwiftXCTestBinary) testComponent.addExecutable("executable", debugVariant, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
            }
            testComponent.getTestBinary().set(binary);
            // TODO: Publishing for test executable?
            final ProductionSwiftComponent mainComponent = project.getComponents().withType(ProductionSwiftComponent.class).findByName("main");
            if (mainComponent != null) {
                testComponent.getTestedComponent().set(mainComponent);
                // Test configuration extends main configuration
                testComponent.getImplementationDependencies().extendsFrom(mainComponent.getImplementationDependencies());
                project.getDependencies().add(binary.getImportPathConfiguration().getName(), project);
            }
            testComponent.getBinaries().whenElementKnown(DefaultSwiftXCTestBinary.class, new Action<DefaultSwiftXCTestBinary>() {

                @Override
                public void execute(DefaultSwiftXCTestBinary binary) {
                    // Create test suite test task
                    XCTest testingTask = createTestingTask(project);
                    binary.getRunTask().set(testingTask);
                    // Configure tasks
                    configureTestingTask(binary, testingTask);
                    configureTestSuiteBuildingTasks((ProjectInternal) project, binary);
                    configureTestSuiteWithTestedComponentWhenAvailable(project, testComponent, binary);
                }
            });
            testComponent.getBinaries().realizeNow();
        }
    });
}
Also used : OperatingSystemFamily(org.gradle.nativeplatform.OperatingSystemFamily) Usage(org.gradle.api.attributes.Usage) Action(org.gradle.api.Action) ProductionSwiftComponent(org.gradle.language.swift.ProductionSwiftComponent) AttributeContainer(org.gradle.api.attributes.AttributeContainer) NativeVariantIdentity(org.gradle.language.cpp.internal.NativeVariantIdentity) Callable(java.util.concurrent.Callable) Provider(org.gradle.api.provider.Provider) PlatformToolProvider(org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider) Project(org.gradle.api.Project) DefaultSwiftXCTestBinary(org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestBinary) DefaultUsageContext(org.gradle.language.cpp.internal.DefaultUsageContext) DefaultSwiftXCTestSuite(org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestSuite) SwiftPlatform(org.gradle.language.swift.SwiftPlatform) XCTest(org.gradle.nativeplatform.test.xctest.tasks.XCTest)

Example 2 with ProductionSwiftComponent

use of org.gradle.language.swift.ProductionSwiftComponent in project gradle by gradle.

the class XCTestConventionPlugin method configureTestSuiteWithTestedComponentWhenAvailable.

private void configureTestSuiteWithTestedComponentWhenAvailable(final Project project, final DefaultSwiftXCTestSuite testSuite, final DefaultSwiftXCTestBinary testExecutable) {
    SwiftComponent target = testSuite.getTestedComponent().getOrNull();
    if (!(target instanceof ProductionSwiftComponent)) {
        return;
    }
    final ProductionSwiftComponent testedComponent = (ProductionSwiftComponent) target;
    final TaskContainer tasks = project.getTasks();
    testedComponent.getBinaries().whenElementFinalized(testedBinary -> {
        if (testedBinary != testedComponent.getDevelopmentBinary().get()) {
            return;
        }
        // Setup the dependency on the main binary
        // This should all be replaced by a single dependency that points at some "testable" variants of the main binary
        // Inherit implementation dependencies
        testExecutable.getImplementationDependencies().extendsFrom(((DefaultSwiftBinary) testedBinary).getImplementationDependencies());
        // Configure test binary to compile against binary under test
        Dependency compileDependency = project.getDependencies().create(project.files(testedBinary.getModuleFile()));
        testExecutable.getImportPathConfiguration().getDependencies().add(compileDependency);
        // Configure test binary to link against tested component compiled objects
        ConfigurableFileCollection testableObjects = project.files();
        if (testedComponent instanceof SwiftApplication) {
            TaskProvider<UnexportMainSymbol> unexportMainSymbol = tasks.register("relocateMainForTest", UnexportMainSymbol.class, task -> {
                String dirName = ((DefaultSwiftBinary) testedBinary).getNames().getDirName();
                task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("obj/for-test/" + dirName));
                task.getObjects().from(testedBinary.getObjects());
            });
            testableObjects.from(unexportMainSymbol.map(task -> task.getRelocatedObjects()));
        } else {
            testableObjects.from(testedBinary.getObjects());
        }
        Dependency linkDependency = project.getDependencies().create(testableObjects);
        testExecutable.getLinkConfiguration().getDependencies().add(linkDependency);
    });
}
Also used : ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) Arrays(java.util.Arrays) UnexportMainSymbol(org.gradle.language.nativeplatform.tasks.UnexportMainSymbol) NativeToolChainRegistryInternal(org.gradle.nativeplatform.toolchain.internal.NativeToolChainRegistryInternal) ProductionSwiftComponent(org.gradle.language.swift.ProductionSwiftComponent) DefaultSwiftXCTestExecutable(org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestExecutable) GUtil(org.gradle.util.internal.GUtil) Provider(org.gradle.api.provider.Provider) ModelRegistry(org.gradle.model.internal.registry.ModelRegistry) DefaultNativePlatform(org.gradle.nativeplatform.platform.internal.DefaultNativePlatform) DefaultSwiftXCTestSuite(org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestSuite) TaskProvider(org.gradle.api.tasks.TaskProvider) XCTest(org.gradle.nativeplatform.test.xctest.tasks.XCTest) TargetMachineFactory(org.gradle.nativeplatform.TargetMachineFactory) SwiftXCTestBundle(org.gradle.nativeplatform.test.xctest.SwiftXCTestBundle) ProjectInternal(org.gradle.api.internal.project.ProjectInternal) SwiftBasePlugin(org.gradle.language.swift.plugins.SwiftBasePlugin) PlatformToolProvider(org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider) ImmutableAttributesFactory(org.gradle.api.internal.attributes.ImmutableAttributesFactory) Project(org.gradle.api.Project) TargetMachine(org.gradle.nativeplatform.TargetMachine) Dimensions(org.gradle.language.nativeplatform.internal.Dimensions) ToolChainSelector(org.gradle.language.nativeplatform.internal.toolchains.ToolChainSelector) MacOSSdkPlatformPathLocator(org.gradle.nativeplatform.toolchain.internal.xcode.MacOSSdkPlatformPathLocator) TaskContainer(org.gradle.api.tasks.TaskContainer) Names(org.gradle.language.nativeplatform.internal.Names) NativeToolChain(org.gradle.nativeplatform.toolchain.NativeToolChain) SwiftPlatform(org.gradle.language.swift.SwiftPlatform) SwiftXCTestBinary(org.gradle.nativeplatform.test.xctest.SwiftXCTestBinary) DefaultSwiftXCTestBinary(org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestBinary) NativeTestingBasePlugin(org.gradle.nativeplatform.test.plugins.NativeTestingBasePlugin) Dimensions.useHostAsDefaultTargetMachine(org.gradle.language.nativeplatform.internal.Dimensions.useHostAsDefaultTargetMachine) Dimensions.tryToBuildOnHost(org.gradle.language.nativeplatform.internal.Dimensions.tryToBuildOnHost) Inject(javax.inject.Inject) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) Lists(com.google.common.collect.Lists) Sync(org.gradle.api.tasks.Sync) LinkMachOBundle(org.gradle.nativeplatform.tasks.LinkMachOBundle) DefaultSwiftPlatform(org.gradle.language.swift.internal.DefaultSwiftPlatform) NativeComponentFactory(org.gradle.language.internal.NativeComponentFactory) SwiftApplication(org.gradle.language.swift.SwiftApplication) SwiftComponent(org.gradle.language.swift.SwiftComponent) NativeToolChainInternal(org.gradle.nativeplatform.toolchain.internal.NativeToolChainInternal) SwiftCompile(org.gradle.language.swift.tasks.SwiftCompile) File(java.io.File) SetProperty(org.gradle.api.provider.SetProperty) ProviderFactory(org.gradle.api.provider.ProviderFactory) DefaultSwiftBinary(org.gradle.language.swift.internal.DefaultSwiftBinary) ObjectFactory(org.gradle.api.model.ObjectFactory) DefaultSwiftXCTestBundle(org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestBundle) RegularFile(org.gradle.api.file.RegularFile) InstallXCTestBundle(org.gradle.nativeplatform.test.xctest.tasks.InstallXCTestBundle) Dependency(org.gradle.api.artifacts.Dependency) Plugin(org.gradle.api.Plugin) SwiftXCTestSuite(org.gradle.nativeplatform.test.xctest.SwiftXCTestSuite) TaskContainer(org.gradle.api.tasks.TaskContainer) ProductionSwiftComponent(org.gradle.language.swift.ProductionSwiftComponent) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) UnexportMainSymbol(org.gradle.language.nativeplatform.tasks.UnexportMainSymbol) Dependency(org.gradle.api.artifacts.Dependency) ProductionSwiftComponent(org.gradle.language.swift.ProductionSwiftComponent) SwiftComponent(org.gradle.language.swift.SwiftComponent) SwiftApplication(org.gradle.language.swift.SwiftApplication)

Example 3 with ProductionSwiftComponent

use of org.gradle.language.swift.ProductionSwiftComponent in project gradle by gradle.

the class SwiftBasePlugin method apply.

@Override
public void apply(final ProjectInternal project) {
    project.getPluginManager().apply(NativeBasePlugin.class);
    project.getPluginManager().apply(SwiftCompilerPlugin.class);
    final TaskContainerInternal tasks = project.getTasks();
    final DirectoryProperty buildDirectory = project.getLayout().getBuildDirectory();
    final ProviderFactory providers = project.getProviders();
    project.getDependencies().getAttributesSchema().attribute(Usage.USAGE_ATTRIBUTE).getCompatibilityRules().add(SwiftCppUsageCompatibilityRule.class);
    project.getComponents().withType(DefaultSwiftBinary.class, new Action<DefaultSwiftBinary>() {

        @Override
        public void execute(final DefaultSwiftBinary binary) {
            final Names names = binary.getNames();
            SwiftCompile compile = tasks.create(names.getCompileTaskName("swift"), SwiftCompile.class);
            compile.getModules().from(binary.getCompileModules());
            compile.getSource().from(binary.getSwiftSource());
            compile.getDebuggable().set(binary.isDebuggable());
            compile.getOptimized().set(binary.isOptimized());
            if (binary.isTestable()) {
                compile.getCompilerArgs().add("-enable-testing");
            }
            if (binary.getTargetPlatform().getOperatingSystem().isMacOsX()) {
                compile.getCompilerArgs().add("-sdk");
                compile.getCompilerArgs().add(locator.find().getAbsolutePath());
            }
            compile.getModuleName().set(binary.getModule());
            compile.getObjectFileDir().set(buildDirectory.dir("obj/" + names.getDirName()));
            compile.getModuleFile().set(buildDirectory.file(providers.provider(new Callable<String>() {

                @Override
                public String call() {
                    return "modules/" + names.getDirName() + binary.getModule().get() + ".swiftmodule";
                }
            })));
            compile.getSourceCompatibility().set(binary.getSourceCompatibility());
            binary.getModuleFile().set(compile.getModuleFile());
            compile.getTargetPlatform().set(binary.getTargetPlatform());
            // TODO - make this lazy
            compile.getToolChain().set(binary.getToolChain());
            binary.getCompileTask().set(compile);
            binary.getObjectsDir().set(compile.getObjectFileDir());
        }
    });
    project.getComponents().withType(SwiftSharedLibrary.class, new Action<SwiftSharedLibrary>() {

        @Override
        public void execute(SwiftSharedLibrary library) {
            // Specific compiler arguments
            library.getCompileTask().get().getCompilerArgs().add("-parse-as-library");
        }
    });
    project.getComponents().withType(SwiftStaticLibrary.class, new Action<SwiftStaticLibrary>() {

        @Override
        public void execute(SwiftStaticLibrary library) {
            // Specific compiler arguments
            library.getCompileTask().get().getCompilerArgs().add("-parse-as-library");
        }
    });
    project.getComponents().withType(DefaultSwiftComponent.class, new Action<DefaultSwiftComponent>() {

        @Override
        public void execute(final DefaultSwiftComponent component) {
            project.afterEvaluate(new Action<Project>() {

                @Override
                public void execute(Project project) {
                    component.getSourceCompatibility().lockNow();
                }
            });
            component.getBinaries().whenElementKnown(DefaultSwiftBinary.class, new Action<DefaultSwiftBinary>() {

                @Override
                public void execute(final DefaultSwiftBinary binary) {
                    Provider<SwiftVersion> swiftLanguageVersionProvider = project.provider(new Callable<SwiftVersion>() {

                        @Override
                        public SwiftVersion call() throws Exception {
                            SwiftVersion swiftSourceCompatibility = component.getSourceCompatibility().getOrNull();
                            if (swiftSourceCompatibility == null) {
                                return toSwiftVersion(binary.getPlatformToolProvider().getCompilerMetadata(ToolType.SWIFT_COMPILER).getVersion());
                            }
                            return swiftSourceCompatibility;
                        }
                    });
                    binary.getSourceCompatibility().set(swiftLanguageVersionProvider);
                }
            });
        }
    });
    project.getComponents().withType(ProductionSwiftComponent.class, new Action<ProductionSwiftComponent>() {

        @Override
        public void execute(final ProductionSwiftComponent component) {
            project.afterEvaluate(new Action<Project>() {

                @Override
                public void execute(Project project) {
                    DefaultSwiftComponent componentInternal = (DefaultSwiftComponent) component;
                    publicationRegistry.registerPublication(project.getPath(), new DefaultProjectPublication(componentInternal.getDisplayName(), new SwiftPmTarget(component.getModule().get()), false));
                }
            });
        }
    });
}
Also used : Action(org.gradle.api.Action) SwiftStaticLibrary(org.gradle.language.swift.SwiftStaticLibrary) Callable(java.util.concurrent.Callable) Names(org.gradle.language.nativeplatform.internal.Names) ProviderFactory(org.gradle.api.provider.ProviderFactory) SwiftVersion(org.gradle.language.swift.SwiftVersion) ProductionSwiftComponent(org.gradle.language.swift.ProductionSwiftComponent) SwiftPmTarget(org.gradle.swiftpm.internal.SwiftPmTarget) TaskContainerInternal(org.gradle.api.internal.tasks.TaskContainerInternal) Project(org.gradle.api.Project) DirectoryProperty(org.gradle.api.file.DirectoryProperty) SwiftCompile(org.gradle.language.swift.tasks.SwiftCompile) SwiftSharedLibrary(org.gradle.language.swift.SwiftSharedLibrary) DefaultSwiftBinary(org.gradle.language.swift.internal.DefaultSwiftBinary) DefaultSwiftComponent(org.gradle.language.swift.internal.DefaultSwiftComponent) DefaultProjectPublication(org.gradle.api.internal.artifacts.ivyservice.projectmodule.DefaultProjectPublication)

Aggregations

Project (org.gradle.api.Project)3 ProductionSwiftComponent (org.gradle.language.swift.ProductionSwiftComponent)3 Callable (java.util.concurrent.Callable)2 Action (org.gradle.api.Action)2 Provider (org.gradle.api.provider.Provider)2 ProviderFactory (org.gradle.api.provider.ProviderFactory)2 Names (org.gradle.language.nativeplatform.internal.Names)2 SwiftPlatform (org.gradle.language.swift.SwiftPlatform)2 DefaultSwiftXCTestBinary (org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestBinary)2 DefaultSwiftXCTestSuite (org.gradle.nativeplatform.test.xctest.internal.DefaultSwiftXCTestSuite)2 XCTest (org.gradle.nativeplatform.test.xctest.tasks.XCTest)2 PlatformToolProvider (org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider)2 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 Arrays (java.util.Arrays)1 Inject (javax.inject.Inject)1 Plugin (org.gradle.api.Plugin)1 Dependency (org.gradle.api.artifacts.Dependency)1 AttributeContainer (org.gradle.api.attributes.AttributeContainer)1 Usage (org.gradle.api.attributes.Usage)1