From 255c6368b6507c9c492d66c183f8f8802f835e5c Mon Sep 17 00:00:00 2001 From: Kai Brennenstuhl Date: Mon, 22 Jun 2020 18:21:49 +0200 Subject: [PATCH] Enhancment of header-only-library sample: Removing the static library from the outgoing config, disabling CppCompile and CreateStaticLibrary tasks. Signed-off-by: Kai Brennenstuhl --- .../plugins/CppHeaderLibraryPlugin.java | 30 ++++++---------- .../samples/tasks/GenerateDummyCppSource.java | 34 ------------------- 2 files changed, 10 insertions(+), 54 deletions(-) delete mode 100644 cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/tasks/GenerateDummyCppSource.java diff --git a/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/plugins/CppHeaderLibraryPlugin.java b/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/plugins/CppHeaderLibraryPlugin.java index cdbc2e40..d46388b9 100644 --- a/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/plugins/CppHeaderLibraryPlugin.java +++ b/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/plugins/CppHeaderLibraryPlugin.java @@ -1,17 +1,12 @@ package org.gradle.samples.plugins; -import org.gradle.api.Action; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.file.ConfigurableFileCollection; -import org.gradle.api.file.FileCollection; -import org.gradle.api.file.RegularFile; -import org.gradle.api.provider.Provider; -import org.gradle.api.tasks.TaskContainer; -import org.gradle.api.tasks.TaskProvider; import org.gradle.language.cpp.CppLibrary; +import org.gradle.language.cpp.internal.DefaultCppStaticLibrary; +import org.gradle.language.cpp.tasks.CppCompile; import org.gradle.nativeplatform.Linkage; -import org.gradle.samples.tasks.GenerateDummyCppSource; +import org.gradle.nativeplatform.tasks.CreateStaticLibrary; import java.util.Arrays; @@ -23,19 +18,14 @@ public void apply(Project project) { CppLibrary library = project.getExtensions().getByType(CppLibrary.class); library.getLinkage().set(Arrays.asList(Linkage.STATIC)); - TaskProvider generateTask = createTask(project.getTasks(), project); - library.getSource().from(generateTask.flatMap(it -> it.getOutputFile())); - } + project.getComponents().withType(DefaultCppStaticLibrary.class, staticLibrary -> + staticLibrary.getLinkElements().get().outgoing(outgoingLinkElementsConfiguration -> + outgoingLinkElementsConfiguration.getArtifacts().removeIf(publishArtifact -> + publishArtifact.getFile().equals(staticLibrary.getLinkFile().get().getAsFile())))); - private static TaskProvider createTask(TaskContainer tasks, Project project) { - return tasks.register("generateCppHeaderSourceFile", GenerateDummyCppSource.class, (task) -> { - Provider sourceFile = project.getLayout().getBuildDirectory().file("dummy-source.cpp"); - task.getOutputFile().set(sourceFile); - task.getSymbolName().set("__" + toSymbol(project.getPath()) + "_" + toSymbol(project.getName()) + "__"); - }); - } + project.getTasks().withType(CreateStaticLibrary.class, task -> task.setEnabled(false)); + project.getTasks().withType(CppCompile.class, task -> task.setEnabled(false)); - private static String toSymbol(String s) { - return s.replace(":", "_").replace("-", "_"); } + } diff --git a/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/tasks/GenerateDummyCppSource.java b/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/tasks/GenerateDummyCppSource.java deleted file mode 100644 index 81626cc8..00000000 --- a/cpp/header-only-library/header-gradle-plugin/src/main/java/org/gradle/samples/tasks/GenerateDummyCppSource.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.gradle.samples.tasks; - -import org.gradle.api.DefaultTask; -import org.gradle.api.file.RegularFileProperty; -import org.gradle.api.provider.Property; -import org.gradle.api.tasks.CacheableTask; -import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.OutputFile; -import org.gradle.api.tasks.TaskAction; - -import java.io.IOException; -import java.nio.file.Files; - -@CacheableTask -public class GenerateDummyCppSource extends DefaultTask { - private final Property symbolName = getProject().getObjects().property(String.class); - private final RegularFileProperty outputFile = getProject().getObjects().fileProperty(); - - @TaskAction - private void doGenerate() throws IOException { - String source = "void " + symbolName.get() + "() {}"; - Files.write(outputFile.getAsFile().get().toPath(), source.getBytes()); - } - - @Input - public Property getSymbolName() { - return symbolName; - } - - @OutputFile - public RegularFileProperty getOutputFile() { - return outputFile; - } -}