| 1 | import glob |
| 2 | import hashlib |
| 3 | import json |
| 4 | import os |
| 5 | import re |
| 6 | import sys |
| 7 | |
| 8 | condition_mapping = { |
| 9 | 'test $EXTENDED_SPARSE -ne 1': 'MATIO_EXTENDED_SPARSE', |
| 10 | 'test $COMPRESSION_ZLIB -ne 1': 'HAVE_ZLIB', |
| 11 | 'test $MAT73 -ne 1': 'MAT73 AND HAVE_HDF5', |
| 12 | 'test $HAVE_INT64 -eq 0': 'HAVE_INT64', |
| 13 | 'test $HAVE_UINT64 -eq 0': 'HAVE_UINT64', |
| 14 | '! grep -q "#define SIZEOF_VOID_P 4" $abs_top_builddir/src/matioConfig.h': 'CMAKE_SIZEOF_VOID_P EQUAL 4', |
| 15 | '! grep -q "#define SIZEOF_VOID_P 8" $abs_top_builddir/src/matioConfig.h': 'CMAKE_SIZEOF_VOID_P EQUAL 8', |
| 16 | } |
| 17 | |
| 18 | command_mapping = { |
| 19 | '$srcdir/': '${PROJECT_SOURCE_DIR}/test/', |
| 20 | '$builddir/test_mat': '$<TARGET_FILE:test_mat>', |
| 21 | '$builddir/../tools/matdump': '$<TARGET_FILE:matdump>', |
| 22 | } |
| 23 | |
| 24 | |
| 25 | def convert_autotest_to_ctest(autotest_file, cmake_output_file): |
| 26 | base_name = os.path.splitext(os.path.basename(autotest_file))[0] |
| 27 | |
| 28 | with open(autotest_file, 'r') as atf, open(cmake_output_file, 'w') as cmakef: |
| 29 | cmakef.write('# Auto-generated CMake/CTest tests\n') |
| 30 | |
| 31 | setup_re = re.compile(r'AT_SETUP\(\[([^\]]+)\]\)') |
| 32 | skip_if_re = re.compile(r'AT_SKIP_IF\(\[([^\]]+)\]\)') |
| 33 | check_re = re.compile(r'AT_CHECK\(\[([^\]]+)\]') |
| 34 | check_copy_re = re.compile(r'AT_CHECK\(\[cp (.*?) expout') |
| 35 | check_diff_re = re.compile(r'\s*(.*?)\],\[0\],\[expout\],\[\]\)') |
| 36 | cleanup_re = re.compile(r'AT_CLEANUP') |
| 37 | keyword_re = re.compile(r'AT_KEYWORDS\(\[([^\]]+)\]\)') |
| 38 | |
| 39 | skip_conditions = [] |
| 40 | test_keywords = [] |
| 41 | counter = 1 |
| 42 | check_copy_match = None |
| 43 | check_diff_match = None |
| 44 | |
| 45 | for line in atf: |
| 46 | setup_match = setup_re.search(line) |
| 47 | skip_if_match = skip_if_re.search(line) |
| 48 | check_match = check_re.search(line) |
| 49 | if check_copy_match is None: |
| 50 | check_copy_match = check_copy_re.search(line) |
| 51 | if check_diff_match is None: |
| 52 | check_diff_match = check_diff_re.search(line) |
| 53 | cleanup_match = cleanup_re.search(line) |
| 54 | keyword_match = keyword_re.search(line) |
| 55 | |
| 56 | if setup_match: |
| 57 | test_name = f'{base_name}_{re.sub(r"[ ()]", "_", setup_match.group(1))}'.lower() |
| 58 | cmakef.write(f'\n# {setup_match.group(1)}\n') |
| 59 | skip_conditions = ['True'] |
| 60 | test_keywords = [base_name] |
| 61 | test_keywords.extend(base_name.split('_')) |
| 62 | counter = 1 |
| 63 | check_copy_match = None |
| 64 | check_diff_match = None |
| 65 | mat_file_name = None |
| 66 | |
| 67 | elif keyword_match: |
| 68 | keywords = keyword_match.group(1).strip().split() |
| 69 | test_keywords.extend(keywords) |
| 70 | |
| 71 | elif skip_if_match: |
| 72 | condition = skip_if_match.group(1).strip() |
| 73 | |
| 74 | if condition in condition_mapping: |
| 75 | mapped_condition = condition_mapping[condition] |
| 76 | skip_conditions.append(mapped_condition) |
| 77 | else: |
| 78 | skip_conditions.append(condition) |
| 79 | |
| 80 | elif check_match: |
| 81 | command = check_match.group(1) |
| 82 | for original, replacement in command_mapping.items(): |
| 83 | command = command.replace(original, replacement) |
| 84 | |
| 85 | if skip_conditions: |
| 86 | if len(skip_conditions) > 1: |
| 87 | skip_conditions = skip_conditions[1:] |
| 88 | combined_conditions = ' AND '.join(skip_conditions) |
| 89 | cmakef.write(f'if({combined_conditions})\n') |
| 90 | skip_conditions = [] |
| 91 | |
| 92 | if 'delete' not in test_keywords and command.startswith('$<TARGET_FILE:'): |
| 93 | mat_file_name = f'test_{test_name}_{counter}.mat' |
| 94 | command += f' -o {mat_file_name}' |
| 95 | |
| 96 | cmakef.write(f' add_test(NAME {test_name}_{counter}\n') |
| 97 | cmakef.write(f' COMMAND {command}\n') |
| 98 | cmakef.write(' WORKING_DIRECTORY ${MATIO_TESTING_DIR})\n') |
| 99 | cmakef.write(f' set_tests_properties({test_name}_{counter} PROPERTIES FIXTURES_REQUIRED TEMPDIR)\n') |
| 100 | |
| 101 | if test_keywords: |
| 102 | keyword_str = ';'.join(sorted(list(set(test_keywords)))) |
| 103 | cmakef.write(f' set_tests_properties({test_name}_{counter} PROPERTIES LABELS "{keyword_str}")\n') |
| 104 | |
| 105 | if counter > 1: |
| 106 | depends_str = f'{test_name}_{counter - 1}' |
| 107 | cmakef.write(f' set_tests_properties({test_name}_{counter} PROPERTIES DEPENDS {depends_str})\n') |
| 108 | |
| 109 | counter += 1 |
| 110 | |
| 111 | elif check_copy_match and check_diff_match: |
| 112 | command = check_diff_match.group(1) |
| 113 | for original, replacement in command_mapping.items(): |
| 114 | command = command.replace(original, replacement) |
| 115 | |
| 116 | if 'delete' not in test_keywords and counter > 1 and mat_file_name is not None and command.startswith('$<TARGET_FILE:'): |
| 117 | command_items = command.split(' ') |
| 118 | command_items = [item if not item.startswith('test_') or not item.endswith('.mat') else mat_file_name for item in command_items] |
| 119 | command = ' '.join(command_items) |
| 120 | |
| 121 | output_name = f'{test_name}_{counter}_output.txt' |
| 122 | if command.startswith('$<TARGET_FILE:'): |
| 123 | command += f' -o {output_name}' |
| 124 | |
| 125 | if skip_conditions: |
| 126 | if len(skip_conditions) > 1: |
| 127 | skip_conditions = skip_conditions[1:] |
| 128 | combined_conditions = ' AND '.join(skip_conditions) |
| 129 | cmakef.write(f'if({combined_conditions})\n') |
| 130 | skip_conditions = [] |
| 131 | |
| 132 | cmakef.write(f' add_test(NAME {test_name}_{counter}\n') |
| 133 | cmakef.write(f' COMMAND {command}\n') |
| 134 | cmakef.write(' WORKING_DIRECTORY ${MATIO_TESTING_DIR})\n') |
| 135 | cmakef.write(f' set_tests_properties({test_name}_{counter} PROPERTIES FIXTURES_REQUIRED TEMPDIR)\n') |
| 136 | |
| 137 | if test_keywords: |
| 138 | keyword_str = ';'.join(sorted(list(set(test_keywords)))) |
| 139 | cmakef.write(f' set_tests_properties({test_name}_{counter} PROPERTIES LABELS "{keyword_str}")\n') |
| 140 | |
| 141 | if counter > 1: |
| 142 | depends_str = f'{test_name}_{counter - 1}' |
| 143 | cmakef.write(f' set_tests_properties({test_name}_{counter} PROPERTIES DEPENDS {depends_str})\n') |
| 144 | |
| 145 | expected_name = check_copy_match.group(1).replace('$srcdir/', '${PROJECT_SOURCE_DIR}/test/') |
| 146 | command = f'${{CMAKE_COMMAND}} -E compare_files --ignore-eol {expected_name} {output_name}' |
| 147 | cmakef.write(f' add_test(NAME {test_name}_{counter + 1}\n') |
| 148 | cmakef.write(f' COMMAND {command}\n') |
| 149 | cmakef.write(' WORKING_DIRECTORY ${MATIO_TESTING_DIR})\n') |
| 150 | cmakef.write(f' set_tests_properties({test_name}_{counter + 1} PROPERTIES FIXTURES_REQUIRED TEMPDIR)\n') |
| 151 | |
| 152 | if test_keywords: |
| 153 | keyword_str = ';'.join(sorted(list(set(test_keywords)))) |
| 154 | cmakef.write(f' set_tests_properties({test_name}_{counter + 1} PROPERTIES LABELS "diff;{keyword_str}")\n') |
| 155 | |
| 156 | depends_str = f'{test_name}_{counter}' |
| 157 | cmakef.write(f' set_tests_properties({test_name}_{counter + 1} PROPERTIES DEPENDS {depends_str})\n') |
| 158 | |
| 159 | check_copy_match = None |
| 160 | check_diff_match = None |
| 161 | counter += 2 |
| 162 | |
| 163 | elif cleanup_match: |
| 164 | if counter > 1: |
| 165 | cmakef.write('endif()\n') |
| 166 | |
| 167 | |
| 168 | def get_file_hash(file_path, algorithm='sha256'): |
| 169 | hash_obj = hashlib.new(algorithm) |
| 170 | with open(file_path, 'rb') as f: |
| 171 | for chunk in iter(lambda: f.read(4096), b""): |
| 172 | hash_obj.update(chunk) |
| 173 | return hash_obj.hexdigest() |
| 174 | |
| 175 | |
| 176 | def generate_ctest_files(file_list, output_dir, global_force, hash_dict): |
| 177 | os.makedirs(output_dir, exist_ok=True) |
| 178 | |
| 179 | for autotest_file in file_list: |
| 180 | base_name = os.path.splitext(os.path.basename(autotest_file))[0] |
| 181 | cmake_output_file = os.path.join(output_dir, f'{base_name}.cmake') |
| 182 | hash = get_file_hash(autotest_file) |
| 183 | force = hash != hash_dict.get(base_name, '') |
| 184 | hash_dict[base_name] = hash |
| 185 | if not force and not global_force and os.path.isfile(cmake_output_file): |
| 186 | continue |
| 187 | convert_autotest_to_ctest(autotest_file, cmake_output_file) |
| 188 | |
| 189 | |
| 190 | if __name__ == '__main__': |
| 191 | hash = get_file_hash(__file__) |
| 192 | output_dir = sys.argv[1] |
| 193 | hash_json_file = os.path.join(output_dir, 'generator.json') |
| 194 | try: |
| 195 | with open(hash_json_file, 'r') as json_file: |
| 196 | hash_dict = json.load(json_file) |
| 197 | except OSError: |
| 198 | hash_dict = {} |
| 199 | |
| 200 | force = hash != hash_dict.get('generator', '') |
| 201 | hash_dict['generator'] = hash |
| 202 | hash_dict.pop('sha256', None) |
| 203 | generate_ctest_files(glob.glob(os.path.join('tests', '*.at')), output_dir, force, hash_dict) |
| 204 | |
| 205 | with open(hash_json_file, 'w') as json_file: |
| 206 | json.dump(hash_dict, json_file) |