| 1 |
|
| 2 |
import os |
| 3 |
import tempfile |
| 4 |
import shutil |
| 5 |
import subprocess |
| 6 |
import argparse |
| 7 |
import re |
| 8 |
|
| 9 |
|
| 10 |
def parse_version(version_string): |
| 11 |
match = re.fullmatch(r'v(\d+)\.(\d+)\.(\d+)', version_string) |
| 12 |
if not match: |
| 13 |
raise ValueError("Invalid version string") |
| 14 |
return match.group(1), match.group(2), match.group(3) |
| 15 |
|
| 16 |
|
| 17 |
argument_parser = argparse.ArgumentParser( |
| 18 |
description="Helper to import libcbor as external dependency.") |
| 19 |
|
| 20 |
argument_parser.add_argument("--version", |
| 21 |
required=True, help="Version string to import") |
| 22 |
|
| 23 |
args = argument_parser.parse_args() |
| 24 |
major_version, minor_version, patch_version = parse_version(args.version) |
| 25 |
|
| 26 |
GENERATED_NOTES = """/** |
| 27 |
* DO NOT DIRECTLY MODIFY THIS FILE: |
| 28 |
* |
| 29 |
* The code in this file is generated from scripts/import_libcbor.py |
| 30 |
* and any modifications should be in there. |
| 31 |
*/ |
| 32 |
""" |
| 33 |
|
| 34 |
CBOR_EXPORT_H = """ |
| 35 |
#ifndef CBOR_EXPORT_H |
| 36 |
#define CBOR_EXPORT_H |
| 37 |
|
| 38 |
/* Don't export anything from libcbor */ |
| 39 |
#define CBOR_EXPORT |
| 40 |
|
| 41 |
#endif /* CBOR_EXPORT_H */ |
| 42 |
""" |
| 43 |
|
| 44 |
CONFIGURATION_H = f""" |
| 45 |
#ifndef LIBCBOR_CONFIGURATION_H |
| 46 |
#define LIBCBOR_CONFIGURATION_H |
| 47 |
|
| 48 |
#define CBOR_MAJOR_VERSION {major_version} |
| 49 |
#define CBOR_MINOR_VERSION {minor_version} |
| 50 |
#define CBOR_PATCH_VERSION {patch_version} |
| 51 |
|
| 52 |
#define CBOR_BUFFER_GROWTH 2 |
| 53 |
#define CBOR_MAX_STACK_SIZE 2048 |
| 54 |
#define CBOR_PRETTY_PRINTER 1 |
| 55 |
|
| 56 |
#if defined(_MSC_VER) |
| 57 |
# define CBOR_RESTRICT_SPECIFIER |
| 58 |
#else |
| 59 |
# define CBOR_RESTRICT_SPECIFIER restrict |
| 60 |
#endif |
| 61 |
|
| 62 |
#define CBOR_INLINE_SPECIFIER |
| 63 |
|
| 64 |
/* Ignore the compiler warnings for libcbor. */ |
| 65 |
#ifdef _MSC_VER |
| 66 |
# pragma warning(disable : 4028) |
| 67 |
# pragma warning(disable : 4189) |
| 68 |
# pragma warning(disable : 4715) |
| 69 |
# pragma warning(disable : 4232) |
| 70 |
# pragma warning(disable : 4068) |
| 71 |
# pragma warning(disable : 4244) |
| 72 |
# pragma warning(disable : 4701) |
| 73 |
# pragma warning(disable : 4703) |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifdef __clang__ |
| 77 |
# pragma clang diagnostic ignored "-Wreturn-type" |
| 78 |
#elif defined(__GNUC__) |
| 79 |
# pragma GCC diagnostic ignored "-Wreturn-type" |
| 80 |
# pragma GCC diagnostic ignored "-Wunknown-pragmas" |
| 81 |
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 82 |
#endif |
| 83 |
|
| 84 |
#endif // LIBCBOR_CONFIGURATION_H |
| 85 |
""" |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
temp_repo_dir = tempfile.mkdtemp() |
| 90 |
|
| 91 |
try: |
| 92 |
|
| 93 |
repo_url = "https://github.com/PJK/libcbor.git" |
| 94 |
clone_command = f"git clone {repo_url} {temp_repo_dir}" |
| 95 |
subprocess.run(clone_command, shell=True, check=True) |
| 96 |
subprocess.run(["git", "checkout", "tags/" + args.version], |
| 97 |
cwd=temp_repo_dir, check=True) |
| 98 |
version_number = args.version[1:] |
| 99 |
|
| 100 |
|
| 101 |
base_dir = os.path.join(os.path.dirname( |
| 102 |
__file__), "..") |
| 103 |
output_dir = os.path.join( |
| 104 |
base_dir, "source", "external", "libcbor") |
| 105 |
shutil.rmtree(output_dir, ignore_errors=True) |
| 106 |
os.makedirs(output_dir, exist_ok=True) |
| 107 |
|
| 108 |
|
| 109 |
src_dir = os.path.join(temp_repo_dir, "src") |
| 110 |
for root, dirs, files in os.walk(src_dir): |
| 111 |
dir_name = os.path.basename(root) |
| 112 |
for file in files: |
| 113 |
if file.endswith((".c", ".h")): |
| 114 |
|
| 115 |
src_file = os.path.join(root, file) |
| 116 |
rel_path = os.path.relpath(src_file, src_dir) |
| 117 |
dest_file = os.path.join(output_dir, rel_path) |
| 118 |
os.makedirs(os.path.dirname(dest_file), exist_ok=True) |
| 119 |
shutil.copy(src_file, dest_file) |
| 120 |
|
| 121 |
|
| 122 |
with open(os.path.join(output_dir, "cbor/cbor_export.h"), "w") as file: |
| 123 |
file.write(GENERATED_NOTES) |
| 124 |
file.write(CBOR_EXPORT_H) |
| 125 |
with open(os.path.join(output_dir, "cbor/configuration.h"), "w") as file: |
| 126 |
file.write(GENERATED_NOTES) |
| 127 |
file.write(CONFIGURATION_H) |
| 128 |
|
| 129 |
|
| 130 |
license_path = os.path.join(base_dir, "THIRD-PARTY-LICENSES.txt") |
| 131 |
with open(license_path, 'r') as f: |
| 132 |
content = f.read() |
| 133 |
|
| 134 |
|
| 135 |
pattern = r'\*\* libcbor; version \d+\.\d+\.\d+ --' |
| 136 |
replacement = f'** libcbor; version {version_number} --' |
| 137 |
content = re.sub(pattern, replacement, content) |
| 138 |
|
| 139 |
with open(license_path, 'w') as f: |
| 140 |
f.write(content) |
| 141 |
|
| 142 |
print(f"Updated libcbor to version {version_number}") |
| 143 |
|
| 144 |
except Exception as e: |
| 145 |
print(f"An error occurred: {e}") |
| 146 |
|
| 147 |
finally: |
| 148 |
|
| 149 |
shutil.rmtree(temp_repo_dir, ignore_errors=True) |