Skip to content

Refactor SConstruct, and add scu_build option. #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,64 @@ import sys
from methods import print_error


if not (os.path.isdir("godot-cpp") and os.listdir("godot-cpp")):
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)


# ============================= General Lib Info =============================

libname = "EXTENSION-NAME"
projectdir = "demo"

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

# ============================= Setup Options =============================

# Load variables from custom.py, in case someone wants to store their own arguments.
# See https://scons.org/doc/production/HTML/scons-user.html#app-tools // search custom.py
customs = ["custom.py"]
customs = [os.path.abspath(path) for path in customs]

opts = Variables(customs, ARGUMENTS)
opts.Update(localEnv)

Help(opts.GenerateHelpText(localEnv))
gdextension_tool.options(opts)

env = localEnv.Clone()
# Remove our custom options to avoid passing to godot-cpp; godot-cpp has its own check for unknown options.
for opt in opts.options:
ARGUMENTS.pop(opt.key, None)

submodule_initialized = False
dir_name = 'godot-cpp'
if os.path.isdir(dir_name):
if os.listdir(dir_name):
submodule_initialized = True
# ============================= Setup godot-cpp =============================

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:
godot_cpp_env = SConscript("godot-cpp/SConstruct", {"customs": customs})

git submodule update --init --recursive""")
sys.exit(1)
gdextension_env = godot_cpp_env.Clone()
opts.Update(gdextension_env)
Help(opts.GenerateHelpText(gdextension_env))

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

env.Append(CPPPATH=["src/"])
sources = Glob("src/*.cpp")
sources = []
targets = []

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.")
gdextension_tool.generate(gdextension_env, godot_cpp_env, sources)

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

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

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

copy = env.InstallAs("{}/bin/{}/{}lib{}".format(projectdir, env["platform"], filepath, file), library)
targets.append(gdextension_env.Install("{}/bin/{}/{}".format(projectdir, godot_cpp_env["platform"], filepath), library))

default_args = [library, copy]
Default(*default_args)
Default(targets)
40 changes: 40 additions & 0 deletions site_scons/site_tools/gdextension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pathlib
from SCons.Tool import Tool


def exists(env):
return True

def options(opts):
# Add custom options here
# opts.Add(
# "custom_option",
# "Custom option help text",
# "default_value"
# )

scu_tool = Tool("scu")
scu_tool.options(opts)

def generate(env, godot_cpp_env, sources):
scu_tool = Tool("scu")

# read custom options values
# custom_option = env["custom_option"]

env.Append(CPPPATH=["src/"])

sources.extend([
f for f in env.Glob("src/*.cpp") + env.Glob("src/**/*.cpp")
# Generated files will be added selectively and maintained by tools.
if not "/gen/" in str(f.path)
])

scu_tool.generate(env, sources)

if godot_cpp_env["target"] in ["editor", "template_debug"]:
try:
doc_data = godot_cpp_env.GodotCPPDocData("src/gen/doc_data.gen.cpp", source=env.Glob("doc_classes/*.xml"))
sources.append(doc_data)
except AttributeError:
print("Not including class reference as we're targeting a pre-4.3 baseline.")
31 changes: 31 additions & 0 deletions site_scons/site_tools/scu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pathlib
from SCons.Variables import BoolVariable


def exists(env):
return True

def options(opts):
opts.Add(
BoolVariable(
"scu_build",
"Use single compilation unit build.",
False
)
)

def generate(env, sources):
if not env.get('scu', False):
return

scu_path = pathlib.Path('src/gen/scu.cpp')
scu_path.parent.mkdir(exist_ok=True)

scu_path.write_text(
'\n'.join(
f'#include "{pathlib.Path(source.path).relative_to("src")}"'
for source in sources
) + '\n'
)

sources[:] = [str(scu_path.absolute())]