configure.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python
  2. # plugin names, relative to `scripting/`
  3. plugins = [
  4. 'auto_steam_update.sp',
  5. ]
  6. # files to copy to builddir, relative to root
  7. # plugin names from previous list will be copied automatically
  8. copy_files = [ ]
  9. # additional directories for sourcepawn include lookup
  10. # `scripting/include` is explicitly included
  11. include_dirs = [
  12. 'third_party/vendored',
  13. ]
  14. # required version of spcomp (presumably pinned to SM version)
  15. spcomp_min_version = (1, 10)
  16. ########################
  17. # build.ninja script generation below.
  18. import contextlib
  19. import misc.ninja_syntax as ninja_syntax
  20. import misc.spcomp_util
  21. import os
  22. import sys
  23. import argparse
  24. import platform
  25. import shutil
  26. parser = argparse.ArgumentParser('Configures the project.')
  27. parser.add_argument('--spcomp-dir',
  28. help = 'Directory with the SourcePawn compiler. Will check PATH if not specified.')
  29. args = parser.parse_args()
  30. print("""Checking for SourcePawn compiler...""")
  31. spcomp = shutil.which('spcomp', path = args.spcomp_dir)
  32. if 'x86_64' in platform.machine():
  33. # Use 64-bit spcomp if architecture supports it
  34. spcomp = shutil.which('spcomp64', path = args.spcomp_dir) or spcomp
  35. if not spcomp:
  36. raise FileNotFoundError('Could not find SourcePawn compiler.')
  37. available_version = misc.spcomp_util.extract_version(spcomp)
  38. version_string = '.'.join(map(str, available_version))
  39. print('Found SourcePawn compiler version', version_string, 'at', os.path.abspath(spcomp))
  40. if spcomp_min_version > available_version:
  41. raise ValueError("Failed to meet required compiler version "
  42. + '.'.join(map(str, spcomp_min_version)))
  43. with contextlib.closing(ninja_syntax.Writer(open('build.ninja', 'wt'))) as build:
  44. build.comment('This file is used to build SourceMod plugins with ninja.')
  45. build.comment('The file is automatically generated by configure.py')
  46. build.newline()
  47. vars = {
  48. 'configure_args': sys.argv[1:],
  49. 'root': '.',
  50. 'builddir': 'build',
  51. 'spcomp': spcomp,
  52. 'spcflags': [ '-i${root}/scripting/include', '-h', '-v0' ]
  53. }
  54. vars['spcflags'] += ('-i{}'.format(d) for d in include_dirs)
  55. for key, value in vars.items():
  56. build.variable(key, value)
  57. build.newline()
  58. build.comment("""Regenerate build files if build script changes.""")
  59. build.rule('configure',
  60. command = sys.executable + ' ${root}/configure.py ${configure_args}',
  61. description = 'Reconfiguring build', generator = 1)
  62. build.build('build.ninja', 'configure',
  63. implicit = [ '${root}/configure.py', '${root}/misc/ninja_syntax.py' ])
  64. build.newline()
  65. build.rule('spcomp', deps = 'msvc',
  66. command = '${spcomp} ${in} ${spcflags} -o ${out}',
  67. description = 'Compiling ${out}')
  68. build.newline()
  69. # Platform-specific copy instructions
  70. if platform.system() == "Windows":
  71. build.rule('copy', command = 'cmd /c copy ${in} ${out} > NUL',
  72. description = 'Copying ${out}')
  73. elif platform.system() == "Linux":
  74. build.rule('copy', command = 'cp ${in} ${out}', description = 'Copying ${out}')
  75. build.newline()
  76. build.comment("""Compile plugins specified in `plugins` list""")
  77. for plugin in plugins:
  78. smx_plugin = os.path.splitext(plugin)[0] + '.smx'
  79. sp_file = os.path.normpath(os.path.join('$root', 'scripting', plugin))
  80. smx_file = os.path.normpath(os.path.join('$builddir', 'plugins', smx_plugin))
  81. build.build(smx_file, 'spcomp', sp_file)
  82. build.newline()
  83. build.comment("""Copy plugin sources to build output""")
  84. for plugin in plugins:
  85. sp_file = os.path.normpath(os.path.join('$root', 'scripting', plugin))
  86. dist_sp = os.path.normpath(os.path.join('$builddir', 'scripting', plugin))
  87. build.build(dist_sp, 'copy', sp_file)
  88. build.newline()
  89. build.comment("""Copy other files from source tree""")
  90. for filepath in copy_files:
  91. build.build(os.path.normpath(os.path.join('$builddir', filepath)), 'copy',
  92. os.path.normpath(os.path.join('$root', filepath)))