SquirrelJME/SquirrelJME

View on GitHub
settings.gradle

Summary

Maintainability
Test Coverage
import java.nio.file.Paths
import java.nio.file.Files
import java.util.regex.Pattern
import java.util.stream.Collectors

// Poking PATH is used by multiple systems
def envRawPathProperty = System.getenv("PATH")
def envPath = (envRawPathProperty != null ?
    Arrays.asList(
        envRawPathProperty.split(Pattern.quote(File.pathSeparator)))
            .stream().<java.nio.file.Path>map({it -> Paths.get(it)})
            .collect(Collectors.toList()) :
            Collections.<java.nio.file.Path>emptyList())

// Do we have Termux in our PATH?
def foundTermuxRoot = false
for (java.nio.file.Path path : envPath) {
    def fullPath = path.toAbsolutePath();
    if (fullPath.toString().startsWith("/data/data/com.termux/")) {
        foundTermuxRoot = true;
    }
}

// Did we find Termux in our PATH?
if ((foundTermuxRoot && System.getProperty("force.termux") == null) ||
    Boolean.getBoolean("force.termux")) {
    // squirreljmeTermuxCompiler
    logger.lifecycle("Appears we are in Termux, kludging compiler build...")
    
    // Enable it
    gradle.beforeProject({proj ->
        proj.ext.squirreljmeTermuxCompiler = true
    })
}

// Is this Mac OS?
def osName = System.getProperty("os.name")
if (osName.equalsIgnoreCase('Mac OS X')) {
    gradle.beforeProject({proj ->
        proj.ext.squirreljmeIsOnMacOs = true
    })
}

// If we are on M1 Macs, perform some modifications so that Gradle can compile
// C++ code
def osArch = System.getProperty("os.arch")  
if (osName.equalsIgnoreCase('Mac OS X') &&
    (osArch.equalsIgnoreCase('aarch64') || osArch.equalsIgnoreCase('arm64') ||
    osArch.equalsIgnoreCase('arm64-v8'))) {
    logger.warn("Faking x86_64 for M1 Macs...")
    
    gradle.beforeProject({proj ->
        proj.ext.squirreljmeMacOsArmCpp = true
    })
} else if (osArch.equalsIgnoreCase("ppc") ||
    osArch.equalsIgnoreCase("powerpc") ||
    osArch.equalsIgnoreCase("ppc64") ||
    osArch.equalsIgnoreCase("ppc64le") ||
    osArch.equalsIgnoreCase("riscv") ||
    osArch.equalsIgnoreCase("riscv32") ||
    osArch.equalsIgnoreCase("riscv64") ||
    osArch.equalsIgnoreCase('aarch64') ||
    osArch.equalsIgnoreCase('arm64') ||
    osArch.equalsIgnoreCase('arm64-v8')) {
    logger.warn("Faking x86_64 for other architectures...")
    
    gradle.beforeProject({proj ->
        proj.ext.squirreljmeClaimX8664 = true
    })
}

// If we have a really high Java version being used then the parameters
// for -source and -target were likely removed, so as such we cannot rely on
// the project being able to be built in such versions.
if (JavaVersion.current() >= JavaVersion.VERSION_HIGHER ||
    Boolean.getBoolean("force.ecj")) {
    // Emit a warning to indicate that the version is quite new
    logger.warn("The current Java version is quite new, " +
        "if Eclipse Java Compiler (ECJ) exists on the system " +
        "then it will be used to compile the modules instead.")
    
    // Does ECJ exist in the system PATH?
    def foundEcjBinary = false
    for (java.nio.file.Path path : envPath) {
        if (Files.exists(path.resolve("ecj"))) {
            foundEcjBinary = true
        }
    }
    
    // If we have the binary, use it
    if (foundEcjBinary) {
        logger.lifecycle("Found ECJ binary, using it!")
        
        gradle.beforeProject({proj ->
            proj.ext.squirreljmeEcjEnabled = true
        })
    } else {
        logger.warn("Could not find ECJ, build may fail!")
    }
}

// On Java 8 we have tools.jar for JavaDoc
FileCollection toolsJar    = files(Paths.get("${System.properties['java.home']}")
    .getParent().resolve("lib").resolve("tools.jar"))
if (!Files.exists(toolsJar.singleFile.toPath())) {
    // If on Java 9 or higher the module must be pulled in, but it does not
    // exist since Java 17
    if (JavaVersion.current() >= JavaVersion.VERSION_1_9 &&
        JavaVersion.current() < JavaVersion.VERSION_16) {
        // Windows/Linux
        toolsJar = files(Paths.get("${System.properties['java.home']}")
            .getParent().resolve("jmods").resolve("jdk.javadoc.jmod"))
        
        // Mac OS
        if (!Files.exists(toolsJar.singleFile.toPath())) {
            toolsJar = files(Paths.get("${System.properties['java.home']}")
                .resolve("jmods").resolve("jdk.javadoc.jmod"))
        }
    }
}

// What should we do for this?
def squirreljmeFakeJavaDocSdk =
    !Files.exists(toolsJar.singleFile.toPath())
if (squirreljmeFakeJavaDocSdk) {
    // Use fake API for building
    gradle.beforeProject({proj ->
        proj.ext.squirreljmeFakeJavaDocSdk = true
    })
} else {
    // Use the real tools Jar for this
    gradle.beforeProject({proj ->
        proj.ext.squirreljmeToolsJar = toolsJar
    })
}

// Modules and available platforms
include "modules"
include "emulators"
include "sdk"
include "tools"

// Recursively include all modules
file(rootProject.projectDir.toPath().resolve("modules"))
    .eachDir(
    { subdir ->
        if (subdir.toPath().resolve("build.gradle").toFile().exists())
        {
            include "modules:" + subdir.name
        }
    })

// Recursively include all emulators
file(rootProject.projectDir.toPath().resolve("emulators"))
    .eachDir(
    { subdir ->
        if (subdir.toPath().resolve("build.gradle").toFile().exists())
        {
            include "emulators:" + subdir.name
        }
    })

// Recursively include all tools
file(rootProject.projectDir.toPath().resolve("tools"))
    .eachDir(
    { subdir ->
        // We may or may not want to include this accordingly
        if ("fake-javadoc-sdk".equals(subdir.name)) {
            if (squirreljmeFakeJavaDocSdk) {
                logger.lifecycle("Including Fake JavaDoc SDK!")
            } else {
                logger.lifecycle("Not including Fake JavaDoc SDK, becuase " +
                    "${toolsJar.getFiles()} exists...")
                return
            }
        }
        
        if (subdir.toPath().resolve("build.gradle").toFile().exists())
        {
            include "tools:" + subdir.name
        }
    })