Skip to content

Commit

Permalink
Fix amalgamation
Browse files Browse the repository at this point in the history
  • Loading branch information
VioletXF committed May 1, 2024
1 parent 7f587c2 commit 1b53bdb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/publish_amal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
fetch-depth: 0
- name: amalgamate
run: |
rm -f bms_parser.hpp
python3 scripts/amalgamate.py bms_parser.hpp src/**
rm -f bms_parser.hpp bms_parser.cpp
python3 scripts/amalgamate.py bms_parser.hpp bms_parser.cpp src/**
# create new release
- name: Create Release
id: create_release
Expand All @@ -27,7 +27,7 @@ jobs:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
You can find the amalgamated code in the file `bms_parser.hpp`.
You can find the amalgamated code in the file `bms_parser.hpp` and `bms_parser.cpp`.
draft: false
prerelease: false
# upload amalgamated code
Expand All @@ -40,4 +40,12 @@ jobs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./bms_parser.hpp
asset_name: bms_parser.hpp
asset_content_type: text/plain
asset_content_type: text/plain
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./bms_parser.cpp
asset_name: bms_parser.cpp
asset_content_type: text/plain

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ example/sqlite3.o: example/sqlite3.c
example: all example/sqlite3.o $(BUILD_PATH)
$(CC) $(CCFLAGS) -o $(BUILD_PATH)/main example/main.cpp example/sqlite3.o $(OBJ_FILES)
amalgamate: $(BUILD_PATH)
python3 scripts/amalgamate.py $(BUILD_PATH)/bms_parser.hpp $(SRC_FILES)
python3 scripts/amalgamate.py $(BUILD_PATH)/bms_parser.hpp $(BUILD_PATH)/bms_parser.cpp $(SRC_FILES)
test_amalgamation: amalgamate $(BUILD_PATH)
$(CP) $(BUILD_PATH)$(SLASH)bms_parser.hpp test
$(CC) $(CCFLAGS) -DWITH_AMALGAMATION=1 -o test/test_amalgamation test/main.cpp
Expand Down
46 changes: 28 additions & 18 deletions scripts/amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import os
import re

def amalgamate(paths: [str], out: str):
def amalgamate(paths: [str], out_header: str, out_source: str):
visited = set()
content = "#pragma once\n"
files = []
header_content = "#pragma once\n"
source_content = f'#include "{os.path.basename(out_header)}"\n'
header_files = []

def dfs(path):
nonlocal content
def dfs(path, is_source=False):
if path in visited:
return
visited.add(path)
Expand All @@ -26,31 +26,41 @@ def dfs(path):
include = m.group(1)
include_path = os.path.join(os.path.dirname(path), include)
dfs(include_path)
files.append(path)
if not is_source:
header_files.append(path)

for path in paths:
dfs(path)
for path in files:
dfs(path, is_source=True)
with open(path, "r") as f:
for line in f:
if line.strip().startswith("#pragma once"):
continue
m = re.match(r'#include\s+<(.*)>', line)
if not line.strip().startswith("#include") or m:
content += line
source_content += line
for path in header_files:
with open(path, "r") as f:
for line in f:
if line.strip().startswith("#pragma once"):
continue
m = re.match(r'#include\s+<(.*)>', line)
if not line.strip().startswith("#include") or m:
header_content += line

with open(out, "w") as f:
f.write(content)
with open(out_header, "w") as f:
f.write(header_content)
with open(out_source, "w") as f:
f.write(source_content)


if __name__ == "__main__":
# glob to get all files
import sys
if len(sys.argv) < 3:
print("Usage: python amalgamate.py <out> <in>...")
if len(sys.argv) < 4:
print("Usage: python amalgamate.py <out_header> <out_source> <in>...")
sys.exit(1)
out = sys.argv[1]
ins = sys.argv[2:]
print("Amalgamating", ins, "into", out)
amalgamate(ins, out)

out_header = sys.argv[1]
out_source = sys.argv[2]
ins = sys.argv[3:]
print("Amalgamating", ins, "into", out_header, out_source)
amalgamate(ins, out_header, out_source)

0 comments on commit 1b53bdb

Please sign in to comment.