From 5df3f0d897ecd23605e76a73fbe73a3985899143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Wed, 27 Nov 2024 17:32:37 +0100 Subject: [PATCH] SCons: Add optional `detect.py` `get_tools` method to let platforms register custom tools This helps move this logic out of SConstruct, keeping platforms more self contained, and helping thirdparty platforms define their own custom tools. This logic was also unreliable (the `use_mingw` one would only work if passed manually on the command line, not in e.g. `get_flags`). --- SConstruct | 17 +++++++---------- platform/android/detect.py | 4 ++++ platform/web/detect.py | 5 +++++ platform/windows/detect.py | 7 +++++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/SConstruct b/SConstruct index fa324b2aa0fa..4bdc6c07fd71 100644 --- a/SConstruct +++ b/SConstruct @@ -86,6 +86,7 @@ if sys.stdout.isatty() and sys.platform == "win32": # Scan possible build platforms platform_list = [] # list of platforms +platform_tools = {} # SCons custom tools for each platform platform_opts = {} # options for each platform platform_flags = {} # flags for each platform platform_doc_class_path = {} @@ -121,6 +122,10 @@ for x in sorted(glob.glob("platform/*")): x = x.replace("platform/", "") # rest of world x = x.replace("platform\\", "") # win32 platform_list += [x] + try: # Custom tools is optional + platform_tools[x] = detect.get_tools() + except Exception: + platform_tools[x] = ["default"] platform_opts[x] = detect.get_opts() platform_flags[x] = detect.get_flags() if isinstance(platform_flags[x], list): # backwards compatibility @@ -128,22 +133,14 @@ for x in sorted(glob.glob("platform/*")): sys.path.remove(tmppath) sys.modules.pop("detect") -custom_tools = ["default"] +# Define main SCons Environment platform_arg = ARGUMENTS.get("platform", ARGUMENTS.get("p", False)) - -if platform_arg == "android": - custom_tools = ["clang", "clang++", "as", "ar", "link"] -elif platform_arg == "web": - # Use generic POSIX build toolchain for Emscripten. - custom_tools = ["cc", "c++", "ar", "link", "textfile", "zip"] -elif os.name == "nt" and methods.get_cmdline_bool("use_mingw", False): - custom_tools = ["mingw"] +env = Environment(tools=platform_tools[platform_arg]) # We let SCons build its default ENV as it includes OS-specific things which we don't # want to have to pull in manually. # Then we prepend PATH to make it take precedence, while preserving SCons' own entries. -env = Environment(tools=custom_tools) env.PrependENVPath("PATH", os.getenv("PATH")) env.PrependENVPath("PKG_CONFIG_PATH", os.getenv("PKG_CONFIG_PATH")) if "TERM" in os.environ: # Used for colored output. diff --git a/platform/android/detect.py b/platform/android/detect.py index 03a208391cf3..a729478eb107 100644 --- a/platform/android/detect.py +++ b/platform/android/detect.py @@ -19,6 +19,10 @@ def can_build(): return os.path.exists(get_env_android_sdk_root()) +def get_tools(): + return ["clang", "clang++", "as", "ar", "link"] + + def get_opts(): from SCons.Variables import BoolVariable diff --git a/platform/web/detect.py b/platform/web/detect.py index 25a5bbe5a596..9b6710702fc0 100644 --- a/platform/web/detect.py +++ b/platform/web/detect.py @@ -28,6 +28,11 @@ def can_build(): return WhereIs("emcc") is not None +def get_tools(): + # Use generic POSIX build toolchain for Emscripten. + return ["cc", "c++", "ar", "link", "textfile", "zip"] + + def get_opts(): from SCons.Variables import BoolVariable diff --git a/platform/windows/detect.py b/platform/windows/detect.py index e1109db24fa5..52049fed4ec5 100644 --- a/platform/windows/detect.py +++ b/platform/windows/detect.py @@ -155,6 +155,13 @@ def detect_build_env_arch(): return "" +def get_tools(): + if os.name == "nt" and methods.get_cmdline_bool("use_mingw", False): + return ["mingw"] + else: + return ["default"] + + def get_opts(): from SCons.Variables import BoolVariable, EnumVariable