Skip to content

Commit e1353f4

Browse files
committed
MCU8MASS-1790 Migrate to having cryptoauthlib in a folder and include scripts for updating include path of cryptoauthlib
1 parent 6c8c91b commit e1353f4

File tree

123 files changed

+177
-36354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+177
-36354
lines changed

scripts/clear_cryptoauthlib.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
#!/bin/bash
22

33
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
4-
CRYPTOAUTH_PATH=$SCRIPTPATH/../lib/cryptoauth
54
SRC_PATH=$SCRIPTPATH/../src
65

7-
pushd "$SRC_PATH" 1> /dev/null
8-
rm -rf calib crypto hal host atcacert
9-
find . -name "atca_*" -exec rm -f {} +
10-
find . -name "cryptoauth*" -exec rm -f {} +
11-
find . -name "tng*" -exec rm -f {} +
12-
find . -name "tflxtls*" -exec rm -f {} +
13-
popd 1> /dev/null
6+
rm -r "$SRC_PATH/cryptoauthlib"

scripts/inject_cryptoauthlib.sh

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
44
CRYPTOAUTH_PATH=$SCRIPTPATH/../lib/cryptoauth
5+
TEMPDIR_PATH=$CRYPTOAUTH_PATH/temp
56
SRC_PATH=$SCRIPTPATH/../src
67

78
# Update cryptoauthlib
@@ -15,28 +16,37 @@ else
1516
git clone https://github.com/MicrochipTech/cryptoauthlib.git $CRYPTOAUTH_PATH/cryptoauthlib
1617
fi
1718

18-
echo -n "Injecting... "
19-
20-
# Copy sources
21-
cp -r "$CRYPTOAUTH_PATH/cryptoauthlib/lib/" "$SRC_PATH/cryptoauthlib/"
22-
cp -r "$CRYPTOAUTH_PATH/atca_config.h" "$SRC_PATH/cryptoauthlib/"
23-
24-
# Remove everything we don't need, so we are only left with .h files
25-
pushd "$SRC_PATH/cryptoauthlib" 1> /dev/null
26-
rm -rf mbedtls pkcs11 openssl wolfssl jwt cmake CMakeLists.txt
27-
28-
pushd hal 1> /dev/null
29-
find . ! -name "atca_hal.*" -type f -exec rm -f {} +
19+
echo "Injecting... "
20+
21+
# Fix the include paths so that all are relative to src since Arduino doesn't
22+
# allow us to add extra include paths for the compiler
23+
python "$SCRIPTPATH/update_include_paths.py" "$CRYPTOAUTH_PATH/cryptoauthlib" "$TEMPDIR_PATH"
24+
25+
# Prevent removing items if the temporary directory was not created, just to be
26+
# safe and not removing files from the project
27+
if [ ! -d "$TEMPDIR_PATH" ]; then
28+
echo "Temporary directory for cryptoauthlib not created, aborting"
29+
exit 1
30+
else
31+
# Remove everything we don't need, so we are only left with .h files
32+
pushd "$TEMPDIR_PATH/lib" 1> /dev/null
33+
rm -rf mbedtls pkcs11 openssl wolfssl jwt cmake CMakeLists.txt
34+
35+
pushd hal 1> /dev/null
36+
find . ! -name "atca_hal.*" -type f -exec rm -f {} +
37+
popd 1> /dev/null
3038
popd 1> /dev/null
31-
popd 1> /dev/null
39+
fi
3240

33-
cp -r "$CRYPTOAUTH_PATH/cryptoauthlib/app/tng/"*.c "$SRC_PATH/cryptoauthlib/"
34-
cp -r "$CRYPTOAUTH_PATH/cryptoauthlib/app/tng/"*.h "$SRC_PATH/cryptoauthlib/"
41+
# Make the destination folders
42+
mkdir "$SRC_PATH/cryptoauthlib"
43+
mkdir "$SRC_PATH/cryptoauthlib/app"
3544

36-
# Move everything to src since we can't add other include paths for
37-
# Arduino, has to be top level in src
38-
mv "$SRC_PATH/cryptoauthlib/"* "$SRC_PATH" 1> /dev/null
45+
# Copy sources
46+
cp -r "$TEMPDIR_PATH/lib" "$SRC_PATH/cryptoauthlib/"
47+
cp -r "$TEMPDIR_PATH/app/tng" "$SRC_PATH/cryptoauthlib/app/"
48+
cp -r "$CRYPTOAUTH_PATH/atca_config.h" "$SRC_PATH/cryptoauthlib/"
3949

40-
rm -r "$SRC_PATH/cryptoauthlib"
50+
rm -r "$TEMPDIR_PATH"
4151

4252
echo "Done!"

scripts/update_include_paths.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
from pathlib import Path
2+
import re
3+
import argparse
4+
5+
6+
def get_path_relative_to_root(root_path: Path, file_path: Path):
7+
"""Returns the relative path of the file path to the root
8+
path, appended with the root path.
9+
10+
Args:
11+
root_path (Path): Root path directory
12+
file_path (Path): File path in the root directory
13+
14+
Returns:
15+
Path: The full path to the file relative to the root path
16+
"""
17+
return Path((root_path / file_path.relative_to(root_path)).as_posix())
18+
19+
20+
def get_include_paths_relative_to_root(root_path: Path):
21+
"""Fetches every .h file in a directory to build a list of include paths.
22+
23+
Args:
24+
root_path (Path): The root path of the directory
25+
26+
Returns:
27+
list: List of paths to the include files
28+
"""
29+
30+
include_paths = []
31+
32+
for path in root_path.rglob("*.h"):
33+
include_paths.append(get_path_relative_to_root(root_path, path))
34+
35+
return include_paths
36+
37+
38+
def find_matching_root_include_path(include_path: str, include_paths_relative_to_root):
39+
"""Given a include path, finds the corresponding include path relative to the root.
40+
41+
Args:
42+
include_path (str): The include path in the file.
43+
include_paths (list[Path]): The list of include paths which are
44+
relative to the root directory.
45+
46+
Returns:
47+
Path: The matched path relative to root, or None if not found
48+
"""
49+
50+
for include_path_relative_to_root in include_paths_relative_to_root:
51+
52+
if Path(include_path).name == include_path_relative_to_root.name:
53+
return include_path_relative_to_root
54+
55+
return None
56+
57+
58+
def get_update_line(line: str, include_paths_relative_to_root):
59+
"""Given a line in a file, replaces a #include <...> or #include "..." with
60+
a path relative to the root folder given in the include paths relative
61+
to root list.
62+
63+
Args:
64+
line (str): The line in the file
65+
include_paths_relative_to_root (list[Path]): List of the include paths
66+
relative to the root.
67+
68+
Returns:
69+
str: The updated line if "#include" was found.
70+
"""
71+
72+
# Find includes with <> or ""
73+
r = re.search('["<](.*)[">]', line)
74+
75+
if r is not None and r.group(1).endswith(".h"):
76+
include_path = r.group(1)
77+
78+
include_path_relative_to_root = find_matching_root_include_path(
79+
include_path,
80+
include_paths_relative_to_root
81+
)
82+
83+
# If the match was not found, the include is likely a system header
84+
if include_path_relative_to_root is not None:
85+
return line.replace(include_path, include_path_relative_to_root.as_posix())
86+
87+
return line
88+
89+
90+
if __name__ == "__main__":
91+
92+
parser = argparse.ArgumentParser(
93+
prog="update_include_paths",
94+
description="Updates the include paths of a library so that all are relative to a root folder",
95+
)
96+
97+
parser.add_argument("input_directory")
98+
parser.add_argument("output_directory")
99+
parser.add_argument("-v", "--verbose", action="store_true")
100+
101+
args = parser.parse_args()
102+
103+
root_path = Path(args.input_directory)
104+
output_root_path = Path(args.output_directory)
105+
106+
# Fetch every .h file to build a reference for the include paths
107+
include_paths_relative_to_root = get_include_paths_relative_to_root(root_path)
108+
109+
# Find every .h and .c file
110+
paths = []
111+
112+
for path in root_path.rglob("*.c"):
113+
paths.append(path)
114+
115+
for path in root_path.rglob("*.h"):
116+
paths.append(path)
117+
118+
# Loop through every file and update includes so that every include is
119+
# relative (and including) the root path
120+
for path in paths:
121+
122+
if args.verbose:
123+
print(f"Opening {path}")
124+
125+
with open(path) as input_file:
126+
127+
# We need to remove the directories leading to the input directory
128+
output_path = output_root_path / path.relative_to(Path(args.input_directory))
129+
output_path.parent.mkdir(exist_ok=True, parents=True)
130+
131+
with open(output_path, "w") as output_file:
132+
133+
for line in input_file:
134+
updated_line = get_update_line(line, include_paths_relative_to_root)
135+
136+
if args.verbose:
137+
if line != updated_line:
138+
print(f"\tChanged {line.strip()} to {updated_line.strip()}")
139+
140+
output_file.write(updated_line)

0 commit comments

Comments
 (0)