https://github.com/halide/Halide
Raw File
Tip revision: 25524c2c0bc5e0c15bd905eb247eaf415822dc72 authored by Matthias Kramm on 04 June 2018, 17:21:03 UTC
Add unit test for NotImplementedError from Python extension.
Tip revision: 25524c2
build.gradle
import org.apache.tools.ant.taskdefs.condition.Os

// Avoid conflicts with Bazel on case-insensitive filesystems
buildDir = 'gradle_build' 

repositories {
    jcenter()
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'
    }

}

////////////////////////////////////////////////////////////////////////////////
// Use gradle's native C++ plugin to build the Halide generator.
//
// sources: defines all the C++ source files. We only have one SourceSet called
// hello_generator.
//
// executables: we only make one binary called hello_generator. Here is where
// we pass compiler and linker flags.
//
// binaries.withType: binaries is a collection, which in our case is just the
// hello_generator executable. withType() filters the collection by type.
// binary is the iteration variable. -> defines the body of the lambda:
// for each binary:
//     for each halide_target / Android ABI mapping:
//         for each generator:
//             run the generator with -g and target set
//         make the later ndkBuild task depend on this task.

apply plugin: "cpp"

sources {
    hello_generator {
        cpp(CppSourceSet) {
            source {
                srcDirs "jni/", "${projectDir}/../../tools/"
                include "hello_generator.cpp", "GenGen.cpp"
            }
        }
    }
}

executables {
    hello_generator {
        binaries.all {
            cppCompiler.args "-std=c++11", "-g", "-Wall", "-fno-rtti", "-I", "${projectDir}/../../include", "-I", "${projectDir}/../../build/include"
            // "/bin" assumes Makefile build for Halide; "/build/lib" assumes CMake build
            linker.args "-lHalide", "-ldl", "-lpthread", "-lz", "-L", "${projectDir}/../../bin", "-L", "${projectDir}/../../build/lib"
        }
    }
}

binaries.withType(NativeExecutableBinary) { binary ->
    def bin = "${projectDir}/bin"
    def linkTask = binary.tasks.link
    println "linktask output file is " + linkTask.outputFile
    Map<String, String> archs = [
        // armeabi and armeabi-v7a are the same as far as Halide is concerned
        "armeabi":      "arm-32-android",
        "armeabi-v7a":  "arm-32-android",
        "arm64-v8a":    "arm-64-android",
        "mips":         "mips-32-android",
        "x86_64":       "x86-64-android-sse41",
        "x86":          "x86-32-android"
    ]
    archs.each {
        arch -> println "creating task for: " + arch.key + " -> " + arch.value
        def android_abi = arch.key
        def hl_target = arch.value
        def task_name = "generate_halide_binary_${binary.name.capitalize()}_${android_abi}"
        def destDir = new File(bin, "${android_abi}")
        def generateHalideTask = task(task_name) {
            dependsOn linkTask
            doFirst {
                println "Executing: " + linkTask.outputFile + " ..."
                destDir.mkdirs()
                def envVars = [ "DYLD_LIBRARY_PATH=${projectDir}/../../bin", "LD_LIBRARY_PATH=${projectDir}/../../bin" ]
                def proc = [linkTask.outputFile, "-g", "hello", "-o", "${destDir}", "target=${hl_target}"].execute(envVars, destDir)
                proc.waitFor()
                if (proc.exitValue() != 0) {
                    println "return code: ${proc.exitValue()}"
                    println "stderr: ${proc.err.text}"
                    println "stdout: ${proc.in.text}"
                }
            }
        }

        // Call this task generateHalideTask.
        binary.builtBy generateHalideTask

        // Tell gradle that the task called "ndkBuild" below depends
        // on generateHalideTask.
        ndkBuild.dependsOn generateHalideTask
    }
    println "done with archs"
}


////////////////////////////////////////////////////////////////////////////////

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.hellohalide"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            java.srcDirs = ["src/"]
            // Setting jni.srcDirs to [] disables the automatic ndk-build call
            // which would use parameters defined in build.gradle. Use our own
            // task (ndkBuild) below.
            jni.srcDirs = []
            jniLibs.srcDirs = ["bin/lib/"] // default is src/main/jniLibs
            manifest.srcFile "AndroidManifest.xml"
            res.srcDirs = ["res/"] // default is src/main/res
        }
    }

    // Call regular ndk-build (ndk-build.cmd on Windows) script from
    // app directory.
    task ndkBuild(type: Exec) {
        def ndkDir = project.android.ndkDirectory
        def ndkBuildCmd = ""
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            ndkBuildCmd = "ndk-build.cmd"
        } else {
            ndkBuildCmd = "ndk-build"
        }
        commandLine "$ndkDir/$ndkBuildCmd", "NDK_GEN_OUT=./bin/gen", "NDK_LIBS_OUT=./bin/lib", "NDK_OUT=./bin/obj"
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.2'
}
back to top