configure.py 3.8 KB

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