Skip to content

Commit

Permalink
Android: Makefile build functionality added to build.gradle to fully …
Browse files Browse the repository at this point in the history
…support Android Studio builds on Windows. Resolves tensorflow#6385

Change: 149358567
  • Loading branch information
andrewharp authored and tensorflower-gardener committed Mar 7, 2017
1 parent b397419 commit 6bac753
Showing 1 changed file with 65 additions and 14 deletions.
79 changes: 65 additions & 14 deletions tensorflow/examples/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
// This file provides basic support for building the TensorFlow demo
// in Android Studio with Gradle.
//
// Note that Bazel is still used to compile the native libs, and must be
// installed at the location noted below. This build configuration merely
// Note that Bazel is still used by default to compile the native libs,
// and should be installed at the location noted below. This build file
// automates the process of calling out to it and copying the compiled
// libraries back into the appropriate directory.
//
// Alternatively, experimental support for Makefile builds is provided by
// setting buildWithMake below to true. This will allow building the demo
// on Windows machines, but note that full equivalence with the Bazel
// build is not yet guaranteed. See comments below for caveats and tips
// for speeding up the build, such as as enabling ccache.

// Set to true to build with make.
// NOTE: Running a make build will cause subsequent Bazel builds to *fail*
// unless the contrib/makefile/downloads/ and gen/ dirs are deleted afterwards.
def buildWithMake = false

def bazel_location = '/usr/local/bin/bazel'
// Controls output directory in APK and CPU type for Bazel builds.
// NOTE: Does not affect the Makefile build target API (yet), which currently
// assumes armeabi-v7a. If building with make, changing this will require
// editing the Makefile as well.
def cpuType = 'armeabi-v7a'
def nativeDir = 'libs/' + cpuType

// Output directory in the local directory for packaging into the APK.
def nativeOutDir = 'libs/' + cpuType

// Default to building with Bazel and override with make if requested.
def nativeBuildRule = 'buildNativeBazel'
def demoLibPath = '../../../bazel-bin/tensorflow/examples/android/libtensorflow_demo.so'
def inferenceLibPath = '../../../bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so'
if (buildWithMake) {
nativeBuildRule = 'buildNativeMake'
demoLibPath = '../../../tensorflow/contrib/makefile/gen/lib/libtensorflow_demo.so'
inferenceLibPath = '../../../tensorflow/contrib/makefile/gen/lib/libtensorflow_inference.so'
}

// Defines the NDK location for Makefile builds. Does *not* affect Bazel builds.
// Override with your absolute NDK location if this fails to get the location
// automatically.
def makeNdkRoot = System.getenv('NDK_ROOT')

// If building with Bazel, this is the location of the bazel binary.
// NOTE: Bazel does not yet support building for Android on Windows,
// so in this case the Makefile build must be used as described above.
def bazelLocation = '/usr/local/bin/bazel'

project.buildDir = 'gradleBuild'
getProject().setBuildDir('gradleBuild')
Expand Down Expand Up @@ -65,21 +101,36 @@ android {
}
}

task buildNative(type:Exec) {
task buildNativeBazel(type: Exec) {
workingDir '../../..'
commandLine bazelLocation, 'build', '-c', 'opt', \
'tensorflow/examples/android:tensorflow_native_libs', \
'--crosstool_top=https://external:android/crosstool', \
'--cpu=' + cpuType, \
'--host_crosstool_top=@bazel_tools//tools/cpp:toolchain'
}

task buildNativeMake(type: Exec) {
environment "NDK_ROOT", makeNdkRoot
// Tip: install ccache and uncomment the following to speed up
// builds significantly.
// environment "CC_PREFIX", 'ccache'
workingDir '../../..'
commandLine bazel_location, 'build', '-c', 'opt', \
'tensorflow/examples/android:tensorflow_native_libs', \
'--crosstool_top=https://external:android/crosstool', \
'--cpu=' + cpuType, \
'--host_crosstool_top=@bazel_tools//tools/cpp:toolchain'
commandLine 'tensorflow/contrib/makefile/build_all_android.sh', \
'-s', \
'tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in', \
'-t', \
'libtensorflow_inference.so libtensorflow_demo.so' \
//, '-T' // Uncomment to skip protobuf and speed up subsequent builds.
}

task copyNativeLibs(type: Copy) {
from('../../../bazel-bin/tensorflow/contrib/android/') { include '*.so' }
from('../../../bazel-bin/tensorflow/examples/android/') { include '*.so' }
into nativeDir
from demoLibPath
from inferenceLibPath
into nativeOutDir
duplicatesStrategy = 'include'
dependsOn 'buildNative'
dependsOn nativeBuildRule
fileMode 0644
}

assemble.dependsOn copyNativeLibs
Expand Down

0 comments on commit 6bac753

Please sign in to comment.