Home | History | Annotate | Download | only in scripts
      1 #!/bin/bash
      2 # See usage() below for the description.
      3 
      4 function usage() {
      5   cat <<EOF
      6 # This script copies the .jar files that each plugin depends on into the plugins libs folder.
      7 # By default, on Mac & Linux, this script creates symlinks from the libs folder to the jar file.
      8 # Since Windows does not support symlinks, the jar files are copied.
      9 #
     10 # Options:
     11 # -f : to copy files rather than creating symlinks on the Mac/Linux platforms.
     12 # -d : print make dependencies instead of running make; doesn't copy files.
     13 # -c : copy files expected after make dependencies (reported by -d) have been built.
     14 #
     15 # The purpose of -d/-c is to include the workflow in a make file:
     16 # - the make rule should depend on \$(shell create_all_symlinks -d)
     17 # - the rule body should perform   \$(shell create_all_symlinks -c [-f])
     18 EOF
     19 }
     20 
     21 # CD to the top android directory
     22 PROG_DIR=`dirname "$0"`
     23 cd "${PROG_DIR}/../../../"
     24 
     25 HOST=`uname`
     26 USE_COPY=""        # force copy dependent jar files rather than creating symlinks
     27 ONLY_SHOW_DEPS=""  # only report make dependencies but don't build them nor copy.
     28 ONLY_COPY_DEPS=""  # only copy dependencies built by make; uses -f as needed.
     29 
     30 function die() {
     31   echo "Error: $*" >/dev/stderr
     32   exit 1
     33 }
     34 
     35 function warn() {
     36   # Only print something if not in show-deps mode
     37   if [[ -z $ONLY_SHOW_DEPS ]]; then
     38     echo "$*"
     39   fi
     40 }
     41 
     42 function printGradleJarPath() {
     43   # Prints to stdout the absolute path of the JAR assembled for a given gradle project.
     44   # $1 = source dir, e.g. tools/base or tools/swt
     45   # $2 = the gradle project name e.g. common or lint-api
     46   echo "## Quering Gradle properties for '$2' in '$1'." > /dev/stderr
     47   ( cd $1 && \
     48     ./gradlew :$2:properties | awk '
     49         BEGIN { B=""; N=""; V="" }
     50         /^archivesBaseName:/ { N=$2 }
     51         /^buildDir:/         { D=$2 }
     52         /^version:/          { V=$2 }
     53         END { print D "/libs/" N "-" V ".jar" }' )
     54 }
     55 
     56 ## parse arguments
     57 while [ $# -gt 0 ]; do
     58   case "$1" in
     59     "-f" )
     60       USE_COPY="1"
     61       ;;
     62     "-d" )
     63       ONLY_SHOW_DEPS="1"
     64       ;;
     65     "-c" )
     66       ONLY_COPY_DEPS="1"
     67       ;;
     68     * )
     69       usage
     70       exit 2
     71   esac
     72   shift
     73 done
     74 
     75 warn "## Running $0"
     76 
     77 if [[ "${HOST:0:6}" == "CYGWIN" || "$USE_MINGW" == "1" ]]; then
     78   # This is either Cygwin or Linux/Mingw cross-compiling to Windows.
     79   PLATFORM="windows-x86"
     80   if [[ "${HOST:0:6}" == "CYGWIN" ]]; then
     81     # We can't use symlinks under Cygwin
     82     USE_COPY="1"
     83   fi
     84 elif [[ "$HOST" == "Linux" ]]; then
     85   PLATFORM="linux-x86"
     86 elif [[ "$HOST" == "Darwin" ]]; then
     87   PLATFORM="darwin-x86"
     88 else
     89   die "Unsupported platform ($HOST). Aborting."
     90 fi
     91 
     92 if [[ "$USE_COPY" == "1" ]]; then
     93   function cpfile { # $1=source $2=dest $3=optional dest filename
     94     cp -fv $1 $2/$3
     95   }
     96 else
     97   # computes the "reverse" path, e.g. "a/b/c" => "../../.."
     98   function back() {
     99     echo $1 | sed 's@[^/]*@..@g'
    100   }
    101 
    102   function cpfile { # $1=source $2=dest $3=optional dest filename
    103     local src=$1
    104     if [[ "${src:0:1}" != "/" ]]; then
    105       # Not an absolute path. We assume a relative path to be
    106       # relative to the android root and we want to make it
    107       # relative to the destination dir.
    108       src=$(back $2)/$1
    109     fi
    110     ln -svf $src $2/$3
    111   }
    112 fi
    113 
    114 DEST="sdk/eclipse/scripts"
    115 
    116 set -e # fail early
    117 LIBS=""
    118 CP_FILES=""
    119 
    120 
    121 ### Configure which libs to build.
    122 #
    123 # Each entry for LIBS needs to be prefixed with the way we want to build it:
    124 # make: - a library built using its traditional Android.mk
    125 # base: - a gradle library located in tools/base
    126 # swt:  - a gradle library located in toosl/swt
    127 #
    128 # LIBS entries without or with an unknown ":" prefix will generate an error.
    129 
    130 ### BASE ###
    131 
    132 BASE_PLUGIN_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.base/libs"
    133 BASE_PLUGIN_LIBS="base:common swt:sdkstats base:sdklib base:dvlib base:layoutlib-api base:sdk-common"
    134 BASE_PLUGIN_PREBUILTS="\
    135     prebuilts/tools/common/m2/repository/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.jar \
    136     prebuilts/tools/common/m2/repository/org/apache/commons/commons-compress/1.0/commons-compress-1.0.jar \
    137     prebuilts/tools/common/m2/repository/com/google/guava/guava/13.0.1/guava-13.0.1.jar \
    138     prebuilts/tools/common/m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar \
    139     prebuilts/tools/common/m2/repository/commons-codec/commons-codec/1.4/commons-codec-1.4.jar \
    140     prebuilts/tools/common/m2/repository/org/apache/httpcomponents/httpclient/4.1.1/httpclient-4.1.1.jar \
    141     prebuilts/tools/common/m2/repository/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar \
    142     prebuilts/tools/common/m2/repository/org/apache/httpcomponents/httpmime/4.1/httpmime-4.1.jar \
    143     prebuilts/tools/common/m2/repository/org/bouncycastle/bcpkix-jdk15on/1.48/bcpkix-jdk15on-1.48.jar \
    144     prebuilts/tools/common/m2/repository/org/bouncycastle/bcprov-jdk15on/1.48/bcprov-jdk15on-1.48.jar"
    145 
    146 LIBS="$LIBS $BASE_PLUGIN_LIBS"
    147 CP_FILES="$CP_FILES @:$BASE_PLUGIN_DEST $BASE_PLUGIN_LIBS $BASE_PLUGIN_PREBUILTS"
    148 
    149 
    150 ### ADT ###
    151 
    152 ADT_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.adt/libs"
    153 ADT_LIBS="make:ant-glob base:asset-studio base:lint-api base:lint-checks base:ninepatch make:propertysheet \
    154           base:rule-api swt:sdkuilib swt:swtmenubar base:manifest-merger"
    155 ADT_PREBUILTS="\
    156     prebuilts/tools/common/freemarker/freemarker-2.3.19.jar \
    157     prebuilts/tools/common/m2/repository/org/ow2/asm/asm/4.0/asm-4.0.jar \
    158     prebuilts/tools/common/m2/repository/org/ow2/asm/asm-tree/4.0/asm-tree-4.0.jar \
    159     prebuilts/tools/common/m2/repository/org/ow2/asm/asm-analysis/4.0/asm-analysis-4.0.jar \
    160     prebuilts/tools/common/lombok-ast/lombok-ast-0.2.jar"
    161 
    162 LIBS="$LIBS $ADT_LIBS"
    163 CP_FILES="$CP_FILES @:$ADT_DEST $ADT_LIBS $ADT_PREBUILTS"
    164 
    165 
    166 ### DDMS ###
    167 
    168 DDMS_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.ddms/libs"
    169 DDMS_LIBS="base:ddmlib swt:ddmuilib swt:swtmenubar swt:uiautomatorviewer"
    170 
    171 DDMS_PREBUILTS="\
    172     prebuilts/tools/common/m2/repository/jfree/jcommon/1.0.12/jcommon-1.0.12.jar \
    173     prebuilts/tools/common/m2/repository/jfree/jfreechart/1.0.9/jfreechart-1.0.9.jar \
    174     prebuilts/tools/common/m2/repository/jfree/jfreechart-swt/1.0.9/jfreechart-swt-1.0.9.jar"
    175 
    176 LIBS="$LIBS $DDMS_LIBS"
    177 CP_FILES="$CP_FILES @:$DDMS_DEST $DDMS_LIBS $DDMS_PREBUILTS"
    178 
    179 
    180 ### TEST ###
    181 
    182 TEST_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.tests"
    183 TEST_LIBS="make:easymock base:testutils"
    184 TEST_PREBUILTS="prebuilts/tools/common/m2/repository/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.jar"
    185 
    186 LIBS="$LIBS $TEST_LIBS"
    187 CP_FILES="$CP_FILES @:$TEST_DEST $TEST_LIBS $TEST_PREBUILTS"
    188 
    189 
    190 ### BRIDGE ###
    191 
    192 if [[ $PLATFORM != "windows-x86" ]]; then
    193   # We can't build enough of the platform on Cygwin to create layoutlib
    194   BRIDGE_LIBS="make:layoutlib base:ninepatch"
    195 
    196   LIBS="$LIBS $BRIDGE_LIBS"
    197 fi
    198 
    199 
    200 ### HIERARCHYVIEWER ###
    201 
    202 HV_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.hierarchyviewer/libs"
    203 HV_LIBS="swt:hierarchyviewer2lib swt:swtmenubar"
    204 
    205 LIBS="$LIBS $HV_LIBS"
    206 CP_FILES="$CP_FILES @:$HV_DEST $HV_LIBS"
    207 
    208 
    209 ### TRACEVIEW ###
    210 
    211 TV_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.traceview/libs"
    212 TV_LIBS="swt:traceview"
    213 
    214 LIBS="$LIBS $TV_LIBS"
    215 CP_FILES="$CP_FILES @:$TV_DEST $TV_LIBS"
    216 
    217 
    218 ### MONITOR ###
    219 
    220 MONITOR_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.monitor/libs"
    221 MONITOR_LIBS="swt:sdkuilib"
    222 
    223 LIBS="$LIBS $MONITOR_LIBS"
    224 CP_FILES="$CP_FILES @:$MONITOR_DEST $MONITOR_LIBS"
    225 
    226 
    227 ### SDKMANAGER ###
    228 
    229 SDKMAN_LIBS="swt:swtmenubar"
    230 
    231 LIBS="$LIBS $SDKMAN_LIBS"
    232 
    233 
    234 ### GL DEBUGGER ###
    235 
    236 if [[ $PLATFORM != "windows-x86" ]]; then
    237   # liblzf doesn't build under cygwin. If necessary, this should be fixed first.
    238 
    239   GLD_DEST="sdk/eclipse/plugins/com.android.ide.eclipse.gldebugger/libs"
    240   GLD_LIBS="make:host-libprotobuf-java-2.3.0-lite make:liblzf"
    241 
    242   LIBS="$LIBS $GLD_LIBS"
    243   CP_FILES="$CP_FILES @:$GLD_DEST $GLD_LIBS"
    244 fi
    245 
    246 
    247 #--- Determine what to build
    248 
    249 UNPROCESSED=""
    250 GRADLE_SWT=""
    251 GRADLE_BASE=""
    252 MAKE_TARGETS=""
    253 for LIB in $LIBS; do
    254   if [[ "${LIB:0:5}" == "base:" ]]; then
    255     GRADLE_BASE="$GRADLE_BASE :${LIB:5}:assemble"
    256   elif [[ "${LIB:0:4}" == "swt:" ]]; then
    257     GRADLE_SWT="$GRADLE_SWT :${LIB:4}:assemble"
    258   elif [[ "${LIB:0:5}" == "make:" ]]; then
    259     MAKE_TARGETS="$MAKE_TARGETS ${LIB:5}"
    260   else
    261     UNPROCESSED="$UNPROCESSED $LIB"
    262   fi
    263 done
    264 
    265 unset LIBS   # we shouldn't use this anymore, it has been split up just above.
    266 
    267 
    268 if [[ -n $UNPROCESSED ]]; then
    269   die "## The following libs lack a prefix (make:, base: or swt:): $UNPROCESSED"
    270 fi
    271 
    272 # In the mode to only echo dependencies, output them and we're done
    273 if [[ -n $ONLY_SHOW_DEPS ]]; then
    274   echo $MAKE_TARGETS
    275   exit 0
    276 fi
    277 
    278 # --- Gradle Build ---
    279 
    280 # tools/base: if we need it for SWT, we build them all and public local.
    281 # Otherwise we do a specific tools/base build on just the requested targets.
    282 
    283 if [[ -n "$GRADLE_SWT" ]]; then
    284   echo "### Starting tools/base: gradlew publishLocal"
    285   (cd tools/base && ./gradlew publishLocal)
    286 elif [[ -n "$GRADLE_BASE" ]]; then
    287   echo "### Starting tools/base: gradlew $GRADLE_BASE"
    288   (cd tools/base && ./gradlew $GRADLE_BASE)
    289 fi
    290 
    291 # tools/swt: build requested targets
    292 
    293 if [[ -n "$GRADLE_SWT" ]]; then
    294   echo "### Starting tools/swt: gradlew $GRADLE_SWT"
    295   (cd tools/swt && ./gradlew $GRADLE_SWT)
    296 fi
    297 
    298 # --- Android.mk Build ---
    299 
    300 # If some of the libs are available in prebuilts/devtools, use link to them directly
    301 # instead of trying to rebuild them so remove them from the libs to build. Note that
    302 # they are already listed in CP_FILES so we'll adjust the source to copy later.
    303 
    304 NEW_TARGETS=""
    305 for LIB in $MAKE_TARGETS; do
    306   J="prebuilts/devtools/tools/lib/$LIB.jar"
    307   if [[ ! -f $J ]]; then
    308     J="prebuilts/devtools/adt/lib/$LIB.jar"
    309   fi
    310   if [[ -f $J ]]; then
    311     warn "## Using existing $J"
    312   else
    313     NEW_TARGETS="$NEW_TARGETS $LIB"
    314   fi
    315 done
    316 MAKE_TARGETS="$NEW_TARGETS"
    317 unset NEW_TARGETS
    318 
    319 if [[ -z $ONLY_COPY_DEPS ]]; then
    320   if [[ -n $MAKE_TARGETS ]]; then
    321     ( # Make sure we have lunch sdk-<something>
    322       if [[ ! "$TARGET_PRODUCT" ]]; then
    323         warn "## TARGET_PRODUCT is not set, running build/envsetup.sh"
    324         . build/envsetup.sh
    325         warn "## lunch sdk-eng"
    326         lunch sdk-eng
    327       fi
    328 
    329       J="4"
    330       [[ $(uname) == "Darwin" ]] && J=$(sysctl hw.ncpu | cut -d : -f 2 | tr -d ' ')
    331       [[ $(uname) == "Linux"  ]] && J=$(cat /proc/cpuinfo | grep processor | wc -l)
    332 
    333       warn "## Building libs: make -j$J $MAKE_TARGETS"
    334       make -j${J} $MAKE_TARGETS
    335     )
    336   fi
    337 fi
    338 
    339 # --- Copy resulting files ---
    340 
    341 DEST=""
    342 for SRC in $CP_FILES; do
    343   if [[ "${SRC:0:2}" == "@:" ]]; then
    344     DEST="${SRC:2}"
    345     mkdir -vp "$DEST"
    346     continue
    347   fi
    348 
    349   ORIG_SRC="$SRC"
    350   DEST_FILE=""
    351 
    352   if [[ "${SRC:0:5}" == "base:" ]]; then
    353     SRC="${SRC:5}"
    354     ORIG_SRC="$SRC"
    355     DEST_FILE="$SRC.jar"
    356     SRC=$(printGradleJarPath tools/base $SRC)
    357   elif [[ "${SRC:0:4}" == "swt:" ]]; then
    358     SRC="${SRC:4}"
    359     ORIG_SRC="$SRC"
    360     DEST_FILE="$SRC.jar"
    361     SRC=$(printGradleJarPath tools/swt $SRC)
    362   elif [[ "${SRC:0:5}" == "make:" ]]; then
    363     SRC="${SRC:5}"
    364     ORIG_SRC="$SRC"
    365   fi
    366 
    367   if [[ ! -f "$SRC" ]]; then
    368     # Take a prebuilts/devtools instead of a framework one if possible.
    369     SRC="prebuilts/devtools/tools/lib/$SRC.jar"
    370     if [[ ! -f "$SRC" ]]; then
    371       SRC="prebuilts/devtools/adt/lib/$ORIG_SRC.jar"
    372     fi
    373     if [[ ! -f "$SRC" ]]; then
    374       SRC="out/host/$PLATFORM/framework/$ORIG_SRC.jar"
    375     fi
    376   fi
    377   if [[ -f "$SRC" ]]; then
    378     if [[ ! -d "$DEST" ]]; then
    379       die "Invalid cp_file dest directory: $DEST"
    380     fi
    381 
    382     cpfile "$SRC" "$DEST" "$DEST_FILE"
    383   else
    384     die "## Unknown source '$ORIG_SRC' to copy in '$DEST'"
    385   fi
    386 done
    387 
    388 # OS-specific post operations
    389 
    390 if [ "${HOST:0:6}" == "CYGWIN" ]; then
    391   chmod -v a+rx "$ADT_DEST"/*.jar
    392 fi
    393 
    394 echo "### $0 done"
    395