blob: 1f5839c27bb1266b9251994315476b23259086d1 [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001#!/usr/bin/env python3
2
3import os
4import shutil
5
6from upstream_utils import (
7 get_repo_root,
8 clone_repo,
9 walk_if,
10 git_am,
11)
12
13
14def main():
15 upstream_root = clone_repo("https://github.com/nlohmann/json", "v3.11.2")
16 wpilib_root = get_repo_root()
17 wpiutil = os.path.join(wpilib_root, "wpiutil")
18
19 # Apply patches to upstream Git repo
20 os.chdir(upstream_root)
21 for f in [
22 "0001-Remove-version-from-namespace.patch",
23 "0002-Make-serializer-public.patch",
24 "0003-Make-dump_escaped-take-std-string_view.patch",
25 "0004-Add-llvm-stream-support.patch",
26 ]:
27 git_am(
28 os.path.join(wpilib_root, "upstream_utils/json_patches", f),
29 use_threeway=True,
30 )
31
32 # Delete old install
33 for d in [
34 "src/main/native/thirdparty/json/include",
35 ]:
36 shutil.rmtree(os.path.join(wpiutil, d), ignore_errors=True)
37
38 # Create lists of source and destination files
39 os.chdir(os.path.join(upstream_root, "include/nlohmann"))
40 files = walk_if(".", lambda dp, f: True)
41 src_include_files = [
42 os.path.join(os.path.join(upstream_root, "include/nlohmann"), f) for f in files
43 ]
44 wpiutil_json_root = os.path.join(
45 wpiutil, "src/main/native/thirdparty/json/include/wpi"
46 )
47 dest_include_files = [
48 os.path.join(wpiutil_json_root, f.replace(".hpp", ".h")) for f in files
49 ]
50
51 # Copy json header files into allwpilib
52 for i in range(len(src_include_files)):
53 dest_dir = os.path.dirname(dest_include_files[i])
54 if not os.path.exists(dest_dir):
55 os.makedirs(dest_dir)
56 shutil.copyfile(src_include_files[i], dest_include_files[i])
57
58 for include_file in dest_include_files:
59 with open(include_file) as f:
60 content = f.read()
61
62 # Rename namespace from nlohmann to wpi
63 content = content.replace("namespace nlohmann", "namespace wpi")
64 content = content.replace("nlohmann::", "wpi::")
65
66 # Fix internal includes
67 content = content.replace(".hpp>", ".h>")
68 content = content.replace("include <nlohmann/", "include <wpi/")
69
70 # Fix include guards and other #defines
71 content = content.replace("NLOHMANN_", "WPI_")
72
73 with open(include_file, "w") as f:
74 f.write(content)
75
76
77if __name__ == "__main__":
78 main()