11#!/usr/bin/env python
22
33import os
4+ import platform
45import sys
56import subprocess
67from binding_generator import scons_generate_bindings , scons_emit_files
@@ -101,15 +102,6 @@ if hasattr(os, "cpu_count") and env.GetOption("num_jobs") == altered_num_jobs:
101102 )
102103 env .SetOption ("num_jobs" , safer_cpu_count )
103104
104- is64 = sys .maxsize > 2 ** 32
105- if (
106- env ["TARGET_ARCH" ] == "amd64"
107- or env ["TARGET_ARCH" ] == "emt64"
108- or env ["TARGET_ARCH" ] == "x86_64"
109- or env ["TARGET_ARCH" ] == "arm64-v8a"
110- ):
111- is64 = True
112-
113105opts = Variables ([], ARGUMENTS )
114106opts .Add (
115107 EnumVariable (
@@ -120,7 +112,7 @@ opts.Add(
120112 ignorecase = 2 ,
121113 )
122114)
123- opts .Add (EnumVariable ("bits" , "Target platform bits" , "64" if is64 else "32 " , ( "32" , "64" )))
115+ opts .Add (EnumVariable ("bits" , "Target platform bits" , "auto" , ( "auto " , "32" , "64" )))
124116opts .Add (BoolVariable ("use_llvm" , "Use the LLVM compiler - only effective when targeting Linux or FreeBSD" , False ))
125117opts .Add (BoolVariable ("use_mingw" , "Use the MinGW compiler instead of MSVC - only effective on Windows" , False ))
126118# Must be the same setting as used for cpp_bindings
@@ -173,6 +165,25 @@ opts.Add(
173165
174166opts .Add (BoolVariable ("build_library" , "Build the godot-cpp library." , True ))
175167
168+ # CPU architecture options.
169+ architectures = ["auto" , "universal" , "x86_32" , "x86_64" , "arm32" , "arm64" , "rv64" , "ppc32" , "ppc64" , "wasm32" ]
170+ architecture_aliases = {
171+ "x86" : "x86_32" ,
172+ "x64" : "x86_64" ,
173+ "amd64" : "x86_64" ,
174+ "armv7" : "arm32" ,
175+ "armv8" : "arm64" ,
176+ "arm64v8" : "arm64" ,
177+ "aarch64" : "arm64" ,
178+ "rv" : "rv64" ,
179+ "riscv" : "rv64" ,
180+ "riscv64" : "rv64" ,
181+ "ppcle" : "ppc32" ,
182+ "ppc" : "ppc32" ,
183+ "ppc64le" : "ppc64" ,
184+ }
185+ opts .Add (EnumVariable ("arch" , "CPU architecture" , "auto" , architectures , architecture_aliases ))
186+
176187opts .Update (env )
177188Help (opts .GenerateHelpText (env ))
178189
@@ -183,16 +194,50 @@ if unknown:
183194 for item in unknown .items ():
184195 print (" " + item [0 ] + "=" + item [1 ])
185196
197+ # Compatibility with old bits argument on Windows/Linux.
198+ # Not needed on macOS or web. Android and iOS don't use env["arch"].
199+ if env ["platform" ] in ["windows" , "linux" ]:
200+ if env ["bits" ] == "64" :
201+ env ["arch" ] = "x86_64"
202+ elif env ["bits" ] == "32" :
203+ env ["arch" ] = "x86_32"
204+
205+ # Process CPU architecture argument.
206+ if env ["arch" ] == "auto" :
207+ # Use env["arch"] on all platforms except Android and iOS.
208+ # For macOS, default arch to macos_arch, which defaults to universal (compat with old argument).
209+ # For the web (javascript), default arch to wasm32.
210+ # For Windows and Linux, default arch to the host architecture.
211+ if env ["platform" ] == "osx" :
212+ env ["arch" ] = env ["macos_arch" ]
213+ elif env ["platform" ] == "javascript" :
214+ env ["arch" ] = "wasm32"
215+ else :
216+ host_machine = platform .machine ().lower ()
217+ if host_machine in architectures :
218+ env ["arch" ] = host_machine
219+ elif host_machine in architecture_aliases .keys ():
220+ env ["arch" ] = architecture_aliases [host_machine ]
221+ elif "86" in host_machine :
222+ # Catches x86, i386, i486, i586, i686, etc.
223+ env ["arch" ] = "x86_32"
224+ else :
225+ print ("Unsupported CPU architecture: " + host_machine )
226+ Exit ()
227+
228+ env_arch = env ["arch" ]
229+
186230# This makes sure to keep the session environment variables on Windows.
187231# This way, you can run SCons in a Visual Studio 2017 prompt and it will find
188232# all the required tools
189233if host_platform == "windows" and env ["platform" ] != "android" :
190- if env ["bits " ] == "64 " :
234+ if env ["arch " ] == "x86_64 " :
191235 env = Environment (TARGET_ARCH = "amd64" )
192- elif env ["bits " ] == "32 " :
236+ elif env ["arch " ] == "x86_32 " :
193237 env = Environment (TARGET_ARCH = "x86" )
194238
195239 opts .Update (env )
240+ env ["arch" ] = env_arch
196241
197242# Require C++14
198243if host_platform == "windows" and env ["platform" ] == "windows" and not env ["use_mingw" ]:
@@ -213,26 +258,27 @@ if env["platform"] == "linux" or env["platform"] == "freebsd":
213258 elif env ["target" ] == "release" :
214259 env .Append (CCFLAGS = ["-O3" ])
215260
216- if env ["bits " ] == "64 " :
261+ if env ["arch " ] == "x86_64 " :
217262 env .Append (CCFLAGS = ["-m64" ])
218263 env .Append (LINKFLAGS = ["-m64" ])
219- elif env ["bits " ] == "32 " :
264+ elif env ["arch " ] == "x86_32 " :
220265 env .Append (CCFLAGS = ["-m32" ])
221266 env .Append (LINKFLAGS = ["-m32" ])
222267
223268elif env ["platform" ] == "osx" :
224269 # Use Clang on macOS by default
225270 env ["CXX" ] = "clang++"
226271
227- if env ["bits" ] == "32" :
228- raise ValueError ("Only 64-bit builds are supported for the macOS target." )
272+ if env ["arch" ] not in ("universal" , "x86_64" , "arm64" ):
273+ print ("Only universal, x86_64, and arm64 are supported on macOS. Exiting." )
274+ Exit ()
229275
230- if env ["macos_arch " ] == "universal" :
276+ if env ["arch " ] == "universal" :
231277 env .Append (LINKFLAGS = ["-arch" , "x86_64" , "-arch" , "arm64" ])
232278 env .Append (CCFLAGS = ["-arch" , "x86_64" , "-arch" , "arm64" ])
233279 else :
234- env .Append (LINKFLAGS = ["-arch" , env ["macos_arch " ]])
235- env .Append (CCFLAGS = ["-arch" , env ["macos_arch " ]])
280+ env .Append (LINKFLAGS = ["-arch" , env ["arch " ]])
281+ env .Append (CCFLAGS = ["-arch" , env ["arch " ]])
236282
237283 if env ["macos_deployment_target" ] != "default" :
238284 env .Append (CCFLAGS = ["-mmacosx-version-min=" + env ["macos_deployment_target" ]])
@@ -299,12 +345,12 @@ elif env["platform"] == "windows":
299345
300346 elif host_platform == "linux" or host_platform == "freebsd" or host_platform == "osx" :
301347 # Cross-compilation using MinGW
302- if env ["bits " ] == "64 " :
348+ if env ["arch " ] == "x86_64 " :
303349 env ["CXX" ] = "x86_64-w64-mingw32-g++"
304350 env ["AR" ] = "x86_64-w64-mingw32-ar"
305351 env ["RANLIB" ] = "x86_64-w64-mingw32-ranlib"
306352 env ["LINK" ] = "x86_64-w64-mingw32-g++"
307- elif env ["bits " ] == "32 " :
353+ elif env ["arch " ] == "x86_32 " :
308354 env ["CXX" ] = "i686-w64-mingw32-g++"
309355 env ["AR" ] = "i686-w64-mingw32-ar"
310356 env ["RANLIB" ] = "i686-w64-mingw32-ranlib"
@@ -314,6 +360,7 @@ elif env["platform"] == "windows":
314360 # Don't Clone the environment. Because otherwise, SCons will pick up msvc stuff.
315361 env = Environment (ENV = os .environ , tools = ["mingw" ])
316362 opts .Update (env )
363+ env ["arch" ] = env_arch
317364
318365 # Still need to use C++14.
319366 env .Append (CCFLAGS = ["-std=c++14" ])
@@ -342,6 +389,7 @@ elif env["platform"] == "android":
342389 # Don't Clone the environment. Because otherwise, SCons will pick up msvc stuff.
343390 env = Environment (ENV = os .environ , tools = ["mingw" ])
344391 opts .Update (env )
392+ env ["arch" ] = env_arch
345393
346394 # Long line hack. Use custom spawn, quick AR append (to avoid files with the same names to override each other).
347395 env ["SPAWN" ] = mySpawn
@@ -433,6 +481,10 @@ elif env["platform"] == "android":
433481 env .Append (CCFLAGS = ["-O3" ])
434482
435483elif env ["platform" ] == "javascript" :
484+ if env ["arch" ] != "wasm32" :
485+ print ("Only the WebAssembly architecture (wasm32) is supported on the web. Exiting." )
486+ Exit ()
487+
436488 env ["ENV" ] = os .environ
437489 env ["CC" ] = "emcc"
438490 env ["CXX" ] = "em++"
@@ -497,18 +549,13 @@ sources = []
497549add_sources (sources , "src/core" , "cpp" )
498550sources .extend (f for f in bindings if str (f ).endswith (".cpp" ))
499551
500- arch_suffix = env ["bits " ]
552+ arch_suffix = env ["arch " ]
501553if env ["platform" ] == "android" :
502554 arch_suffix = env ["android_arch" ]
503555elif env ["platform" ] == "ios" :
504556 arch_suffix = env ["ios_arch" ]
505557 if env ["ios_simulator" ]:
506558 arch_suffix += ".simulator"
507- elif env ["platform" ] == "osx" :
508- if env ["macos_arch" ] != "universal" :
509- arch_suffix = env ["macos_arch" ]
510- elif env ["platform" ] == "javascript" :
511- arch_suffix = "wasm"
512559# Expose it to projects that import this env.
513560env ["arch_suffix" ] = arch_suffix
514561
0 commit comments