Skip to content

Commit

Permalink
[3.x] Unify bits, macos_arch into arch, support non-x86 Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Jul 8, 2023
1 parent bbcf901 commit 2638dd5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 36 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
fail-fast: false
matrix:
include:
- name: 🐧 Linux (GCC)
- name: 🐧 Linux (x86_64, GCC)
os: ubuntu-20.04
platform: linux
artifact-name: godot-cpp-linux-glibc2.27-x86_64-release
artifact-path: bin/libgodot-cpp.linux.release.64.a
artifact-path: bin/libgodot-cpp.linux.release.x86_64.a
godot_zip: Godot_v3.5-stable_linux_server.64.zip
executable: Godot_v3.5-stable_linux_server.64
cache-name: linux-x86_64
Expand All @@ -30,22 +30,22 @@ jobs:
os: windows-2019
platform: windows
artifact-name: godot-cpp-windows-msvc2019-x86_64-release
artifact-path: bin/libgodot-cpp.windows.release.64.lib
artifact-path: bin/libgodot-cpp.windows.release.x86_64.lib
cache-name: windows-x86_64-msvc

- name: 🏁 Windows (x86_64, MinGW)
os: windows-2019
platform: windows
artifact-name: godot-cpp-linux-mingw-x86_64-release
artifact-path: bin/libgodot-cpp.windows.release.64.a
artifact-path: bin/libgodot-cpp.windows.release.x86_64.a
flags: use_mingw=yes
cache-name: windows-x86_64-mingw

- name: 🍎 macOS (universal)
os: macos-11
platform: osx
artifact-name: godot-cpp-macos-universal-release
artifact-path: bin/libgodot-cpp.osx.release.64.a
artifact-path: bin/libgodot-cpp.osx.release.universal.a
flags: macos_arch=universal
godot_zip: Godot_v3.5-stable_osx.universal.zip
executable: Godot.app/Contents/MacOS/Godot
Expand Down
101 changes: 74 additions & 27 deletions SConstruct
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import os
import platform
import sys
import subprocess
from binding_generator import scons_generate_bindings, scons_emit_files
Expand Down Expand Up @@ -101,15 +102,6 @@ if hasattr(os, "cpu_count") and env.GetOption("num_jobs") == altered_num_jobs:
)
env.SetOption("num_jobs", safer_cpu_count)

is64 = sys.maxsize > 2 ** 32
if (
env["TARGET_ARCH"] == "amd64"
or env["TARGET_ARCH"] == "emt64"
or env["TARGET_ARCH"] == "x86_64"
or env["TARGET_ARCH"] == "arm64-v8a"
):
is64 = True

opts = Variables([], ARGUMENTS)
opts.Add(
EnumVariable(
Expand All @@ -120,7 +112,7 @@ opts.Add(
ignorecase=2,
)
)
opts.Add(EnumVariable("bits", "Target platform bits", "64" if is64 else "32", ("32", "64")))
opts.Add(EnumVariable("bits", "Target platform bits", "auto", ("auto", "32", "64")))
opts.Add(BoolVariable("use_llvm", "Use the LLVM compiler - only effective when targeting Linux or FreeBSD", False))
opts.Add(BoolVariable("use_mingw", "Use the MinGW compiler instead of MSVC - only effective on Windows", False))
# Must be the same setting as used for cpp_bindings
Expand Down Expand Up @@ -173,6 +165,25 @@ opts.Add(

opts.Add(BoolVariable("build_library", "Build the godot-cpp library.", True))

# CPU architecture options.
architectures = ["auto", "universal", "x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64", "wasm32"]
architecture_aliases = {
"x86": "x86_32",
"x64": "x86_64",
"amd64": "x86_64",
"armv7": "arm32",
"armv8": "arm64",
"arm64v8": "arm64",
"aarch64": "arm64",
"rv": "rv64",
"riscv": "rv64",
"riscv64": "rv64",
"ppcle": "ppc32",
"ppc": "ppc32",
"ppc64le": "ppc64",
}
opts.Add(EnumVariable("arch", "CPU architecture", "auto", architectures, architecture_aliases))

opts.Update(env)
Help(opts.GenerateHelpText(env))

Expand All @@ -183,16 +194,50 @@ if unknown:
for item in unknown.items():
print(" " + item[0] + "=" + item[1])

# Compatibility with old bits argument on Windows/Linux.
# Not needed on macOS or web. Android and iOS don't use env["arch"].
if env["platform"] in ["windows", "linux"]:
if env["bits"] == "64":
env["arch"] = "x86_64"
elif env["bits"] == "32":
env["arch"] = "x86_32"

# Process CPU architecture argument.
if env["arch"] == "auto":
# Use env["arch"] on all platforms except Android and iOS.
# For macOS, default arch to macos_arch, which defaults to universal (compat with old argument).
# For the web (javascript), default arch to wasm32.
# For Windows and Linux, default arch to the host architecture.
if env["platform"] == "osx":
env["arch"] = env["macos_arch"]
elif env["platform"] == "javascript":
env["arch"] = "wasm32"
else:
host_machine = platform.machine().lower()
if host_machine in architectures:
env["arch"] = host_machine
elif host_machine in architecture_aliases.keys():
env["arch"] = architecture_aliases[host_machine]
elif "86" in host_machine:
# Catches x86, i386, i486, i586, i686, etc.
env["arch"] = "x86_32"
else:
print("Unsupported CPU architecture: " + host_machine)
Exit()

env_arch = env["arch"]

# This makes sure to keep the session environment variables on Windows.
# This way, you can run SCons in a Visual Studio 2017 prompt and it will find
# all the required tools
if host_platform == "windows" and env["platform"] != "android":
if env["bits"] == "64":
if env["arch"] == "x86_64":
env = Environment(TARGET_ARCH="amd64")
elif env["bits"] == "32":
elif env["arch"] == "x86_32":
env = Environment(TARGET_ARCH="x86")

opts.Update(env)
env["arch"] = env_arch

# Require C++14
if host_platform == "windows" and env["platform"] == "windows" and not env["use_mingw"]:
Expand All @@ -213,26 +258,27 @@ if env["platform"] == "linux" or env["platform"] == "freebsd":
elif env["target"] == "release":
env.Append(CCFLAGS=["-O3"])

if env["bits"] == "64":
if env["arch"] == "x86_64":
env.Append(CCFLAGS=["-m64"])
env.Append(LINKFLAGS=["-m64"])
elif env["bits"] == "32":
elif env["arch"] == "x86_32":
env.Append(CCFLAGS=["-m32"])
env.Append(LINKFLAGS=["-m32"])

elif env["platform"] == "osx":
# Use Clang on macOS by default
env["CXX"] = "clang++"

if env["bits"] == "32":
raise ValueError("Only 64-bit builds are supported for the macOS target.")
if env["arch"] not in ("universal", "x86_64", "arm64"):
print("Only universal, x86_64, and arm64 are supported on macOS. Exiting.")
Exit()

if env["macos_arch"] == "universal":
if env["arch"] == "universal":
env.Append(LINKFLAGS=["-arch", "x86_64", "-arch", "arm64"])
env.Append(CCFLAGS=["-arch", "x86_64", "-arch", "arm64"])
else:
env.Append(LINKFLAGS=["-arch", env["macos_arch"]])
env.Append(CCFLAGS=["-arch", env["macos_arch"]])
env.Append(LINKFLAGS=["-arch", env["arch"]])
env.Append(CCFLAGS=["-arch", env["arch"]])

if env["macos_deployment_target"] != "default":
env.Append(CCFLAGS=["-mmacosx-version-min=" + env["macos_deployment_target"]])
Expand Down Expand Up @@ -299,12 +345,12 @@ elif env["platform"] == "windows":

elif host_platform == "linux" or host_platform == "freebsd" or host_platform == "osx":
# Cross-compilation using MinGW
if env["bits"] == "64":
if env["arch"] == "x86_64":
env["CXX"] = "x86_64-w64-mingw32-g++"
env["AR"] = "x86_64-w64-mingw32-ar"
env["RANLIB"] = "x86_64-w64-mingw32-ranlib"
env["LINK"] = "x86_64-w64-mingw32-g++"
elif env["bits"] == "32":
elif env["arch"] == "x86_32":
env["CXX"] = "i686-w64-mingw32-g++"
env["AR"] = "i686-w64-mingw32-ar"
env["RANLIB"] = "i686-w64-mingw32-ranlib"
Expand All @@ -314,6 +360,7 @@ elif env["platform"] == "windows":
# Don't Clone the environment. Because otherwise, SCons will pick up msvc stuff.
env = Environment(ENV=os.environ, tools=["mingw"])
opts.Update(env)
env["arch"] = env_arch

# Still need to use C++14.
env.Append(CCFLAGS=["-std=c++14"])
Expand Down Expand Up @@ -342,6 +389,7 @@ elif env["platform"] == "android":
# Don't Clone the environment. Because otherwise, SCons will pick up msvc stuff.
env = Environment(ENV=os.environ, tools=["mingw"])
opts.Update(env)
env["arch"] = env_arch

# Long line hack. Use custom spawn, quick AR append (to avoid files with the same names to override each other).
env["SPAWN"] = mySpawn
Expand Down Expand Up @@ -433,6 +481,10 @@ elif env["platform"] == "android":
env.Append(CCFLAGS=["-O3"])

elif env["platform"] == "javascript":
if env["arch"] != "wasm32":
print("Only the WebAssembly architecture (wasm32) is supported on the web. Exiting.")
Exit()

env["ENV"] = os.environ
env["CC"] = "emcc"
env["CXX"] = "em++"
Expand Down Expand Up @@ -497,18 +549,13 @@ sources = []
add_sources(sources, "src/core", "cpp")
sources.extend(f for f in bindings if str(f).endswith(".cpp"))

arch_suffix = env["bits"]
arch_suffix = env["arch"]
if env["platform"] == "android":
arch_suffix = env["android_arch"]
elif env["platform"] == "ios":
arch_suffix = env["ios_arch"]
if env["ios_simulator"]:
arch_suffix += ".simulator"
elif env["platform"] == "osx":
if env["macos_arch"] != "universal":
arch_suffix = env["macos_arch"]
elif env["platform"] == "javascript":
arch_suffix = "wasm"
# Expose it to projects that import this env.
env["arch_suffix"] = arch_suffix

Expand Down
8 changes: 4 additions & 4 deletions test/gdexample.gdnlib
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ reloadable=false

[entry]

X11.64="res://bin/libgdexample.linux.release.64.so"
Server.64="res://bin/libgdexample.linux.release.64.so"
Windows.64="res://bin/libgdexample.windows.release.64.dll"
OSX.64="res://bin/libgdexample.osx.release.64.dylib"
X11.64="res://bin/libgdexample.linux.release.x86_64.so"
Server.64="res://bin/libgdexample.linux.release.x86_64.so"
Windows.64="res://bin/libgdexample.windows.release.x86_64.dll"
OSX.64="res://bin/libgdexample.osx.release.universal.dylib"

[dependencies]

Expand Down

0 comments on commit 2638dd5

Please sign in to comment.