Skip to content

Commit ae16e27

Browse files
Add CRT version check at cmake and runtime
1 parent 475e79d commit ae16e27

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if (LEGACY_BUILD)
7070
option(ENABLE_PROTOCOL_TESTS "Enable protocol tests" OFF)
7171
option(DISABLE_DNS_REQUIRED_TESTS "Disable unit tests that require DNS lookup to succeed, useful when using a http client that does not perform DNS lookup" OFF)
7272
option(AWS_APPSTORE_SAFE "Remove reference to private Apple APIs for AES GCM in Common Crypto. If set to OFF you application will get rejected from the apple app store." OFF)
73-
73+
option(ENABLE_CRT_VERSION_CHECK "Forces the check of found AWS-CRT-CPP version to match the expected one (only at the SDK build configuration)" ON)
7474

7575
set(AWS_USER_AGENT_CUSTOMIZATION "" CACHE STRING "User agent extension")
7676
set(AWS_TEST_REGION "US_EAST_1" CACHE STRING "Region to target integration tests against")
@@ -249,6 +249,12 @@ if (LEGACY_BUILD)
249249
include(AwsFindPackage)
250250
set(IN_SOURCE_BUILD OFF)
251251
endif ()
252+
253+
if (ENABLE_CRT_VERSION_CHECK)
254+
include(check_crt_version)
255+
check_crt_version()
256+
endif()
257+
252258
aws_use_package(aws-crt-cpp)
253259
aws_use_package(aws-c-http)
254260
aws_use_package(aws-c-mqtt)

cmake/check_crt_version.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
function(check_crt_version)
5+
include(expected_crt_version)
6+
7+
file(READ "${aws-crt-cpp_SOURCE_DIR}/VERSION" CRT_SIMPLE_VERSION)
8+
string(STRIP ${CRT_SIMPLE_VERSION} CRT_SIMPLE_VERSION)
9+
string(REPLACE "." ";" CRT_SIMPLE_VERSION ${CRT_SIMPLE_VERSION})
10+
list(GET CRT_SIMPLE_VERSION 0 CRT_VERSION_MAJOR)
11+
list(GET CRT_SIMPLE_VERSION 1 CRT_VERSION_MINOR)
12+
list(GET CRT_SIMPLE_VERSION 2 CRT_VERSION_PATCH)
13+
14+
if(EXPECTED_CRT_VERSION_MAJOR EQUAL CRT_VERSION_MAJOR AND
15+
EXPECTED_CRT_VERSION_MINOR EQUAL CRT_VERSION_MINOR AND
16+
EXPECTED_CRT_VERSION_PATCH EQUAL CRT_VERSION_PATCH)
17+
message(TRACE "AWS-CRT-CPP version matches the expected")
18+
else()
19+
message(FATAL_ERROR "AWS-CRT-CPP version mismatch detected!\n"
20+
"Expected: ${EXPECTED_CRT_VERSION_MAJOR}.${EXPECTED_CRT_VERSION_MINOR}.${EXPECTED_CRT_VERSION_PATCH}, "
21+
"Found: ${CRT_VERSION_MAJOR}.${CRT_VERSION_MINOR}.${CRT_VERSION_PATCH}\n"
22+
"Please use \"git pull --recurse-submodules\" to git pull and update the CRT submodule.\n"
23+
"Or disable this check with \"-DENABLE_CRT_VERSION_CHECK=OFF\"")
24+
endif ()
25+
26+
endfunction(check_crt_version)

cmake/expected_crt_version.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
set(EXPECTED_CRT_VERSION_MAJOR 0)
5+
set(EXPECTED_CRT_VERSION_MINOR 32)
6+
set(EXPECTED_CRT_VERSION_PATCH 7)
7+
8+
set(EXPECTED_CRT_VERSION_MAJOR ${EXPECTED_CRT_VERSION_MAJOR} PARENT_SCOPE)
9+
set(EXPECTED_CRT_VERSION_MINOR ${EXPECTED_CRT_VERSION_MINOR} PARENT_SCOPE)
10+
set(EXPECTED_CRT_VERSION_PATCH ${EXPECTED_CRT_VERSION_PATCH} PARENT_SCOPE)

src/aws-cpp-sdk-core/source/Globals.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <aws/crt/Api.h>
7+
#include <aws/crt/Config.h>
78
#include <aws/crt/io/TlsOptions.h>
89
#include <aws/crt/io/Bootstrap.h>
910
#include <aws/core/Globals.h>
@@ -52,6 +53,19 @@ namespace Aws
5253
auto crtVersion = g_apiHandle->GetCrtVersion();
5354
AWS_LOGSTREAM_INFO(TAG, "Initialized AWS-CRT-CPP with version "
5455
<< crtVersion.major << "." << crtVersion.minor << "." << crtVersion.patch);
56+
57+
if(crtVersion.major != AWS_CRT_CPP_VERSION_MAJOR ||
58+
crtVersion.minor != AWS_CRT_CPP_VERSION_MINOR ||
59+
crtVersion.patch != AWS_CRT_CPP_VERSION_PATCH)
60+
{
61+
AWS_LOGSTREAM_ERROR(TAG, "AWS-CRT-CPP version mismatch detected.");
62+
AWS_LOGSTREAM_INFO(TAG, "Initialized CRT with version "
63+
<< crtVersion.major << "." << crtVersion.minor << "." << crtVersion.patch << "; "
64+
<< "However, the AWS-SDK-CPP had been built with CRT version: "
65+
<< AWS_CRT_CPP_VERSION_MAJOR << "."
66+
<< AWS_CRT_CPP_VERSION_MINOR << "."
67+
<< AWS_CRT_CPP_VERSION_PATCH << ";");
68+
}
5569
}
5670

5771
void CleanupCrt()

tools/scripts/ops/update_crt.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
"""
99

1010
import os
11+
import re
1112
import shutil
1213
import subprocess
1314
import time
1415

1516
from jinja2 import Environment, select_autoescape
1617

1718
PREFETCH_DEPS_SH_NAME = "prefetch_crt_dependency.sh"
19+
EXPECTED_CRT_VER_CMAKE = "cmake/expected_crt_version.cmake"
20+
CRT_VERSION_F = "VERSION"
1821
CRT_DIR = "./crt/aws-crt-cpp"
1922
GIT_EXE = shutil.which("git")
2023

24+
CRT_VERSION_PATTERN = re.compile("^(?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+).*")
2125

2226
PREFETCH_DEPS_TEMPLATE = \
2327
"""#!/bin/sh
@@ -64,6 +68,18 @@
6468
6569
"""
6670

71+
EXPECTED_CRT_VERSION_TEMPLATE = \
72+
"""# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
73+
# SPDX-License-Identifier: Apache-2.0.
74+
75+
set(EXPECTED_CRT_VERSION_MAJOR {{ t_major }})
76+
set(EXPECTED_CRT_VERSION_MINOR {{ t_minor }})
77+
set(EXPECTED_CRT_VERSION_PATCH {{ t_patch }})
78+
79+
set(EXPECTED_CRT_VERSION_MAJOR ${EXPECTED_CRT_VERSION_MAJOR} PARENT_SCOPE)
80+
set(EXPECTED_CRT_VERSION_MINOR ${EXPECTED_CRT_VERSION_MINOR} PARENT_SCOPE)
81+
set(EXPECTED_CRT_VERSION_PATCH ${EXPECTED_CRT_VERSION_PATCH} PARENT_SCOPE)
82+
"""
6783

6884
def call_git(cwd: str, timeout: int, command: str):
6985
git_cmd = [GIT_EXE]
@@ -114,12 +130,22 @@ def main():
114130
with open(f"{PREFETCH_DEPS_SH_NAME}", mode="w", encoding="utf-8") as prefetch_script_f:
115131
prefetch_script_f.write(rendered_script)
116132

133+
with open(f"{CRT_DIR}/{CRT_VERSION_F}", mode="r", encoding="utf-8") as version_f:
134+
ver_match = CRT_VERSION_PATTERN.match(version_f.read())
135+
cmake_exp_ver_template = jinja2_env.from_string(EXPECTED_CRT_VERSION_TEMPLATE)
136+
rendered_cmake_ver = cmake_exp_ver_template.render(t_major=ver_match.group("major"),
137+
t_minor=ver_match.group("minor"),
138+
t_patch=ver_match.group("patch"))
139+
140+
with open(f"{EXPECTED_CRT_VER_CMAKE}", mode="w", encoding="utf-8") as cmake_f:
141+
cmake_f.write(rendered_cmake_ver)
142+
117143
print(f"CRT submodule is updated to {latest_crt_version}\n"
118-
f"Script {PREFETCH_DEPS_SH_NAME} content is updated.")
144+
f"Scripts {PREFETCH_DEPS_SH_NAME} and {EXPECTED_CRT_VER_CMAKE} are updated.")
119145

120146
print("Don't forget to git add, commit, and push:\n")
121147
print(f" git checkout -b updateCrt/{latest_crt_version} && "
122-
f"git add {CRT_DIR} {PREFETCH_DEPS_SH_NAME} && "
148+
f"git add {CRT_DIR} {PREFETCH_DEPS_SH_NAME} {EXPECTED_CRT_VER_CMAKE} && "
123149
f"git commit -m \"Update CRT to {latest_crt_version}\" && "
124150
f"git push origin updateCrt/{latest_crt_version}")
125151

0 commit comments

Comments
 (0)