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);
}
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();
});
}
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();
});
}
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);
}
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();
});
}
Aggregations