// Copyright 2011-2024 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.

import proguard.gradle.ProGuardTask

buildscript {
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.5'
        classpath 'com.github.johnrengelman:shadow:8.1.1'
        classpath 'com.guardsquare:proguard-gradle:7.4.1'
    }
}

apply plugin: 'com.google.protobuf'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

protobuf {
    // Configure the protoc executable
    protoc {
        // To manually specify the compiler binary, use:
        // path = '/path/to/protoc'
        artifact = 'com.google.protobuf:protoc:4.31.0'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.75.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
          grpc {}
        }
    }
}

dependencies {
    compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
    implementation ':y:'
    implementation ':ysvg:'
    implementation 'com.google.code.findbugs:jsr305:3.0.2'
    implementation 'com.google.errorprone:error_prone_annotations:2.30.0'
    implementation 'com.google.flogger:flogger-system-backend:0.8'
    implementation 'com.google.flogger:flogger:0.8'
    implementation 'com.google.guava:guava:33.4.8-jre'
    implementation 'com.google.protobuf:protobuf-java-util:4.31.0'
    implementation 'com.google.protobuf:protobuf-java:4.31.0'
    implementation 'commons-cli:commons-cli:1.6.0'
    implementation 'io.grpc:grpc-netty-shaded:1.75.0'
    implementation 'io.grpc:grpc-protobuf:1.75.0'
    implementation 'io.grpc:grpc-stub:1.75.0'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'org.apache.xmlgraphics:batik-anim:1.17'
    implementation 'org.apache.xmlgraphics:batik-bridge:1.17'
    implementation 'org.apache.xmlgraphics:batik-gvt:1.17'
    implementation 'org.apache.xmlgraphics:batik-svggen:1.17'
    implementation 'org.slf4j:slf4j-nop:2.0.10'
    implementation 'org.jfree:jcommon:1.0.24'
    implementation 'org.jfree:jfreechart:1.0.19'
    implementation 'org.xerial.thirdparty:nestedvm:1.0'
    implementation 'org.xerial:sqlite-jdbc:3.44.1.0'
    implementation project(':zylib')
}

mainClassName = 'com.google.security.zynamics.bindiff.BinDiff'

shadowJar {
    archiveBaseName = 'bindiff-ui'
    // Exclude non-essential files from jar
    exclude('META-INF/LICENSE*',
            'META-INF/NOTICE*',
            'META-INF/maven/',
            'bindiff_config.proto',
            'binexport2.proto',
            'google/protobuf/**/*.proto',
            'license/**',
            // Unsupported platforms: FreeBSD/DragonFly, remove 32-bit
            'org/sqlite/native/DragonFlyBSD/**',
            'org/sqlite/native/FreeBSD/**',
            'org/sqlite/native/Linux-Android/**',
            'org/sqlite/native/Linux/arm/**',
            'org/sqlite/native/Linux/armv6/**',
            'org/sqlite/native/Linux/armv7/**',
            'org/sqlite/native/Linux/ppc64/**',
            'org/sqlite/native/Linux/x86/**',
            'org/sqlite/native/Linux-Musl/x86/**',
            'org/sqlite/native/Windows/aarch64/**',
            'org/sqlite/native/Windows/armv7/**',
            'org/sqlite/native/Windows/x86/**')
}

task obfuscatedJar(type: ProGuardTask) {
    injars shadowJar
    outjars 'build/libs/bindiff-ui-all.out.jar'

    // Java 11 modules
    libraryjars "${System.getProperty('java.home')}/jmods"

    // Only obfuscate yFiles, as required per their license
    keep 'class !y.**,!yext.**,!y.x.**,**'
    keepclassmembers 'class !y.**,!yext.**,!y.x.**,** { *; }'
    keepattributes '*Annotation*,Signature,InnerClasses,EnclosingMethod,Module*'
    renamesourcefileattribute 'SourceFile'
    useuniqueclassmembernames

    dontoptimize  // Optimization uses ~2G RAM
    dontusemixedcaseclassnames  // Prevent macOS/Windows unpacking problems

    // Generally no notices
    dontnote

    // gRPC compile time dependency
    dontwarn 'io.grpc.netty.shaded.io.netty.**'

    // Annotations
    dontwarn 'com.google.errorprone.**'
    dontwarn 'com.google.j2objc.annotations.**'
    dontwarn 'javax.annotation.**'
    dontwarn 'org.codehaus.mojo.animal_sniffer.**'

    // System and system-like packages
    dontwarn 'com.sun.**'
    dontwarn 'java.**'
    dontwarn 'javax.**'
    dontwarn 'jdk.**'
    dontwarn 'sun.**'

    // Class unexpectedly contains class
    dontwarn 'javax.xml.bind.ModuleUtil'
    dontwarn 'module-info'  // Java 9 or higher

    // Library class depends on program class
    dontwarn 'org.w3c.dom.**'

    // Unused packages
    dontwarn 'javax.servlet.**'
    dontwarn 'org.ibex.nestedvm.ClassFileCompiler'
    dontwarn 'org.mozilla.javascript.**'

    // Unused methods/classes
    dontwarn 'org.apache.batik.bridge.**'
    dontwarn 'org.apache.batik.script.**'
    dontwarn 'org.apache.commons.logging.**'
    dontwarn 'org.apache.xml.dtm.**'

    // SAXSVGDocumentFactory has moved to batik-anim
    dontwarn 'yext.svg.view.**'
}
