detect.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import methods
  2. import os
  3. import sys
  4. def is_active():
  5. return True
  6. def get_name():
  7. return "Windows"
  8. def can_build():
  9. if (os.name == "nt"):
  10. # Building natively on Windows
  11. if (os.getenv("VCINSTALLDIR")): # MSVC
  12. return True
  13. print("MSVC not detected (no VCINSTALLDIR environment variable), attempting MinGW.")
  14. mingw32 = ""
  15. mingw64 = ""
  16. if (os.getenv("MINGW32_PREFIX")):
  17. mingw32 = os.getenv("MINGW32_PREFIX")
  18. if (os.getenv("MINGW64_PREFIX")):
  19. mingw64 = os.getenv("MINGW64_PREFIX")
  20. test = "gcc --version > NUL 2>&1"
  21. if (os.system(test) == 0 or os.system(mingw32 + test) == 0 or os.system(mingw64 + test) == 0):
  22. return True
  23. if (os.name == "posix"):
  24. # Cross-compiling with MinGW-w64 (old MinGW32 is not supported)
  25. mingw32 = "i686-w64-mingw32-"
  26. mingw64 = "x86_64-w64-mingw32-"
  27. if (os.getenv("MINGW32_PREFIX")):
  28. mingw32 = os.getenv("MINGW32_PREFIX")
  29. if (os.getenv("MINGW64_PREFIX")):
  30. mingw64 = os.getenv("MINGW64_PREFIX")
  31. test = "gcc --version > /dev/null 2>&1"
  32. if (os.system(mingw64 + test) == 0 or os.system(mingw32 + test) == 0):
  33. return True
  34. print("Could not detect MinGW. Ensure its binaries are in your PATH or that MINGW32_PREFIX or MINGW64_PREFIX are properly defined.")
  35. return False
  36. def get_opts():
  37. mingw32 = ""
  38. mingw64 = ""
  39. if (os.name == "posix"):
  40. mingw32 = "i686-w64-mingw32-"
  41. mingw64 = "x86_64-w64-mingw32-"
  42. if (os.getenv("MINGW32_PREFIX")):
  43. mingw32 = os.getenv("MINGW32_PREFIX")
  44. if (os.getenv("MINGW64_PREFIX")):
  45. mingw64 = os.getenv("MINGW64_PREFIX")
  46. return [
  47. ('mingw_prefix_32', 'MinGW prefix (Win32)', mingw32),
  48. ('mingw_prefix_64', 'MinGW prefix (Win64)', mingw64),
  49. ]
  50. def get_flags():
  51. return [
  52. ]
  53. def build_res_file(target, source, env):
  54. if (env["bits"] == "32"):
  55. cmdbase = env['mingw_prefix_32']
  56. else:
  57. cmdbase = env['mingw_prefix_64']
  58. cmdbase = cmdbase + 'windres --include-dir . '
  59. import subprocess
  60. for x in range(len(source)):
  61. cmd = cmdbase + '-i ' + str(source[x]) + ' -o ' + str(target[x])
  62. try:
  63. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  64. if len(out[1]):
  65. return 1
  66. except:
  67. return 1
  68. return 0
  69. def configure(env):
  70. env.Append(CPPPATH=['#platform/windows'])
  71. # Targeted Windows version: 7 (and later), minimum supported version
  72. # XP support dropped after EOL due to missing API for IPv6 and other issues
  73. # Vista support dropped after EOL due to GH-10243
  74. winver = "0x0601"
  75. if (os.name == "nt" and os.getenv("VCINSTALLDIR")): # MSVC
  76. env['ENV']['TMP'] = os.environ['TMP']
  77. ## Build type
  78. if (env["target"] == "release"):
  79. env.Append(CCFLAGS=['/O2'])
  80. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  81. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  82. elif (env["target"] == "release_debug"):
  83. env.Append(CCFLAGS=['/O2', '/DDEBUG_ENABLED'])
  84. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  85. elif (env["target"] == "debug_release"):
  86. env.Append(CCFLAGS=['/Z7', '/Od'])
  87. env.Append(LINKFLAGS=['/DEBUG'])
  88. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  89. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  90. elif (env["target"] == "debug"):
  91. env.Append(CCFLAGS=['/Z7', '/DDEBUG_ENABLED', '/DDEBUG_MEMORY_ENABLED', '/DD3D_DEBUG_INFO', '/Od', '/EHsc'])
  92. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  93. env.Append(LINKFLAGS=['/DEBUG'])
  94. ## Architecture
  95. # Note: this detection/override code from here onward should be here instead of in SConstruct because it's platform and compiler specific (MSVC/Windows)
  96. if (env["bits"] != "default"):
  97. print("Error: bits argument is disabled for MSVC")
  98. print("""
  99. Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console
  100. (or Visual Studio settings) that is being used to run SCons. As a consequence, bits argument is disabled. Run scons again without bits
  101. argument (example: scons p=windows) and SCons will attempt to detect what MSVC compiler will be executed and inform you.
  102. """)
  103. sys.exit()
  104. # Forcing bits argument because MSVC does not have a flag to set this through SCons... it's different compilers (cl.exe's) called from the proper command prompt
  105. # that decide the architecture that is build for. Scons can only detect the os.getenviron (because vsvarsall.bat sets a lot of stuff for cl.exe to work with)
  106. env["bits"] = "32"
  107. env["x86_libtheora_opt_vc"] = True
  108. ## Compiler configuration
  109. env['ENV'] = os.environ
  110. # This detection function needs the tools env (that is env['ENV'], not SCons's env), and that is why it's this far bellow in the code
  111. compiler_version_str = methods.detect_visual_c_compiler_version(env['ENV'])
  112. print("Detected MSVC compiler: " + compiler_version_str)
  113. # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writting)... vc compiler for 64bit can not compile _asm
  114. if(compiler_version_str == "amd64" or compiler_version_str == "x86_amd64"):
  115. env["bits"] = "64"
  116. env["x86_libtheora_opt_vc"] = False
  117. print("Compiled program architecture will be a 64 bit executable (forcing bits=64).")
  118. elif (compiler_version_str == "x86" or compiler_version_str == "amd64_x86"):
  119. print("Compiled program architecture will be a 32 bit executable. (forcing bits=32).")
  120. else:
  121. print("Failed to detect MSVC compiler architecture version... Defaulting to 32bit executable settings (forcing bits=32). Compilation attempt will continue, but SCons can not detect for what architecture this build is compiled for. You should check your settings/compilation setup.")
  122. ## Compile flags
  123. env.Append(CCFLAGS=['/MT', '/Gd', '/GR', '/nologo'])
  124. env.Append(CXXFLAGS=['/TP'])
  125. env.Append(CPPFLAGS=['/DMSVC', '/GR', ])
  126. env.Append(CCFLAGS=['/I' + os.getenv("WindowsSdkDir") + "/Include"])
  127. env.Append(CCFLAGS=['/DWINDOWS_ENABLED'])
  128. env.Append(CCFLAGS=['/DOPENGL_ENABLED'])
  129. env.Append(CCFLAGS=['/DRTAUDIO_ENABLED'])
  130. env.Append(CCFLAGS=['/DWASAPI_ENABLED'])
  131. env.Append(CCFLAGS=['/DTYPED_METHOD_BIND'])
  132. env.Append(CCFLAGS=['/DWIN32'])
  133. env.Append(CCFLAGS=['/DWINVER=%s' % winver, '/D_WIN32_WINNT=%s' % winver])
  134. if env["bits"] == "64":
  135. env.Append(CCFLAGS=['/D_WIN64'])
  136. LIBS = ['winmm', 'opengl32', 'dsound', 'kernel32', 'ole32', 'oleaut32', 'user32', 'gdi32', 'IPHLPAPI', 'Shlwapi', 'wsock32', 'Ws2_32', 'shell32', 'advapi32', 'dinput8', 'dxguid']
  137. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  138. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  139. if (os.getenv("VCINSTALLDIR")):
  140. VC_PATH = os.getenv("VCINSTALLDIR")
  141. else:
  142. VC_PATH = ""
  143. env.Append(CCFLAGS=["/I" + p for p in os.getenv("INCLUDE").split(";")])
  144. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  145. # Incremental linking fix
  146. env['BUILDERS']['ProgramOriginal'] = env['BUILDERS']['Program']
  147. env['BUILDERS']['Program'] = methods.precious_program
  148. else: # MinGW
  149. # Workaround for MinGW. See:
  150. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  151. env.use_windows_spawn_fix()
  152. ## Build type
  153. if (env["target"] == "release"):
  154. env.Append(CCFLAGS=['-msse2'])
  155. if (env["bits"] == "64"):
  156. env.Append(CCFLAGS=['-O3'])
  157. else:
  158. env.Append(CCFLAGS=['-O2'])
  159. env.Append(LINKFLAGS=['-Wl,--subsystem,windows'])
  160. elif (env["target"] == "release_debug"):
  161. env.Append(CCFLAGS=['-O2', '-DDEBUG_ENABLED'])
  162. elif (env["target"] == "debug"):
  163. env.Append(CCFLAGS=['-g', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  164. ## Compiler configuration
  165. if (os.name == "nt"):
  166. env['ENV']['TMP'] = os.environ['TMP'] # way to go scons, you can be so stupid sometimes
  167. else:
  168. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  169. if (env["bits"] == "default"):
  170. if (os.name == "nt"):
  171. env["bits"] = "64" if "PROGRAMFILES(X86)" in os.environ else "32"
  172. else: # default to 64-bit on Linux
  173. env["bits"] = "64"
  174. mingw_prefix = ""
  175. if (env["bits"] == "32"):
  176. env.Append(LINKFLAGS=['-static'])
  177. env.Append(LINKFLAGS=['-static-libgcc'])
  178. env.Append(LINKFLAGS=['-static-libstdc++'])
  179. mingw_prefix = env["mingw_prefix_32"]
  180. else:
  181. env.Append(LINKFLAGS=['-static'])
  182. mingw_prefix = env["mingw_prefix_64"]
  183. env["CC"] = mingw_prefix + "gcc"
  184. env['AS'] = mingw_prefix + "as"
  185. env['CXX'] = mingw_prefix + "g++"
  186. env['AR'] = mingw_prefix + "ar"
  187. env['RANLIB'] = mingw_prefix + "ranlib"
  188. env['LD'] = mingw_prefix + "g++"
  189. env["x86_libtheora_opt_gcc"] = True
  190. ## Compile flags
  191. env.Append(CCFLAGS=['-DWINDOWS_ENABLED', '-mwindows'])
  192. env.Append(CCFLAGS=['-DOPENGL_ENABLED'])
  193. env.Append(CCFLAGS=['-DRTAUDIO_ENABLED'])
  194. env.Append(CCFLAGS=['-DWASAPI_ENABLED'])
  195. env.Append(CCFLAGS=['-DWINVER=%s' % winver, '-D_WIN32_WINNT=%s' % winver])
  196. env.Append(LIBS=['mingw32', 'opengl32', 'dsound', 'ole32', 'd3d9', 'winmm', 'gdi32', 'iphlpapi', 'shlwapi', 'wsock32', 'ws2_32', 'kernel32', 'oleaut32', 'dinput8', 'dxguid', 'ksuser'])
  197. env.Append(CPPFLAGS=['-DMINGW_ENABLED'])
  198. # resrc
  199. env.Append(BUILDERS={'RES': env.Builder(action=build_res_file, suffix='.o', src_suffix='.rc')})