detect.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. # Targeted Windows version: Vista (and later)
  178. env.Append(CPPFLAGS=['-D_WIN32_WINNT=0x0600'])
  179. env['is_mingw'] = False
  180. if (os.name == "nt" and os.getenv("VCINSTALLDIR")):
  181. # build using visual studio
  182. env['ENV']['TMP'] = os.environ['TMP']
  183. env.Append(CPPPATH=['#platform/windows/include'])
  184. env.Append(LIBPATH=['#platform/windows/lib'])
  185. if (env["target"] == "release"):
  186. env.Append(CCFLAGS=['/O2'])
  187. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  188. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  189. elif (env["target"] == "release_debug"):
  190. env.Append(CCFLAGS=['/O2', '/DDEBUG_ENABLED'])
  191. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  192. elif (env["target"] == "debug_release"):
  193. env.Append(CCFLAGS=['/Z7', '/Od'])
  194. env.Append(LINKFLAGS=['/DEBUG'])
  195. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  196. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  197. elif (env["target"] == "debug"):
  198. env.Append(CCFLAGS=['/Z7', '/DDEBUG_ENABLED', '/DDEBUG_MEMORY_ENABLED', '/DD3D_DEBUG_INFO', '/Od'])
  199. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  200. env.Append(LINKFLAGS=['/DEBUG'])
  201. env.Append(CCFLAGS=['/MT', '/Gd', '/GR', '/nologo'])
  202. env.Append(CXXFLAGS=['/TP'])
  203. env.Append(CPPFLAGS=['/DMSVC', '/GR', ])
  204. env.Append(CCFLAGS=['/I' + os.getenv("WindowsSdkDir") + "/Include"])
  205. env.Append(CCFLAGS=['/DWINDOWS_ENABLED'])
  206. env.Append(CCFLAGS=['/DRTAUDIO_ENABLED'])
  207. env.Append(CCFLAGS=['/DWIN32'])
  208. env.Append(CCFLAGS=['/DTYPED_METHOD_BIND'])
  209. env.Append(CCFLAGS=['/DOPENGL_ENABLED'])
  210. LIBS = ['winmm', 'opengl32', 'dsound', 'kernel32', 'ole32', 'oleaut32', 'user32', 'gdi32', 'IPHLPAPI', 'Shlwapi', 'wsock32', 'Ws2_32', 'shell32', 'advapi32', 'dinput8', 'dxguid']
  211. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  212. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  213. if (os.getenv("DXSDK_DIR")):
  214. DIRECTX_PATH = os.getenv("DXSDK_DIR")
  215. else:
  216. DIRECTX_PATH = "C:/Program Files/Microsoft DirectX SDK (March 2009)"
  217. if (os.getenv("VCINSTALLDIR")):
  218. VC_PATH = os.getenv("VCINSTALLDIR")
  219. else:
  220. VC_PATH = ""
  221. env.Append(CCFLAGS=["/I" + p for p in os.getenv("INCLUDE").split(";")])
  222. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  223. env.Append(CCFLAGS=["/I" + DIRECTX_PATH + "/Include"])
  224. env.Append(LIBPATH=[DIRECTX_PATH + "/Lib/x86"])
  225. env['ENV'] = os.environ
  226. # 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
  227. compiler_version_str = methods.detect_visual_c_compiler_version(env['ENV'])
  228. # Note: this detection/override code from here onward should be here instead of in SConstruct because it's platform and compiler specific (MSVC/Windows)
  229. if(env["bits"] != "default"):
  230. print "Error: bits argument is disabled for MSVC"
  231. print ("Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console (or Visual Studio settings)"
  232. + " 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"
  233. + " will be executed and inform you.")
  234. sys.exit()
  235. # 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
  236. # 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)
  237. env["bits"] = "32"
  238. env["x86_libtheora_opt_vc"] = True
  239. print "Detected MSVC compiler: " + compiler_version_str
  240. # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writting)... vc compiler for 64bit can not compile _asm
  241. if(compiler_version_str == "amd64" or compiler_version_str == "x86_amd64"):
  242. env["bits"] = "64"
  243. env["x86_libtheora_opt_vc"] = False
  244. print "Compiled program architecture will be a 64 bit executable (forcing bits=64)."
  245. elif (compiler_version_str == "x86" or compiler_version_str == "amd64_x86"):
  246. print "Compiled program architecture will be a 32 bit executable. (forcing bits=32)."
  247. else:
  248. 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."
  249. if env["bits"] == "64":
  250. env.Append(CCFLAGS=['/D_WIN64'])
  251. # Incremental linking fix
  252. env['BUILDERS']['ProgramOriginal'] = env['BUILDERS']['Program']
  253. env['BUILDERS']['Program'] = methods.precious_program
  254. else:
  255. # Workaround for MinGW. See:
  256. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  257. env.use_windows_spawn_fix()
  258. # build using mingw
  259. if (os.name == "nt"):
  260. env['ENV']['TMP'] = os.environ['TMP'] # way to go scons, you can be so stupid sometimes
  261. else:
  262. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  263. mingw_prefix = ""
  264. if (env["bits"] == "default"):
  265. env["bits"] = "32"
  266. if (env["bits"] == "32"):
  267. env.Append(LINKFLAGS=['-static'])
  268. env.Append(LINKFLAGS=['-static-libgcc'])
  269. env.Append(LINKFLAGS=['-static-libstdc++'])
  270. mingw_prefix = env["mingw_prefix"]
  271. else:
  272. env.Append(LINKFLAGS=['-static'])
  273. mingw_prefix = env["mingw_prefix_64"]
  274. nulstr = ""
  275. if (os.name == "posix"):
  276. nulstr = ">/dev/null"
  277. else:
  278. nulstr = ">nul"
  279. # if os.system(mingw_prefix+"gcc --version"+nulstr)!=0:
  280. # #not really super consistent but..
  281. # print("Can't find Windows compiler: "+mingw_prefix)
  282. # sys.exit(255)
  283. if (env["target"] == "release"):
  284. env.Append(CCFLAGS=['-msse2'])
  285. if (env["bits"] == "64"):
  286. env.Append(CCFLAGS=['-O3'])
  287. else:
  288. env.Append(CCFLAGS=['-O2'])
  289. env.Append(LINKFLAGS=['-Wl,--subsystem,windows'])
  290. elif (env["target"] == "release_debug"):
  291. env.Append(CCFLAGS=['-O2', '-DDEBUG_ENABLED'])
  292. elif (env["target"] == "debug"):
  293. env.Append(CCFLAGS=['-g', '-Wall', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  294. env["CC"] = mingw_prefix + "gcc"
  295. env['AS'] = mingw_prefix + "as"
  296. env['CXX'] = mingw_prefix + "g++"
  297. env['AR'] = mingw_prefix + "ar"
  298. env['RANLIB'] = mingw_prefix + "ranlib"
  299. env['LD'] = mingw_prefix + "g++"
  300. env["x86_libtheora_opt_gcc"] = True
  301. #env['CC'] = "winegcc"
  302. #env['CXX'] = "wineg++"
  303. env.Append(CCFLAGS=['-DWINDOWS_ENABLED', '-mwindows'])
  304. env.Append(CPPFLAGS=['-DRTAUDIO_ENABLED'])
  305. env.Append(CCFLAGS=['-DOPENGL_ENABLED'])
  306. env.Append(LIBS=['mingw32', 'opengl32', 'dsound', 'ole32', 'd3d9', 'winmm', 'gdi32', 'iphlpapi', 'shlwapi', 'wsock32', 'ws2_32', 'kernel32', 'oleaut32', 'dinput8', 'dxguid'])
  307. # if (env["bits"]=="32"):
  308. # env.Append(LIBS=['gcc_s'])
  309. # #--with-arch=i686
  310. # env.Append(CPPFLAGS=['-march=i686'])
  311. # env.Append(LINKFLAGS=['-march=i686'])
  312. #'d3dx9d'
  313. env.Append(CPPFLAGS=['-DMINGW_ENABLED'])
  314. # env.Append(LINKFLAGS=['-g'])
  315. # resrc
  316. env['is_mingw'] = True
  317. env.Append(BUILDERS={'RES': env.Builder(action=build_res_file, suffix='.o', src_suffix='.rc')})
  318. env.Append(BUILDERS={'GLSL120': env.Builder(action=methods.build_legacygl_headers, suffix='glsl.h', src_suffix='.glsl')})
  319. env.Append(BUILDERS={'GLSL': env.Builder(action=methods.build_glsl_headers, suffix='glsl.h', src_suffix='.glsl')})
  320. env.Append(BUILDERS={'HLSL9': env.Builder(action=methods.build_hlsl_dx9_headers, suffix='hlsl.h', src_suffix='.hlsl')})
  321. env.Append(BUILDERS={'GLSL120GLES': env.Builder(action=methods.build_gles2_headers, suffix='glsl.h', src_suffix='.glsl')})