detect.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #
  2. # tested on | Windows native | Linux cross-compilation
  3. # ------------------------+-------------------+---------------------------
  4. # MSVS C++ 2010 Express | WORKS | n/a
  5. # Mingw-w64 | WORKS | WORKS
  6. # Mingw-w32 | WORKS | WORKS
  7. # MinGW | WORKS | untested
  8. #
  9. #####
  10. # Notes about MSVS C++ :
  11. #
  12. # - MSVC2010-Express compiles to 32bits only.
  13. #
  14. #####
  15. # Notes about Mingw-w64 and Mingw-w32 under Windows :
  16. #
  17. # - both can be installed using the official installer :
  18. # http://mingw-w64.sourceforge.net/download.php#mingw-builds
  19. #
  20. # - if you want to compile both 32bits and 64bits, don't forget to
  21. # run the installer twice to install them both.
  22. #
  23. # - install them into a path that does not contain spaces
  24. # ( example : "C:/Mingw-w32", "C:/Mingw-w64" )
  25. #
  26. # - if you want to compile faster using the "-j" option, don't forget
  27. # to install the appropriate version of the Pywin32 python extension
  28. # available from : http://sourceforge.net/projects/pywin32/files/
  29. #
  30. # - before running scons, you must add into the environment path
  31. # the path to the "/bin" directory of the Mingw version you want
  32. # to use :
  33. #
  34. # set PATH=C:/Mingw-w32/bin;%PATH%
  35. #
  36. # - then, scons should be able to detect gcc.
  37. # - Mingw-w32 only compiles 32bits.
  38. # - Mingw-w64 only compiles 64bits.
  39. #
  40. # - it is possible to add them both at the same time into the PATH env,
  41. # if you also define the MINGW32_PREFIX and MINGW64_PREFIX environment
  42. # variables.
  43. # For instance, you could store that set of commands into a .bat script
  44. # that you would run just before scons :
  45. #
  46. # set PATH=C:\mingw-w32\bin;%PATH%
  47. # set PATH=C:\mingw-w64\bin;%PATH%
  48. # set MINGW32_PREFIX=C:\mingw-w32\bin\
  49. # set MINGW64_PREFIX=C:\mingw-w64\bin\
  50. #
  51. #####
  52. # Notes about Mingw, Mingw-w64 and Mingw-w32 under Linux :
  53. #
  54. # - default toolchain prefixes are :
  55. # "i586-mingw32msvc-" for MinGW
  56. # "i686-w64-mingw32-" for Mingw-w32
  57. # "x86_64-w64-mingw32-" for Mingw-w64
  58. #
  59. # - if both MinGW and Mingw-w32 are installed on your system
  60. # Mingw-w32 should take the priority over MinGW.
  61. #
  62. # - it is possible to manually override prefixes by defining
  63. # the MINGW32_PREFIX and MINGW64_PREFIX environment variables.
  64. #
  65. #####
  66. # Notes about Mingw under Windows :
  67. #
  68. # - this is the MinGW version from http://mingw.org/
  69. # - install it into a path that does not contain spaces
  70. # ( example : "C:/MinGW" )
  71. # - several DirectX headers might be missing. You can copy them into
  72. # the C:/MinGW/include" directory from this page :
  73. # https://code.google.com/p/mingw-lib/source/browse/trunk/working/avcodec_to_widget_5/directx_include/
  74. # - before running scons, add the path to the "/bin" directory :
  75. # set PATH=C:/MinGW/bin;%PATH%
  76. # - scons should be able to detect gcc.
  77. #
  78. #####
  79. # TODO :
  80. #
  81. # - finish to cleanup this script to remove all the remains of previous hacks and workarounds
  82. # - make it work with the Windows7 SDK that is supposed to enable 64bits compilation for MSVC2010-Express
  83. # - confirm it works well with other Visual Studio versions.
  84. # - update the wiki about the pywin32 extension required for the "-j" option under Windows.
  85. # - update the wiki to document MINGW32_PREFIX and MINGW64_PREFIX
  86. #
  87. import os
  88. import sys
  89. import methods
  90. def is_active():
  91. return True
  92. def get_name():
  93. return "Windows"
  94. def can_build():
  95. if (os.name == "nt"):
  96. # building natively on windows!
  97. if (os.getenv("VCINSTALLDIR")):
  98. return True
  99. else:
  100. print("\nMSVC not detected, attempting Mingw.")
  101. mingw32 = ""
  102. mingw64 = ""
  103. if (os.getenv("MINGW32_PREFIX")):
  104. mingw32 = os.getenv("MINGW32_PREFIX")
  105. if (os.getenv("MINGW64_PREFIX")):
  106. mingw64 = os.getenv("MINGW64_PREFIX")
  107. test = "gcc --version > NUL 2>&1"
  108. if os.system(test) != 0 and os.system(mingw32 + test) != 0 and os.system(mingw64 + test) != 0:
  109. print("- could not detect gcc.")
  110. print("Please, make sure a path to a Mingw /bin directory is accessible into the environment PATH.\n")
  111. return False
  112. else:
  113. print("- gcc detected.")
  114. return True
  115. if (os.name == "posix"):
  116. mingw = "i586-mingw32msvc-"
  117. mingw64 = "x86_64-w64-mingw32-"
  118. mingw32 = "i686-w64-mingw32-"
  119. if (os.getenv("MINGW32_PREFIX")):
  120. mingw32 = os.getenv("MINGW32_PREFIX")
  121. mingw = mingw32
  122. if (os.getenv("MINGW64_PREFIX")):
  123. mingw64 = os.getenv("MINGW64_PREFIX")
  124. test = "gcc --version > /dev/null 2>&1"
  125. if (os.system(mingw + test) == 0 or os.system(mingw64 + test) == 0 or os.system(mingw32 + test) == 0):
  126. return True
  127. return False
  128. def get_opts():
  129. mingw = ""
  130. mingw32 = ""
  131. mingw64 = ""
  132. if (os.name == "posix"):
  133. mingw = "i586-mingw32msvc-"
  134. mingw32 = "i686-w64-mingw32-"
  135. mingw64 = "x86_64-w64-mingw32-"
  136. if os.system(mingw32 + "gcc --version > /dev/null 2>&1") != 0:
  137. mingw32 = mingw
  138. if (os.getenv("MINGW32_PREFIX")):
  139. mingw32 = os.getenv("MINGW32_PREFIX")
  140. mingw = mingw32
  141. if (os.getenv("MINGW64_PREFIX")):
  142. mingw64 = os.getenv("MINGW64_PREFIX")
  143. return [
  144. ('mingw_prefix', 'Mingw Prefix', mingw32),
  145. ('mingw_prefix_64', 'Mingw Prefix 64 bits', mingw64),
  146. # Targeted Windows version: Vista (and later)
  147. ('target_win_version', 'Targeted Windows version, >= 0x0600 (Vista)', '0x0600'),
  148. ]
  149. def get_flags():
  150. return [
  151. ]
  152. def build_res_file(target, source, env):
  153. cmdbase = ""
  154. if (env["bits"] == "32"):
  155. cmdbase = env['mingw_prefix']
  156. else:
  157. cmdbase = env['mingw_prefix_64']
  158. CPPPATH = env['CPPPATH']
  159. cmdbase = cmdbase + 'windres --include-dir . '
  160. import subprocess
  161. for x in range(len(source)):
  162. cmd = cmdbase + '-i ' + str(source[x]) + ' -o ' + str(target[x])
  163. try:
  164. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  165. if len(out[1]):
  166. return 1
  167. except:
  168. return 1
  169. return 0
  170. def configure(env):
  171. env.Append(CPPPATH=['#platform/windows'])
  172. env['is_mingw'] = False
  173. if (os.name == "nt" and os.getenv("VCINSTALLDIR")):
  174. # build using visual studio
  175. env['ENV']['TMP'] = os.environ['TMP']
  176. env.Append(CPPPATH=['#platform/windows/include'])
  177. env.Append(LIBPATH=['#platform/windows/lib'])
  178. env.Append(CCFLAGS=['/DWINVER=%s' % env['target_win_version'], '/D_WIN32_WINNT=%s' % env['target_win_version']])
  179. if (env["target"] == "release"):
  180. env.Append(CCFLAGS=['/O2'])
  181. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  182. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  183. elif (env["target"] == "release_debug"):
  184. env.Append(CCFLAGS=['/O2', '/DDEBUG_ENABLED'])
  185. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  186. elif (env["target"] == "debug_release"):
  187. env.Append(CCFLAGS=['/Z7', '/Od'])
  188. env.Append(LINKFLAGS=['/DEBUG'])
  189. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  190. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  191. elif (env["target"] == "debug"):
  192. env.Append(CCFLAGS=['/Z7', '/DDEBUG_ENABLED', '/DDEBUG_MEMORY_ENABLED', '/DD3D_DEBUG_INFO', '/Od', '/EHsc'])
  193. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  194. env.Append(LINKFLAGS=['/DEBUG'])
  195. env.Append(CCFLAGS=['/MT', '/Gd', '/GR', '/nologo'])
  196. env.Append(CXXFLAGS=['/TP'])
  197. env.Append(CPPFLAGS=['/DMSVC', '/GR', ])
  198. env.Append(CCFLAGS=['/I' + os.getenv("WindowsSdkDir") + "/Include"])
  199. env.Append(CCFLAGS=['/DWINDOWS_ENABLED'])
  200. env.Append(CCFLAGS=['/DRTAUDIO_ENABLED'])
  201. env.Append(CCFLAGS=['/DWASAPI_ENABLED'])
  202. env.Append(CCFLAGS=['/DWIN32'])
  203. env.Append(CCFLAGS=['/DTYPED_METHOD_BIND'])
  204. env.Append(CCFLAGS=['/DGLES2_ENABLED'])
  205. LIBS = ['winmm', 'opengl32', 'dsound', 'kernel32', 'ole32', 'oleaut32', 'user32', 'gdi32', 'IPHLPAPI', 'Shlwapi', 'wsock32', 'ws2_32', 'shell32', 'advapi32', 'dinput8', 'dxguid']
  206. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  207. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  208. if (os.getenv("DXSDK_DIR")):
  209. DIRECTX_PATH = os.getenv("DXSDK_DIR")
  210. else:
  211. DIRECTX_PATH = "C:/Program Files/Microsoft DirectX SDK (March 2009)"
  212. if (os.getenv("VCINSTALLDIR")):
  213. VC_PATH = os.getenv("VCINSTALLDIR")
  214. else:
  215. VC_PATH = ""
  216. env.Append(CCFLAGS=["/I" + p for p in os.getenv("INCLUDE").split(";")])
  217. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  218. env.Append(CCFLAGS=["/I" + DIRECTX_PATH + "/Include"])
  219. env.Append(LIBPATH=[DIRECTX_PATH + "/Lib/x86"])
  220. env['ENV'] = os.environ
  221. # 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
  222. compiler_version_str = methods.detect_visual_c_compiler_version(env['ENV'])
  223. # Note: this detection/override code from here onward should be here instead of in SConstruct because it's platform and compiler specific (MSVC/Windows)
  224. if(env["bits"] != "default"):
  225. print("Error: bits argument is disabled for MSVC")
  226. print ("Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console (or Visual Studio settings)"
  227. + " that is being used to run SCons. As a consequence, bits argument is disabled. Run scons again without bits argument (example: scons p=windows) and SCons will attempt to detect what MSVC compiler"
  228. + " will be executed and inform you.")
  229. sys.exit()
  230. # 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 propper command prompt
  231. # 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)
  232. env["bits"] = "32"
  233. env["x86_libtheora_opt_vc"] = True
  234. print("Detected MSVC compiler: " + compiler_version_str)
  235. # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writting)... vc compiler for 64bit can not compile _asm
  236. if(compiler_version_str == "amd64" or compiler_version_str == "x86_amd64"):
  237. env["bits"] = "64"
  238. env["x86_libtheora_opt_vc"] = False
  239. print("Compiled program architecture will be a 64 bit executable (forcing bits=64).")
  240. elif (compiler_version_str == "x86" or compiler_version_str == "amd64_x86"):
  241. print("Compiled program architecture will be a 32 bit executable. (forcing bits=32).")
  242. else:
  243. 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.")
  244. if env["bits"] == "64":
  245. env.Append(CCFLAGS=['/D_WIN64'])
  246. else:
  247. # Workaround for MinGW. See:
  248. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  249. env.use_windows_spawn_fix()
  250. # build using mingw
  251. env.Append(CCFLAGS=['-DWINVER=%s' % env['target_win_version'], '-D_WIN32_WINNT=%s' % env['target_win_version']])
  252. if (os.name == "nt"):
  253. env['ENV']['TMP'] = os.environ['TMP'] # way to go scons, you can be so stupid sometimes
  254. else:
  255. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  256. mingw_prefix = ""
  257. if (env["bits"] == "default"):
  258. env["bits"] = "64" if "PROGRAMFILES(X86)" in os.environ else "32"
  259. if (env["bits"] == "32"):
  260. env.Append(LINKFLAGS=['-static'])
  261. env.Append(LINKFLAGS=['-static-libgcc'])
  262. env.Append(LINKFLAGS=['-static-libstdc++'])
  263. mingw_prefix = env["mingw_prefix"]
  264. else:
  265. env.Append(LINKFLAGS=['-static'])
  266. mingw_prefix = env["mingw_prefix_64"]
  267. if (env["target"] == "release"):
  268. env.Append(CCFLAGS=['-msse2'])
  269. if (env["bits"] == "64"):
  270. env.Append(CCFLAGS=['-O3'])
  271. else:
  272. env.Append(CCFLAGS=['-O2'])
  273. env.Append(LINKFLAGS=['-Wl,--subsystem,windows'])
  274. elif (env["target"] == "release_debug"):
  275. env.Append(CCFLAGS=['-O2', '-DDEBUG_ENABLED'])
  276. elif (env["target"] == "debug"):
  277. env.Append(CCFLAGS=['-g', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  278. env["CC"] = mingw_prefix + "gcc"
  279. env['AS'] = mingw_prefix + "as"
  280. env['CXX'] = mingw_prefix + "g++"
  281. env['AR'] = mingw_prefix + "ar"
  282. env['RANLIB'] = mingw_prefix + "ranlib"
  283. env['LD'] = mingw_prefix + "g++"
  284. env["x86_libtheora_opt_gcc"] = True
  285. #env['CC'] = "winegcc"
  286. #env['CXX'] = "wineg++"
  287. env.Append(CCFLAGS=['-DWINDOWS_ENABLED', '-mwindows'])
  288. env.Append(CPPFLAGS=['-DRTAUDIO_ENABLED'])
  289. env.Append(CPPFLAGS=['-DWASAPI_ENABLED'])
  290. env.Append(CCFLAGS=['-DGLES2_ENABLED'])
  291. env.Append(LIBS=['mingw32', 'opengl32', 'dsound', 'ole32', 'd3d9', 'winmm', 'gdi32', 'iphlpapi', 'shlwapi', 'wsock32', 'ws2_32', 'kernel32', 'oleaut32', 'dinput8', 'dxguid', 'ksuser'])
  292. # if (env["bits"]=="32"):
  293. # env.Append(LIBS=['gcc_s'])
  294. # #--with-arch=i686
  295. # env.Append(CPPFLAGS=['-march=i686'])
  296. # env.Append(LINKFLAGS=['-march=i686'])
  297. #'d3dx9d'
  298. env.Append(CPPFLAGS=['-DMINGW_ENABLED'])
  299. # env.Append(LINKFLAGS=['-g'])
  300. # resrc
  301. env['is_mingw'] = True
  302. env.Append(BUILDERS={'RES': env.Builder(action=build_res_file, suffix='.o', src_suffix='.rc')})
  303. env.Append(BUILDERS={'GLSL120': env.Builder(action=methods.build_legacygl_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
  304. env.Append(BUILDERS={'GLSL': env.Builder(action=methods.build_glsl_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
  305. env.Append(BUILDERS={'HLSL9': env.Builder(action=methods.build_hlsl_dx9_headers, suffix='hlsl.gen.h', src_suffix='.hlsl')})
  306. env.Append(BUILDERS={'GLSL120GLES': env.Builder(action=methods.build_gles2_headers, suffix='glsl.gen.h', src_suffix='.glsl')})