Skip to content

Commit

Permalink
Merge pull request #97 from tudortimi/add-support-for-test-compile-de…
Browse files Browse the repository at this point in the history
…pendencies-other-than-svunit

Add support for test compile dependencies other than SVUnit
  • Loading branch information
tudortimi authored Jan 17, 2023
2 parents 3ef6f61 + 6b77290 commit 26a8011
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 19 deletions.
9 changes: 9 additions & 0 deletions examples/svunit-with-test-compile-dependency/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'com.verificationgentleman.gradle.hdvl.systemverilog'
id 'com.verificationgentleman.gradle.hdvl.svunit'
}

dependencies {
testCompile "org.svunit:svunit:v3.34.2"
testCompile "com.verificationgentleman.gradle.hdvl.examples.svunit:test_utils"
}
3 changes: 3 additions & 0 deletions examples/svunit-with-test-compile-dependency/gradlew
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

exec "$(dirname "$0")/../../gradlew" "$@"
11 changes: 11 additions & 0 deletions examples/svunit-with-test-compile-dependency/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includeBuild("../..")
includeBuild("test_utils")

sourceControl {
gitRepository("https://github.com/svunit/svunit.git") {
producesModule("org.svunit:svunit")
plugins {
id "com.verificationgentleman.gradle.hdvl.svunit-build-injector"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class some_class;

function bit is_working();
return 1;
endfunction

endclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package some_package;

`include "some_class.svh"

endpackage
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module some_class_unit_test;

import svunit_pkg::svunit_testcase;
`include "svunit_defines.svh"

string name = "some_class_unit_test";
svunit_testcase svunit_ut;


import some_package::*;
import test_utils::*;


function void build();
svunit_ut = new(name);
endfunction

task setup();
svunit_ut.setup();
endtask

task teardown();
svunit_ut.teardown();
endtask


`SVUNIT_TESTS_BEGIN

`SVTEST(is_working__returns_1)
some_class obj = new();
`FAIL_UNLESS(confirm(obj.is_working()))
`SVTEST_END

`SVUNIT_TESTS_END

endmodule
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
id 'com.verificationgentleman.gradle.hdvl.systemverilog'
}

group = "com.verificationgentleman.gradle.hdvl.examples.svunit"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Intentionally empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test_utils;

function bit confirm(bit val);
return val;
endfunction

endpackage
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class SVUnitPluginSpec extends Specification {
!lines.any { it.contains('some_other_unit_test.sv') }
}

def "'testWithXrun' task passes args file for 'test' source set to 'runSVUnit'"() {
def "'testWithXrun' task passes full args file for 'test' source set to 'runSVUnit'"() {
File mainSv = testProjectDir.newFolder('src', 'main', 'sv')
new File(mainSv, 'dummy_main.sv').createNewFile()

Expand All @@ -365,7 +365,7 @@ class SVUnitPluginSpec extends Specification {
then:
result.task(":testWithXrun").outcome == SUCCESS
def dummyLog = new File(testProjectDir.root, 'build/svunit/runSVUnit.log')
dummyLog.text.contains "-f ${testProjectDir.root}/build/test_xrun_args.f"
dummyLog.text.contains "-f ${testProjectDir.root}/build/full_test_xrun_args.f"
}

def newGradleRunnerWithFakeRunSVunit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ private Class<? extends AbstractGenArgsFile> getGenArgsFileClass(String toolName
private void configureGenFullArgsFile(Project project, String toolName) {
AbstractGenArgsFile genArgsFile = (AbstractGenArgsFile) project.getTasks()
.getByName(Names.getMainGenArgsFileTaskName(toolName));
project.getTasks().register(Names.getGenFullArgsFileTaskName(toolName), GenFullArgsFile.class, new Action<GenFullArgsFile>() {
project.getTasks().register(Names.getGenFullArgsFileTaskName("main", toolName), GenFullArgsFile.class, new Action<GenFullArgsFile>() {
@Override
public void execute(GenFullArgsFile genFullArgsFile) {
genFullArgsFile.setDescription("Generates an argument file for the main source code and its dependencies.");
genFullArgsFile.getSource().set(genArgsFile.getDestination());
genFullArgsFile.getDestination().set(new File(project.getBuildDir(), Names.getFullArgsFileName(toolName)));
genFullArgsFile.setArgsFiles(project.getConfigurations().getByName(Names.getArgsFilesConfigurationName(toolName)));
genFullArgsFile.getDestination().set(new File(project.getBuildDir(), Names.getFullArgsFileName("main", toolName)));
genFullArgsFile.setArgsFiles(project.getConfigurations().getByName(Names.getArgsFilesConfigurationName("main", toolName)));
}
});
}
Expand All @@ -104,7 +104,7 @@ private void configureCompileConfiguration(Project project) {
}

private void configureArgsFilesConfiguration(Project project, String toolName) {
Configuration argsFiles = project.getConfigurations().create(Names.getArgsFilesConfigurationName(toolName));
Configuration argsFiles = project.getConfigurations().create(Names.getArgsFilesConfigurationName("main", toolName));
argsFiles.extendsFrom(project.getConfigurations().getByName("compile"));
argsFiles.setCanBeConsumed(true);
argsFiles.setCanBeResolved(true);
Expand All @@ -120,7 +120,7 @@ public void execute(ConfigurablePublishArtifact configurablePublishArtifact) {
configurablePublishArtifact.builtBy(genArgsFile);
}
};
project.getArtifacts().add(Names.getArgsFilesConfigurationName(toolName), genArgsFile.getDestination(), configureAction);
project.getArtifacts().add(Names.getArgsFilesConfigurationName("main", toolName), genArgsFile.getDestination(), configureAction);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ public static String getGenArgsFileTaskName(String sourceSetName, String toolNam
return GUtil.toLowerCamelCase("gen" + " " + sourceSetName + "" + toolName + "ArgsFile");
}

public static String getGenFullArgsFileTaskName(String toolName) {
return "genFull" + toolName + "ArgsFile";
public static String getGenFullArgsFileTaskName(String sourceSetName, String toolName) {
if (sourceSetName.equals("main"))
return "genFull" + toolName + "ArgsFile";
return GUtil.toLowerCamelCase("genFull" + " " + sourceSetName + "" + toolName + "ArgsFile");
}

public static String getFullArgsFileName(String toolName) {
public static String getFullArgsFileName(String sourceSetName, String toolName) {
String toolNameLower = toolName.toLowerCase();
return "full" + "_" + toolNameLower + "_" + "args.f";
if (sourceSetName.equals("main"))
return "full" + "_" + toolNameLower + "_" + "args.f";
return "full" + "_" + sourceSetName + "_"+ toolNameLower + "_" + "args.f";
}

public static String getArgsFilesConfigurationName(String toolName) {
return GUtil.toLowerCamelCase(toolName + "ArgsFiles");
public static String getArgsFilesConfigurationName(String sourceSetName, String toolName) {
if (sourceSetName.equals("main"))
return GUtil.toLowerCamelCase(toolName + "ArgsFiles");
return GUtil.toLowerCamelCase(sourceSetName + " " + toolName + "ArgsFiles");
}

public static String getTestTaskName(String toolName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;

public class SVUnitBuildPlugin implements Plugin<Project> {
public void apply(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.verificationgentleman.gradle.hdvl.AbstractGenArgsFile;
import com.verificationgentleman.gradle.hdvl.GenFullArgsFile;
import com.verificationgentleman.gradle.hdvl.HDVLBasePlugin;
import com.verificationgentleman.gradle.hdvl.SourceSet;
import com.verificationgentleman.gradle.hdvl.internal.Names;
import com.verificationgentleman.gradle.hdvl.svunit.internal.DefaultToolChains;
Expand All @@ -28,10 +29,15 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.internal.HasConvention;
import org.gradle.api.reflect.TypeOf;
import org.gradle.util.GUtil;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SVUnitPlugin implements Plugin<Project> {

Expand All @@ -45,6 +51,14 @@ public void apply(Project project) {
final SourceSet testSourceSet = sourceSets.create("test");

configureConfiguration(project);
configureSVUnitRootConfiguration(project);

String[] toolNames = {"Xrun", "Qrun"};
for (String toolName: toolNames) {
configureArgsFilesConfiguration(project, toolName);
configureGenFullArgsFile(project, toolName);
}

configureToolChain(project);

sourceSets.named("test", new Action<SourceSet>() {
Expand All @@ -66,6 +80,58 @@ public void execute(SourceSet sourceSet) {

private void configureConfiguration(Project project) {
Configuration testCompileConfiguration = project.getConfigurations().create("testCompile");
testCompileConfiguration.setCanBeConsumed(false);
testCompileConfiguration.setCanBeResolved(false);
}

private void configureSVUnitRootConfiguration(Project project) {
Configuration svUnitRoot = project.getConfigurations().create("svUnitRoot");
svUnitRoot.setCanBeConsumed(false);
svUnitRoot.setCanBeResolved(true);
project.getConfigurations().getByName("testCompile").getDependencies().whenObjectAdded(dependency -> {
if (isSVUnit(dependency)) {
svUnitRoot.getDependencies().add(dependency);
}
});
project.getConfigurations().getByName("testCompile").getDependencies().whenObjectRemoved(dependency -> {
if (isSVUnit(dependency)) {
svUnitRoot.getDependencies().remove(dependency);
}
});
}

private boolean isSVUnit(Dependency dependency) {
return dependency.getGroup().equals("org.svunit") && dependency.getName().equals("svunit");
}

private void configureArgsFilesConfiguration(Project project, String toolName) {
Configuration argsFiles = project.getConfigurations().create(Names.getArgsFilesConfigurationName("test", toolName));
argsFiles.extendsFrom(project.getConfigurations().getByName("testCompile"));
argsFiles.exclude(getExcludeForSVUnit());
argsFiles.setCanBeConsumed(true);
argsFiles.setCanBeResolved(true);
argsFiles.getAttributes().attribute(HDVLBasePlugin.TOOL_ATTRIBUTE, toolName);
}

private Map<String, String> getExcludeForSVUnit() {
HashMap<String, String> exclude = new HashMap<>();
exclude.put("group", "org.svunit");
exclude.put("module", "svunit");
return exclude;
}

private void configureGenFullArgsFile(Project project, String toolName) {
AbstractGenArgsFile genArgsFile = (AbstractGenArgsFile) project.getTasks()
.getByName(Names.getGenArgsFileTaskName("test", toolName));
project.getTasks().register(Names.getGenFullArgsFileTaskName("test", toolName), GenFullArgsFile.class, new Action<GenFullArgsFile>() {
@Override
public void execute(GenFullArgsFile genFullArgsFile) {
genFullArgsFile.setDescription("Generates an argument file for the test source code and its dependencies.");
genFullArgsFile.getSource().set(genArgsFile.getDestination());
genFullArgsFile.getDestination().set(new File(project.getBuildDir(), Names.getFullArgsFileName("test", toolName)));
genFullArgsFile.setArgsFiles(project.getConfigurations().getByName(Names.getArgsFilesConfigurationName("test", toolName)));
}
});
}

private void configureToolChain(Project project) {
Expand All @@ -78,19 +144,19 @@ private void ignoreUnitTests(SystemVerilogSourceSet svSourceSet) {

private void configureTestTask(Project project, SystemVerilogSourceSet testSourceSet, String toolName) {
GenFullArgsFile genFullArgsFile
= (GenFullArgsFile) project.getTasks().getByName(Names.getGenFullArgsFileTaskName(toolName));
AbstractGenArgsFile genTestArgsFile
= (AbstractGenArgsFile) project.getTasks().getByName(Names.getGenArgsFileTaskName("test", toolName));
Configuration testCompileConfiguration = project.getConfigurations().getByName("testCompile");
= (GenFullArgsFile) project.getTasks().getByName(Names.getGenFullArgsFileTaskName("main", toolName));
GenFullArgsFile genFullTestArgsFile
= (GenFullArgsFile) project.getTasks().getByName(Names.getGenFullArgsFileTaskName("test", toolName));
Configuration svUnitRoot = project.getConfigurations().getByName("svUnitRoot");
project.getTasks().register(Names.getTestTaskName(toolName), TestTask.class, new Action<TestTask>() {
@Override
public void execute(TestTask testTask) {
testTask.setDescription("Runs the unit tests using SVUnit.");
testTask.getToolName().set(toolName.toLowerCase());
testTask.getMainArgsFile().set(genFullArgsFile.getDestination());
testTask.getTestArgsFile().set(genTestArgsFile.getDestination());
testTask.getTestArgsFile().set(genFullTestArgsFile.getDestination());
testTask.setTestsRoot(testSourceSet.getSv().getSourceDirectories().getSingleFile());
testTask.setSvunitRoot(testCompileConfiguration);
testTask.setSvunitRoot(svUnitRoot);
testTask.getWorkingDir().set(new File(project.getBuildDir(), "svunit"));
testTask.getExtraArgs().set(toolChains.getRunSVUnit().getArgs());
}
Expand Down

0 comments on commit 26a8011

Please sign in to comment.