Skip to content

Commit

Permalink
Add Windows build scripts and fix script bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Dec 5, 2016
1 parent 670fe85 commit eea3cb3
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 46 deletions.
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text eol=lf

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
# *.c text
# *.h text

# Declare files that will always have CRLF line endings on checkout.
*.cmd text eol=crlf

# Denote all files that are truly binary and should not be modified.
busybox binary
futility binary
*.jar binary
*.exe binary
11 changes: 6 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* Busybox: http://forum.xda-developers.com/android/software-hacking/tool-busybox-flashable-archs-t3348543

###How to build Magisk
1. Only support MacOS and Linux
2. Download and install NDK
3. Add the NDK directory into PATH.
To check if success, please try calling `which ndk-build` and see if it returns the NDK directory
4. Execute `./build.sh`, it will give you further information
1. Download and install NDK
2. Add the NDK directory into PATH
To check if the PATH is set correctly, try calling `which ndk-build` (`where ndk-build` on Windows) and see if it shows the NDK directory
3. Unix-like users (e.g. Linux & MacOS) please execute `build.sh` through shell
Windows users please execute `build.cmd` through cmd
4. The scripts will show you further details
159 changes: 159 additions & 0 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET me=%~nx0
SET parent=%~dp0
SET tab=
SET OK=

CD %parent%

call :%~1 "%~2"
IF NOT DEFINED OK CALL :usage

EXIT /B %ERRORLEVEL%

:usage
ECHO %me% all ^<version name^>
ECHO %tab%Build binaries, zip, and sign Magisk
ECHO %tab%This is equlivant to first ^<build^>, then ^<zip^>
ECHO %me% clean
ECHO %tab%Cleanup compiled / generated files
ECHO %me% build
ECHO %tab%Build the binaries with ndk
ECHO %me% zip ^<version name^>
ECHO %tab%Zip and sign Magisk
ECHO %me% uninstaller
ECHO %tab%Zip and sign the uninstaller
EXIT /B 1

:all
SET OK=y
IF [%~1] == [] (
CALL :error "Missing version number"
CALL :usage
EXIT /B %ERRORLEVEL%
)
CALL :build
CALL :zip "%~1"
EXIT /B %ERRORLEVEL%

:build
SET OK=y
ECHO ************************
ECHO * Building binaries
ECHO ************************
FOR /F "tokens=* USEBACKQ" %%F IN (`where ndk-build`) DO (
IF [%%F] == [] (
CALL :error "Please add ndk-build to PATH!"
EXIT /B 1
)
)
CALL ndk-build -j4 || CALL :error "Magisk binary tools build failed...."
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
ECHO ************************
ECHO * Copying binaries
ECHO ************************
COPY /Y libs\armeabi\* zip_static\arm
COPY /Y libs\arm64-v8a\* zip_static\arm64
COPY /Y libs\x86\* zip_static\x86
COPY /Y libs\x86_64\* zip_static\x64
CALL :mkcp libs\armeabi\bootimgtools uninstaller\arm
CALL :mkcp libs\arm64-v8a\bootimgtools uninstaller\arm64
CALL :mkcp libs\x86\bootimgtools uninstaller\x86
CALL :mkcp libs\x86_64\bootimgtools uninstaller\x64
EXIT /B %ERRORLEVEL%

:clean
SET OK=y
ECHO ************************
ECHO * Cleaning up
ECHO ************************
CALL ndk-build clean
forfiles /P zip_static\arm /C "cmd /C IF NOT @file == \"busybox\" DEL @file"
forfiles /P zip_static\arm64 /C "cmd /C IF NOT @file == \"busybox\" DEL @file"
forfiles /P zip_static\x86 /C "cmd /C IF NOT @file == \"busybox\" DEL @file"
forfiles /P zip_static\x64 /C "cmd /C IF NOT @file == \"busybox\" DEL @file"
2>NUL DEL zip_static\META-INF\com\google\android\update-binary
2>NUL DEL zip_static\common\magic_mask.sh
2>NUL RMDIR /S /Q uninstaller\arm
2>NUL RMDIR /S /Q uninstaller\arm64
2>NUL RMDIR /S /Q uninstaller\x86
2>NUL RMDIR /S /Q uninstaller\x64
EXIT /B 0

:zip
SET OK=y
IF [%~1] == [] (
CALL :error "Missing version number"
CALL :usage
EXIT /B %ERRORLEVEL%
)
IF NOT EXIST "zip_static\arm\bootimgtools" CALL :error "Missing binaries! Please run '%me% build' before zipping!"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
ECHO ************************
ECHO * Adding version info
ECHO ************************
powershell.exe -nologo -noprofile -command "(gc -Raw scripts\flash_script.sh) -replace 'MAGISK_VERSION_STUB', 'Magisk v%~1 Boot Image Patcher' | sc zip_static\META-INF\com\google\android\update-binary"
powershell.exe -nologo -noprofile -command "(gc -Raw scripts\magic_mask.sh) -replace 'MAGISK_VERSION_STUB', 'setprop magisk.version \"%~1\"' | sc zip_static\common\magic_mask.sh"
ECHO ************************
ECHO * Zipping Magisk v%~1
ECHO ************************
CD zip_static
2>NUL DEL "..\Magisk-v%~1.zip"
..\ziptools\win_bin\zip "..\Magisk-v%~1.zip" -r .
CD ..\
CALL :sign_zip "Magisk-v%~1.zip"
EXIT /B %ERRORLEVEL%

:uninstaller
SET OK=y
IF NOT EXIST "uninstaller\arm\bootimgtools" CALL :error "Missing binaries! Please run '%me% build' before zipping!"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
ECHO ************************
ECHO * Zipping uninstaller
ECHO ************************
FOR /F "tokens=* USEBACKQ" %%F IN (`ziptools\win_bin\date "+%%Y%%m%%d"`) DO (set timestamp=%%F)
CD uninstaller
2>NUL DEL "../Magisk-uninstaller-%timestamp%.zip"
..\ziptools\win_bin\zip "../Magisk-uninstaller-%timestamp%.zip" -r .
CD ..\
CALL :sign_zip "Magisk-uninstaller-%timestamp%.zip"
EXIT /B %ERRORLEVEL%

:sign_zip
IF NOT EXIST "ziptools\win_bin\zipadjust.exe" (
ECHO ************************
ECHO * Compiling ZipAdjust
ECHO ************************
gcc -o ziptools\win_bin\zipadjust ziptools\src\*.c -lz || CALL :error "ZipAdjust Build failed...."
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
)
SET basename="%~1"
SET basename="%basename:.zip=%"
ECHO ************************
ECHO * First sign %~1
ECHO ************************
java -jar "ziptools\signapk.jar" "ziptools\test.certificate.x509.pem" "ziptools\test.key.pk8" "%~1" "%basename:"=%-firstsign.zip"
ECHO ************************
ECHO * Adjusting %~1
ECHO ************************
ziptools\win_bin\zipadjust "%basename:"=%-firstsign.zip" "%basename:"=%-adjusted.zip"
ECHO ************************
ECHO * Final sign %~1
ECHO ************************
java -jar "ziptools\minsignapk.jar" "ziptools\test.certificate.x509.pem" "ziptools\test.key.pk8" "%basename:"=%-adjusted.zip" "%basename:"=%-signed.zip"

MOVE /Y "%basename:"=%-signed.zip" "%~1"
DEL "%basename:"=%-adjusted.zip" "%basename:"=%-firstsign.zip"
EXIT /B %ERRORLEVEL%

:mkcp
2>NUL MKDIR "%~2"
2>NUL COPY /Y "%~1" "%~2"
EXIT /B 0

:error
ECHO.
ECHO ! %~1
ECHO.
EXIT /B 1
56 changes: 17 additions & 39 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
usage() {
echo "$0 all <version name>"
echo -e "\tBuild binaries, zip, and sign Magisk"
echo -e "\tThis is equlivant to first --build, then --zip"
echo -e "\tThis is equlivant to first <build>, then <zip>"
echo "$0 clean"
echo -e "\tCleanup compiled / generated files"
echo "$0 build"
Expand Down Expand Up @@ -37,23 +37,17 @@ mkcp() {
cp -afv $1 $2
}

error() {
echo -e "\n! $1\n"
exit 1
}

build_bin() {
echo "************************"
echo "* Building binaries"
echo "************************"
if [ -z `which ndk-build` ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
echo "! Please add ndk-build to PATH!"
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
exit 1
fi
ndk-build -j4
if [ $? -ne 0 ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
echo "! Magisk binary tools build failed...."
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
exit 1
fi
[ -z `which ndk-build` ] && error "Please add ndk-build to PATH!"
ndk-build -j4 || error "Magisk binary tools build failed...."
echo "************************"
echo "* Copying binaries"
echo "************************"
Expand All @@ -68,13 +62,7 @@ build_bin() {
}

zip_package() {
if [ ! -f "zip_static/arm/bootimgtools" ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
echo "! Missing binaries!!"
echo "! Please run \"$0 build\" before zipping"
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
exit 1
fi
[ ! -f "zip_static/arm/bootimgtools" ] && error "Missing binaries!! Please run '$0 build' before zipping"
echo "************************"
echo "* Adding version info"
echo "************************"
Expand All @@ -93,50 +81,40 @@ zip_package() {
}

zip_uninstaller() {
if [ ! -f "uninstaller/arm/bootimgtools" ]; then
echo "! Missing binaries!!"
echo "! Please run \"$0 build\" before zipping"
exit 1
fi
[ ! -f "uninstaller/arm/bootimgtools" ] && error "Missing binaries!! Please run '$0 build' before zipping"
echo "************************"
echo "* Zipping uninstaller"
echo "************************"
cd uninstaller
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
TIMESTAMP=$(date "+%Y%m%d")
TIMESTAMP=`date "+%Y%m%d"`
rm -rf "../Magisk-uninstaller-$TIMESTAMP.zip"
zip "../Magisk-uninstaller-$TIMESTAMP.zip" -r .
cd ../
sign_zip "Magisk-uninstaller-$TIMESTAMP.zip"
}

sign_zip() {
if [ ! -f "zipsigntools/zipadjust" ]; then
if [ ! -f "ziptools/zipadjust" ]; then
echo "************************"
echo "* Compiling ZipAdjust"
echo "************************"
gcc -o zipsigntools/zipadjust zipsigntools/src/*.c -lz
if [ $? -ne 0 ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
echo "! ZipAdjust Build failed...."
echo "!!!!!!!!!!!!!!!!!!!!!!!!"
exit 1
fi
chmod 755 zipsigntools/zipadjust
gcc -o ziptools/zipadjust ziptools/src/*.c -lz || error "ZipAdjust Build failed...."
chmod 755 ziptools/zipadjust
fi
echo "************************"
echo "* First sign $1"
echo "************************"
java -jar "zipsigntools/signapk.jar" "zipsigntools/test.certificate.x509.pem" "zipsigntools/test.key.pk8" "$1" "${1%.*}-firstsign.zip"
java -jar "ziptools/signapk.jar" "ziptools/test.certificate.x509.pem" "ziptools/test.key.pk8" "$1" "${1%.*}-firstsign.zip"
echo "************************"
echo "* Adjusting $1"
echo "************************"
zipsigntools/zipadjust "${1%.*}-firstsign.zip" "${1%.*}-adjusted.zip"
ziptools/zipadjust "${1%.*}-firstsign.zip" "${1%.*}-adjusted.zip"
echo "************************"
echo "* Final sign $1"
echo "************************"
java -jar "zipsigntools/signapk.jar" "zipsigntools/test.certificate.x509.pem" "zipsigntools/test.key.pk8" "${1%.*}-adjusted.zip" "${1%.*}-signed.zip"
java -jar "ziptools/minsignapk.jar" "ziptools/test.certificate.x509.pem" "ziptools/test.key.pk8" "${1%.*}-adjusted.zip" "${1%.*}-signed.zip"

mv "${1%.*}-signed.zip" "$1"
rm "${1%.*}-adjusted.zip" "${1%.*}-firstsign.zip"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 7 additions & 2 deletions zipsigntools/src/zipadjust.c → ziptools/src/zipadjust.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#include <errno.h>
#include <zlib.h>

#ifndef O_BINARY
#define O_BINARY 0
#define O_TEXT 0
#endif

#pragma pack(1)
struct local_header_struct {
uint32_t signature;
Expand Down Expand Up @@ -194,7 +199,7 @@ static int xdecompress(int fdIn, int fdOut, off_t offsetIn, off_t offsetOut, siz
int zipadjust(char* filenameIn, char* filenameOut, int decompress) {
int ok = 0;

int fin = open(filenameIn, O_RDONLY);
int fin = open(filenameIn, O_RDONLY | O_BINARY);
if (fin > 0) {
unsigned int size = lseek(fin, 0, SEEK_END);
lseek(fin, 0, SEEK_SET);
Expand Down Expand Up @@ -234,7 +239,7 @@ int zipadjust(char* filenameIn, char* filenameOut, int decompress) {
memset(central_directory_out, 0, central_directory_in_size);

unlink(filenameOut);
int fout = open(filenameOut, O_CREAT | O_WRONLY, 0644);
int fout = open(filenameOut, O_CREAT | O_WRONLY | O_BINARY, 0644);
if (fout > 0) {

uintptr_t central_directory_in_index = 0;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added ziptools/win_bin/date.exe
Binary file not shown.
Binary file added ziptools/win_bin/zip.exe
Binary file not shown.
Binary file added ziptools/win_bin/zipadjust.exe
Binary file not shown.

0 comments on commit eea3cb3

Please sign in to comment.