Skip to content

Commit

Permalink
build: add wrapper function around subprocess.run
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Sep 16, 2024
1 parent 3d7fd09 commit a7039a8
Showing 1 changed file with 56 additions and 30 deletions.
86 changes: 56 additions & 30 deletions addons/gdcef/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
import os, sys, subprocess, hashlib, tarfile, shutil, glob, progressbar, urllib.request
from platform import machine, system
from pathlib import Path
from subprocess import run
from multiprocessing import cpu_count
from packaging import version
from shutil import move, copymode
from shutil import copymode

###############################################################################
###
Expand All @@ -66,7 +65,7 @@
# /!\ BEWARE /!\
# - Do not use version 4.1 since gdextension is not compatible.
# - Do not use version 3.x since not compatible. git checkout godot-3.x the gdcef branch instead.
GODOT_VERSION = "4.3" # or "4.2.2" or "4.2" or tag
GODOT_VERSION = "4.3" # or "4.2" or tag

# Use "godot-<version>-stable" for a tag on https://github.com/godotengine/godot-cpp/tags
# Else "<version>" to track the HEAD of a branch https://github.com/godotengine/godot-cpp/branches
Expand All @@ -90,7 +89,7 @@
# Scons is the build system used by Godot. For some people "scons" command does not
# work, they need to call "python -m SCons" command. The array is needed by the func
# calling scons.
SCONS = ["scons"] # or ["python", "-m", "SCons"]
SCONS = "scons" # or ["python3", "-m", "SCons"]

###############################################################################
###
Expand Down Expand Up @@ -134,6 +133,29 @@ def fatal(msg):
print("\033[31m[FATAL] " + msg + "\033[00m", flush=True)
sys.exit(2)

###############################################################################
###
### Wrap the subprocess command
###
###############################################################################
def exec(*args):
command = list(args)
try:
subprocess.run(command, text=True, check=True)
except subprocess.CalledProcessError as e:
fatal("Failed executing the command " + ' '.join(map(str,command)))

###############################################################################
###
### Wrap the scons command
###
###############################################################################
def scons(*args):
if type(SCONS) == str:
exec(SCONS, *args, "--jobs=" + NPROC)
else:
exec(*SCONS, *args, "--jobs=" + NPROC)

###############################################################################
###
### Equivalent to test -L e on alias + ln -s
Expand Down Expand Up @@ -389,19 +411,19 @@ def compile_cef():
if OSTYPE == "Windows":
# Windows: force compiling CEF as static library. This is needed
# for beeing used with Godot.
run(["cmake", "-DCEF_RUNTIME_LIBRARY_FLAG=/MD", "-DCMAKE_BUILD_TYPE=" + CEF_TARGET, "."], check=True)
run(["cmake", "--build", ".", "--config", CEF_TARGET], check=True)
exec("cmake", "-DCEF_RUNTIME_LIBRARY_FLAG=/MD", "-DCMAKE_BUILD_TYPE=" + CEF_TARGET, ".")
exec("cmake", "--build", ".", "--config", CEF_TARGET)
else:
# Linux, MacOS: Compile CEF if Ninja is available else use default
# GNU Makefile.
mkdir("build")
os.chdir("build")
if shutil.which('ninja') != None:
run(["cmake", "-G", "Ninja", "-DCMAKE_BUILD_TYPE=" + CEF_TARGET, ".."], check=True)
run(["ninja", "-v", "-j" + NPROC, "cefsimple"], check=True)
exec("cmake", "-G", "Ninja", "-DCMAKE_BUILD_TYPE=" + CEF_TARGET, "..")
exec("ninja", "-v", "-j" + NPROC, "cefsimple")
else:
run(["cmake", "-G", "Unix Makefiles", "-DCMAKE_BUILD_TYPE=" + CEF_TARGET, ".."], check=True)
run(["make", "cefsimple", "-j" + NPROC], check=True)
exec("cmake", "-G", "Unix Makefiles", "-DCMAKE_BUILD_TYPE=" + CEF_TARGET, "..")
exec("make", "cefsimple", "-j" + NPROC)

###############################################################################
###
Expand Down Expand Up @@ -454,9 +476,9 @@ def download_godot_cpp():
if not os.path.exists(GODOT_CPP_API_PATH):
info("Clone cpp wrapper for Godot " + GODOT_VERSION + " into " + GODOT_CPP_API_PATH)
mkdir(GODOT_CPP_API_PATH)
run(["git", "ls-remote", "https://github.com/godotengine/godot-cpp", GODOT_CPP_GIT_TAG_OR_BRANCH])
run(["git", "clone", "--recursive", "-b", GODOT_CPP_GIT_TAG_OR_BRANCH,
"https://github.com/godotengine/godot-cpp", GODOT_CPP_API_PATH])
exec("git", "ls-remote", "https://github.com/godotengine/godot-cpp", GODOT_CPP_GIT_TAG_OR_BRANCH)
exec("git", "clone", "--recursive", "-b", GODOT_CPP_GIT_TAG_OR_BRANCH,
"https://github.com/godotengine/godot-cpp", GODOT_CPP_API_PATH)

###############################################################################
###
Expand All @@ -469,19 +491,23 @@ def compile_godot_cpp():
info("Compiling Godot C++ API (inside " + GODOT_CPP_API_PATH + ") ...")
os.chdir(GODOT_CPP_API_PATH)
if OSTYPE == "Linux":
run(SCONS + ["platform=linux", "target=" + GODOT_CPP_TARGET, "use_static_cpp=no",
"--jobs=" + NPROC], check=True)
scons("platform=linux",
"target=" + GODOT_CPP_TARGET,
"use_static_cpp=no")
elif OSTYPE == "Darwin":
run(SCONS + ["platform=osx", "macos_arch=" + ARCHI,
"target=" + GODOT_CPP_TARGET, "use_static_cpp=no",
"--jobs=" + NPROC], check=True)
scons("platform=osx",
"macos_arch=" + ARCHI,
"target=" + GODOT_CPP_TARGET,
"use_static_cpp=no")
elif OSTYPE == "MinGW":
run(SCONS + ["platform=windows", "use_mingw=True",
"target=" + GODOT_CPP_TARGET, "use_static_cpp=no",
"--jobs=" + NPROC], check=True)
scons("platform=windows",
"use_mingw=True",
"target=" + GODOT_CPP_TARGET,
"use_static_cpp=no")
elif OSTYPE == "Windows":
run(SCONS + ["platform=windows", "target=" + GODOT_CPP_TARGET, "use_static_cpp=no",
"--jobs=" + NPROC], check=True)
scons("platform=windows",
"target=" + GODOT_CPP_TARGET,
"use_static_cpp=no")
else:
fatal("Unknown architecture " + OSTYPE + ": I dunno how to compile Godot-cpp")

Expand All @@ -491,12 +517,12 @@ def compile_godot_cpp():
###
###############################################################################
def gdnative_scons_cmd(plateform):
run(SCONS + ["api_path=" + GODOT_CPP_API_PATH,
"cef_artifacts_folder=\\\"" + CEF_ARTIFACTS_FOLDER_NAME + "\\\"",
"build_path=" + CEF_ARTIFACTS_BUILD_PATH,
"target=" + MODULE_TARGET, "--jobs=" + NPROC,
"platform=" + plateform,
"cpu_parallelism=" + CEF_USE_CPU_PARALLELISM], check=True)
scons("api_path=" + GODOT_CPP_API_PATH,
"cef_artifacts_folder=\\\"" + CEF_ARTIFACTS_FOLDER_NAME + "\\\"",
"build_path=" + CEF_ARTIFACTS_BUILD_PATH,
"target=" + MODULE_TARGET,
"platform=" + plateform,
"cpu_parallelism=" + CEF_USE_CPU_PARALLELISM)

###############################################################################
###
Expand All @@ -510,7 +536,7 @@ def compile_gdnative_cef(path):
gdnative_scons_cmd("x11")
elif OSTYPE == "Darwin":
gdnative_scons_cmd("osx")
elif OSTYPE == "Windows": # or OSTYPE == "MinGW":
elif OSTYPE == "Windows" or OSTYPE == "MinGW":
gdnative_scons_cmd("windows")
else:
fatal("Unknown archi " + OSTYPE + ": I dunno how to compile CEF module primary process")
Expand Down

0 comments on commit a7039a8

Please sign in to comment.