Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle C source files in published HDVL sources archives #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module some_project;
function automatic void do_stuff();
some_class o = new();
`some_published_dependency_macro
some_dpi_func();
endfunction
endmodule
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'com.verificationgentleman.gradle.hdvl.systemverilog'
id 'com.verificationgentleman.gradle.hdvl.c'
id 'maven-publish'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

void some_dpi_func() {
printf("Hello from C\n");
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package some_published_dependency;
`include "some_class.svh"
import "DPI-C" function void some_dpi_func();
endpackage
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import org.junit.rules.TemporaryFolder
import spock.lang.Ignore
import spock.lang.Specification

import java.nio.file.Files
import java.util.zip.ZipFile

import static org.gradle.testkit.runner.TaskOutcome.NO_SOURCE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

Expand All @@ -38,6 +41,28 @@ class CPluginSpec extends Specification {
"""
}

/**
* Creates a new project in a new directory, with a standard layout.
*
* @param name The project name
* @return The build file of the project
*/
def newStandardProject(String name) {
File folder = testProjectDir.newFolder(name)

File sv = testProjectDir.newFolder(name,'src', 'main', 'c')
new File(sv, "${name}.c").createNewFile()

File buildFile = new File(folder, "build.gradle")
buildFile << """
plugins {
id 'com.verificationgentleman.gradle.hdvl.c'
}
"""

return buildFile
}

def "can successfully import the plugin"() {
when:
def result = GradleRunner.create()
Expand Down Expand Up @@ -201,4 +226,78 @@ class CPluginSpec extends Specification {
new File(testProjectDir.root, "build/dummy_xrun_args.f").exists()
new File(testProjectDir.root, "build/dummy_xrun_args.f").text.contains('dummy.c')
}

def "can produce archive with source file"() {
File mainSv = testProjectDir.newFolder('src', 'main', 'c')
new File(mainSv, "main.c").createNewFile()

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

then:
new File(testProjectDir.root, 'build/hdvl-sources.zip').exists()
def zipFile = new ZipFile(new File(testProjectDir.root, 'build/hdvl-sources.zip'))
def entries = zipFile.entries().findAll { !it.directory }
entries.size() == 2
entries[1].name == 'src/main/c/main.c'
}

def "can consume source archive"() {
File dependencyProjectBuildFile = newStandardProject('dependency-project')
dependencyProjectBuildFile << """
plugins {
id 'maven-publish'
}

group = "org.example"
version = "1.0.0"

publishing {
repositories {
maven {
name = 'dummy'
url = layout.buildDirectory.dir('dummy-repo')
}
}
}
"""

GradleRunner.create()
.withProjectDir(dependencyProjectBuildFile.parentFile)
.withPluginClasspath()
.withArguments(':publish')
.build()

File mainProjectBuildFile = newStandardProject('main-project')
mainProjectBuildFile << """
dependencies {
compile 'org.example:dependency-project:1.0.0'
}

repositories {
maven {
url = layout.projectDirectory.dir('../dependency-project/build/dummy-repo')
}
}
"""

when:
def result = GradleRunner.create()
.withProjectDir(mainProjectBuildFile.parentFile)
.withPluginClasspath()
.withDebug(true)
.withArguments(':genFullXrunArgsFile')
.build()

then:
def lines = new File(mainProjectBuildFile.parentFile, 'build/full_xrun_args.f').text.split("\n")
def xrunArgsForDependencyProject = new File(lines[0].split(/\s+/)[1])
Files.lines(xrunArgsForDependencyProject.toPath()).anyMatch { line ->
line.endsWith 'src/main/c/dependency-project.c'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface HDVLCompileSpec {
Set<File> getSvSourceFiles();
Set<File> getSvPrivateIncludeDirs();
Set<File> getSvExportedHeaderDirs();
Set<File> getCSourceFiles();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package com.verificationgentleman.gradle.hdvl.c;

import com.verificationgentleman.gradle.hdvl.*;
import com.verificationgentleman.gradle.hdvl.AbstractGenArgsFile;
import com.verificationgentleman.gradle.hdvl.HDVLBasePlugin;
import com.verificationgentleman.gradle.hdvl.HDVLPluginExtension;
import com.verificationgentleman.gradle.hdvl.SourceSet;
import com.verificationgentleman.gradle.hdvl.c.internal.DefaultCSourceSet;
import com.verificationgentleman.gradle.hdvl.internal.WriteCompileSpecFile;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.tasks.bundling.Zip;

public class CPlugin implements Plugin<Project> {

Expand All @@ -45,6 +50,18 @@ public void execute(SourceSet sourceSet) {
= (AbstractGenArgsFile) project.getTasks().getByName(sourceSet.getGenArgsFileTaskName(toolName));
genArgsFile.setCSource(cSourceSet.getC());
}

if (sourceSet.getName() == "main") {
project.getTasks().withType(WriteCompileSpecFile.class, task -> {
task.getCSource().from(cSourceSet.getC());
});
project.getTasks().getByName("hdvlSourcesArchive", task -> {
Zip hdvlSourcesArchive = (Zip) task;
hdvlSourcesArchive.from(cSourceSet.getC(), it -> {
it.into("src/main/c"); // FIXME Assumes source in conventional location
});
});
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ public class DefaultHDVLCompileSpec implements HDVLCompileSpec {
@XmlJavaTypeAdapter(value=FileAdapter.class)
private final File[] svExportedHeaderDirs;

public DefaultHDVLCompileSpec(Set<File> svSourceFiles, Set<File> svPrivateIncludeDirs, Set<File> svExportedHeaderDirs) {
@XmlElementWrapper
@XmlElement(name="cSourceFile")
@XmlJavaTypeAdapter(value=FileAdapter.class)
private final File[] cSourceFiles;

public DefaultHDVLCompileSpec(Set<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]);
this.cSourceFiles = cSourceFiles.toArray(new File[0]);
}

// Needed for JAXB
Expand All @@ -40,6 +47,7 @@ private DefaultHDVLCompileSpec() {
this.svSourceFiles = new File[0];
this.svPrivateIncludeDirs = new File[0];
this.svExportedHeaderDirs = new File[0];
this.cSourceFiles = new File[0];
}

@Override
Expand All @@ -56,4 +64,9 @@ public Set<File> getSvPrivateIncludeDirs() {
public Set<File> getSvExportedHeaderDirs() {
return new HashSet<>(Arrays.asList(svExportedHeaderDirs));
}

@Override
public Set<File> getCSourceFiles() {
return new HashSet<>(Arrays.asList(cSourceFiles));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public class WriteCompileSpecFile extends DefaultTask {
private final ConfigurableFileCollection svSourceFiles;
private final ConfigurableFileCollection svPrivateIncludeDirs;
private final ConfigurableFileCollection svExportedHeaderDirs;
private final ConfigurableFileCollection cSourceFiles;


public WriteCompileSpecFile() {
destination = getProject().getObjects().fileProperty();
svSourceFiles = getProject().getObjects().fileCollection();
svPrivateIncludeDirs = getProject().getObjects().fileCollection();
svExportedHeaderDirs = getProject().getObjects().fileCollection();
cSourceFiles = getProject().getObjects().fileCollection();
}

@OutputFile
Expand Down Expand Up @@ -49,10 +51,17 @@ public ConfigurableFileCollection getSvExportedHeaderDirs() {
return svExportedHeaderDirs;
}

@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.ABSOLUTE)
public ConfigurableFileCollection getCSource() {
return cSourceFiles;
}

@TaskAction
protected void generate() {
DefaultHDVLCompileSpec compileSpec = new DefaultHDVLCompileSpec(getSvSource().getFiles(),
svPrivateIncludeDirs.getFiles(), svExportedHeaderDirs.getFiles());
svPrivateIncludeDirs.getFiles(), svExportedHeaderDirs.getFiles(), cSourceFiles.getFiles());
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DefaultHDVLCompileSpec.class);
Marshaller marshaller = jaxbContext.createMarshaller();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ private static DefaultHDVLCompileSpec getCompileSpec(File input) {
assert svExportedHeaderDir.isAbsolute() : "not absolute: " + svExportedHeaderDir;
assert svExportedHeaderDir.exists() : "doesn't exist: " + svExportedHeaderDir;
}
for (File cSourceFile : result.getCSourceFiles()) {
assert cSourceFile.isAbsolute() : "not absolute: " + cSourceFile;
assert cSourceFile.exists() : "doesn't exist: " + cSourceFile;
}

return result;
} catch (JAXBException e) {
Expand All @@ -66,6 +70,8 @@ private static void writeXrunArgsFile(File xrunArgsFile, HDVLCompileSpec compile
writer.write(" -incdir " + svPrivateIncludeDir + "\n");
for (File svSourceFile : compileSpec.getSvSourceFiles())
writer.write(" " + svSourceFile + "\n");
for (File cSourceFile : compileSpec.getCSourceFiles())
writer.write(" " + cSourceFile + "\n");
writer.write("-endlib\n");
}
catch (IOException ex) {
Expand Down
Loading