Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4b7a811d authored by {n8}'s avatar {n8}
Browse files

added squisher and opticharger to envsetup

parent 7d5830d7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1074,7 +1074,7 @@ otapackage: $(INTERNAL_OTA_PACKAGE_TARGET)
bacon: otapackage
	$(hide) \
		WANT_SQUASHFS=$(WANT_SQUASHFS) \
		./vendor/cyanogen/tools/squisher
		squisher

endif    # TARGET_DEVICE != generic
endif    # TARGET_PRODUCT != sdk
+311 −0
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ Invoke ". build/envsetup.sh" from your shell to add the following functions to y
- shgrep:  Greps on all local .sh files.
- godir:   Go to the directory containing a file.
- cmgerrit: Send patch request request to CyanogenMod repos
- opticharger: Point to a valid apkcerts.txt to optimize apk files
- squisher: Completely optimize an otapackage

Look at the source to view more functions. The complete list is:
EOF
@@ -1153,6 +1155,315 @@ function cmgerrit()
	git push ssh://$user@review.cyanogenmod.com:29418/CyanogenMod/$repo HEAD:refs/$mode/$target
}

function opticharger()
{

	#
	# Super-mega opticharger of doom
	# Shrinks apks by running pngcrush on png images
	#
	# Point APKCERTS at the full path to a generated apkcerts.txt file, such as:
	# /home/shade/dev/sources/android-cm-eclair/out/target/product/dream_sapphire/obj/PACKAGING/target_files_intermediates/cyanogen_dream_sapphire-target_files-eng.shade/META/apkcerts.txt
	#
	# cyanogen - shade@chemlab.org
	# ChrisSoyars - me@ctso.me
	#

	for VARS in "$TARGET_PRODUCT" "$TARGET_BUILD_VARIANT"
	do
		if [ ! "$VARS" ]
		then
			echo "Build environment not complete. Try running 'lunch' first"
			return
		fi
	done

	if [ "`uname -a | grep Darwin`" = "" ]
	then
	    local OUT_TARGET_HOST=linux-x86
	else
	    local OUT_TARGET_HOST=darwin-x86
	fi

	set -e
	local QUIET=1
	local QFLAG=-q
	local BASE=`pwd`
	T=$(gettop)
	if [ ! "$T" ]; then
        	echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        	return
    	fi
	if [ "$APKCERTS" = "" ];
	then
    	APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$LOGNAME/META/apkcerts.txt
	    	if [ ! -f "$APKCERTS" ];
	    	then
	    	    echo "Set APKCERTS to the path to your apkcerts.txt file"
	    	    return
	    	fi
	fi

	if [ ! -f "$APKCERTS" ];
	then
	    echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
	    return
	fi

	if [ "`which pngcrush`" = "" ];
	then
	    echo "Please install pngcrush"
	    return
	fi

	for TOOL in "aapt" "zipalign"
	do
		if [ "`which $TOOL`" = "" ]
		then
			echo "Please ensure $TOOL is in your \$PATH"
			return
		fi
	done

	if [ -e "$1" ];
	then
	    local NAME=`basename $1`;
	    echo "Optimizing $NAME...";

	    if [ "$2" != "" ];
	    then
	        local CERT=build/target/product/security/$2.x509.pem
	        local KEY=build/target/product/security/$2.pk8
	        if [ ! -f "$T/$CERT" ];
	        then
	            echo "$CERT does not exist!";
	            return
	        fi
	    else
	        local APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
	        [ $QUIET ] || echo "APKINFO: $APKINFO";
	        if [ "$APKINFO" = "" ];
	        then
	            echo "No apk info for $NAME";
	            return
	        fi
	        local CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
	        local KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
	        if [ "$CERT" = "" ];
	        then
	            echo "Unable to find certificate for $NAME"
	            return
	        fi
	    fi

	    [ $QUIET ] || echo "Certificate: $CERT";

	    [ -d /tmp/$NAME ] && rm -rf /tmp/$NAME
	    mkdir /tmp/$NAME
	    trap "rm -rf /tmp/$NAME; exit" INT TERM EXIT
	    cd /tmp/$NAME
	    unzip -q $BASE/$1
	    for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
	    do
	        [ $QUIET ] || echo "Crushing $x"
	        pngcrush $QFLAG $x $x.crushed 1>/dev/null
	        if [ -e "$x.crushed" ];
	        then
	            mv $x.crushed $x
	        fi
	    done
	    cp $BASE/$1 $BASE/$1.old

	    [ $QUIET ] || echo "Repacking apk.."
	    aapt p -0 res/raw -0 res/raw-en -F $NAME .
    
	    [ $QUIET ] || echo "Resigning with cert: `echo $CERT`"

	    [ $QUIET ] || echo java -jar $T/out/host/$OUT_TARGET_HOST/framework/signapk.jar $T/$CERT $T/$KEY $NAME signed_$NAME
	    java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $T/$CERT $T/$KEY $NAME signed_$NAME
	    [ $QUIET ] || echo "Zipalign.."
	    zipalign -f 4 signed_$NAME $BASE/$1
	    if [ ! $QUIET ]; then
	        ls -l $BASE/$1.old
	        ls -l $BASE/$1
	    fi
	    rm $BASE/$1.old
	else
	    echo "Usage: opticharger [apk file]"
	    return
	fi	
}

function squisher()
{

				###########################################
				# Squish a CM otapackage for distribution #
				# 		cyanogen                  #
				###########################################

	if [ "`uname -a | grep Darwin`" = "" ]
	then
	   local OUT_TARGET_HOST=linux-x86
	   local MD5=md5sum
	else
	   local OUT_TARGET_HOST=darwin-x86
	   local MD5=md5
	fi

	for VARS in "$TARGET_PRODUCT" "$TARGET_BUILD_VARIANT" "$OUT"
	do
		if [ -z "$VARS" ]
		then
			echo "Build environment not complete. Try running 'lunch' first"
			return
		fi
	done

	local OTAPACKAGE=$OUT/$TARGET_PRODUCT-ota-$TARGET_BUILD_VARIANT.$LOGNAME.zip
	if [ ! -f "$OTAPACKAGE" ]; then
		echo "$OTAPACKAGE doesn't exist!";
		return
	fi

	local QUIET=-q
	local DELETE_BINS="applypatch applypatch_static check_prereq recovery updater"


	# Some products want a squashfs for xbin for space
	case "$TARGET_PRODUCT" in
		cyanogen_dream_sapphire) local WANT_SQUASHFS=1 ;;
		*) local WANT_SQUASHFS=0 ;;
	esac

	if [ "$WANT_SQUASHFS" -eq 1 ]; then
		local fatal=0
		local MKSQUASHFS_VER_REQ=4
		if type mksquashfs >/dev/null 2>&1; then
			if mksquashfs -version | grep -q "version $MKSQUASHFS_VER_REQ"; then :; else
				echo
				echo "ERROR: mksquashfs must be at least version $MKSQUASHFS_VER_REQ for this build."
				unset fatal; local fatal=1
			fi
		else
			echo
			echo "ERROR: $TARGET_PRODUCT requires mksquashfs."
			unset fatal; local fatal=1
		fi

		if [ "$fatal" -ne 0 ]; then
			echo
			echo "       Unoptimized package is still available at"
			echo "         $OTAPACKAGE"
			return
		fi
	fi

	local REPACK=$OUT/repack.d
	printf "Sanitizing environment..."
	rm -rf $REPACK
	mkdir -p $REPACK
	echo


	# Unpack the otapackage and opticharge all apks
	mkdir $REPACK/ota
	(
	cd $REPACK/ota
	printf "Unpacking $OTAPACKAGE..."
	unzip $QUIET $OTAPACKAGE
	echo
	cd $REPACK/ota/system/framework
	opticharger framework-res.apk
	cd $REPACK/ota/system/app
	for i in *.apk; do opticharger $i; done
	)


	if [ "$WANT_SQUASHFS" -eq 1 ]; then
		local squash_opts="-force-uid 1000 -force-gid 1000 -no-progress -noappend -no-exports -no-recovery"
		local updater=$REPACK/ota/META-INF/com/google/android/updater-script

		# Relocate su
		cp -a $REPACK/ota/system/xbin $REPACK/_xbin/
		rm -f $REPACK/_xbin/su $REPACK/ota/system/bin/su
		mv $REPACK/ota/system/xbin/su $REPACK/ota/system/bin/su
		chmod -R 555 $REPACK/_xbin/*

		# Create symlinks for su and busybox (since updater-script can't work on the squashfs filesystem).
		# Forgive me for the regex hell here. 
		ln -s ../bin/su $REPACK/_xbin/su
		for link in `sed -n -e's/,//g' -e'/symlink(.*busybox/,/xbin.*);/p' $updater | tr '"' '\n' | sed -n -e'\,/system/xbin/,s,/system/xbin/,,p'`
		do
			ln -s busybox $REPACK/_xbin/$link
		done

		# Create the squashfs with new and improved symlinkage!
		mksquashfs $REPACK/_xbin/* $REPACK/_xbin.sqf $squash_opts
		rm -rf $REPACK/ota/system/xbin/*
		mv $REPACK/_xbin.sqf $REPACK/ota/system/xbin/xbin.sqf
		chmod 444 $REPACK/ota/system/xbin/xbin.sqf

		# Remove xbin stuff and fix up updater-script
		sed -i -e's,system/xbin/su,system/bin/su,g' -e'/xbin/d' $updater
	fi


	# Fix build.prop
	sed -i \
		-e '/ro\.kernel\.android\.checkjni/d' \
		-e '/ro\.build\.type/s/eng/user/' \
		$REPACK/ota/system/build.prop


	# Delete unnecessary binaries
	( cd $REPACK/ota/system/bin; echo $DELETE_BINS | xargs rm -f; )

	# Delete leftover wireless driver
	rm -rf $REPACK/ota/system/lib/modules/*/kernel/drivers/net

	# No need for recovery
	rm -rf $REPACK/ota/recovery

	# Strip modules
	find $REPACK/ota/system/lib/modules -name "*.ko" -print0 | xargs -0 arm-eabi-strip --strip-unneeded

	# Determine what to name the new signed package
	if [ -z "$CYANOGEN_NIGHTLY" ]; then
		MODVERSION=`sed -n -e'/ro\.modversion/s/^.*CyanogenMod-//p' $REPACK/ota/system/build.prop`
		: ${MODVERSION:=nightly}
		local OUTFILE=$OUT/update-cm-$MODVERSION-signed.zip
	else
		local OUTFILE=$OUT/update-squished.zip
	fi

	# Pack it up and sign
	printf "Zipping package..."
	( cd $REPACK/ota; zip $QUIET -r $REPACK/update.zip . )
	echo
	printf "Signing package..."
	local SECURITYDIR=$T/build/target/product/security
	java -Xmx256m \
		-jar $T/out/host/$OUT_TARGET_HOST/framework/signapk.jar \
		-w $SECURITYDIR/testkey.x509.pem $SECURITYDIR/testkey.pk8 \
		$REPACK/update.zip $OUTFILE
	echo
	printf "Cleaning up..."
	rm -rf $REPACK
	echo

	# Create a md5 checksum image of the repacked package
	(
	local img=`basename $OUTFILE`
	cd `dirname $OUTFILE`
	$MD5 $img >$img.md5sum
	echo
	echo "Package complete: $OUTFILE"
	cat $img.md5sum
	echo
	)
}

# Force JAVA_HOME to point to java 1.6 if it isn't already set
function set_java_home() {
    if [ ! "$JAVA_HOME" ]; then