Skip to content

Commit

Permalink
Use the godot cpp example to add a git workflow. Adjust file structur…
Browse files Browse the repository at this point in the history
…e and SConstruct to fit its design.
  • Loading branch information
Ivorforce committed Sep 17, 2024
1 parent a06b57e commit 56c7fc2
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 39 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Build GDExtension
on:
workflow_call:
push:

jobs:
build:
strategy:
fail-fast: false
matrix:
include:
- platform: linux
arch: x86_64
os: ubuntu-20.04
- platform: windows
arch: x86_32
os: windows-latest
- platform: windows
arch: x86_64
os: windows-latest
- platform: macos
arch: universal
os: macos-latest
- platform: android
arch: arm64
os: ubuntu-20.04
- platform: android
arch: arm32
os: ubuntu-20.04
- platform: android
arch: x86_64
os: ubuntu-20.04
- platform: android
arch: x86_32
os: ubuntu-20.04
- platform: ios
arch: arm64
os: macos-latest
- platform: web
arch: wasm32
os: ubuntu-20.04

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
- name: 🔗 GDExtension Build
uses: godotengine/godot-cpp-template/.github/actions/build@main
with:
platform: ${{ matrix.platform }}
arch: ${{ matrix.arch }}
float-precision: single
build-target-type: template_release
- name: 🔗 GDExtension Build
uses: ./.github/actions/build
with:
platform: ${{ matrix.platform }}
arch: ${{ matrix.arch }}
float-precision: ${{ matrix.float-precision }}
build-target-type: template_debug
- name: Mac Sign
if: ${{ matrix.platform == 'macos' && env.APPLE_CERT_BASE64 }}
env:
APPLE_CERT_BASE64: ${{ secrets.APPLE_CERT_BASE64 }}
uses: godotengine/godot-cpp-template/.github/actions/sign@main
with:
FRAMEWORK_PATH: bin/macos/macos.framework
APPLE_CERT_BASE64: ${{ secrets.APPLE_CERT_BASE64 }}
APPLE_CERT_PASSWORD: ${{ secrets.APPLE_CERT_PASSWORD }}
APPLE_DEV_PASSWORD: ${{ secrets.APPLE_DEV_PASSWORD }}
APPLE_DEV_ID: ${{ secrets.APPLE_DEV_ID }}
APPLE_DEV_TEAM_ID: ${{ secrets.APPLE_DEV_TEAM_ID }}
APPLE_DEV_APP_ID: ${{ secrets.APPLE_DEV_APP_ID }}
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: GDExtension
path: |
${{ github.workspace }}/bin/**
101 changes: 78 additions & 23 deletions SConstruct
Original file line number Diff line number Diff line change
@@ -1,32 +1,82 @@
#!/usr/bin/env python
import os
import sys
from SCons.Script import Variables, Command, File, DefaultEnvironment

# This works like passing disable_exceptions=false by default.
from methods import print_error


def normalize_path(val, env):
return val if os.path.isabs(val) else os.path.join(env.Dir("#").abspath, val)


def validate_parent_dir(key, val, env):
if not os.path.isdir(normalize_path(os.path.dirname(val), env)):
raise UserError("'%s' is not a directory: %s" % (key, os.path.dirname(val)))


libname = "numdot"
projectdir = "demo"

localEnv = Environment(tools=["default"], PLATFORM="")

customs = ["custom.py"]
customs = [os.path.abspath(path) for path in customs]

opts = Variables(customs, ARGUMENTS)
opts.Add(
BoolVariable(
key="compiledb",
help="Generate compilation DB (`compile_commands.json`) for external tools",
default=localEnv.get("compiledb", False),
)
)
opts.Add(
PathVariable(
key="compiledb_file",
help="Path to a custom `compile_commands.json` file",
default=localEnv.get("compiledb_file", "compile_commands.json"),
validator=validate_parent_dir,
)
)
opts.Update(localEnv)

Help(opts.GenerateHelpText(localEnv))

env = localEnv.Clone()
# To read up on why exceptions must be enabled, read further below.
env = Environment()
env['disable_exceptions'] = False
env["compiledb"] = False

env.Tool("compilation_db")
compilation_db = env.CompilationDatabase(
normalize_path(localEnv["compiledb_file"], localEnv)
)
env.Alias("compiledb", compilation_db)

env = SConscript("godot-cpp/SConstruct", 'env')
submodule_initialized = False
dir_name = 'godot-cpp'
if os.path.isdir(dir_name):
if os.listdir(dir_name):
submodule_initialized = True

# For reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags
if not submodule_initialized:
print_error("""godot-cpp is not available within this folder, as Git submodules haven't been initialized.
Run the following command to download godot-cpp:
git submodule update --init --recursive""")
sys.exit(1)

env = SConscript("godot-cpp/SConstruct", {"env": env, "customs": customs})

env.Append(CPPFLAGS=[
'-DXTENSOR_USE_XSIMD=1',

# Explicitly enable exceptions (see https://github.com/godotengine/godot-cpp/blob/master/CMakeLists.txt).
# '-DGODOT_DISABLE_EXCEPTIONS=OFF',
# XTensor could disable exceptions, but then we would have to duplicate all our checks.
# Would have to be passed to xtensor build too, I think.
# '-DXTENSOR_DISABLE_EXCEPTIONS=1',

# ffast-math: See https://stackoverflow.com/questions/57442255/xtensor-and-xsimd-improve-performance-on-reduction
# And https://stackoverflow.com/questions/7420665/what-does-gccs-ffast-math-actually-do
# We should not enable this flag by default, because infinite maths is definitely expected in many situations.
Expand All @@ -45,24 +95,29 @@ env.Append(CPPPATH=["xtl/include", "xsimd/include", "xtensor/include"])
env.Append(CPPPATH=["src/"])
sources = Glob("src/*.cpp") + Glob("src/*/*.cpp")

if env["platform"] == "macos":
target_file_path = "demo/bin/libnumdot.{}.{}.framework/libnumdot.{}.{}".format(
env["platform"], env["target"], env["platform"], env["target"]
)
else:
target_file_path = "demo/bin/libnumdot{}{}".format(env["suffix"], env["SHLIBSUFFIX"])

# Via https://docs.godotengine.org/en/stable/tutorials/scripting/gdextension/gdextension_docs_system.html
if env["target"] in ["editor", "template_debug"]:
try:
doc_data = env.GodotCPPDocData("src/gen/doc_data.gen.cpp", source=Glob("doc_classes/*.xml"))
sources.append(doc_data)
except AttributeError:
print("Not including class reference as we're targeting a pre-4.3 baseline.")

file = "{}{}{}".format(libname, env["suffix"], env["SHLIBSUFFIX"])
filepath = ""

if env["platform"] == "macos" or env["platform"] == "ios":
filepath = "{}.framework/".format(env["platform"])
file = "{}.{}.{}".format(libname, env["platform"], env["target"])

libraryfile = "bin/{}/{}{}".format(env["platform"], filepath, file)
library = env.SharedLibrary(
target_file_path,
libraryfile,
source=sources,
)

Default(library)
copy = env.InstallAs("{}/bin/{}/{}lib{}".format(projectdir, env["platform"], filepath, file), library)

default_args = [library, copy]
if localEnv.get("compiledb", False):
default_args += [compilation_db]
Default(*default_args)
14 changes: 14 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Ignore all files
**

# Add back .gitignore
!.gitignore

# Ensure directories themselves are not ignored, so that .gitkeep and Info.plist can be tracked
!*/

# Add back .gitkeep files
!**/*.gitkeep

# Add back Info.plist files
!**/Info.plist
Empty file added bin/android/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions bin/ios/ios.framework/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>libEXTENSION-NAME.macos.template_release</string>
<key>CFBundleName</key>
<string>NumDot</string>
<key>CFBundleDisplayName</key>
<string>NumDot</string>
<key>CFBundleIdentifier</key>
<string>de.ivorius.numdot</string>
<key>NSHumanReadableCopyright</key>
<string>Unlicensed</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CSResourcesFileMapped</key>
<true/>
<key>DTPlatformName</key>
<string>iphoneos</string>
<key>MinimumOSVersion</key>
<string>12.0</string>
</dict>
</plist>
Empty file added bin/linux/.gitkeep
Empty file.
Empty file added bin/macos/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions bin/macos/macos.framework/Resources/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>libEXTENSION-NAME.macos.template_release</string>
<key>CFBundleName</key>
<string>NumDot</string>
<key>CFBundleDisplayName</key>
<string>NumDot</string>
<key>CFBundleIdentifier</key>
<string>de.ivorius.numdot</string>
<key>NSHumanReadableCopyright</key>
<string>Unlicensed</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CSResourcesFileMapped</key>
<true/>
<key>DTPlatformName</key>
<string>macosx</string>
<key>LSMinimumSystemVersion</key>
<string>10.12</string>
</dict>
</plist>
Empty file added bin/windows/.gitkeep
Empty file.
34 changes: 18 additions & 16 deletions demo/bin/numdot.gdextension
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ reloadable = true

[libraries]

macos.debug = "res://bin/libnumdot.macos.template_debug.framework"
macos.release = "res://bin/libnumdot.macos.template_release.framework"
windows.debug.x86_32 = "res://bin/libnumdot.windows.template_debug.x86_32.dll"
windows.release.x86_32 = "res://bin/libnumdot.windows.template_release.x86_32.dll"
windows.debug.x86_64 = "res://bin/libnumdot.windows.template_debug.x86_64.dll"
windows.release.x86_64 = "res://bin/libnumdot.windows.template_release.x86_64.dll"
linux.debug.x86_64 = "res://bin/libnumdot.linux.template_debug.x86_64.so"
linux.release.x86_64 = "res://bin/libnumdot.linux.template_release.x86_64.so"
linux.debug.arm64 = "res://bin/libnumdot.linux.template_debug.arm64.so"
linux.release.arm64 = "res://bin/libnumdot.linux.template_release.arm64.so"
linux.debug.rv64 = "res://bin/libnumdot.linux.template_debug.rv64.so"
linux.release.rv64 = "res://bin/libnumdot.linux.template_release.rv64.so"
android.debug.x86_64 = "res://bin/libnumdot.android.template_debug.x86_64.so"
android.release.x86_64 = "res://bin/libnumdot.android.template_release.x86_64.so"
android.debug.arm64 = "res://bin/libnumdot.android.template_debug.arm64.so"
android.release.arm64 = "res://bin/libnumdot.android.template_release.arm64.so"
macos.debug = "res://bin/macos/macos.framework/libnumdot.macos.template_debug"
macos.release = "res://bin/macos/macos.framework/libnumdot.macos.template_release"
ios.debug = "res://bin/ios/libnumdot.ios.template_debug"
ios.release = "res://bin/ios/libnumdot.ios.template_release"
windows.debug.x86_32 = "res://bin/windows/libnumdot.windows.template_debug.x86_32.dll"
windows.release.x86_32 = "res://bin/windows/libnumdot.windows.template_release.x86_32.dll"
windows.debug.x86_64 = "res://bin/windows/libnumdot.windows.template_debug.x86_64.dll"
windows.release.x86_64 = "res://bin/windows/libnumdot.windows.template_release.x86_64.dll"
linux.debug.x86_64 = "res://bin/linux/libnumdot.linux.template_debug.x86_64.so"
linux.release.x86_64 = "res://bin/linux/libnumdot.linux.template_release.x86_64.so"
linux.debug.arm64 = "res://bin/linux/libnumdot.linux.template_debug.arm64.so"
linux.release.arm64 = "res://bin/linux/libnumdot.linux.template_release.arm64.so"
linux.debug.rv64 = "res://bin/linux/libnumdot.linux.template_debug.rv64.so"
linux.release.rv64 = "res://bin/linux/libnumdot.linux.template_release.rv64.so"
android.debug.x86_64 = "res://bin/android/libnumdot.android.template_debug.x86_64.so"
android.release.x86_64 = "res://bin/android/libnumdot.android.template_release.x86_64.so"
android.debug.arm64 = "res://bin/android/libnumdot.android.template_debug.arm64.so"
android.release.arm64 = "res://bin/android/libnumdot.android.template_release.arm64.so"
52 changes: 52 additions & 0 deletions methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import sys
from enum import Enum

# Colors are disabled in non-TTY environments such as pipes. This means
# that if output is redirected to a file, it won't contain color codes.
# Colors are always enabled on continuous integration.
_colorize = bool(sys.stdout.isatty() or os.environ.get("CI"))


class ANSI(Enum):
"""
Enum class for adding ansi colorcodes directly into strings.
Automatically converts values to strings representing their
internal value, or an empty string in a non-colorized scope.
"""

RESET = "\x1b[0m"

BOLD = "\x1b[1m"
ITALIC = "\x1b[3m"
UNDERLINE = "\x1b[4m"
STRIKETHROUGH = "\x1b[9m"
REGULAR = "\x1b[22;23;24;29m"

BLACK = "\x1b[30m"
RED = "\x1b[31m"
GREEN = "\x1b[32m"
YELLOW = "\x1b[33m"
BLUE = "\x1b[34m"
MAGENTA = "\x1b[35m"
CYAN = "\x1b[36m"
WHITE = "\x1b[37m"

PURPLE = "\x1b[38;5;93m"
PINK = "\x1b[38;5;206m"
ORANGE = "\x1b[38;5;214m"
GRAY = "\x1b[38;5;244m"

def __str__(self) -> str:
global _colorize
return str(self.value) if _colorize else ""


def print_warning(*values: object) -> None:
"""Prints a warning message with formatting."""
print(f"{ANSI.YELLOW}{ANSI.BOLD}WARNING:{ANSI.REGULAR}", *values, ANSI.RESET, file=sys.stderr)


def print_error(*values: object) -> None:
"""Prints an error message with formatting."""
print(f"{ANSI.RED}{ANSI.BOLD}ERROR:{ANSI.REGULAR}", *values, ANSI.RESET, file=sys.stderr)

0 comments on commit 56c7fc2

Please sign in to comment.