Skip to content

Commit

Permalink
Improve source sets (#59)
Browse files Browse the repository at this point in the history
* Rewire source sets and tasks via intermediate file collections.

Add "compile", "import", "include" and "link" collections to consume the corresponding type. Rename the resolvable providers to "compilables", ""importables", "includables" and "linkables".

* Update nomenclature and remove obsolete properties.

* Remove MetalApplication.archive
  • Loading branch information
pedrolamarao authored Oct 23, 2023
1 parent cb3d258 commit bf2ed76
Show file tree
Hide file tree
Showing 27 changed files with 492 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main (int argc, char * argv [])
}
cxx {
create("main") {
includes.from( cpp.named("main").map { it.sources } )
include.from( cpp.named("main").map { it.includables } )
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ int greet (int argc, char * argv[])
ixx {
create("main") {
compileOptions = listOf("-std=c++20")
includes.from( cpp.named("main").map { it.sources } )
include.from( cpp.named("main").map { it.includables } )
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ public void apply (Project project)

final var metal = project.getExtensions().getByType(MetalExtension.class);

final var asm = project.getObjects().domainObjectContainer(MetalAsmSources.class, name -> createSources(project,name));
final var asm = project.getObjects().domainObjectContainer(MetalAsmSourceSet.class, name -> createSourceSet(project,name));
metal.getExtensions().add("asm", asm);
}

static MetalAsmSources createSources (Project project, String name)
static MetalAsmSourceSet createSourceSet (Project project, String name)
{
final var configurations = project.getConfigurations();
final var layout = project.getLayout();
final var metal = project.getExtensions().getByType(MetalExtension.class);
final var objects = project.getObjects();
final var tasks = project.getTasks();

final var linkables = objects.fileCollection();

final var commandsTask = tasks.register("commands-%s-asm".formatted(name),MetalAsmCommandsTask.class);
final var compileTask = tasks.register("compile-%s-asm".formatted(name),MetalAsmCompileTask.class);
final var sourceSet = objects.newInstance(MetalAsmSources.class,commandsTask,compileTask,name);
linkables.from(compileTask);
final var sourceSet = objects.newInstance(MetalAsmSourceSet.class,linkables,name);
sourceSet.getCompileOptions().convention(metal.getCompileOptions());
sourceSet.getIncludes().from(configurations.named(INCLUDABLE_DEPENDENCIES));
sourceSet.getInclude().from(configurations.named(INCLUDABLE_DEPENDENCIES));
sourceSet.getSources().from(layout.getProjectDirectory().dir("src/%s/asm".formatted(name)));
sourceSet.getTargets().convention(metal.getTargets());

Expand All @@ -50,7 +53,7 @@ static MetalAsmSources createSources (Project project, String name)
commandsTask.configure(task ->
{
task.getCompileOptions().convention(sourceSet.getCompileOptions());
task.getIncludables().from(sourceSet.getIncludes());
task.getIncludables().from(sourceSet.getInclude());
task.getObjectDirectory().convention(objectDirectory.map(Directory::getAsFile));
task.getOutputDirectory().convention(commandsDirectory);
task.setSource(sourceSet.getSources());
Expand All @@ -61,7 +64,7 @@ static MetalAsmSources createSources (Project project, String name)
{
task.onlyIf("target is enabled",it -> sourceSet.getTargets().zip(task.getTarget(),(targets,target) -> targets.isEmpty() || targets.contains(target)).get());
task.getCompileOptions().convention(sourceSet.getCompileOptions());
task.getIncludables().from(sourceSet.getSources());
task.getIncludables().from(sourceSet.getInclude());
task.getOutputDirectory().convention(objectDirectory);
task.setSource(sourceSet.getSources());
task.getTarget().convention(metal.getTarget());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package br.dev.pedrolamarao.gradle.metal.asm;

import br.dev.pedrolamarao.gradle.metal.base.MetalSourceSet;
import org.gradle.api.NonNullApi;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.ListProperty;

import javax.inject.Inject;

/**
* Assembler source set.
*/
@NonNullApi
public abstract class MetalAsmSourceSet extends MetalSourceSet
{
private final FileCollection linkables;

private final String name;

/**
* Constructor.
*
* @param linkables linkable elements
* @param name source set name
*/
@Inject
public MetalAsmSourceSet (FileCollection linkables, String name)
{
this.linkables = linkables;
this.name = name;
}

/**
* Compile options.
*
* @return property
*/
public abstract ListProperty<String> getCompileOptions ();

/**
* Include dependencies.
*
* @return configurable collection
*/
public abstract ConfigurableFileCollection getInclude ();

/**
* {@inheritDoc}
*/
@Override
public String getName ()
{
return name;
}

/**
* Link elements.
*
* @return collection
*/
public FileCollection getLinkables ()
{
return linkables;
}

/**
* {@inheritDoc}
*/
@Override
public String toString ()
{
return "MetalAsmSourceSet[%s]".formatted(name);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import org.gradle.api.Named;
import org.gradle.api.NonNullApi;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.TaskProvider;

import javax.inject.Inject;

Expand All @@ -16,47 +14,26 @@
@NonNullApi
public abstract class MetalApplication extends MetalComponent implements Named
{
private final TaskProvider<MetalLinkTask> linkTask;

private final String name;

/**
* Constructor.
*
* @param linkTask link task
* @param name name
* @param name component name
*/
@Inject
public MetalApplication (TaskProvider<MetalLinkTask> linkTask, String name)
public MetalApplication (String name)
{
this.linkTask = linkTask;
this.name = name;
}

/**
* Archives.
*
* @return collection
*/
public abstract ConfigurableFileCollection getArchives ();

/**
* Link options.
*
* @return property
*/
public abstract ListProperty<String> getLinkOptions ();

/**
* Link task.
*
* @return provider
*/
public TaskProvider<MetalLinkTask> getLinkTask ()
{
return linkTask;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.gradle.api.Named;
import org.gradle.api.NonNullApi;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.TaskProvider;

import javax.inject.Inject;

Expand All @@ -15,20 +14,16 @@
@NonNullApi
public abstract class MetalArchive extends MetalComponent implements Named
{
private final TaskProvider<MetalArchiveTask> archiveTask;

private final String name;

/**
* Constructor.
*
* @param archiveTask archive task
* @param name name
* @param name component name
*/
@Inject
public MetalArchive (TaskProvider<MetalArchiveTask> archiveTask, String name)
public MetalArchive (String name)
{
this.archiveTask = archiveTask;
this.name = name;
}

Expand All @@ -39,16 +34,6 @@ public MetalArchive (TaskProvider<MetalArchiveTask> archiveTask, String name)
*/
public abstract ListProperty<String> getArchiveOptions ();

/**
* Archive task.
*
* @return provider
*/
public TaskProvider<MetalArchiveTask> getArchiveTask ()
{
return archiveTask;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ static MetalApplication createApplication (Project project, String name)
final var tasks = project.getTasks();

final var linkTask = tasks.register("link-%s".formatted(name),MetalLinkTask.class);
final var component = objects.newInstance(MetalApplication.class,linkTask,name);
final var component = objects.newInstance(MetalApplication.class,name);
component.getLink().from(configurations.named(Metal.LINKABLE_DEPENDENCIES));
component.getLinkOptions().convention(metal.getLinkOptions());
component.getTargets().convention(metal.getTargets());
component.getArchives().from(configurations.named(Metal.LINKABLE_DEPENDENCIES));

linkTask.configure(task ->
{
task.onlyIf("target is enabled",it -> component.getTargets().zip(task.getTarget(),(targets,target) -> targets.isEmpty() || targets.contains(target)).get());
task.getArchives().from(component.getArchives());
task.getLink().from(component.getLink());
task.getLinkOptions().convention(component.getLinkOptions());
task.getOutputDirectory().convention(layout.getBuildDirectory().dir("exe/%s".formatted(name)));
task.setSource(component.getSources());
task.setSource(component.getSource());
task.getTarget().convention(metal.getTarget());
});

Expand Down Expand Up @@ -224,7 +224,7 @@ static MetalArchive createArchive (Project project, String name)
final var tasks = project.getTasks();

final var archiveTask = tasks.register("archive-%s".formatted(name),MetalArchiveTask.class);
final var component = objects.newInstance(MetalArchive.class,archiveTask,name);
final var component = objects.newInstance(MetalArchive.class,name);
component.getArchiveOptions().convention(metal.getArchiveOptions());
component.getTargets().convention(metal.getTargets());

Expand All @@ -233,7 +233,7 @@ static MetalArchive createArchive (Project project, String name)
task.onlyIf("target is enabled",it -> component.getTargets().zip(task.getTarget(),(targets,target) -> targets.isEmpty() || targets.contains(target)).get());
task.getArchiveOptions().convention(component.getArchiveOptions());
task.getOutputDirectory().convention(layout.getBuildDirectory().dir("lib/%s".formatted(name)));
task.setSource(component.getSources());
task.setSource(component.getSource());
task.getTarget().convention(metal.getTarget());
});

Expand Down
Loading

0 comments on commit bf2ed76

Please sign in to comment.