@@ -68,23 +68,23 @@ def can_build():
6868
6969
7070def get_mingw_bin_prefix (prefix , arch ):
71- if not prefix :
72- mingw_bin_prefix = ""
73- elif prefix [- 1 ] != "/" :
74- mingw_bin_prefix = prefix + "/bin/"
75- else :
76- mingw_bin_prefix = prefix + "bin/"
71+ bin_prefix = (os .path .normpath (os .path .join (prefix , "bin" )) + os .sep ) if prefix else ""
72+ ARCH_PREFIXES = {
73+ "x86_64" : "x86_64-w64-mingw32-" ,
74+ "x86_32" : "i686-w64-mingw32-" ,
75+ "arm32" : "armv7-w64-mingw32-" ,
76+ "arm64" : "aarch64-w64-mingw32-" ,
77+ }
78+ arch_prefix = ARCH_PREFIXES [arch ] if arch else ""
79+ return bin_prefix + arch_prefix
7780
78- if arch == "x86_64" :
79- mingw_bin_prefix += "x86_64-w64-mingw32-"
80- elif arch == "x86_32" :
81- mingw_bin_prefix += "i686-w64-mingw32-"
82- elif arch == "arm32" :
83- mingw_bin_prefix += "armv7-w64-mingw32-"
84- elif arch == "arm64" :
85- mingw_bin_prefix += "aarch64-w64-mingw32-"
8681
87- return mingw_bin_prefix
82+ def get_detected (env : "SConsEnvironment" , tool : str ) -> str :
83+ checks = [
84+ get_mingw_bin_prefix (env ["mingw_prefix" ], env ["arch" ]) + tool ,
85+ get_mingw_bin_prefix (env ["mingw_prefix" ], "" ) + tool ,
86+ ]
87+ return str (env .Detect (checks ))
8888
8989
9090def detect_build_env_arch ():
@@ -245,41 +245,6 @@ def get_flags():
245245 }
246246
247247
248- def build_res_file (target , source , env : "SConsEnvironment" ):
249- arch_aliases = {
250- "x86_32" : "pe-i386" ,
251- "x86_64" : "pe-x86-64" ,
252- "arm32" : "armv7-w64-mingw32" ,
253- "arm64" : "aarch64-w64-mingw32" ,
254- }
255- cmdbase = "windres --include-dir . --target=" + arch_aliases [env ["arch" ]]
256-
257- mingw_bin_prefix = get_mingw_bin_prefix (env ["mingw_prefix" ], env ["arch" ])
258-
259- for x in range (len (source )):
260- ok = True
261- # Try prefixed executable (MinGW on Linux).
262- cmd = mingw_bin_prefix + cmdbase + " -i " + str (source [x ]) + " -o " + str (target [x ])
263- try :
264- out = subprocess .Popen (cmd , shell = True , stderr = subprocess .PIPE ).communicate ()
265- if len (out [1 ]):
266- ok = False
267- except Exception :
268- ok = False
269-
270- # Try generic executable (MSYS2).
271- if not ok :
272- cmd = cmdbase + " -i " + str (source [x ]) + " -o " + str (target [x ])
273- try :
274- out = subprocess .Popen (cmd , shell = True , stderr = subprocess .PIPE ).communicate ()
275- if len (out [1 ]):
276- return - 1
277- except Exception :
278- return - 1
279-
280- return 0
281-
282-
283248def setup_msvc_manual (env : "SConsEnvironment" ):
284249 """Running from VCVARS environment"""
285250
@@ -361,6 +326,10 @@ def setup_mingw(env: "SConsEnvironment"):
361326 print_error ("No valid compilers found, use MINGW_PREFIX environment variable to set MinGW path." )
362327 sys .exit (255 )
363328
329+ env .Tool ("mingw" )
330+ env .AppendUnique (CCFLAGS = env .get ("ccflags" , "" ).split ())
331+ env .AppendUnique (RCFLAGS = env .get ("rcflags" , "" ).split ())
332+
364333 print ("Using MinGW, arch %s" % (env ["arch" ]))
365334
366335
@@ -716,6 +685,13 @@ def configure_mingw(env: "SConsEnvironment"):
716685 # https://www.scons.org/wiki/LongCmdLinesOnWin32
717686 env .use_windows_spawn_fix ()
718687
688+ # HACK: For some reason, Windows-native shells have their MinGW tools
689+ # frequently fail as a result of parsing path separators incorrectly.
690+ # For some other reason, this issue is circumvented entirely if the
691+ # `mingw_prefix` bin is prepended to PATH.
692+ if os .sep == "\\ " :
693+ env .PrependENVPath ("PATH" , os .path .join (env ["mingw_prefix" ], "bin" ))
694+
719695 # In case the command line to AR is too long, use a response file.
720696 env ["ARCOM_ORIG" ] = env ["ARCOM" ]
721697 env ["ARCOM" ] = "${TEMPFILE('$ARCOM_ORIG', '$ARCOMSTR')}"
@@ -770,29 +746,31 @@ def configure_mingw(env: "SConsEnvironment"):
770746
771747 env .Append (CCFLAGS = ["-ffp-contract=off" ])
772748
773- mingw_bin_prefix = get_mingw_bin_prefix (env ["mingw_prefix" ], env ["arch" ])
774-
775749 if env ["use_llvm" ]:
776- env ["CC" ] = mingw_bin_prefix + "clang"
777- env ["CXX" ] = mingw_bin_prefix + "clang++"
778- if try_cmd ("as --version" , env ["mingw_prefix" ], env ["arch" ]):
779- env ["AS" ] = mingw_bin_prefix + "as"
780- env .Append (ASFLAGS = ["-c" ])
781- if try_cmd ("ar --version" , env ["mingw_prefix" ], env ["arch" ]):
782- env ["AR" ] = mingw_bin_prefix + "ar"
783- if try_cmd ("ranlib --version" , env ["mingw_prefix" ], env ["arch" ]):
784- env ["RANLIB" ] = mingw_bin_prefix + "ranlib"
750+ env ["CC" ] = get_detected (env , "clang" )
751+ env ["CXX" ] = get_detected (env , "clang++" )
752+ env ["AR" ] = get_detected (env , "ar" )
753+ env ["RANLIB" ] = get_detected (env , "ranlib" )
754+ env .Append (ASFLAGS = ["-c" ])
785755 env .extra_suffix = ".llvm" + env .extra_suffix
786756 else :
787- env ["CC" ] = mingw_bin_prefix + "gcc"
788- env ["CXX" ] = mingw_bin_prefix + "g++"
789- if try_cmd ("as --version" , env ["mingw_prefix" ], env ["arch" ]):
790- env ["AS" ] = mingw_bin_prefix + "as"
791- ar = "ar" if os .name == "nt" else "gcc-ar"
792- if try_cmd (f"{ ar } --version" , env ["mingw_prefix" ], env ["arch" ]):
793- env ["AR" ] = mingw_bin_prefix + ar
794- if try_cmd ("gcc-ranlib --version" , env ["mingw_prefix" ], env ["arch" ]):
795- env ["RANLIB" ] = mingw_bin_prefix + "gcc-ranlib"
757+ env ["CC" ] = get_detected (env , "gcc" )
758+ env ["CXX" ] = get_detected (env , "g++" )
759+ env ["AR" ] = get_detected (env , "gcc-ar" if os .name != "nt" else "ar" )
760+ env ["RANLIB" ] = get_detected (env , "gcc-ranlib" )
761+
762+ env ["RC" ] = get_detected (env , "windres" )
763+ ARCH_TARGETS = {
764+ "x86_32" : "pe-i386" ,
765+ "x86_64" : "pe-x86-64" ,
766+ "arm32" : "armv7-w64-mingw32" ,
767+ "arm64" : "aarch64-w64-mingw32" ,
768+ }
769+ env .AppendUnique (RCFLAGS = f"--target={ ARCH_TARGETS [env ['arch' ]]} " )
770+
771+ env ["AS" ] = get_detected (env , "as" )
772+ env ["OBJCOPY" ] = get_detected (env , "objcopy" )
773+ env ["STRIP" ] = get_detected (env , "strip" )
796774
797775 ## LTO
798776
@@ -944,9 +922,6 @@ def configure_mingw(env: "SConsEnvironment"):
944922
945923 env .Append (CPPDEFINES = ["MINGW_ENABLED" , ("MINGW_HAS_SECURE_API" , 1 )])
946924
947- # resrc
948- env .Append (BUILDERS = {"RES" : env .Builder (action = build_res_file , suffix = ".o" , src_suffix = ".rc" )})
949-
950925
951926def configure (env : "SConsEnvironment" ):
952927 # Validate arch.
0 commit comments