Skip to content

Commit

Permalink
Implement amalgamator
Browse files Browse the repository at this point in the history
  • Loading branch information
VioletXF committed Mar 12, 2024
1 parent 3ec067f commit 9edf5db
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
21 changes: 2 additions & 19 deletions .github/workflows/publish_amal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,10 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Cache cpp-amalgamate
uses: actions/cache@v2
with:
path: |
~/.cargo/bin/cpp-amalgamate
~/.cargo/.crates.toml
~/.cargo/.crates2.json
key: ${{ runner.os }}-cpp-amalgamate-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cpp-amalgamate-
- name: Install cpp-amalgamate if not cached
run: |
if ! command -v cpp-amalgamate &> /dev/null
then
cargo install cpp-amalgamate
fi
- name: cpp-amalgamate
- name: amalgamate
run: |
rm -f bms_parser.hpp
cpp-amalgamate ./src/** >> bms_parser.hpp
python3 scripts/amalgamate.py bms_parser.hpp src/**
# create new release
- name: Create Release
id: create_release
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ example/sqlite3.o: example/sqlite3.c
gcc -c -o 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:
python3 scripts/amalgamate.py $(BUILD_PATH)/bms_parser.hpp $(SRC_FILES)
test_amalgamation: amalgamate example/sqlite3.o $(BUILD_PATH)
@cp $(BUILD_PATH)/bms_parser.hpp example/
$(CC) $(CCFLAGS) -DWITH_AMALGAMATION=1 -o $(BUILD_PATH)/test_amalgamation example/main.cpp example/sqlite3.o
./$(BUILD_PATH)/test_amalgamation example/example.bme
clean:
@rm -rf $(OBJ_PATH) $(BUILD_PATH)
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bms_parser.hpp
5 changes: 4 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#if WITH_AMALGAMATION
#include "bms_parser.hpp"
#else
#include "../src/Parser.h"
#include "../src/Chart.h"
#endif
#include <atomic>
#include <iostream>
#include <string>
Expand Down
56 changes: 56 additions & 0 deletions scripts/amalgamate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# C++ amalgamator
# Copyright (C) 2024 VioletXF
# This script is used to generate a single C++ file from a set of C++ files.
# This implementation doesn't follow the standard C++ include resolution, it
# simply concatenates the files in the order they are given, to preserve the
# copyright notice.

import os
import re

def amalgamate(paths: [str], out: str):
visited = set()
content = "#pragma once\n"
files = []

def dfs(path):
nonlocal content
if path in visited:
return
visited.add(path)
with open(path, "r") as f:
for line in f:
if line.strip().startswith("#include"):
m = re.match(r'#include\s+"(.*)"', line)
if m:
include = m.group(1)
include_path = os.path.join(os.path.dirname(path), include)
dfs(include_path)
files.append(path)

for path in paths:
dfs(path)
for path in 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:
content += line

with open(out, "w") as f:
f.write(content)


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

0 comments on commit 9edf5db

Please sign in to comment.