detect.py 15 KB

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