diff --git a/Makefile b/Makefile index 32b3d573..9ba2c480 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ gh-pages: docs # Build amalgamation of all Wren library files. wren.c: src/include/wren.h src/vm/*.h src/vm/*.c - ./script/generate_amalgamation.py $^ > $@ + ./script/generate_amalgamation.py > $@ .DELETE_ON_ERROR: wren.c .PHONY: debug release all clean test builtin docs watchdocs gh-pages diff --git a/script/generate_amalgamation.py b/script/generate_amalgamation.py index 01faad6c..6f862e90 100755 --- a/script/generate_amalgamation.py +++ b/script/generate_amalgamation.py @@ -1,10 +1,12 @@ #!/usr/bin/env python import sys -from os.path import basename, dirname, join +from os.path import basename, dirname, join, realpath +from glob import iglob import re INCLUDE_PATTERN = re.compile(r'^\s*#include "([\w.]+)"') +GUARD_PATTERN = re.compile(r'^#ifndef wren(_\w+)?_h$') WREN_DIR = dirname(dirname(realpath(__file__))) seen_files = set() @@ -23,7 +25,7 @@ def add_file(filename): # Only include each file at most once. if bname in seen_files: return - seen_files.add(bname) + once = False path = dirname(filename) out.write('// Begin file "{0}"\n'.format(filename)) @@ -34,11 +36,21 @@ def add_file(filename): add_file(join(path, m.group(1))) else: out.write(line) + if GUARD_PATTERN.match(line): + once = True out.write('// End file "{0}"\n'.format(filename)) + # Only skip header files which use #ifndef guards. + # This is necessary because of the X Macro technique. + if once: + seen_files.add(bname) + # Print license on top. add_comment_file(join(WREN_DIR, 'LICENSE')) out.write('\n') # Source files. -for f in sys.argv[1:]: +add_file(join(WREN_DIR, 'src', 'include', 'wren.h')) +# Must be included here because of conditional compilation. +add_file(join(WREN_DIR, 'src', 'vm', 'wren_debug.h')) +for f in iglob(join(WREN_DIR, 'src', 'vm', '*.c')): add_file(f)