From a11e1638312453b164975156092a5d91b236df8f Mon Sep 17 00:00:00 2001 From: bottiger1 <55270538+bottiger1@users.noreply.github.com> Date: Mon, 13 May 2024 01:33:52 -0700 Subject: [PATCH 1/2] Add --polyfill option to amalgamate to modify source to use std::expected polyfill so it can be compiled on C++20. https://raw.githubusercontent.com/TartanLlama/expected/master/include/tl/expected.hpp --- amalgamate.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index 2f2b51c..7dad719 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -7,6 +7,8 @@ import os import re +import sys +import argparse SAFETYHOOK_ROOT = Path(__file__).resolve().parent PUBLIC_INCLUDE_PATHS = [ @@ -18,6 +20,10 @@ OUTPUT_DIR = SAFETYHOOK_ROOT / 'amalgamated-dist' FILE_HEADER = ['// DO NOT EDIT. This file is auto-generated by `amalgamate.py`.', ''] +parser = argparse.ArgumentParser(description='bundles cpp and hpp files together') +parser.add_argument('--polyfill', action='store_true', + help='replace std::except with a polyfill so it can be compiled on C++20 or older. https://raw.githubusercontent.com/TartanLlama/expected/master/include/tl/expected.hpp') + # Python versions before 3.10 don't have the root_dir argument for glob, so we # crudely emulate it here. @@ -154,8 +160,15 @@ def merge_sources(*, source_dir: Path, covered_headers: Set[Path]): return output +def do_polyfill(content): + return content.replace('', '') \ + .replace('std::expected', 'tl::expected') \ + .replace('std::unexpected', 'tl::unexpected') def main(): + args = parser.parse_args() + polyfill = args.polyfill is True + if OUTPUT_DIR.exists(): print('Output directory exists. Deleting.') rmtree(OUTPUT_DIR) @@ -164,20 +177,26 @@ def main(): covered_headers = set() with open(OUTPUT_DIR / 'safetyhook.hpp', 'w') as f: - f.write('\n'.join(FILE_HEADER + merge_headers( + content = '\n'.join(FILE_HEADER + merge_headers( header='safetyhook.hpp', search_paths=PUBLIC_INCLUDE_PATHS, covered_headers=covered_headers, stack=[], - ))) + )) + if polyfill: + content = do_polyfill(content) + f.write(content) print(covered_headers) with open(OUTPUT_DIR / 'safetyhook.cpp', 'w') as f: - f.write('\n'.join(FILE_HEADER + merge_sources( + content = '\n'.join(FILE_HEADER + merge_sources( source_dir=SAFETYHOOK_ROOT / 'src', covered_headers=covered_headers, - ))) + )) + if polyfill: + content = do_polyfill(content) + f.write(content) if __name__ == '__main__': From 17be414633036b063063b1c761ca574886d64b6f Mon Sep 17 00:00:00 2001 From: bottiger1 <55270538+bottiger1@users.noreply.github.com> Date: Mon, 13 May 2024 17:46:10 -0700 Subject: [PATCH 2/2] allow for tl/expected.hpp --- amalgamate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amalgamate.py b/amalgamate.py index 7dad719..710744b 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -161,7 +161,7 @@ def merge_sources(*, source_dir: Path, covered_headers: Set[Path]): return output def do_polyfill(content): - return content.replace('', '') \ + return content.replace('#include ', '#if __has_include("expected.hpp")\n#include \n#else\n#include \n#endif\n') \ .replace('std::expected', 'tl::expected') \ .replace('std::unexpected', 'tl::unexpected')