export_lib.sh 4.3 KB
Newer Older
Y
yejianwu 已提交
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash

set -e

Usage() {
  echo "Usage: ./tools/export_lib.sh android_abi[armeabi-v7a/arm64-v8a] runtime[gpu/dsp] export_include_dir export_lib_dir"
  echo "eg: ./tools/export_lib.sh armeabi-v7a ../include ../lib/libmace_v7"
}

if [ $# -lt 4 ]; then
  Usage
L
Liangliang He 已提交
12
  exit 1
Y
yejianwu 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
fi

# ANDROID_ABI=arm64-v8a
# ANDROID_ABI=armeabi-v7a
ANDROID_ABI=$1
RUNTIME=$2
EXPORT_INCLUDE_DIR=$3
EXPORT_LIB_DIR=$4

if [ x"${RUNTIME}" = x"dsp" ]; then
  DSP_MODE_BUILD_FLAGS="--define hexagon=true"
fi

MACE_SOURCE_DIR=`/bin/pwd`
CODEGEN_DIR=${MACE_SOURCE_DIR}/mace/codegen
CL_CODEGEN_DIR=${CODEGEN_DIR}/opencl
VERSION_CODEGEN_DIR=${CODEGEN_DIR}/version
STRIP="--strip always"

32 33 34
# TO REMOVE
LIBMACE_TEMP_DIR=`mktemp -d -t libmace.XXXX`

Y
yejianwu 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
LIBMACE_NAME="libmace"
LIBMACE_DEV_NAME="libmace_dev"
LIBMACE_PROD_NAME="libmace_prod"

libmace_targets=(
  "//mace/ops:ops"
  "//mace/kernels:kernels"
  "//mace/codegen:generated_version"
  "//mace/core:core"
  "//mace/utils:logging"
)

libmace_dev_targets=(
  "//mace/codegen:generated_opencl_dev"
  "//mace/core:opencl_dev"
  "//mace/utils:tuner_dev"
)

libmace_prod_targets=(
  "//mace/core:opencl_prod"
  "//mace/utils:tuner_prod"
)

all_targets=(${libmace_targets[*]} ${libmace_dev_targets[*]} ${libmace_prod_targets[*]})

build_target()
{
  BAZEL_TARGET=$1
  bazel build --verbose_failures -c opt --strip always $BAZEL_TARGET \
    --crosstool_top=//external:android/crosstool \
    --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
     --cpu=$ANDROID_ABI \
    --copt="-std=c++11" \
    --copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
    --copt="-Werror=return-type" \
    --copt="-DMACE_OBFUSCATE_LITERALS" \
L
Liangliang He 已提交
71
    $DSP_MODE_BUILD_FLAGS || exit 1
Y
yejianwu 已提交
72 73
}

L
liuqi 已提交
74 75 76 77 78 79 80 81 82 83 84
build_local_target()
{
  BAZEL_TARGET=$1
  bazel build --verbose_failures -c opt --strip always $BAZEL_TARGET \
    --copt="-std=c++11" \
    --copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
    --copt="-Werror=return-type" \
    --copt="-DMACE_OBFUSCATE_LITERALS" \
    --define openmp=true || exit -1
}

Y
yejianwu 已提交
85 86 87 88
merge_libs()
{
  CREATE_LIB_NAME=$1
  LIBS_LIST=$2
L
Liangliang He 已提交
89
  echo "create ${LIBMACE_TEMP_DIR}/${CREATE_LIB_NAME}.a" > ${LIBMACE_TEMP_DIR}/${CREATE_LIB_NAME}.mri || exit 1
Y
yejianwu 已提交
90 91 92 93 94 95 96

  for lib_target in ${LIBS_LIST[*]}
  do
    lib_dir=`echo ${lib_target} | cut -d: -f1`
    lib_dir=${lib_dir#//}
    lib_name_prefix=lib`echo ${lib_target} | cut -d: -f2`
    bin_path="${MACE_SOURCE_DIR}/bazel-bin/${lib_dir}/${lib_name_prefix}"
L
liuqi 已提交
97 98 99 100 101 102
    if [ x"${RUNTIME}" = x"local" ]; then
      if [ -f "${bin_path}.pic.a" ]; then
        bin_path="${bin_path}.pic.a"
      else
        bin_path="${bin_path}.pic.lo"
      fi
Y
yejianwu 已提交
103
    else
L
liuqi 已提交
104 105 106 107 108
      if [ -f "${bin_path}.a" ]; then
        bin_path="${bin_path}.a"
      else
        bin_path="${bin_path}.lo"
      fi
Y
yejianwu 已提交
109
    fi
L
Liangliang He 已提交
110
    echo "addlib ${bin_path}" >> ${LIBMACE_TEMP_DIR}/${CREATE_LIB_NAME}.mri || exit 1
Y
yejianwu 已提交
111 112
  done

L
Liangliang He 已提交
113 114
  echo "save" >> ${LIBMACE_TEMP_DIR}/${CREATE_LIB_NAME}.mri || exit 1
  echo "end" >> ${LIBMACE_TEMP_DIR}/${CREATE_LIB_NAME}.mri || exit 1
Y
yejianwu 已提交
115 116

  $ANDROID_NDK_HOME/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar \
L
Liangliang He 已提交
117
    -M < ${LIBMACE_TEMP_DIR}/${CREATE_LIB_NAME}.mri || exit 1
Y
yejianwu 已提交
118 119 120 121 122 123
}


echo "Step 1: Generate encrypted opencl source"
python mace/python/tools/encrypt_opencl_codegen.py \
    --cl_kernel_dir=./mace/kernels/opencl/cl/ \
L
Liangliang He 已提交
124
    --output_path=${CODEGEN_DIR}/opencl/opencl_encrypt_program.cc || exit 1
Y
yejianwu 已提交
125 126 127 128 129


echo "Step 2: Generate version source"
rm -rf ${VERSION_CODEGEN_DIR}
mkdir ${VERSION_CODEGEN_DIR}
L
Liangliang He 已提交
130
bash mace/tools/git/gen_version_source.sh ${CODEGEN_DIR}/version/version.cc || exit 1
Y
yejianwu 已提交
131 132 133 134


echo "Step 3: Build libmace targets"
bazel clean
L
liuqi 已提交
135 136 137 138 139 140 141 142 143 144 145
if [ x"${RUNTIME}" = x"local" ]; then
  for target in ${all_targets[*]}
  do
    build_local_target ${target}
  done
else
  for target in ${all_targets[*]}
  do
    build_target ${target}
  done
fi
Y
yejianwu 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159


echo "Step 4: Create mri files and generate merged libs"
merge_libs "libmace" "${libmace_targets[*]}"
merge_libs "libmace_dev" "${libmace_dev_targets[*]}"
merge_libs "libmace_prod" "${libmace_prod_targets[*]}"


echo "Step 5: Export lib"
rm -rf ${EXPORT_INCLUDE_DIR}
mkdir -p ${EXPORT_INCLUDE_DIR}/mace/core/public
rm -rf ${EXPORT_LIB_DIR}
mkdir -p ${EXPORT_LIB_DIR}

L
Liangliang He 已提交
160 161
cp ${MACE_SOURCE_DIR}/mace/core/public/* ${EXPORT_INCLUDE_DIR}/mace/core/public || exit 1
cp ${LIBMACE_TEMP_DIR}/libmace.a ${LIBMACE_TEMP_DIR}/libmace_dev.a ${LIBMACE_TEMP_DIR}/libmace_prod.a ${EXPORT_LIB_DIR}/ || exit 1
162 163 164

echo "Step 6: Remove temporary file"
rm -rf ${LIBMACE_TEMP_DIR}
Y
yejianwu 已提交
165 166

echo "Done!"