detect.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. return False
  35. def get_opts():
  36. from SCons.Variables import BoolVariable, EnumVariable
  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. BoolVariable('use_lto', 'Use link time optimization (when using MingW)', False),
  50. EnumVariable('debug_symbols', 'Add debug symbols to release version', 'yes', ('yes', 'no', 'full')),
  51. ]
  52. def get_flags():
  53. return [
  54. ]
  55. def build_res_file(target, source, env):
  56. if (env["bits"] == "32"):
  57. cmdbase = env['mingw_prefix_32']
  58. else:
  59. cmdbase = env['mingw_prefix_64']
  60. cmdbase = cmdbase + 'windres --include-dir . '
  61. import subprocess
  62. for x in range(len(source)):
  63. cmd = cmdbase + '-i ' + str(source[x]) + ' -o ' + str(target[x])
  64. try:
  65. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  66. if len(out[1]):
  67. return 1
  68. except:
  69. return 1
  70. return 0
  71. def configure(env):
  72. env.Append(CPPPATH=['#platform/windows'])
  73. # Targeted Windows version: 7 (and later), minimum supported version
  74. # XP support dropped after EOL due to missing API for IPv6 and other issues
  75. # Vista support dropped after EOL due to GH-10243
  76. winver = "0x0601"
  77. if (os.name == "nt" and os.getenv("VCINSTALLDIR")): # MSVC
  78. env['ENV']['TMP'] = os.environ['TMP']
  79. ## Build type
  80. if (env["target"] == "release"):
  81. env.Append(CCFLAGS=['/O2'])
  82. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  83. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  84. elif (env["target"] == "release_debug"):
  85. env.Append(CCFLAGS=['/O2', '/DDEBUG_ENABLED'])
  86. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  87. elif (env["target"] == "debug_release"):
  88. env.Append(CCFLAGS=['/Z7', '/Od'])
  89. env.Append(LINKFLAGS=['/DEBUG'])
  90. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  91. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  92. elif (env["target"] == "debug"):
  93. env.Append(CCFLAGS=['/Z7', '/DDEBUG_ENABLED', '/DDEBUG_MEMORY_ENABLED', '/DD3D_DEBUG_INFO', '/Od', '/EHsc'])
  94. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  95. env.Append(LINKFLAGS=['/DEBUG'])
  96. ## Architecture
  97. # Note: this detection/override code from here onward should be here instead of in SConstruct because it's platform and compiler specific (MSVC/Windows)
  98. if (env["bits"] != "default"):
  99. print("Error: bits argument is disabled for MSVC")
  100. print("""
  101. Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console
  102. (or Visual Studio settings) that is being used to run SCons. As a consequence, bits argument is disabled. Run scons again without bits
  103. argument (example: scons p=windows) and SCons will attempt to detect what MSVC compiler will be executed and inform you.
  104. """)
  105. sys.exit()
  106. # 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
  107. # 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)
  108. env["bits"] = "32"
  109. env["x86_libtheora_opt_vc"] = True
  110. ## Compiler configuration
  111. env['ENV'] = os.environ
  112. # 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
  113. compiler_version_str = methods.detect_visual_c_compiler_version(env['ENV'])
  114. print("Detected MSVC compiler: " + compiler_version_str)
  115. # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writting)... vc compiler for 64bit can not compile _asm
  116. if(compiler_version_str == "amd64" or compiler_version_str == "x86_amd64"):
  117. env["bits"] = "64"
  118. env["x86_libtheora_opt_vc"] = False
  119. print("Compiled program architecture will be a 64 bit executable (forcing bits=64).")
  120. elif (compiler_version_str == "x86" or compiler_version_str == "amd64_x86"):
  121. print("Compiled program architecture will be a 32 bit executable. (forcing bits=32).")
  122. else:
  123. 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.")
  124. ## Compile flags
  125. env.Append(CCFLAGS=['/MT', '/Gd', '/GR', '/nologo'])
  126. env.Append(CXXFLAGS=['/TP'])
  127. env.Append(CPPFLAGS=['/DMSVC', '/GR', ])
  128. env.Append(CCFLAGS=['/I' + os.getenv("WindowsSdkDir") + "/Include"])
  129. env.Append(CCFLAGS=['/DWINDOWS_ENABLED'])
  130. env.Append(CCFLAGS=['/DOPENGL_ENABLED'])
  131. env.Append(CCFLAGS=['/DRTAUDIO_ENABLED'])
  132. env.Append(CCFLAGS=['/DWASAPI_ENABLED'])
  133. env.Append(CCFLAGS=['/DTYPED_METHOD_BIND'])
  134. env.Append(CCFLAGS=['/DWIN32'])
  135. env.Append(CCFLAGS=['/DWINVER=%s' % winver, '/D_WIN32_WINNT=%s' % winver])
  136. if env["bits"] == "64":
  137. env.Append(CCFLAGS=['/D_WIN64'])
  138. LIBS = ['winmm', 'opengl32', 'dsound', 'kernel32', 'ole32', 'oleaut32', 'user32', 'gdi32', 'IPHLPAPI', 'Shlwapi', 'wsock32', 'Ws2_32', 'shell32', 'advapi32', 'dinput8', 'dxguid']
  139. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  140. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  141. if (os.getenv("VCINSTALLDIR")):
  142. VC_PATH = os.getenv("VCINSTALLDIR")
  143. else:
  144. VC_PATH = ""
  145. env.Append(CCFLAGS=["/I" + p for p in os.getenv("INCLUDE").split(";")])
  146. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  147. # Incremental linking fix
  148. env['BUILDERS']['ProgramOriginal'] = env['BUILDERS']['Program']
  149. env['BUILDERS']['Program'] = methods.precious_program
  150. else: # MinGW
  151. # Workaround for MinGW. See:
  152. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  153. env.use_windows_spawn_fix()
  154. ## Build type
  155. if (env["target"] == "release"):
  156. env.Append(CCFLAGS=['-msse2'])
  157. if (env["bits"] == "64"):
  158. env.Append(CCFLAGS=['-O3'])
  159. else:
  160. env.Append(CCFLAGS=['-O2'])
  161. env.Append(LINKFLAGS=['-Wl,--subsystem,windows'])
  162. if (env["debug_symbols"] == "yes"):
  163. env.Prepend(CCFLAGS=['-g1'])
  164. if (env["debug_symbols"] == "full"):
  165. env.Prepend(CCFLAGS=['-g2'])
  166. elif (env["target"] == "release_debug"):
  167. env.Append(CCFLAGS=['-O2', '-DDEBUG_ENABLED'])
  168. if (env["debug_symbols"] == "yes"):
  169. env.Prepend(CCFLAGS=['-g1'])
  170. if (env["debug_symbols"] == "full"):
  171. env.Prepend(CCFLAGS=['-g2'])
  172. elif (env["target"] == "debug"):
  173. env.Append(CCFLAGS=['-g3', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  174. ## Compiler configuration
  175. if (os.name == "nt"):
  176. env['ENV']['TMP'] = os.environ['TMP'] # way to go scons, you can be so stupid sometimes
  177. else:
  178. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  179. if (env["bits"] == "default"):
  180. if (os.name == "nt"):
  181. env["bits"] = "64" if "PROGRAMFILES(X86)" in os.environ else "32"
  182. else: # default to 64-bit on Linux
  183. env["bits"] = "64"
  184. mingw_prefix = ""
  185. if (env["bits"] == "32"):
  186. env.Append(LINKFLAGS=['-static'])
  187. env.Append(LINKFLAGS=['-static-libgcc'])
  188. env.Append(LINKFLAGS=['-static-libstdc++'])
  189. mingw_prefix = env["mingw_prefix_32"]
  190. else:
  191. env.Append(LINKFLAGS=['-static'])
  192. mingw_prefix = env["mingw_prefix_64"]
  193. env["CC"] = mingw_prefix + "gcc"
  194. env['AS'] = mingw_prefix + "as"
  195. env['CXX'] = mingw_prefix + "g++"
  196. env['AR'] = mingw_prefix + "gcc-ar"
  197. env['RANLIB'] = mingw_prefix + "gcc-ranlib"
  198. env['LD'] = mingw_prefix + "g++"
  199. env["x86_libtheora_opt_gcc"] = True
  200. if env['use_lto']:
  201. env.Append(CCFLAGS=['-flto'])
  202. if not env['use_llvm'] and env.GetOption("num_jobs") > 1:
  203. env.Append(LINKFLAGS=['-flto=' + str(env.GetOption("num_jobs"))])
  204. else:
  205. env.Append(LINKFLAGS=['-flto'])
  206. ## Compile flags
  207. env.Append(CCFLAGS=['-DWINDOWS_ENABLED', '-mwindows'])
  208. env.Append(CCFLAGS=['-DOPENGL_ENABLED'])
  209. env.Append(CCFLAGS=['-DRTAUDIO_ENABLED'])
  210. env.Append(CCFLAGS=['-DWASAPI_ENABLED'])
  211. env.Append(CCFLAGS=['-DWINVER=%s' % winver, '-D_WIN32_WINNT=%s' % winver])
  212. env.Append(LIBS=['mingw32', 'opengl32', 'dsound', 'ole32', 'd3d9', 'winmm', 'gdi32', 'iphlpapi', 'shlwapi', 'wsock32', 'ws2_32', 'kernel32', 'oleaut32', 'dinput8', 'dxguid', 'ksuser'])
  213. env.Append(CPPFLAGS=['-DMINGW_ENABLED'])
  214. # resrc
  215. env.Append(BUILDERS={'RES': env.Builder(action=build_res_file, suffix='.o', src_suffix='.rc')})