Skip to content

Add CRT version check at cmake and runtime #3437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if (LEGACY_BUILD)
option(ENABLE_PROTOCOL_TESTS "Enable protocol tests" OFF)
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)
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)

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)

set(AWS_USER_AGENT_CUSTOMIZATION "" CACHE STRING "User agent extension")
set(AWS_TEST_REGION "US_EAST_1" CACHE STRING "Region to target integration tests against")
Expand Down Expand Up @@ -249,6 +249,12 @@ if (LEGACY_BUILD)
include(AwsFindPackage)
set(IN_SOURCE_BUILD OFF)
endif ()

if (ENABLE_CRT_VERSION_CHECK)
include(check_crt_version)
check_crt_version()
endif()

aws_use_package(aws-crt-cpp)
aws_use_package(aws-c-http)
aws_use_package(aws-c-mqtt)
Expand Down
26 changes: 26 additions & 0 deletions cmake/check_crt_version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

function(check_crt_version)
include(expected_crt_version)

file(READ "${aws-crt-cpp_SOURCE_DIR}/VERSION" CRT_SIMPLE_VERSION)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable probably won't be set if you are importing the CRT library instead of building it as a part of the SDK build, so in case of import, you want to disable the check

-DENABLE_CRT_VERSION_CHECK=OFF

string(STRIP ${CRT_SIMPLE_VERSION} CRT_SIMPLE_VERSION)
string(REPLACE "." ";" CRT_SIMPLE_VERSION ${CRT_SIMPLE_VERSION})
list(GET CRT_SIMPLE_VERSION 0 CRT_VERSION_MAJOR)
list(GET CRT_SIMPLE_VERSION 1 CRT_VERSION_MINOR)
list(GET CRT_SIMPLE_VERSION 2 CRT_VERSION_PATCH)

if(EXPECTED_CRT_VERSION_MAJOR EQUAL CRT_VERSION_MAJOR AND
EXPECTED_CRT_VERSION_MINOR EQUAL CRT_VERSION_MINOR AND
EXPECTED_CRT_VERSION_PATCH EQUAL CRT_VERSION_PATCH)
message(TRACE "AWS-CRT-CPP version matches the expected")
else()
message(FATAL_ERROR "AWS-CRT-CPP version mismatch detected!\n"
"Expected: ${EXPECTED_CRT_VERSION_MAJOR}.${EXPECTED_CRT_VERSION_MINOR}.${EXPECTED_CRT_VERSION_PATCH}, "
"Found: ${CRT_VERSION_MAJOR}.${CRT_VERSION_MINOR}.${CRT_VERSION_PATCH}\n"
"Please use \"git pull --recurse-submodules\" to git pull and update the CRT submodule.\n"
"Or disable this check with \"-DENABLE_CRT_VERSION_CHECK=OFF\"")
endif ()

endfunction(check_crt_version)
10 changes: 10 additions & 0 deletions cmake/expected_crt_version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

set(EXPECTED_CRT_VERSION_MAJOR 0)
set(EXPECTED_CRT_VERSION_MINOR 32)
set(EXPECTED_CRT_VERSION_PATCH 4)

set(EXPECTED_CRT_VERSION_MAJOR ${EXPECTED_CRT_VERSION_MAJOR} PARENT_SCOPE)
set(EXPECTED_CRT_VERSION_MINOR ${EXPECTED_CRT_VERSION_MINOR} PARENT_SCOPE)
set(EXPECTED_CRT_VERSION_PATCH ${EXPECTED_CRT_VERSION_PATCH} PARENT_SCOPE)
14 changes: 14 additions & 0 deletions src/aws-cpp-sdk-core/source/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <aws/crt/Api.h>
#include <aws/crt/Config.h>
#include <aws/crt/io/TlsOptions.h>
#include <aws/crt/io/Bootstrap.h>
#include <aws/core/Globals.h>
Expand Down Expand Up @@ -52,6 +53,19 @@ namespace Aws
auto crtVersion = g_apiHandle->GetCrtVersion();
AWS_LOGSTREAM_INFO(TAG, "Initialized AWS-CRT-CPP with version "
<< crtVersion.major << "." << crtVersion.minor << "." << crtVersion.patch);

if(crtVersion.major != AWS_CRT_CPP_VERSION_MAJOR ||
crtVersion.minor != AWS_CRT_CPP_VERSION_MINOR ||
crtVersion.patch != AWS_CRT_CPP_VERSION_PATCH)
{
AWS_LOGSTREAM_ERROR(TAG, "AWS-CRT-CPP version mismatch detected.");
AWS_LOGSTREAM_INFO(TAG, "Initialized CRT with version "
<< crtVersion.major << "." << crtVersion.minor << "." << crtVersion.patch << "; "
<< "However, the AWS-SDK-CPP had been built with CRT version: "
<< AWS_CRT_CPP_VERSION_MAJOR << "."
<< AWS_CRT_CPP_VERSION_MINOR << "."
<< AWS_CRT_CPP_VERSION_PATCH << ";");
}
}

void CleanupCrt()
Expand Down
30 changes: 28 additions & 2 deletions tools/scripts/ops/update_crt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
"""

import os
import re
import shutil
import subprocess
import time

from jinja2 import Environment, select_autoescape

PREFETCH_DEPS_SH_NAME = "prefetch_crt_dependency.sh"
EXPECTED_CRT_VER_CMAKE = "cmake/expected_crt_version.cmake"
CRT_VERSION_F = "VERSION"
CRT_DIR = "./crt/aws-crt-cpp"
GIT_EXE = shutil.which("git")

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

PREFETCH_DEPS_TEMPLATE = \
"""#!/bin/sh
Expand Down Expand Up @@ -64,6 +68,18 @@

"""

EXPECTED_CRT_VERSION_TEMPLATE = \
"""# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

set(EXPECTED_CRT_VERSION_MAJOR {{ t_major }})
set(EXPECTED_CRT_VERSION_MINOR {{ t_minor }})
set(EXPECTED_CRT_VERSION_PATCH {{ t_patch }})

set(EXPECTED_CRT_VERSION_MAJOR ${EXPECTED_CRT_VERSION_MAJOR} PARENT_SCOPE)
set(EXPECTED_CRT_VERSION_MINOR ${EXPECTED_CRT_VERSION_MINOR} PARENT_SCOPE)
set(EXPECTED_CRT_VERSION_PATCH ${EXPECTED_CRT_VERSION_PATCH} PARENT_SCOPE)
"""

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

with open(f"{CRT_DIR}/{CRT_VERSION_F}", mode="r", encoding="utf-8") as version_f:
ver_match = CRT_VERSION_PATTERN.match(version_f.read())
cmake_exp_ver_template = jinja2_env.from_string(EXPECTED_CRT_VERSION_TEMPLATE)
rendered_cmake_ver = cmake_exp_ver_template.render(t_major=ver_match.group("major"),
t_minor=ver_match.group("minor"),
t_patch=ver_match.group("patch"))

with open(f"{EXPECTED_CRT_VER_CMAKE}", mode="w", encoding="utf-8") as cmake_f:
cmake_f.write(rendered_cmake_ver)

print(f"CRT submodule is updated to {latest_crt_version}\n"
f"Script {PREFETCH_DEPS_SH_NAME} content is updated.")
f"Scripts {PREFETCH_DEPS_SH_NAME} and {EXPECTED_CRT_VER_CMAKE} are updated.")

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

Expand Down
Loading