Skip to content

Commit

Permalink
Merge pull request #275 from gradle/wolfs/multiple-curses
Browse files Browse the repository at this point in the history
Support building multiple ncurses variants
  • Loading branch information
wolfs authored Apr 13, 2021
2 parents 8ad2090 + a0c8b80 commit 1471b66
Showing 1 changed file with 51 additions and 26 deletions.
77 changes: 51 additions & 26 deletions buildSrc/src/main/java/gradlebuild/NcursesPlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gradlebuild;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.gradle.model.Each;
import org.gradle.model.Model;
import org.gradle.model.Mutate;
Expand All @@ -14,13 +15,36 @@
import org.gradle.platform.base.PlatformContainer;

import java.io.File;
import java.util.Collection;
import java.util.Objects;

import static gradlebuild.NativeRulesUtils.addPlatform;

public class NcursesPlugin extends RuleSource {

@Model NcursesVersion ncursesVersion() {
return new NcursesVersion(inferNCursesVersion());
private static final NcursesVersion NCURSES_5 = new NcursesVersion("5");

@Model Collection<NcursesVersion> ncursesVersions() {
OperatingSystem os = new DefaultNativePlatform("current").getOperatingSystem();
ImmutableSet.Builder<NcursesVersion> builder = ImmutableSet.builder();
if (!os.isLinux()) {
builder.add(NCURSES_5);
} else {
for (String d : ImmutableList.of("/lib", "/lib64", "/lib/x86_64-linux-gnu", "/lib/aarch64-linux-gnu", "/usr/lib")) {
File libDir = new File(d);
if (new File(libDir, "libncurses.so.6").isFile() || new File(libDir, "libncursesw.so.6").isFile()) {
builder.add(new NcursesVersion("6"));
}
if (new File(libDir, "libncurses.so.5").isFile()) {
builder.add(new NcursesVersion("5"));
}
}
}
ImmutableSet<NcursesVersion> versions = builder.build();
if (versions.isEmpty()) {
throw new IllegalArgumentException("Could not determine ncurses version installed on this machine.");
}
return versions;
}

@Mutate void createPlatforms(PlatformContainer platformContainer) {
Expand All @@ -30,21 +54,26 @@ public class NcursesPlugin extends RuleSource {
addPlatform(platformContainer, "linux_aarch64_ncurses6", "linux", "aarch64");
}

@Mutate void configureBinaries(@Each NativeBinarySpecInternal binarySpec, NcursesVersion ncursesVersion) {
@Mutate void configureBinaries(@Each NativeBinarySpecInternal binarySpec, Collection<NcursesVersion> ncursesVersions) {
NativePlatform targetPlatform = binarySpec.getTargetPlatform();
if (targetPlatform.getOperatingSystem().isLinux() && targetPlatform.getName().contains("ncurses")) {
if (!targetPlatform.getName().contains("ncurses" + ncursesVersion.getNcursesVersion())) {
if (!ncursesVersions.stream().anyMatch(ncursesVersion -> isNcursesVersion(targetPlatform, ncursesVersion))) {
binarySpec.setBuildable(false);
}
}
if (binarySpec.getComponent().getName().contains("Curses")) {
if (targetPlatform.getOperatingSystem().isLinux() && !ncursesVersion.getNcursesVersion().equals("5")) {
if (targetPlatform.getOperatingSystem().isLinux() && !isNcursesVersion(targetPlatform, NCURSES_5)) {
binarySpec.getLinker().args("-lncursesw");
} else {
binarySpec.getLinker().args("-lcurses");
}
}
}

private boolean isNcursesVersion(NativePlatform targetPlatform, NcursesVersion ncursesVersion) {
return targetPlatform.getName().contains("ncurses" + ncursesVersion.getVersionNumber());
}

@Mutate void configureToolChains(NativeToolChainRegistry toolChainRegistry) {
toolChainRegistry.named("gcc", Gcc.class, toolChain -> {
// The core Gradle toolchain for gcc only targets x86 and x86_64 out of the box.
Expand All @@ -54,32 +83,28 @@ public class NcursesPlugin extends RuleSource {
});
}

private static String inferNCursesVersion() {
OperatingSystem os = new DefaultNativePlatform("current").getOperatingSystem();
if (!os.isLinux()) {
return "5";
}
for (String d : ImmutableList.of("/lib", "/lib64", "/lib/x86_64-linux-gnu", "/lib/aarch64-linux-gnu")) {
File libDir = new File(d);
if (new File(libDir, "libncurses.so.6").isFile()) {
return "6";
}
if (new File(libDir, "libncurses.so.5").isFile()) {
return "5";
}
public static class NcursesVersion {
private final String versionNumber;

public NcursesVersion(String versionNumber) {
this.versionNumber = versionNumber;
}
throw new IllegalArgumentException("Could not determine ncurses version installed on this machine.");
}

public static class NcursesVersion {
private final String ncursesVersion;
public String getVersionNumber() {
return versionNumber;
}

public NcursesVersion(String ncursesVersion) {
this.ncursesVersion = ncursesVersion;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NcursesVersion that = (NcursesVersion) o;
return versionNumber.equals(that.versionNumber);
}

public String getNcursesVersion() {
return ncursesVersion;
@Override
public int hashCode() {
return Objects.hash(versionNumber);
}
}
}

0 comments on commit 1471b66

Please sign in to comment.