configure.py 3.9 KB

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