| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import datetime |
| 6 |
|
| 7 |
from scriptCommon import catchPath |
| 8 |
from releaseCommon import Version |
| 9 |
|
| 10 |
root_path = os.path.join(catchPath, 'src') |
| 11 |
starting_header = os.path.join(root_path, 'catch2', 'catch_all.hpp') |
| 12 |
output_header = os.path.join(catchPath, 'extras', 'catch_amalgamated.hpp') |
| 13 |
output_cpp = os.path.join(catchPath, 'extras', 'catch_amalgamated.cpp') |
| 14 |
|
| 15 |
|
| 16 |
copyright_lines = [ |
| 17 |
'// Copyright Catch2 Authors\n', |
| 18 |
'// Distributed under the Boost Software License, Version 1.0.\n', |
| 19 |
'// (See accompanying file LICENSE.txt or copy at\n', |
| 20 |
'// https://www.boost.org/LICENSE_1_0.txt)\n', |
| 21 |
'// SPDX-License-Identifier: BSL-1.0\n', |
| 22 |
] |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
file_header = '''\ |
| 27 |
// Copyright Catch2 Authors |
| 28 |
// Distributed under the Boost Software License, Version 1.0. |
| 29 |
// (See accompanying file LICENSE.txt or copy at |
| 30 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 31 |
|
| 32 |
// SPDX-License-Identifier: BSL-1.0 |
| 33 |
|
| 34 |
// Catch v{version_string} |
| 35 |
// Generated: {generation_time} |
| 36 |
// ---------------------------------------------------------- |
| 37 |
// This file is an amalgamation of multiple different files. |
| 38 |
// You probably shouldn't edit it directly. |
| 39 |
// ---------------------------------------------------------- |
| 40 |
''' |
| 41 |
|
| 42 |
|
| 43 |
def formatted_file_header(version): |
| 44 |
return file_header.format(version_string=version.getVersionString(), |
| 45 |
generation_time=datetime.datetime.now()) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
concatenated_headers = set() |
| 50 |
|
| 51 |
internal_include_parser = re.compile(r'\s*#include <(catch2/.*)>.*') |
| 52 |
|
| 53 |
def concatenate_file(out, filename: str, expand_headers: bool) -> int: |
| 54 |
|
| 55 |
concatenated = 1 |
| 56 |
with open(filename, mode='r', encoding='utf-8') as input: |
| 57 |
for line in input: |
| 58 |
if line in copyright_lines: |
| 59 |
continue |
| 60 |
m = internal_include_parser.match(line) |
| 61 |
|
| 62 |
|
| 63 |
if not m: |
| 64 |
out.write(line) |
| 65 |
continue |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if not expand_headers: |
| 74 |
continue |
| 75 |
|
| 76 |
next_header = m.group(1) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
if next_header in concatenated_headers: |
| 81 |
continue |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if next_header == 'catch2/catch_user_config.hpp': |
| 88 |
concatenated_headers.add(next_header) |
| 89 |
continue |
| 90 |
|
| 91 |
concatenated_headers.add(next_header) |
| 92 |
concatenated += concatenate_file(out, os.path.join(root_path, next_header), expand_headers) |
| 93 |
|
| 94 |
return concatenated |
| 95 |
|
| 96 |
|
| 97 |
def generate_header(): |
| 98 |
with open(output_header, mode='w', encoding='utf-8') as header: |
| 99 |
header.write(formatted_file_header(Version())) |
| 100 |
header.write('#ifndef CATCH_AMALGAMATED_HPP_INCLUDED\n') |
| 101 |
header.write('#define CATCH_AMALGAMATED_HPP_INCLUDED\n') |
| 102 |
print('Concatenated {} headers'.format(concatenate_file(header, starting_header, True))) |
| 103 |
header.write('#endif // CATCH_AMALGAMATED_HPP_INCLUDED\n') |
| 104 |
|
| 105 |
def generate_cpp(): |
| 106 |
from glob import glob |
| 107 |
cpp_files = sorted(glob(os.path.join(root_path, 'catch2', '**/*.cpp'), recursive=True)) |
| 108 |
with open(output_cpp, mode='w', encoding='utf-8') as cpp: |
| 109 |
cpp.write(formatted_file_header(Version())) |
| 110 |
cpp.write('\n#include "catch_amalgamated.hpp"\n') |
| 111 |
concatenate_file(cpp, os.path.join(root_path, 'catch2/internal/catch_windows_h_proxy.hpp'), False) |
| 112 |
for file in cpp_files: |
| 113 |
concatenate_file(cpp, file, False) |
| 114 |
print('Concatenated {} cpp files'.format(len(cpp_files))) |
| 115 |
|
| 116 |
if __name__ == "__main__": |
| 117 |
generate_header() |
| 118 |
generate_cpp() |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|