detect.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. def is_active():
  90. return True
  91. def get_name():
  92. return "Windows"
  93. def can_build():
  94. if (os.name=="nt"):
  95. #building natively on windows!
  96. if (os.getenv("VSINSTALLDIR")):
  97. return True
  98. else:
  99. print("\nMSVC not detected, attempting Mingw.")
  100. mingw32 = ""
  101. mingw64 = ""
  102. if ( os.getenv("MINGW32_PREFIX") ) :
  103. mingw32 = os.getenv("MINGW32_PREFIX")
  104. if ( os.getenv("MINGW64_PREFIX") ) :
  105. mingw64 = os.getenv("MINGW64_PREFIX")
  106. test = "gcc --version > NUL 2>&1"
  107. if os.system(test)!= 0 and os.system(mingw32+test)!=0 and os.system(mingw64+test)!=0 :
  108. print("- could not detect gcc.")
  109. print("Please, make sure a path to a Mingw /bin directory is accessible into the environment PATH.\n")
  110. return False
  111. else:
  112. print("- gcc detected.")
  113. return True
  114. if (os.name=="posix"):
  115. mingw = "i586-mingw32msvc-"
  116. mingw64 = "x86_64-w64-mingw32-"
  117. mingw32 = "i686-w64-mingw32-"
  118. if (os.getenv("MINGW32_PREFIX")):
  119. mingw32=os.getenv("MINGW32_PREFIX")
  120. mingw = mingw32
  121. if (os.getenv("MINGW64_PREFIX")):
  122. mingw64=os.getenv("MINGW64_PREFIX")
  123. test = "gcc --version &>/dev/null"
  124. if (os.system(mingw+test) == 0 or os.system(mingw64+test) == 0 or os.system(mingw32+test) == 0):
  125. return True
  126. return False
  127. def get_opts():
  128. mingw=""
  129. mingw32=""
  130. mingw64=""
  131. if ( os.name == "posix" ):
  132. mingw = "i586-mingw32msvc-"
  133. mingw32 = "i686-w64-mingw32-"
  134. mingw64 = "x86_64-w64-mingw32-"
  135. if os.system(mingw32+"gcc --version &>/dev/null") != 0 :
  136. mingw32 = mingw
  137. if (os.getenv("MINGW32_PREFIX")):
  138. mingw32=os.getenv("MINGW32_PREFIX")
  139. mingw = mingw32
  140. if (os.getenv("MINGW64_PREFIX")):
  141. mingw64=os.getenv("MINGW64_PREFIX")
  142. return [
  143. ('mingw_prefix','Mingw Prefix',mingw32),
  144. ('mingw_prefix_64','Mingw Prefix 64 bits',mingw64),
  145. ]
  146. def get_flags():
  147. return [
  148. ('glew','yes'),
  149. ('openssl','builtin'), #use builtin openssl
  150. ]
  151. def build_res_file( target, source, env ):
  152. cmdbase = ""
  153. if (env["bits"] == "32"):
  154. cmdbase = env['mingw_prefix']
  155. else:
  156. cmdbase = env['mingw_prefix_64']
  157. CPPPATH = env['CPPPATH']
  158. cmdbase = cmdbase + 'windres --include-dir . '
  159. import subprocess
  160. for x in range(len(source)):
  161. cmd = cmdbase + '-i ' + str(source[x]) + ' -o ' + str(target[x])
  162. try:
  163. out = subprocess.Popen(cmd,shell = True,stderr = subprocess.PIPE).communicate()
  164. if len(out[1]):
  165. return 1
  166. except:
  167. return 1
  168. return 0
  169. def configure(env):
  170. env.Append(CPPPATH=['#platform/windows'])
  171. env['is_mingw']=False
  172. if (os.name=="nt" and os.getenv("VSINSTALLDIR")!=None):
  173. #build using visual studio
  174. env['ENV']['TMP'] = os.environ['TMP']
  175. env.Append(CPPPATH=['#platform/windows/include'])
  176. env.Append(LIBPATH=['#platform/windows/lib'])
  177. if (env["target"]=="release"):
  178. env.Append(CCFLAGS=['/O2'])
  179. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  180. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  181. elif (env["target"]=="release_debug"):
  182. env.Append(CCFLAGS=['/O2','/DDEBUG_ENABLED'])
  183. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  184. elif (env["target"]=="debug_release"):
  185. env.Append(CCFLAGS=['/Z7','/Od'])
  186. env.Append(LINKFLAGS=['/DEBUG'])
  187. env.Append(LINKFLAGS=['/SUBSYSTEM:WINDOWS'])
  188. env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup'])
  189. elif (env["target"]=="debug"):
  190. env.Append(CCFLAGS=['/Z7','/DDEBUG_ENABLED','/DDEBUG_MEMORY_ENABLED','/DD3D_DEBUG_INFO','/Od'])
  191. env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE'])
  192. env.Append(LINKFLAGS=['/DEBUG'])
  193. env.Append(CCFLAGS=['/MT','/Gd','/GR','/nologo'])
  194. env.Append(CXXFLAGS=['/TP'])
  195. env.Append(CPPFLAGS=['/DMSVC', '/GR', ])
  196. env.Append(CCFLAGS=['/I'+os.getenv("WindowsSdkDir")+"/Include"])
  197. env.Append(CCFLAGS=['/DWINDOWS_ENABLED'])
  198. env.Append(CCFLAGS=['/DRTAUDIO_ENABLED'])
  199. env.Append(CCFLAGS=['/DWIN32'])
  200. env.Append(CCFLAGS=['/DTYPED_METHOD_BIND'])
  201. env.Append(CCFLAGS=['/DGLES2_ENABLED'])
  202. LIBS=['winmm','opengl32','dsound','kernel32','ole32','oleaut32','user32','gdi32', 'IPHLPAPI','Shlwapi', 'wsock32','Ws2_32', 'shell32','advapi32','dinput8','dxguid']
  203. env.Append(LINKFLAGS=[p+env["LIBSUFFIX"] for p in LIBS])
  204. env.Append(LIBPATH=[os.getenv("WindowsSdkDir")+"/Lib"])
  205. if (os.getenv("DXSDK_DIR")):
  206. DIRECTX_PATH=os.getenv("DXSDK_DIR")
  207. else:
  208. DIRECTX_PATH="C:/Program Files/Microsoft DirectX SDK (March 2009)"
  209. if (os.getenv("VCINSTALLDIR")):
  210. VC_PATH=os.getenv("VCINSTALLDIR")
  211. else:
  212. VC_PATH=""
  213. env.Append(CCFLAGS=["/I" + p for p in os.getenv("INCLUDE").split(";")])
  214. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  215. env.Append(CCFLAGS=["/I"+DIRECTX_PATH+"/Include"])
  216. env.Append(LIBPATH=[DIRECTX_PATH+"/Lib/x86"])
  217. env['ENV'] = os.environ;
  218. # 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
  219. compiler_version_str = detect_visual_c_compiler_version(env['ENV'])
  220. # Note: this detection/override code from here onward should be here instead of in SConstruct because it's platform and compiler specific (MSVC/Windows)
  221. if(env["bits"] != "default"):
  222. print "Error: bits argument is disabled for MSVC"
  223. print ("Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console (or Visual Studio settings)"
  224. +" 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"
  225. +" will be executed and inform you.")
  226. sys.exit()
  227. # 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
  228. # 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)
  229. env["bits"]="32"
  230. env["x86_opt_vc"]=True
  231. print "Detected MSVC compiler: "+compiler_version_str
  232. # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writting)... vc compiler for 64bit can not compile _asm
  233. if(compiler_version_str == "amd64" or compiler_version_str == "x86_amd64"):
  234. env["bits"]="64"
  235. env["x86_opt_vc"]=False
  236. print "Compiled program architecture will be a 64 bit executable (forcing bits=64)."
  237. elif (compiler_version_str=="x86" or compiler_version_str == "amd64_x86"):
  238. print "Compiled program architecture will be a 32 bit executable. (forcing bits=32)."
  239. else:
  240. 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."
  241. if env["bits"]=="64":
  242. env.Append(CCFLAGS=['/D_WIN64'])
  243. else:
  244. # Workaround for MinGW. See:
  245. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  246. env.use_windows_spawn_fix()
  247. #build using mingw
  248. if (os.name=="nt"):
  249. env['ENV']['TMP'] = os.environ['TMP'] #way to go scons, you can be so stupid sometimes
  250. else:
  251. env["PROGSUFFIX"]=env["PROGSUFFIX"]+".exe" # for linux cross-compilation
  252. mingw_prefix=""
  253. if (env["bits"]=="default"):
  254. env["bits"]="32"
  255. if (env["bits"]=="32"):
  256. env.Append(LINKFLAGS=['-static'])
  257. env.Append(LINKFLAGS=['-static-libgcc'])
  258. env.Append(LINKFLAGS=['-static-libstdc++'])
  259. mingw_prefix=env["mingw_prefix"];
  260. else:
  261. env.Append(LINKFLAGS=['-static'])
  262. mingw_prefix=env["mingw_prefix_64"];
  263. nulstr=""
  264. if (os.name=="posix"):
  265. nulstr=">/dev/null"
  266. else:
  267. nulstr=">nul"
  268. # if os.system(mingw_prefix+"gcc --version"+nulstr)!=0:
  269. # #not really super consistent but..
  270. # print("Can't find Windows compiler: "+mingw_prefix)
  271. # sys.exit(255)
  272. if (env["target"]=="release"):
  273. env.Append(CCFLAGS=['-msse2'])
  274. if (env["bits"]=="64"):
  275. env.Append(CCFLAGS=['-O3'])
  276. else:
  277. env.Append(CCFLAGS=['-O2'])
  278. env.Append(LINKFLAGS=['-Wl,--subsystem,windows'])
  279. elif (env["target"]=="release_debug"):
  280. env.Append(CCFLAGS=['-O2','-DDEBUG_ENABLED'])
  281. elif (env["target"]=="debug"):
  282. env.Append(CCFLAGS=['-g', '-Wall','-DDEBUG_ENABLED','-DDEBUG_MEMORY_ENABLED'])
  283. env["CC"]=mingw_prefix+"gcc"
  284. env['AS']=mingw_prefix+"as"
  285. env['CXX'] = mingw_prefix+"g++"
  286. env['AR'] = mingw_prefix+"ar"
  287. env['RANLIB'] = mingw_prefix+"ranlib"
  288. env['LD'] = mingw_prefix+"g++"
  289. env["x86_opt_gcc"]=True
  290. #env['CC'] = "winegcc"
  291. #env['CXX'] = "wineg++"
  292. env.Append(CCFLAGS=['-DWINDOWS_ENABLED','-mwindows'])
  293. env.Append(CPPFLAGS=['-DRTAUDIO_ENABLED'])
  294. env.Append(CCFLAGS=['-DGLES2_ENABLED'])
  295. env.Append(LIBS=['mingw32','opengl32', 'dsound', 'ole32', 'd3d9','winmm','gdi32','iphlpapi','shlwapi','wsock32','wsa2_32','kernel32', 'oleaut32', 'dinput8', 'dxguid'])
  296. # if (env["bits"]=="32"):
  297. # env.Append(LIBS=['gcc_s'])
  298. # #--with-arch=i686
  299. # env.Append(CPPFLAGS=['-march=i686'])
  300. # env.Append(LINKFLAGS=['-march=i686'])
  301. #'d3dx9d'
  302. env.Append(CPPFLAGS=['-DMINGW_ENABLED'])
  303. #env.Append(LINKFLAGS=['-g'])
  304. # resrc
  305. env['is_mingw']=True
  306. env.Append( BUILDERS = { 'RES' : env.Builder(action = build_res_file, suffix = '.o',src_suffix = '.rc') } )
  307. import methods
  308. env.Append( BUILDERS = { 'GLSL120' : env.Builder(action = methods.build_legacygl_headers, suffix = 'glsl.h',src_suffix = '.glsl') } )
  309. env.Append( BUILDERS = { 'GLSL' : env.Builder(action = methods.build_glsl_headers, suffix = 'glsl.h',src_suffix = '.glsl') } )
  310. env.Append( BUILDERS = { 'HLSL9' : env.Builder(action = methods.build_hlsl_dx9_headers, suffix = 'hlsl.h',src_suffix = '.hlsl') } )
  311. env.Append( BUILDERS = { 'GLSL120GLES' : env.Builder(action = methods.build_gles2_headers, suffix = 'glsl.h',src_suffix = '.glsl') } )
  312. def detect_visual_c_compiler_version(tools_env):
  313. # tools_env is the variable scons uses to call tools that execute tasks, SCons's env['ENV'] that executes tasks...
  314. # (see the SCons documentation for more information on what it does)...
  315. # in order for this function to be well encapsulated i choose to force it to recieve SCons's TOOLS env (env['ENV']
  316. # and not scons setup environment (env)... so make sure you call the right environment on it or it will fail to detect
  317. # the propper vc version that will be called
  318. # These is no flag to give to visual c compilers to set the architecture, ie scons bits argument (32,64,ARM etc)
  319. # There are many different cl.exe files that are run, and each one compiles & links to a different architecture
  320. # As far as I know, the only way to figure out what compiler will be run when Scons calls cl.exe via Program()
  321. # is to check the PATH varaible and figure out which one will be called first. Code bellow does that and returns:
  322. # the following string values:
  323. # "" Compiler not detected
  324. # "amd64" Native 64 bit compiler
  325. # "amd64_x86" 64 bit Cross Compiler for 32 bit
  326. # "x86" Native 32 bit compiler
  327. # "x86_amd64" 32 bit Cross Compiler for 64 bit
  328. # There are other architectures, but Godot does not support them currently, so this function does not detect arm/amd64_arm
  329. # and similar architectures/compilers
  330. # Set chosen compiler to "not detected"
  331. vc_chosen_compiler_index = -1
  332. vc_chosen_compiler_str = ""
  333. # find() works with -1 so big ifs bellow are needed... the simplest solution, in fact
  334. # First test if amd64 and amd64_x86 compilers are present in the path
  335. vc_amd64_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"]+"BIN\\amd64;")
  336. if(vc_amd64_compiler_detection_index > -1):
  337. vc_chosen_compiler_index = vc_amd64_compiler_detection_index
  338. vc_chosen_compiler_str = "amd64"
  339. vc_amd64_x86_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"]+"BIN\\amd64_x86;")
  340. if(vc_amd64_x86_compiler_detection_index > -1
  341. and (vc_chosen_compiler_index == -1
  342. or vc_chosen_compiler_index > vc_amd64_x86_compiler_detection_index)):
  343. vc_chosen_compiler_index = vc_amd64_x86_compiler_detection_index
  344. vc_chosen_compiler_str = "amd64_x86"
  345. # Now check the 32 bit compilers
  346. vc_x86_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"]+"BIN;")
  347. if(vc_x86_compiler_detection_index > -1
  348. and (vc_chosen_compiler_index == -1
  349. or vc_chosen_compiler_index > vc_x86_compiler_detection_index)):
  350. vc_chosen_compiler_index = vc_x86_compiler_detection_index
  351. vc_chosen_compiler_str = "x86"
  352. vc_x86_amd64_compiler_detection_index = tools_env["PATH"].find(tools_env['VCINSTALLDIR']+"BIN\\x86_amd64;")
  353. if(vc_x86_amd64_compiler_detection_index > -1
  354. and (vc_chosen_compiler_index == -1
  355. or vc_chosen_compiler_index > vc_x86_amd64_compiler_detection_index)):
  356. vc_chosen_compiler_index = vc_x86_amd64_compiler_detection_index
  357. vc_chosen_compiler_str = "x86_amd64"
  358. # debug help
  359. #print vc_amd64_compiler_detection_index
  360. #print vc_amd64_x86_compiler_detection_index
  361. #print vc_x86_compiler_detection_index
  362. #print vc_x86_amd64_compiler_detection_index
  363. #print "chosen "+str(vc_chosen_compiler_index)+ " | "+str(vc_chosen_compiler_str)
  364. return vc_chosen_compiler_str