Skip to content

Commit

Permalink
Merge pull request #167 from tudortimi/fix-publishing-to-handle-syste…
Browse files Browse the repository at this point in the history
…mverilog-source-file-order

Fix publishing to handle SystemVerilog source file order
  • Loading branch information
tudortimi authored Jan 4, 2025
2 parents 63d91f4 + e9dec41 commit 32049b0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.verificationgentleman.gradle.hdvl.systemverilog


import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -167,4 +168,31 @@ class SystemVerilogPluginCompileOrderSpec extends Specification {
lineWithFile0 > lineWithFile1
lineWithFile0 > lineWithFile2
}

def "compile spec considers source file order"() {
new File(mainSv, "file0.sv").createNewFile()
new File(mainSv, "file1.sv").createNewFile()
new File(mainSv, "file2.sv").createNewFile()

buildFile << """
sourceSets.main.sv.order.first 'file1.sv'
sourceSets.main.sv.order.last 'file0.sv'
"""

when:
def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withPluginClasspath()
.withArguments(':writeCompileSpecFile')
.build()

then:
result.task(":writeCompileSpecFile").outcome == SUCCESS
def compileSpecFile = new File(testProjectDir.root, 'build/compile-spec.json')
JsonNode compileSpec = new ObjectMapper().readTree(compileSpecFile)
JsonNode svSourceFiles = compileSpec.get("svSourceFiles")
svSourceFiles.get(0).asText().contains 'file1.sv'
svSourceFiles.get(1).asText().contains 'file2.sv'
svSourceFiles.get(2).asText().contains 'file0.sv'
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.verificationgentleman.gradle.hdvl;

import java.io.File;
import java.util.List;
import java.util.Set;

public interface HDVLCompileSpec {
Set<File> getSvSourceFiles();
List<File> getSvSourceFiles();
Set<File> getSvPrivateIncludeDirs();
Set<File> getSvExportedHeaderDirs();
Set<File> getCSourceFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.verificationgentleman.gradle.hdvl.HDVLCompileSpec;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

public class DefaultHDVLCompileSpec implements HDVLCompileSpec {
private final File[] svSourceFiles;
Expand All @@ -18,8 +16,8 @@ public class DefaultHDVLCompileSpec implements HDVLCompileSpec {
@JsonProperty("cSourceFiles")
private final File[] cSourceFiles;

public DefaultHDVLCompileSpec(Set<File> svSourceFiles, Set<File> svPrivateIncludeDirs, Set<File> svExportedHeaderDirs,
Set<File> cSourceFiles) {
public DefaultHDVLCompileSpec(List<File> svSourceFiles, Set<File> svPrivateIncludeDirs, Set<File> svExportedHeaderDirs,
Set<File> cSourceFiles) {
this.svSourceFiles = svSourceFiles.toArray(new File[0]);
this.svPrivateIncludeDirs = svPrivateIncludeDirs.toArray(new File[0]);
this.svExportedHeaderDirs = svExportedHeaderDirs.toArray(new File[0]);
Expand All @@ -36,8 +34,8 @@ private DefaultHDVLCompileSpec() {
}

@Override
public Set<File> getSvSourceFiles() {
return new HashSet<>(Arrays.asList(svSourceFiles));
public List<File> getSvSourceFiles() {
return Arrays.asList(svSourceFiles);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.verificationgentleman.gradle.hdvl.systemverilog.FileOrder;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WriteCompileSpecFile extends DefaultTask {
public abstract class WriteCompileSpecFile extends DefaultTask {
private final RegularFileProperty destination;

private final ConfigurableFileCollection svSourceFiles;
Expand Down Expand Up @@ -39,6 +44,10 @@ public ConfigurableFileCollection getSvSource() {
return svSourceFiles;
}

@Input
@Optional
public abstract Property<FileOrder> getSvSourceOrder();

@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.ABSOLUTE)
Expand All @@ -62,7 +71,7 @@ public ConfigurableFileCollection getCSource() {

@TaskAction
protected void generateJson() {
DefaultHDVLCompileSpec compileSpec = new DefaultHDVLCompileSpec(getSvSource().getFiles(),
DefaultHDVLCompileSpec compileSpec = new DefaultHDVLCompileSpec(getOrderedSystemVerilogSourceFiles(),
svPrivateIncludeDirs.getFiles(), svExportedHeaderDirs.getFiles(), cSourceFiles.getFiles());
try {
ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -75,4 +84,32 @@ protected void generateJson() {
throw new RuntimeException(e);
}
}

// TODO Fix duplication with `AbstractGenArgsFileTask`
private List<File> getOrderedSystemVerilogSourceFiles() {
if (!getSvSourceOrder().isPresent() || (getSvSourceOrder().get().getFirst() == null && getSvSourceOrder().get().getLast() == null))
return new ArrayList<>(getSvSource().getFiles());

FileTree firstFiles = newEmptyFileTree();
FileTree lastFiles = newEmptyFileTree();

String first = getSvSourceOrder().get().getFirst();
if (first != null)
firstFiles = getSvSource().getAsFileTree().matching(patternFilterable -> patternFilterable.include(first));

String last = getSvSourceOrder().get().getLast();
if (last != null)
lastFiles = getSvSource().getAsFileTree().matching(patternFilterable -> patternFilterable.include(last));

List<File> result = new ArrayList<>();
result.addAll(firstFiles.getFiles());
result.addAll(getSvSource().minus(firstFiles).minus(lastFiles).getFiles());
result.addAll(lastFiles.getFiles());

return result;
}

private FileTree newEmptyFileTree() {
return getProject().files().getAsFileTree();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void execute(SourceSet sourceSet) {
if (sourceSet.getName() == "main") {
project.getTasks().withType(WriteCompileSpecFile.class, task -> {
task.getSvSource().from(svSourceSet.getSv());
task.getSvSourceOrder().set(svSourceSet.getSv().getOrder());
task.getSvPrivateIncludeDirs().from(svSourceSet.getSv().getSourceDirectories().filter(File::exists));
task.getSvExportedHeaderDirs().from(svSourceSet.getSvHeaders().getSourceDirectories().filter(File::exists));
});
Expand Down

0 comments on commit 32049b0

Please sign in to comment.