sourceSets.main.java.srcDirs = ["src"]
sourceSets.test.java.srcDirs = ["test"]
sourceSets.test.resources.srcDirs = ["test/resources"]

dependencies{
    testImplementation libraries.junit
    testImplementation aproj(":natives:natives-desktop")
}

test{
    testLogging{
        exceptionFormat = 'full'
        showStandardStreams = true
    }
}

apply plugin: "com.badlogicgames.gdx.gdx-jnigen"

file("jni").mkdir()

jnigen{
    sharedLibName = "arc"
    temporaryDir = file("build/target/native").absolutePath
    libsDir = file("libs").absolutePath

    all{
        headerDirs += ["soloud/include"]
        cppIncludes = ["*.cpp", "soloud/src/core/**", "soloud/src/audiosource/wav/**", "soloud/src/filter/**"]
        cIncludes = ["*.c", "soloud/src/core/**", "soloud/src/audiosource/wav/**", "soloud/src/filter/**"]
    }
    add(Linux, x64){
        cppIncludes += ["soloud/src/backend/miniaudio/*.cpp"]
        cppFlags = "-DWITH_MINIAUDIO " + cppFlags
        libraries += " -lpthread -lrt -lm -ldl"
    }
    add(Windows, x32){
        cppIncludes += ["soloud/src/backend/miniaudio/*.cpp"]
        cppFlags = "-msse -DWITH_MINIAUDIO " + cppFlags
    }
    add(Windows, x64){
        cppIncludes += ["soloud/src/backend/miniaudio/*.cpp"]
        cppFlags = "-msse -DWITH_MINIAUDIO " + cppFlags
    }
    add(Android){
        linkerFlags += " -llog -lOpenSLES"
        cppIncludes += ["soloud/src/backend/opensles/*.cpp"]
        cppFlags = "-DWITH_OPENSLES " + cppFlags
    }
    add(MacOsX, x64){
        cppIncludes += ["soloud/src/backend/coreaudio/*.cpp"]
        cppFlags = "-std=c++11 -DWITH_COREAUDIO " + cppFlags
        libraries += "-Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox"
    }
    add(MacOsX, x64, ARM){
        cppIncludes += ["soloud/src/backend/coreaudio/*.cpp"]
        cppFlags = "-std=c++11 -DWITH_COREAUDIO " + cppFlags.replace("x86_64", "arm64")
        cFlags = cFlags.replace("x86_64", "arm64")
        libraries += "-Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox"
        linkerFlags = linkerFlags.replace("x86_64", "arm64")
    }
    add(IOS){
        headerDirs += ["iosgl"]
        cppIncludes += ["soloud/src/backend/coreaudio/*.cpp", "iosgl/**"]
        cppFlags = "-stdlib=libc++ -std=c++11 -DWITH_COREAUDIO " + cppFlags
        libraries += "-Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox"
        linkerFlags += " -undefined dynamic_lookup "
    }
}

task fixWrap{
    doFirst{
        println "--FIXING SCRIPTS--"
        copy{
            from "../natives/memcpy_wrap.c"
            into "jni/"
        }

        def f = file("$rootDir/arc-core/jni/build-linux64.xml");
        if(f.exists()){
            f.text = f.text.replace("-Wl,-wrap,memcpy", "-Wl,-wrap,memcpy,-wrap,pow,-wrap,powf,-wrap,log,-wrap,logf,-wrap,exp,-wrap,expf,-wrap,clock_gettime")
        }
    }
}

getTasksByName("jnigen", true).each{
    it.finalizedBy fixWrap
}

task cleanNatives{
    doLast{
        delete{delete "$rootDir/arc-core/jni"}
        delete{delete "$rootDir/arc-core/libs"}
        delete{delete "$rootDir/arc-core/csrc/soloud"}
        delete{delete "$rootDir/arc-core/csrc/stb_image.h"}
    }
}

task preJni{
    doFirst{
        if(!file("csrc/stb_image.h").exists()){
            println "Fetching stb_image source..."
            //currently locked to a specific commit
            "curl -o $rootDir/arc-core/csrc/stb_image.h https://raw.githubusercontent.com/nothings/stb/e140649ccf40818781b7e408f6228a486f6d254b/stb_image.h".execute().waitFor()
        }

        if(!file("csrc/soloud").exists()){
            println "Fetching soloud source..."
            "git clone --depth 1 --branch v0.11 https://github.com/Anuken/soloud.git $rootDir/arc-core/csrc/soloud".execute().waitFor()
        }

        copy{
            from "csrc/"
            into "jni/"
            include "**"
        }
    }
}

//copies files into the right locations
task postJni{
    doLast{
        copy{
            from "libs/linux64", "libs/windows32", "libs/windows64", "libs/macosx64"
            into "../natives/natives-desktop/libs"
            include "**"
        }

        ["arm64-v8a", "x86", "x86_64", "armeabi-v7a"].each{ p ->
            copy{
                from "libs/$p"
                into "../natives/natives-android/libs/$p/"
                include "**"
            }
        }

        [".tvos", ""].each{ p ->
            copy{
                from "libs/ios32/libarc.a$p"
                into "../natives/natives-ios/libs/"
                include "**"
            }
        }
    }
}

jnigenBuild.finalizedBy postJni

getTasksByName("jnigen", true).each{
    it.dependsOn preJni
}
