Skip to content

Commit

Permalink
add output option to build script
Browse files Browse the repository at this point in the history
  • Loading branch information
j20001970 committed May 10, 2024
1 parent e280933 commit f9f695b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 75 deletions.
39 changes: 17 additions & 22 deletions .github/workflows/build_addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ jobs:
shell: bash
run: |
source ./venv/Scripts/activate
python ./build.py desktop --type release
python ./build.py desktop --type release --output build/
- name: Upload build files (artifacts)
uses: actions/upload-artifact@v4
with:
name: build-artifact-windows
path: |
mediapipe/bazel-bin/GDMP/desktop/*.dll
build/*.dll
# Build the Linux Artifacts
Expand Down Expand Up @@ -87,14 +87,14 @@ jobs:
- name: Build
run: |
source ./venv/bin/activate
python ./build.py desktop --type release
python ./build.py desktop --type release --output build/
- name: Upload build files (artifacts)
uses: actions/upload-artifact@v4
with:
name: build-artifact-linux
path: |
mediapipe/bazel-bin/GDMP/desktop/*.so
build/*.so
# Build the Android Artifacts
Expand Down Expand Up @@ -153,14 +153,14 @@ jobs:
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
run: |
source ./venv/bin/activate
python ./build.py android --arch arm64-v8a --type release
python ./build.py android --arch arm64-v8a --type release --output build/
- name: Upload build files (artifacts)
uses: actions/upload-artifact@v4
with:
name: build-artifact-android
path: |
android/build/outputs/aar/*.aar
build/*.aar
# Build the iOS Artifacts
Expand Down Expand Up @@ -188,14 +188,14 @@ jobs:
- name: Build
run: |
source ./venv/bin/activate
python ./build.py ios --type release
python ./build.py ios --type release --output build/
- name: Upload build files (artifacts)
uses: actions/upload-artifact@v4
with:
name: build-artifact-ios
path: |
mediapipe/bazel-bin/GDMP/ios/GDMP.zip
build/*.framework
# Assemble the Addon
Expand All @@ -214,24 +214,19 @@ jobs:

- name: Populate Addon
run: |
mkdir dist
cp -a addons dist/
cp LICENSE addons/GDMP/
cp README.md addons/GDMP/
mkdir -p addons/GDMP/libs/x86_64/
cp build-artifact-windows/*.dll addons/GDMP/libs/x86_64/
cp build-artifact-linux/*.so addons/GDMP/libs/x86_64/
cp build-artifact-android/*.aar addons/GDMP/libs
mv addons/GDMP/libs/GDMP-release.aar addons/GDMP/libs/GDMP.android.aar
unzip -o build-artifact-ios/GDMP.zip -d addons/GDMP/libs
- name: Patch GDMP.gdextension
run: |
sed -i 's/libGDMP\..*\.so/libGDMP.so/g' addons/GDMP/GDMP.gdextension
sed -i 's/GDMP\..*\.dll/GDMP.dll/g' addons/GDMP/GDMP.gdextension
sed -i 's/opencv_world3410d/opencv_world3410/g' addons/GDMP/GDMP.gdextension
cp README.md dist/
mkdir -p dist/addons/GDMP/libs/x86_64/
cp build-artifact-windows/*.dll dist/addons/GDMP/libs/x86_64/
cp build-artifact-linux/*.so dist/addons/GDMP/libs/x86_64/
cp build-artifact-android/*.aar dist/addons/GDMP/libs/
cp -a build-artifact-ios/*.framework dist/addons/GDMP/libs/
- name: Create Addon Artifact
uses: actions/upload-artifact@v4
with:
name: Addon
path: |
addons
dist
79 changes: 27 additions & 52 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from argparse import ArgumentParser, Namespace
from collections.abc import Callable
from functools import partial
from os import environ, makedirs, path, remove
from os import makedirs, path
from shutil import copyfile, rmtree, which
from subprocess import run

MEDIAPIPE_DIR = path.join(path.dirname(__file__), "mediapipe")
ANDROID_PROJECT = path.join(path.dirname(__file__), "android")

TARGETS = {
"android": "//GDMP/android:GDMP",
Expand Down Expand Up @@ -40,13 +41,6 @@
],
}

GODOT_PATH: str = None
if "GODOT_PROJECT" in environ:
godot_path = environ["GODOT_PROJECT"]
if path.exists(path.join(godot_path, "project.godot")):
GODOT_PATH = environ["GODOT_PROJECT"]
print(f"Godot project found on {godot_path}")


def bazel_build(args: list[str]) -> Callable:
bazel_exec = which("bazelisk") or which("bazel")
Expand All @@ -64,11 +58,14 @@ def bazel_build(args: list[str]) -> Callable:


def build_android(
project_dir: str, build_type: str, build_args: list[str], abi_list: list[str]
build_type: str, build_args: list[str], abi_list: list[str]
) -> list[Callable]:
cmds = []
if not path.exists(ANDROID_PROJECT):
print("Error: android project does not exist.")
sys.exit(-1)
src = path.join(MEDIAPIPE_DIR, "bazel-bin/GDMP/android/libGDMP.so")
jni_dir = path.join(project_dir, "src/main/jniLibs")
jni_dir = path.join(ANDROID_PROJECT, "src/main/jniLibs")
if path.exists(jni_dir):
cmds.append(partial(rmtree, jni_dir))
for abi in abi_list:
Expand All @@ -86,9 +83,9 @@ def build_android(
cmds.append(partial(makedirs, dst_dir, exist_ok=True))
cmds.append(partial(copyfile, src, dst))
cmds.append(partial(copyfile, src_opencv, dst_opencv))
gradlew_exec = path.join(project_dir, "gradlew")
gradlew_exec = path.join(ANDROID_PROJECT, "gradlew")
gradle_build = [gradlew_exec, "clean", f"assemble{build_type.capitalize()}"]
cmds.append(partial(run, gradle_build, cwd=project_dir, check=True))
cmds.append(partial(run, gradle_build, cwd=ANDROID_PROJECT, check=True))
return cmds


Expand All @@ -107,15 +104,11 @@ def get_build_cmds(args: Namespace) -> list[Callable]:
else:
build_args.extend(TARGET_ARGS[target])
if target == "android":
android_project = path.join(path.dirname(__file__), "android")
if not path.exists(android_project):
print("Error: android project does not exist.")
sys.exit(-1)
if arch is None:
arch = platform.machine().lower()
abi_list = arch.split(",")
build_args.extend(["--cpu={abi}", TARGETS[target]])
cmds.extend(build_android(android_project, build_type, build_args, abi_list))
cmds.extend(build_android(build_type, build_args, abi_list))
return cmds
elif target == "ios" and arch != None:
build_args.append(f"--ios_multi_cpus={arch}")
Expand All @@ -125,66 +118,47 @@ def get_build_cmds(args: Namespace) -> list[Callable]:
return cmds


def copy_to_godot(src, dst):
if GODOT_PATH is None:
return
i = input(f"Copy {path.basename(src)} to {dst}? [Y/n] ")
if len(i) and not i.lower().startswith("y"):
return
dst = path.join(GODOT_PATH, dst)
makedirs(path.dirname(dst), exist_ok=True)
if path.exists(dst):
remove(dst)
copyfile(src, dst)


def copy_android(args: Namespace):
build_type: str = args.type
android_project = path.join(path.dirname(__file__), "android")
src = path.join(android_project, f"build/outputs/aar/GDMP-{build_type}.aar")
dst = "addons/GDMP/libs"
copy_to_godot(src, path.join(dst, "GDMP.android.aar"))
output: str = args.output
src = path.join(ANDROID_PROJECT, f"build/outputs/aar/GDMP-{build_type}.aar")
dst = path.join(output, "GDMP.android.aar")
copyfile(src, dst)


def copy_desktop(args: Namespace):
arch: str = args.arch
if arch is None:
arch = platform.machine().lower()
if arch == "amd64":
arch = "x86_64"
output: str = args.output
desktop_platform = platform.system().lower()
desktop_output = path.join(MEDIAPIPE_DIR, "bazel-bin/GDMP/desktop")
if desktop_platform == "linux":
src = path.join(desktop_output, "libGDMP.so")
elif desktop_platform == "windows":
src = path.join(desktop_output, "GDMP.dll")
dst = path.join("addons/GDMP/libs", arch)
filename = path.basename(src).split(".")
filename = ".".join([filename[0], desktop_platform, filename[-1]])
copy_to_godot(src, path.join(dst, filename))
dst = path.join(output, filename)
copyfile(src, dst)
if desktop_platform == "windows":
opencv_lib = glob.glob(path.join(desktop_output, "opencv_world*.dll"))
if len(opencv_lib) == 0:
return
src = opencv_lib[0]
copy_to_godot(src, path.join(dst, path.basename(src)))
copyfile(src, path.join(output, path.basename(src)))


def copy_ios(args: Namespace):
output: str = args.output
src = path.join(MEDIAPIPE_DIR, "bazel-bin/GDMP/ios/GDMP.zip")
dst = "addons/GDMP/libs"
if GODOT_PATH is None:
return
i = input(f"Extract {path.basename(src)} to {dst}? [Y/n] ")
if len(i) and not i.lower().startswith("y"):
return
dst = path.join(GODOT_PATH, dst)
with zipfile.ZipFile(src) as f:
f.extractall(dst)
f.extractall(output)


def copy_to_project(args: Namespace):
def copy_output(args: Namespace):
target: str = args.target
output: str = args.output
if output is None:
return
makedirs(output, exist_ok=True)
copy_actions = {
"android": copy_android,
"desktop": copy_desktop,
Expand All @@ -200,8 +174,9 @@ def copy_to_project(args: Namespace):
"--type", choices=["debug", "release"], default="debug", help="build type"
)
parser.add_argument("--arch", help="library architecture")
parser.add_argument("--output", help="build output directory")
args = parser.parse_args()
cmds = get_build_cmds(args)
for cmd in cmds:
cmd()
copy_to_project(args)
copy_output(args)
1 change: 0 additions & 1 deletion docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Example:

4. Copy `addons` to your Godot project's root directory.
5. (Optional) Add calculator dependencies in `GDMP/GDMP.bzl` if you have additional calculators to compile into GDMP.
6. (Optional) Set `GODOT_PROJECT` environment variable that points to your Godot project, the build script will try to automate copy process if it is detected.

## Building for Android
**Windows users**: Cross-compiling Android library on Windows is currently not supported, please set up WSL or Linux VM before proceed to the instructions below.
Expand Down

0 comments on commit f9f695b

Please sign in to comment.