SConstruct 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!python
  2. import os, subprocess, platform, sys
  3. def add_sources(sources, dir, extension):
  4. for f in os.listdir(dir):
  5. if f.endswith('.' + extension):
  6. sources.append(dir + '/' + f)
  7. # Try to detect the host platform automatically
  8. # This is used if no `platform` argument is passed
  9. if sys.platform.startswith('linux'):
  10. host_platform = 'linux'
  11. elif sys.platform == 'darwin':
  12. host_platform = 'osx'
  13. elif sys.platform == 'win32':
  14. host_platform = 'windows'
  15. else:
  16. raise ValueError('Could not detect platform automatically, please specify with platform=<platform>')
  17. opts = Variables([], ARGUMENTS)
  18. opts.Add(EnumVariable('platform', 'Target platform', host_platform,
  19. allowed_values=('linux', 'osx', 'windows'),
  20. ignorecase=2))
  21. opts.Add(EnumVariable('bits', 'Target platform bits', 'default', ('default', '32', '64')))
  22. opts.Add(BoolVariable('use_llvm', 'Use the LLVM compiler - only effective when targeting Linux', False))
  23. opts.Add(BoolVariable('use_mingw', 'Use the MinGW compiler - only effective on Windows', False))
  24. # Must be the same setting as used for cpp_bindings
  25. opts.Add(EnumVariable('target', 'Compilation target', 'debug',
  26. allowed_values=('debug', 'release'),
  27. ignorecase=2))
  28. opts.Add(PathVariable('headers_dir', 'Path to the directory containing Godot headers', 'godot_headers', PathVariable.PathIsDir))
  29. opts.Add(BoolVariable('use_custom_api_file', 'Use a custom JSON API file', False))
  30. opts.Add(PathVariable('custom_api_file', 'Path to the custom JSON API file', None, PathVariable.PathIsFile))
  31. opts.Add(BoolVariable('generate_bindings', 'Generate GDNative API bindings', False))
  32. unknown = opts.UnknownVariables()
  33. if unknown:
  34. print("Unknown variables:" + unknown.keys())
  35. Exit(1)
  36. env = Environment()
  37. opts.Update(env)
  38. Help(opts.GenerateHelpText(env))
  39. # This makes sure to keep the session environment variables on Windows
  40. # This way, you can run SCons in a Visual Studio 2017 prompt and it will find all the required tools
  41. if env['platform'] == 'windows':
  42. if env['bits'] == '64':
  43. env = Environment(TARGET_ARCH='amd64')
  44. elif env['bits'] == '32':
  45. env = Environment(TARGET_ARCH='x86')
  46. else:
  47. print("Warning: bits argument not specified, target arch is=" + env['TARGET_ARCH'])
  48. opts.Update(env)
  49. is64 = False
  50. if (env['platform'] == 'osx' or env['TARGET_ARCH'] == 'amd64' or env['TARGET_ARCH'] == 'emt64' or env['TARGET_ARCH'] == 'x86_64'):
  51. is64 = True
  52. if env['bits'] == 'default':
  53. env['bits'] = '64' if is64 else '32'
  54. if env['platform'] == 'linux':
  55. if env['use_llvm']:
  56. env['CXX'] = 'clang++'
  57. env.Append(CCFLAGS=['-fPIC', '-g', '-std=c++14', '-Wwrite-strings'])
  58. env.Append(LINKFLAGS=["-Wl,-R,'$$ORIGIN'"])
  59. if env['target'] == 'debug':
  60. env.Append(CCFLAGS=['-Og'])
  61. elif env['target'] == 'release':
  62. env.Append(CCFLAGS=['-O3'])
  63. if env['bits'] == '64':
  64. env.Append(CCFLAGS=['-m64'])
  65. env.Append(LINKFLAGS=['-m64'])
  66. elif env['bits'] == '32':
  67. env.Append(CCFLAGS=['-m32'])
  68. env.Append(LINKFLAGS=['-m32'])
  69. elif env['platform'] == 'osx':
  70. if env['bits'] == '32':
  71. raise ValueError('Only 64-bit builds are supported for the macOS target.')
  72. env.Append(CCFLAGS=['-g', '-std=c++14', '-arch', 'x86_64'])
  73. env.Append(LINKFLAGS=['-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup'])
  74. if env['target'] == 'debug':
  75. env.Append(CCFLAGS=['-Og'])
  76. elif env['target'] == 'release':
  77. env.Append(CCFLAGS=['-O3'])
  78. elif env['platform'] == 'windows':
  79. if host_platform == 'windows' and not env['use_mingw']:
  80. # MSVC
  81. env.Append(LINKFLAGS=['/WX'])
  82. if env['target'] == 'debug':
  83. env.Append(CCFLAGS=['/EHsc', '/D_DEBUG', '/MDd'])
  84. elif env['target'] == 'release':
  85. env.Append(CCFLAGS=['/O2', '/EHsc', '/DNDEBUG', '/MD'])
  86. else:
  87. # MinGW
  88. if env['bits'] == '64':
  89. env['CXX'] = 'x86_64-w64-mingw32-g++'
  90. elif env['bits'] == '32':
  91. env['CXX'] = 'i686-w64-mingw32-g++'
  92. env.Append(CCFLAGS=['-g', '-O3', '-std=c++14', '-Wwrite-strings'])
  93. env.Append(LINKFLAGS=['--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++'])
  94. env.Append(CPPPATH=['.', env['headers_dir'], 'include', 'include/gen', 'include/core'])
  95. # Generate bindings?
  96. json_api_file = ''
  97. if env['use_custom_api_file']:
  98. json_api_file = env['custom_api_file']
  99. else:
  100. json_api_file = os.path.join(os.getcwd(), 'godot_headers', 'api.json')
  101. if env['generate_bindings']:
  102. # Actually create the bindings here
  103. import binding_generator
  104. binding_generator.generate_bindings(json_api_file)
  105. # source to compile
  106. sources = []
  107. add_sources(sources, 'src/core', 'cpp')
  108. add_sources(sources, 'src/gen', 'cpp')
  109. library = env.StaticLibrary(
  110. target='bin/' + 'libgodot-cpp.{}.{}.{}'.format(env['platform'], env['target'], env['bits']), source=sources
  111. )
  112. Default(library)