Skip to content

Commit 682d48b

Browse files
authored
Add Submodule Version Check to cmake Configuration (#750)
1 parent a834e4a commit 682d48b

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.9...3.31)
33
option(BUILD_DEPS "Builds aws common runtime dependencies as part of build. Turn off if you want to control your dependency chain." ON)
44
option(BYO_CRYPTO "Don't build a tls implementation or link against a crypto interface. This feature is only for unix builds currently" OFF)
55
option(USE_OPENSSL "Set this if you want to use your system's OpenSSL 1.0.2/1.1.1 compatible libcrypto" OFF)
6+
option(ENFORCE_SUBMODULE_VERSIONS "Check and enforce submodule versions to verify they are the minimum expected version or later. Versioning is only checked if BUILD_DEPS is ON." ON)
67

78
# Let aws-iot-device-sdk-cpp-v2 report its own version in MQTT connections (instead of reporting aws-crt-cpp's version).
89
option(AWS_IOT_SDK_VERSION "Set the version reported by Aws::Iot::MqttClientConnectionConfigBuilder")
@@ -27,6 +28,8 @@ project("aws-crt-cpp"
2728

2829
include(CTest)
2930
include(GNUInstallDirs)
31+
include(cmake/EnforceSubmoduleVersions.cmake) # for check_submodule_commit()
32+
3033

3134
if(NOT CMAKE_CXX_STANDARD)
3235
set(CMAKE_CXX_STANDARD 11)
@@ -52,12 +55,19 @@ if(BUILD_DEPS)
5255
set(IN_SOURCE_BUILD ON)
5356
set(BUILD_TESTING_PREV ${BUILD_TESTING})
5457
set(BUILD_TESTING OFF)
58+
if(ENFORCE_SUBMODULE_VERSIONS)
59+
check_submodule_commit("aws-c-common" "crt/aws-c-common")
60+
endif()
5561
add_subdirectory(crt/aws-c-common)
5662

5763
if(UNIX AND NOT APPLE AND NOT BYO_CRYPTO)
5864
if(NOT USE_OPENSSL)
5965
include(AwsPrebuildDependency)
6066

67+
if(ENFORCE_SUBMODULE_VERSIONS)
68+
check_submodule_commit("aws-lc" "crt/aws-lc")
69+
endif()
70+
6171
set(AWSLC_CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
6272

6373
# temporarily disable certain warnings as errors for the aws-lc build
@@ -89,9 +99,25 @@ if(BUILD_DEPS)
8999
endif()
90100

91101
set(UNSAFE_TREAT_WARNINGS_AS_ERRORS OFF CACHE BOOL "Disable warnings-as-errors when building S2N")
102+
if(ENFORCE_SUBMODULE_VERSIONS)
103+
check_submodule_commit("s2n" "crt/s2n")
104+
endif()
92105
add_subdirectory(crt/s2n)
93106
endif()
94107

108+
if(ENFORCE_SUBMODULE_VERSIONS)
109+
check_submodule_commit("aws-c-sdkutils" "crt/aws-c-sdkutils")
110+
check_submodule_commit("aws-c-io" "crt/aws-c-io")
111+
check_submodule_commit("aws-c-cal" "crt/aws-c-cal")
112+
check_submodule_commit("aws-c-compression" "crt/aws-c-compression")
113+
check_submodule_commit("aws-c-http" "crt/aws-c-http")
114+
check_submodule_commit("aws-c-auth" "crt/aws-c-auth")
115+
check_submodule_commit("aws-c-mqtt" "crt/aws-c-mqtt")
116+
check_submodule_commit("aws-checksums" "crt/aws-checksums")
117+
check_submodule_commit("aws-c-event-stream" "crt/aws-c-event-stream")
118+
check_submodule_commit("aws-c-s3" "crt/aws-c-s3")
119+
endif()
120+
95121
add_subdirectory(crt/aws-c-sdkutils)
96122
add_subdirectory(crt/aws-c-io)
97123
add_subdirectory(crt/aws-c-cal)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# cmake/EnforceSubmoduleVersions.cmake
2+
#
3+
# Guard against multiple inclusion (for CMake 3.9 we cannot use
4+
# the include_guard() command yet).
5+
if(DEFINED _AWSCRT_ENFORCE_SUBMODULE_VERSIONS_INCLUDED)
6+
return()
7+
endif()
8+
set(_AWSCRT_ENFORCE_SUBMODULE_VERSIONS_INCLUDED TRUE)
9+
10+
# Nothing to do if the caller disabled the checks *or* if BUILD_DEPS is OFF
11+
if(NOT ENFORCE_SUBMODULE_VERSIONS OR NOT BUILD_DEPS)
12+
return()
13+
endif()
14+
15+
# Ensure Git is available
16+
find_package(Git QUIET)
17+
if(NOT GIT_FOUND)
18+
message(FATAL_ERROR
19+
"ENFORCE_SUBMODULE_VERSIONS is ON but Git was not found.\n"
20+
" • Install Git and ensure it is in PATH, e.g. sudo apt install git\n"
21+
" • or tell CMake where Git lives: -DGIT_EXECUTABLE=/path/git\n"
22+
" • or rerun CMake with commit checks disabled: -DENFORCE_SUBMODULE_VERSIONS=OFF")
23+
endif()
24+
25+
function(check_submodule_commit name rel_path)
26+
set(_sub_dir "${PROJECT_SOURCE_DIR}/${rel_path}")
27+
28+
# determine baseline from super repository (aws-crt-cpp) to get expected commit of submodule
29+
# ask Git for the SHA that is stored in the super-repo’s index
30+
execute_process(
31+
COMMAND ${GIT_EXECUTABLE} "rev-parse" "HEAD:${rel_path}"
32+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
33+
OUTPUT_VARIABLE _baseline
34+
OUTPUT_STRIP_TRAILING_WHITESPACE
35+
ERROR_VARIABLE _err
36+
RESULT_VARIABLE _rc)
37+
38+
if(_rc OR "${_baseline}" STREQUAL "")
39+
message(FATAL_ERROR "Could not query git-link for ${rel_path}: ${_err}")
40+
endif()
41+
42+
# ensure the commit object exists (deepens shallow clones/branches)
43+
execute_process(
44+
COMMAND ${GIT_EXECUTABLE} cat-file -e ${_baseline}^{commit}
45+
WORKING_DIRECTORY "${_sub_dir}"
46+
RESULT_VARIABLE _missing ERROR_QUIET)
47+
48+
if(_missing)
49+
execute_process(
50+
COMMAND ${GIT_EXECUTABLE} fetch --depth 1 origin ${_baseline}
51+
WORKING_DIRECTORY "${_sub_dir}" RESULT_VARIABLE _rc ERROR_QUIET)
52+
if(_rc)
53+
execute_process(
54+
COMMAND ${GIT_EXECUTABLE} fetch --unshallow --tags origin
55+
WORKING_DIRECTORY "${_sub_dir}" ERROR_QUIET)
56+
endif()
57+
endif()
58+
59+
# ancestry check
60+
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --verify HEAD
61+
WORKING_DIRECTORY "${_sub_dir}"
62+
OUTPUT_VARIABLE _head
63+
OUTPUT_STRIP_TRAILING_WHITESPACE)
64+
65+
execute_process(COMMAND ${GIT_EXECUTABLE} merge-base --is-ancestor
66+
${_baseline} ${_head}
67+
WORKING_DIRECTORY "${_sub_dir}"
68+
RESULT_VARIABLE _is_anc ERROR_QUIET)
69+
70+
if(_is_anc GREATER 0)
71+
message(FATAL_ERROR
72+
"Sub-module ${name} at ${_sub_dir}\n"
73+
" HEAD : ${_head}\n"
74+
" baseline : ${_baseline}\n"
75+
"is **older** or on a divergent branch.\n"
76+
"This check can be disabled with: -DENFORCE_SUBMODULE_VERSIONS=OFF")
77+
else()
78+
string(SUBSTRING "${_head}" 0 7 _head_short)
79+
string(SUBSTRING "${_baseline}" 0 7 _base_short)
80+
message(STATUS "Submodule ${name} commit:${_head_short} ≥ baseline:${_base_short}")
81+
endif()
82+
endfunction()

0 commit comments

Comments
 (0)