Skip to content

Commit

Permalink
Fix cmake.toml and cleanup amalgamate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cursey committed May 15, 2024
1 parent 8d47ae0 commit c8d2a14
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ if(SAFETYHOOK_AMALGAMATE) # amalgamate
add_custom_command(
OUTPUT ${AMALGAMATED_FILE} ${AMALGAMATED_HEADER}
DEPENDS ${HEADER_FILES} ${SOURCE_FILES} ${AMALGAMATE_SCRIPT}
COMMAND ${Python3_EXECUTABLE} ${AMALGAMATE_SCRIPT} ${AMALGAMATED_FILE} ${AMALGAMATED_HEADER}
COMMAND ${Python3_EXECUTABLE} ${AMALGAMATE_SCRIPT}
MAIN_DEPENDENCY ${AMALGAMATE_SCRIPT}
COMMENT "Amalgamating"
)
Expand Down
22 changes: 17 additions & 5 deletions amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Set
from glob import glob
from shutil import rmtree
from textwrap import dedent

import os
import re
Expand All @@ -21,8 +22,8 @@
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')
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
Expand Down Expand Up @@ -160,10 +161,21 @@ def merge_sources(*, source_dir: Path, covered_headers: Set[Path]):

return output


def do_polyfill(content):
return content.replace('#include <expected>', '#if __has_include("expected.hpp")\n#include <expected.hpp>\n#else\n#include <tl/expected.hpp>\n#endif\n') \
.replace('std::expected', 'tl::expected') \
.replace('std::unexpected', 'tl::unexpected')
return content.replace('#include <expected>',
dedent('''
#if __has_include("tl/expected.hpp")
#include "tl/expected.hpp"
#elif __has_include("expected.hpp")
#include "expected.hpp"
#else
#error "No <expected> polyfill found"
#endif
''')) \
.replace('std::expected', 'tl::expected') \
.replace('std::unexpected', 'tl::unexpected')


def main():
args = parser.parse_args()
Expand Down
2 changes: 1 addition & 1 deletion cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ set(AMALGAMATE_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/amalgamate.py)
add_custom_command(
OUTPUT ${AMALGAMATED_FILE} ${AMALGAMATED_HEADER}
DEPENDS ${HEADER_FILES} ${SOURCE_FILES} ${AMALGAMATE_SCRIPT}
COMMAND ${Python3_EXECUTABLE} ${AMALGAMATE_SCRIPT} ${AMALGAMATED_FILE} ${AMALGAMATED_HEADER}
COMMAND ${Python3_EXECUTABLE} ${AMALGAMATE_SCRIPT}
MAIN_DEPENDENCY ${AMALGAMATE_SCRIPT}
COMMENT "Amalgamating"
)
Expand Down

0 comments on commit c8d2a14

Please sign in to comment.