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

Fix archive and link large source sets #86

Merged
merged 2 commits into from
Feb 22, 2024
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 @@ -676,4 +676,56 @@ void empty () throws IOException

assertThat( check.task(":run").getOutcome() ).isEqualTo( SKIPPED );
}

@DisplayName("link a large source set")
@Test
void largeSourceSet () throws IOException
{
Files.createDirectories(projectDir.resolve("src/main/cpp"));
Files.writeString(projectDir.resolve("src/main/cpp/foo.h"),
"""
int foo (int i);
"""
);

Files.createDirectories(projectDir.resolve("src/main/c"));
Files.writeString(projectDir.resolve("src/main/c/foo.c"),
"""
#include <foo.h>
int foo (int i) { return i % 42; }
"""
);
for (int i = 0, j = 512; i != j; ++i)
{
Files.writeString(projectDir.resolve("src/main/c/%d.c".formatted(i)),
"""
#include <foo.h>
int foo_%d () { return foo(%d); }
""".formatted(i,i)
);
}
Files.writeString(projectDir.resolve("src/main/c/main.c"),
"""
#include <foo.h>
int main (int argc, char* argv[]) { return foo(0); }
"""
);

Files.writeString(projectDir.resolve("build.gradle.kts"),
"""
plugins {
id("br.dev.pedrolamarao.metal.application")
id("br.dev.pedrolamarao.metal.c")
}
"""
);

final var archive = GradleRunner.create()
.withArguments("--build-cache","--configuration-cache","link")
.withPluginClasspath()
.withProjectDir(projectDir.toFile())
.build();

assertThat( archive.task(":link").getOutcome() ).isEqualTo(SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -781,4 +781,51 @@ void empty () throws IOException
assertThat( check.task(":runTest").getOutcome() ).isEqualTo( SKIPPED );
assertThat( check.task(":check").getOutcome() ).isEqualTo( UP_TO_DATE );
}

@DisplayName("archive a large source set")
@Test
void largeSourceSet () throws IOException
{
Files.createDirectories(projectDir.resolve("src/main/cpp"));
Files.writeString(projectDir.resolve("src/main/cpp/foo.h"),
"""
int foo (int i);
"""
);

Files.createDirectories(projectDir.resolve("src/main/c"));
Files.writeString(projectDir.resolve("src/main/c/foo.c"),
"""
#include <foo.h>
int foo (int i) { return i % 42; }
"""
);

for (int i = 0, j = 512; i != j; ++i)
{
Files.writeString(projectDir.resolve("src/main/c/%d.c".formatted(i)),
"""
#include <foo.h>
int foo_%d () { return foo(%d); }
""".formatted(i,i)
);
}

Files.writeString(projectDir.resolve("build.gradle.kts"),
"""
plugins {
id("br.dev.pedrolamarao.metal.library")
id("br.dev.pedrolamarao.metal.c")
}
"""
);

final var archive = GradleRunner.create()
.withArguments("--build-cache","--configuration-cache","archive")
.withPluginClasspath()
.withProjectDir(projectDir.toFile())
.build();

assertThat( archive.task(":archive").getOutcome() ).isEqualTo(SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,27 @@ public MetalArchive ()
* Archive action.
*/
@TaskAction
public void archive ()
public void archive () throws Exception
{
final var archiver = getMetal().get().locateTool(getArchiver().get());
final var options = getOptions().get();
final var output = getOutput().getAsFile().get();

try
{
Files.createDirectories(output.toPath().getParent());
}
catch (IOException e)
{
throw new RuntimeException(e);
final var atFile = this.getTemporaryDir().toPath().resolve("sources");
try (var writer = Files.newBufferedWriter(atFile)) {
getSource().forEach(file -> {
try { writer.write(file.toString().replace("\\","\\\\") + "\n"); }
catch (IOException e) { throw new RuntimeException(e); }
});
}

final var args = new ArrayList<String>();
args.add("rcs");
args.addAll(options);
args.add(output.toString());
getSource().forEach(source -> args.add(source.toString()));
final var command = new ArrayList<String>();
command.add(archiver.toString());
command.add("rcs");
command.addAll(options);
command.add(output.toString());
command.add("@"+atFile);

getExec().exec(it -> {
it.executable(archiver);
it.args(args);
});
getExec().exec(it -> it.commandLine(command));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public MetalLink ()
* Link action.
*/
@TaskAction
public void link ()
public void link () throws Exception
{
final var metal = getMetal().get();

Expand All @@ -117,28 +117,28 @@ public void link ()
final var output = getOutput().getAsFile().get();
final var target = getTarget().map(this::targetMapper).get();

try
{
Files.createDirectories(output.toPath().getParent());
}
catch (IOException e)
{
throw new RuntimeException(e);
final var atFile = this.getTemporaryDir().toPath().resolve("sources");
try (var writer = Files.newBufferedWriter(atFile)) {
getSource().forEach(file -> {
try { writer.write(file.toString().replace("\\","\\\\") + "\n"); }
catch (IOException e) { throw new RuntimeException(e); }
});
getLinkDependencies().forEach(file -> {
try { writer.write(file.toString().replace("\\","\\\\") + "\n"); }
catch (IOException e) { throw new RuntimeException(e); }
});
}

final var args = new ArrayList<String>();
args.add("--target=%s".formatted(target));
if (! target.contentEquals(host)) args.add("-fuse-ld=lld");
args.addAll(options);
getLibraryPath().get().forEach(path -> args.add("--library-directory=%s".formatted(path)));
args.add("--output=%s".formatted(output));
getSource().forEach(source -> args.add(source.toString()));
getLinkDependencies().forEach(linkable -> args.add(linkable.toString()));

getExec().exec(it -> {
it.executable(linker);
it.args(args);
});
final var command = new ArrayList<String>();
command.add(linker.toString());
command.add("--target=%s".formatted(target));
if (! target.contentEquals(host)) command.add("-fuse-ld=lld");
command.addAll(options);
getLibraryPath().get().forEach(path -> command.add("--library-directory=%s".formatted(path)));
command.add("--output=%s".formatted(output));
command.add("@"+atFile);

getExec().exec(it -> it.commandLine(command));
}

String targetMapper (String target)
Expand Down
Loading