// Copyright 2019-2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Builds the BinExport extension for a given Ghidra installation. An absolute
// path to the Ghidra installation directory must be supplied either by setting
// the GHIDRA_INSTALL_DIR environment variable or Gradle project property.

apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'eclipse'
apply plugin: 'idea'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        maven {
            url "https://maven-central.storage-download.googleapis.com/maven2/"
        }
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.5'
    }
}

configurations {
    // Configuration that holds jars to include in the jar.
    extraLibs
}

dependencies {
    // Should match ghidra.protobuf.java.version in Ghidra's
    // ghidra/gradle.properties
    extraLibs 'com.google.protobuf:protobuf-java:4.31.0'
    configurations.implementation.extendsFrom(configurations.extraLibs)
}

protobuf {
    // Configure the protoc executable
    protoc {
        // Should match protobuf-java above
        artifact = 'com.google.protobuf:protoc:4.31.0'
    }
    // Make generator tasks visible in Eclipse.
    generateProtoTasks {
        all().each {
            it.group = 'generate proto'
        }
    }
}

// Required for IDEs to find the generated sources.
eclipse {
    classpath {
        file {
            whenMerged {
                def source = entries.find {
                    it.path == 'build/generated/source/proto/main/java' ||
                    it.path == 'build/extracted-include-protos/main'
                }
                source.entryAttributes['ignore_optional_problems'] = 'true'
            }
        }
    }
}

// Extend jar task to collect extra dependencies.
jar {
    from {
        configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

// Standard Ghidra extension Gradle code follows.
//----------------------START "DO NOT MODIFY" SECTION------------------------------
def ghidraInstallDir

if (System.env.GHIDRA_INSTALL_DIR) {
    ghidraInstallDir = System.env.GHIDRA_INSTALL_DIR
}
else if (project.hasProperty("GHIDRA_INSTALL_DIR")) {
    ghidraInstallDir = project.getProperty("GHIDRA_INSTALL_DIR")
}

if (ghidraInstallDir) {
    apply from: new File(ghidraInstallDir).getCanonicalPath() + "/support/buildExtension.gradle"
}
else {
    throw new GradleException("GHIDRA_INSTALL_DIR is not defined!")
}
//----------------------END "DO NOT MODIFY" SECTION-------------------------------

// Fix for newer Gradle versions that do not allow undeclared dependencies
tasks.named("extractIncludeProto").configure {
    dependsOn("copyDependencies")
}
tasks.named("extractIncludeTestProto").configure {
    dependsOn("copyDependencies")
}

