Skip to content

Commit

Permalink
Split up release script
Browse files Browse the repository at this point in the history
  • Loading branch information
KuhakuPixel committed Mar 22, 2023
1 parent 540633a commit 1539b2e
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 144 deletions.
118 changes: 118 additions & 0 deletions ACE_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""
Make ACE's release for linux desktop and android (multi archs) automatically
This script run cmake and makefile to generate the build
for running a bash command in a directory
don't use cd but use cwd parameter
https://stackoverflow.com/questions/21406887/subprocess-changing-directory
"""

import subprocess
import os
import shutil
import multiprocessing
from typing import List
from util import assert_is_dir_and_exist, assert_is_file_and_exist, mkdir_overwrite

LINUX_RELEASE_DIR = "./linux"
ANDROID_RELEASE_DIR = "./android"

ANDROID_ARCH_ABI_ARR = [
"armeabi-v7a",
"arm64-v8a",
"x86",
"x86_64",
]
# need to be set to at least 23
# for process_vm_read support
# see ACE's cmake for more detail
ANDROID_PLATFORM = "android-23"

CMAKELIST_PATH = "./ACE/engine/"
BUILD_DIR = "./build"


def gen_make_and_make_ACE(
build_dir: str,
install_dir: str,
CMAKElist_dir_path: str,
toolchain_path: str = None,
extra_args: List[str] = None,
# by default, use all cpu for fastest compilation
# https://unix.stackexchange.com/questions/208568/how-to-determine-the-maximum-number-to-pass-to-make-j-option
# https://stackoverflow.com/a/1006337/14073678
cpu_count_for_compile: int = multiprocessing.cpu_count(),
):

# recreate build and release directory in case its previously
# not empty
mkdir_overwrite(build_dir)
mkdir_overwrite(install_dir)
# get abs path when possible
assert_is_dir_and_exist(install_dir)
install_path = os.path.abspath(install_dir)

assert_is_dir_and_exist(CMAKElist_dir_path)
CMAKElist_path = os.path.abspath(CMAKElist_dir_path)

if toolchain_path != None:
assert_is_file_and_exist(toolchain_path)
toolchain_path = os.path.abspath(toolchain_path)
print(f"install path: {install_path}")
# generate makefile and run make
CMAKE_cmd_args = [
"cmake",
CMAKElist_path,
"-DCMAKE_BUILD_TYPE=Release",
# set install prefix
# https://stackoverflow.com/questions/6003374/what-is-cmake-equivalent-of-configure-prefix-dir-make-all-install
"-DCMAKE_INSTALL_PREFIX:PATH=" + install_path,
]
# append extra arguments
if extra_args != None:
CMAKE_cmd_args += extra_args
pass
# use custom toolchain if provided
if toolchain_path != None:
CMAKE_cmd_args.append("-DCMAKE_TOOLCHAIN_FILE=" + toolchain_path)
# run cmake
subprocess.run(CMAKE_cmd_args, cwd=build_dir)
# make the program and install to the specified target
subprocess.run(
["make", "all", "install", f"-j{cpu_count_for_compile}"], cwd=build_dir
)


def make_release(release_dir: str, android_toolchain_file: str):
print("making release......")

mkdir_overwrite(release_dir)

# ============================ android =====================
# recreate build dir for building engine
android_release_dir = os.path.join(release_dir, ANDROID_RELEASE_DIR)
for arch in ANDROID_ARCH_ABI_ARR:
# create directory for specific arch release
current_android_release_dir = os.path.join(android_release_dir, arch)
#
gen_make_and_make_ACE(
build_dir=BUILD_DIR,
install_dir=current_android_release_dir,
CMAKElist_dir_path=CMAKELIST_PATH,
toolchain_path=android_toolchain_file,
extra_args=[
"-DANDROID_ABI=" + arch,
"-DANDROID_PLATFORM=" + ANDROID_PLATFORM,
],
)

# ============================ linux =====================
# recreate build dir for building engine
linux_release_dir = os.path.join(release_dir, LINUX_RELEASE_DIR)
gen_make_and_make_ACE(
build_dir=BUILD_DIR,
install_dir=linux_release_dir,
CMAKElist_dir_path=CMAKELIST_PATH,
toolchain_path=None,
)
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ RUN unzip ndk.zip -d ndk
RUN rm ndk.zip

# copy sources code
COPY ./ACE/ ./ACE
COPY ./ACE ./ACE
COPY ./util.py ./util.py
COPY ./ACE_release.py ./ACE_release.py
COPY ./make_release.py ./make_release.py

COPY ./Modder ./Modder

# generate build
# TODO: have an automated way to determine toolchain path?
RUN python3 ./make_release.py /ndk/android-ndk-r25c/build/cmake/android.toolchain.cmake
27 changes: 27 additions & 0 deletions Modder_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import subprocess
import shutil
from util import assert_is_file_and_exist
import zipfile

def make_release(release_dir: str):

PATH_TO_ZIP_BUILD = "./Modder/modder/build/distributions/modder.zip"
# clean previous build
subprocess.run(["./gradlew", "clean"], cwd="./Modder/")
# generate code for injection first
subprocess.run(["python3", "./gen_smali.py"], cwd="./Modder/injector/")
# compile project
subprocess.run(["./gradlew", "build"], cwd="./Modder/")

assert_is_file_and_exist(PATH_TO_ZIP_BUILD)

# extract zip build to [release_dir]
with zipfile.ZipFile(PATH_TO_ZIP_BUILD, 'r') as zip_ref:
zip_ref.extractall(release_dir)





if __name__ == "__main__":
make_release("modder_release_build")
2 changes: 1 addition & 1 deletion docker_build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess


# docker image name must be lower case
# docker image name must be lower case
DOCKER_IMAGE_NAME = "ace_the_game"
if __name__ == "__main__":
subprocess.run(["docker", "build", "-t", DOCKER_IMAGE_NAME, "."])
4 changes: 3 additions & 1 deletion docker_run_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

# https://stackoverflow.com/questions/43308319/how-can-i-run-bash-in-a-new-container-of-a-docker-image
if __name__ == "__main__":
subprocess.run(["docker", "run", "--rm", "-it", "--entrypoint", "bash", DOCKER_IMAGE_NAME])
subprocess.run(
["docker", "run", "--rm", "-it", "--entrypoint", "bash", DOCKER_IMAGE_NAME]
)
146 changes: 5 additions & 141 deletions make_release.py
Original file line number Diff line number Diff line change
@@ -1,114 +1,8 @@
"""
Make ACE's release for linux desktop and android (multi archs) automatically
This script run cmake and makefile to generate the build
for running a bash command in a directory
don't use cd but use cwd parameter
https://stackoverflow.com/questions/21406887/subprocess-changing-directory
"""

import subprocess
import os
import glob
import shutil
import ACE_release
import argparse
import multiprocessing
from typing import List

LINUX_RELEASE_DIR = "./linux"
ANDROID_RELEASE_DIR = "./android"

ANDROID_ARCH_ABI_ARR = [
"armeabi-v7a",
"arm64-v8a",
"x86",
"x86_64",
]
# need to be set to at least 23
# for process_vm_read support
# see ACE's cmake for more detail
ANDROID_PLATFORM = "android-23"

CMAKELIST_PATH = "./ACE/engine/"
RELEASE_DIR = "./release"
BUILD_DIR = "./build"


def assert_is_file_and_exist(filename: str):
if not os.path.exists(filename):
raise FileNotFoundError(filename)
if not os.path.isfile(filename):
raise Exception(f"{filename} is not a file")


def assert_is_dir_and_exist(dirname: str):
if not os.path.exists(dirname):
raise FileNotFoundError(dirname)

if not os.path.isdir(dirname):
raise NotADirectoryError()(f"{dirname} is not a dir")


def mkdir_overwrite(dir_name: str):
# remove dir first if exist previously
if os.path.exists(dir_name) and os.path.isdir(dir_name):
shutil.rmtree(dir_name)
# create it
os.makedirs(dir_name)


def gen_make_and_make_ACE(
build_dir: str,
install_dir: str,
CMAKElist_dir_path: str,
toolchain_path: str = None,
extra_args: List[str] = None,
# by default, use all cpu for fastest compilation
# https://unix.stackexchange.com/questions/208568/how-to-determine-the-maximum-number-to-pass-to-make-j-option
# https://stackoverflow.com/a/1006337/14073678
cpu_count_for_compile: int = multiprocessing.cpu_count(),
):

# recreate build and release directory in case its previously
# not empty
mkdir_overwrite(build_dir)
mkdir_overwrite(install_dir)
# get abs path when possible
assert_is_dir_and_exist(install_dir)
install_path = os.path.abspath(install_dir)

assert_is_dir_and_exist(CMAKElist_dir_path)
CMAKElist_path = os.path.abspath(CMAKElist_dir_path)

if toolchain_path != None:
assert_is_file_and_exist(toolchain_path)
toolchain_path = os.path.abspath(toolchain_path)
print(f"install path: {install_path}")
# generate makefile and run make
CMAKE_cmd_args = [
"cmake",
CMAKElist_path,
"-DCMAKE_BUILD_TYPE=Release",
# set install prefix
# https://stackoverflow.com/questions/6003374/what-is-cmake-equivalent-of-configure-prefix-dir-make-all-install
"-DCMAKE_INSTALL_PREFIX:PATH=" + install_path,
]
# append extra arguments
if extra_args != None:
CMAKE_cmd_args += extra_args
pass
# use custom toolchain if provided
if toolchain_path != None:
CMAKE_cmd_args.append("-DCMAKE_TOOLCHAIN_FILE=" + toolchain_path)
# run cmake
subprocess.run(CMAKE_cmd_args, cwd=build_dir)
# make the program and install to the specified target
subprocess.run(
["make", "all", "install", f"-j{cpu_count_for_compile}"], cwd=build_dir
)

def main_func():
if __name__ == "__main__":
# ===================== making commands ========
parser = argparse.ArgumentParser()
parser.add_argument(
Expand All @@ -118,37 +12,7 @@ def main_func():
args = parser.parse_args()
android_toolchain_file = args.android_toolchain_file
# ==============================================
print("making release......")

mkdir_overwrite(RELEASE_DIR)

# ============================ android =====================
# recreate build dir for building engine
android_release_dir = os.path.join(RELEASE_DIR, ANDROID_RELEASE_DIR)
for arch in ANDROID_ARCH_ABI_ARR:
# create directory for specific arch release
current_android_release_dir = os.path.join(android_release_dir, arch)
#
gen_make_and_make_ACE(
build_dir=BUILD_DIR,
install_dir=current_android_release_dir,
CMAKElist_dir_path=CMAKELIST_PATH,
toolchain_path=android_toolchain_file,
extra_args=[
"-DANDROID_ABI=" + arch,
"-DANDROID_PLATFORM=" + ANDROID_PLATFORM,
],
)

# ============================ linux =====================
# recreate build dir for building engine
linux_release_dir = os.path.join(RELEASE_DIR, LINUX_RELEASE_DIR)
gen_make_and_make_ACE(
build_dir=BUILD_DIR,
install_dir=linux_release_dir,
CMAKElist_dir_path=CMAKELIST_PATH,
toolchain_path=None,
ACE_release.make_release(
release_dir=RELEASE_DIR,
android_toolchain_file=android_toolchain_file,
)

if __name__ == "__main__":
main_func()
25 changes: 25 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import shutil


def assert_is_file_and_exist(filename: str):
if not os.path.exists(filename):
raise FileNotFoundError(filename)
if not os.path.isfile(filename):
raise Exception(f"{filename} is not a file")


def assert_is_dir_and_exist(dirname: str):
if not os.path.exists(dirname):
raise FileNotFoundError(dirname)

if not os.path.isdir(dirname):
raise NotADirectoryError()(f"{dirname} is not a dir")


def mkdir_overwrite(dir_name: str):
# remove dir first if exist previously
if os.path.exists(dir_name) and os.path.isdir(dir_name):
shutil.rmtree(dir_name)
# create it
os.makedirs(dir_name)

0 comments on commit 1539b2e

Please sign in to comment.