Skip to content

Commit

Permalink
Add ninja build tooling and Github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nosoop committed Jan 30, 2023
1 parent 2950567 commit d067ff3
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 1 deletion.
65 changes: 65 additions & 0 deletions .github/workflows/build-on-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Builds when a release is published.
name: Build Workflow

on:
release:
types: [published]

jobs:
run:
name: Build Release Package
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v1
with:
submodules: true

- name: Setup SourcePawn Compiler
uses: rumblefrog/setup-sp@v1.2.1
with:
version: "1.10.x"
no-spcomp-proxy: true

- name: Setup Ninja Build
uses: ashutoshvarma/setup-ninja@master
with:
version: 1.10.0

- name: Build Files
run: |
python3 configure.py
ninja
- name: Upload Include
id: upload-release-include
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./build/scripting/include/tf_econ_data.inc
asset_name: tf_econ_data.inc
asset_content_type: text/plain

- name: Upload Plugin
id: upload-release-plugin
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./build/plugins/tf_econ_data.smx
asset_name: tf_econ_data.smx
asset_content_type: application/octet-stream

- name: Upload Game Config
id: upload-release-gamedata
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./build/gamedata/tf2.econ_data.txt
asset_name: tf2.econ_data.txt
asset_content_type: text/plain
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Ignore compiled SourceMod plugins

*.smx
*.smx

# Ignore ninja build stuff
# (These should be handled with configure.py)
**/__pycache__
build/
build.ninja
128 changes: 128 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/python

# plugin names, relative to `scripting/`
plugins = [
"tf_econ_data.sp",
]

# files to copy to builddir, relative to root
# plugin names from previous list will be copied automatically
copy_files = [
"gamedata/tf2.econ_data.txt",
"scripting/include/tf_econ_data.inc",
"scripting/tf_econ_data/attached_particle_systems.sp",
"scripting/tf_econ_data/attribute_definition.sp",
"scripting/tf_econ_data/equip_regions.sp",
"scripting/tf_econ_data/item_definition.sp",
"scripting/tf_econ_data/keyvalues.sp",
"scripting/tf_econ_data/loadout_slot.sp",
"scripting/tf_econ_data/paintkit_definition.sp",
"scripting/tf_econ_data/quality_definition.sp",
"scripting/tf_econ_data/rarity_definition.sp",
]

# additional directories for sourcepawn include lookup
include_dirs = [
'${root}/scripting/include'
]

# required version of spcomp (presumably pinned to SM version)
spcomp_min_version = (1, 9)

########################
# build.ninja script generation below.

import contextlib
import misc.ninja_syntax as ninja_syntax
import misc.spcomp_util
import os
import sys
import argparse
import platform
import shutil

parser = argparse.ArgumentParser('Configures the project.')
parser.add_argument('--spcomp-dir',
help = 'Directory with the SourcePawn compiler. Will check PATH if not specified.')

args = parser.parse_args()

print("""Checking for SourcePawn compiler...""")
spcomp = shutil.which('spcomp', path = args.spcomp_dir)
if 'x86_64' in platform.machine():
# Use 64-bit spcomp if architecture supports it
spcomp = shutil.which('spcomp64', path = args.spcomp_dir) or spcomp
if not spcomp:
raise FileNotFoundError('Could not find SourcePawn compiler.')

available_version = misc.spcomp_util.extract_version(spcomp)
version_string = '.'.join(map(str, available_version))
print('Found SourcePawn compiler version', version_string, 'at', os.path.abspath(spcomp))

if spcomp_min_version > available_version:
raise ValueError("Failed to meet required compiler version "
+ '.'.join(map(str, spcomp_min_version)))

with contextlib.closing(ninja_syntax.Writer(open('build.ninja', 'wt'))) as build:
build.comment('This file is used to build SourceMod plugins with ninja.')
build.comment('The file is automatically generated by configure.py')
build.newline()

vars = {
'configure_args': sys.argv[1:],
'root': '.',
'builddir': 'build',
'spcomp': spcomp,
'spcflags': [ '-h', '-v0' ]
}

vars['spcflags'] += ('-i{}'.format(d) for d in include_dirs)

for key, value in vars.items():
build.variable(key, value)
build.newline()

build.comment("""Regenerate build files if build script changes.""")
build.rule('configure',
command = sys.executable + ' ${root}/configure.py ${configure_args}',
description = 'Reconfiguring build', generator = 1)

build.build('build.ninja', 'configure',
implicit = [ '${root}/configure.py', '${root}/misc/ninja_syntax.py' ])
build.newline()

build.rule('spcomp', deps = 'msvc',
command = '${spcomp} ${in} ${spcflags} -o ${out}',
description = 'Compiling ${out}')
build.newline()

# Platform-specific copy instructions
if platform.system() == "Windows":
build.rule('copy', command = 'cmd /c copy ${in} ${out} > NUL',
description = 'Copying ${out}')
elif platform.system() == "Linux":
build.rule('copy', command = 'cp ${in} ${out}', description = 'Copying ${out}')
build.newline()

build.comment("""Compile plugins specified in `plugins` list""")
for plugin in plugins:
smx_plugin = os.path.splitext(plugin)[0] + '.smx'

sp_file = os.path.normpath(os.path.join('$root', 'scripting', plugin))

smx_file = os.path.normpath(os.path.join('$builddir', 'plugins', smx_plugin))
build.build(smx_file, 'spcomp', sp_file)
build.newline()

build.comment("""Copy plugin sources to build output""")
for plugin in plugins:
sp_file = os.path.normpath(os.path.join('$root', 'scripting', plugin))

dist_sp = os.path.normpath(os.path.join('$builddir', 'scripting', plugin))
build.build(dist_sp, 'copy', sp_file)
build.newline()

build.comment("""Copy other files from source tree""")
for filepath in copy_files:
build.build(os.path.normpath(os.path.join('$builddir', filepath)), 'copy',
os.path.normpath(os.path.join('$root', filepath)))
183 changes: 183 additions & 0 deletions misc/ninja_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#!/usr/bin/python

"""Python module for generating .ninja files.
Note that this is emphatically not a required piece of Ninja; it's
just a helpful utility for build-file-generation systems that already
use Python.
"""

import re
import textwrap

def escape_path(word):
return word.replace('$ ', '$$ ').replace(' ', '$ ').replace(':', '$:')

class Writer(object):
def __init__(self, output, width=78):
self.output = output
self.width = width

def newline(self):
self.output.write('\n')

def comment(self, text):
for line in textwrap.wrap(text, self.width - 2, break_long_words=False,
break_on_hyphens=False):
self.output.write('# ' + line + '\n')

def variable(self, key, value, indent=0):
if value is None:
return
if isinstance(value, list):
value = ' '.join(filter(None, value)) # Filter out empty strings.
self._line('%s = %s' % (key, value), indent)

def pool(self, name, depth):
self._line('pool %s' % name)
self.variable('depth', depth, indent=1)

def rule(self, name, command, description=None, depfile=None,
generator=False, pool=None, restat=False, rspfile=None,
rspfile_content=None, deps=None):
self._line('rule %s' % name)
self.variable('command', command, indent=1)
if description:
self.variable('description', description, indent=1)
if depfile:
self.variable('depfile', depfile, indent=1)
if generator:
self.variable('generator', '1', indent=1)
if pool:
self.variable('pool', pool, indent=1)
if restat:
self.variable('restat', '1', indent=1)
if rspfile:
self.variable('rspfile', rspfile, indent=1)
if rspfile_content:
self.variable('rspfile_content', rspfile_content, indent=1)
if deps:
self.variable('deps', deps, indent=1)

def build(self, outputs, rule, inputs=None, implicit=None, order_only=None,
variables=None, implicit_outputs=None, pool=None):
outputs = as_list(outputs)
out_outputs = [escape_path(x) for x in outputs]
all_inputs = [escape_path(x) for x in as_list(inputs)]

if implicit:
implicit = [escape_path(x) for x in as_list(implicit)]
all_inputs.append('|')
all_inputs.extend(implicit)
if order_only:
order_only = [escape_path(x) for x in as_list(order_only)]
all_inputs.append('||')
all_inputs.extend(order_only)
if implicit_outputs:
implicit_outputs = [escape_path(x)
for x in as_list(implicit_outputs)]
out_outputs.append('|')
out_outputs.extend(implicit_outputs)

self._line('build %s: %s' % (' '.join(out_outputs),
' '.join([rule] + all_inputs)))
if pool is not None:
self._line(' pool = %s' % pool)

if variables:
if isinstance(variables, dict):
iterator = iter(variables.items())
else:
iterator = iter(variables)

for key, val in iterator:
self.variable(key, val, indent=1)

return outputs

def include(self, path):
self._line('include %s' % path)

def subninja(self, path):
self._line('subninja %s' % path)

def default(self, paths):
self._line('default %s' % ' '.join(as_list(paths)))

def _count_dollars_before_index(self, s, i):
"""Returns the number of '$' characters right in front of s[i]."""
dollar_count = 0
dollar_index = i - 1
while dollar_index > 0 and s[dollar_index] == '$':
dollar_count += 1
dollar_index -= 1
return dollar_count

def _line(self, text, indent=0):
"""Write 'text' word-wrapped at self.width characters."""
leading_space = ' ' * indent
while len(leading_space) + len(text) > self.width:
# The text is too wide; wrap if possible.

# Find the rightmost space that would obey our width constraint and
# that's not an escaped space.
available_space = self.width - len(leading_space) - len(' $')
space = available_space
while True:
space = text.rfind(' ', 0, space)
if (space < 0 or
self._count_dollars_before_index(text, space) % 2 == 0):
break

if space < 0:
# No such space; just use the first unescaped space we can find.
space = available_space - 1
while True:
space = text.find(' ', space + 1)
if (space < 0 or
self._count_dollars_before_index(text, space) % 2 == 0):
break
if space < 0:
# Give up on breaking.
break

self.output.write(leading_space + text[0:space] + ' $\n')
text = text[space+1:]

# Subsequent lines are continuations, so indent them.
leading_space = ' ' * (indent+2)

self.output.write(leading_space + text + '\n')

def close(self):
self.output.close()


def as_list(input):
if input is None:
return []
if isinstance(input, list):
return input
return [input]


def escape(string):
"""Escape a string such that it can be embedded into a Ninja file without
further interpretation."""
assert '\n' not in string, 'Ninja syntax does not allow newlines'
# We only have one special metacharacter: '$'.
return string.replace('$', '$$')


def expand(string, vars, local_vars={}):
"""Expand a string containing $vars as Ninja would.
Note: doesn't handle the full Ninja variable syntax, but it's enough
to make configure.py's use of it work.
"""
def exp(m):
var = m.group(1)
if var == '$':
return '$'
return local_vars.get(var, vars.get(var, ''))
return re.sub(r'\$(\$|\w*)', exp, string)
Loading

0 comments on commit d067ff3

Please sign in to comment.