From 8c3534f11244a60cc1817754fc24e66386db535c Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 7 Aug 2024 13:38:01 -0700 Subject: [PATCH 1/4] Implement AwsPrebuildDependency cmake module --- cmake/AwsPrebuildDependency.cmake | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 cmake/AwsPrebuildDependency.cmake diff --git a/cmake/AwsPrebuildDependency.cmake b/cmake/AwsPrebuildDependency.cmake new file mode 100644 index 000000000..b0d1f0f39 --- /dev/null +++ b/cmake/AwsPrebuildDependency.cmake @@ -0,0 +1,69 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0. + +# Build given dependency project during CMake configuration step and install it into CMAKE_BINARY_DIR. +# Arguments: +# DEPENDENCY_NAME Project name that should be built and installed. +# SOURCE_DIR Path to the project. +# CMAKE_ARGUMENTS Additional arguments that will be passed to cmake command. +function(prebuild_dependency) + set(oneValueArgs DEPENDENCY_NAME SOURCE_DIR) + set(multiValueArgs CMAKE_ARGUMENTS) + cmake_parse_arguments(AWS_PREBUILD "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT AWS_PREBUILD_DEPENDENCY_NAME) + message(FATAL_ERROR "Missing DEPENDENCY_NAME argument in prebuild_dependency function") + endif() + + if(NOT AWS_PREBUILD_SOURCE_DIR) + message(FATAL_ERROR "Missing SOURCE_DIR argument in prebuild_dependency function") + endif() + + set(depBinaryDir ${CMAKE_BINARY_DIR}/deps/${AWS_PREBUILD_DEPENDENCY_NAME}) + set(depInstallDir ${depBinaryDir}/install) + file(MAKE_DIRECTORY ${depBinaryDir}) + + # For execute_process to accept a dynamically constructed command, it should be passed in a list format. + set(cmakeCommand "COMMAND" "${CMAKE_COMMAND}") + list(APPEND cmakeCommand -S ${AWS_PREBUILD_SOURCE_DIR}) + list(APPEND cmakeCommand -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}) + list(APPEND cmakeCommand -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}) + list(APPEND cmakeCommand -DCMAKE_INSTALL_PREFIX=${depInstallDir}) + list(APPEND cmakeCommand -DCMAKE_INSTALL_RPATH=${CMAKE_INSTALL_RPATH}) + list(APPEND cmakeCommand -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}) + + # Append provided arguments to CMake command. + if(AWS_PREBUILD_CMAKE_ARGUMENTS) + list(APPEND cmakeCommand ${AWS_PREBUILD_CMAKE_ARGUMENTS}) + endif() + + list(APPEND cmakeCommand WORKING_DIRECTORY ${depBinaryDir}) + list(APPEND cmakeCommand RESULT_VARIABLE result) + + # Configure dependency project. + execute_process(${cmakeCommand}) + if (NOT ${result} EQUAL 0) + message(FATAL_ERROR "Configuration failed for dependency project ${AWS_PREBUILD_DEPENDENCY_NAME}") + endif() + + # Build and install dependency project into depInstallDir directory. + execute_process( + COMMAND ${CMAKE_COMMAND} --build . --target install + WORKING_DIRECTORY ${depBinaryDir} + RESULT_VARIABLE result + ) + if (NOT ${result} EQUAL 0) + message(FATAL_ERROR "Build failed for dependency project ${AWS_PREBUILD_DEPENDENCY_NAME}") + endif() + + # Make the installation visible for others. + list(PREPEND CMAKE_PREFIX_PATH ${depInstallDir}/) + set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE) + + # Generates installation rules for the dependency project. + # On installing targets, CMake will just copy this prebuilt version to a designated installation directory. + install( + DIRECTORY ${depInstallDir}/ + DESTINATION ${CMAKE_INSTALL_PREFIX} + ) +endfunction() From a4ef400b199c9b0c12832a2f4676af503f7ef204 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Thu, 8 Aug 2024 10:50:59 -0700 Subject: [PATCH 2/4] Set cache variable --- cmake/AwsPrebuildDependency.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmake/AwsPrebuildDependency.cmake b/cmake/AwsPrebuildDependency.cmake index b0d1f0f39..95028f439 100644 --- a/cmake/AwsPrebuildDependency.cmake +++ b/cmake/AwsPrebuildDependency.cmake @@ -6,6 +6,8 @@ # DEPENDENCY_NAME Project name that should be built and installed. # SOURCE_DIR Path to the project. # CMAKE_ARGUMENTS Additional arguments that will be passed to cmake command. +# +# Set ${DEPENDENCY_NAME}-PREBUILT variable on success. function(prebuild_dependency) set(oneValueArgs DEPENDENCY_NAME SOURCE_DIR) set(multiValueArgs CMAKE_ARGUMENTS) @@ -60,6 +62,8 @@ function(prebuild_dependency) list(PREPEND CMAKE_PREFIX_PATH ${depInstallDir}/) set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE) + set(${AWS_PREBUILD_DEPENDENCY_NAME}-PREBUILT TRUE CACHE INTERNAL "Indicate that dependency is built and can be used") + # Generates installation rules for the dependency project. # On installing targets, CMake will just copy this prebuilt version to a designated installation directory. install( From 51145744229dd40ae9ebba0122cf6f912f6f3bf2 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Thu, 8 Aug 2024 14:59:35 -0700 Subject: [PATCH 3/4] Use list insert --- cmake/AwsPrebuildDependency.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/AwsPrebuildDependency.cmake b/cmake/AwsPrebuildDependency.cmake index 95028f439..f48a83f83 100644 --- a/cmake/AwsPrebuildDependency.cmake +++ b/cmake/AwsPrebuildDependency.cmake @@ -59,7 +59,7 @@ function(prebuild_dependency) endif() # Make the installation visible for others. - list(PREPEND CMAKE_PREFIX_PATH ${depInstallDir}/) + list(INSERT CMAKE_PREFIX_PATH 0 ${depInstallDir}/) set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE) set(${AWS_PREBUILD_DEPENDENCY_NAME}-PREBUILT TRUE CACHE INTERNAL "Indicate that dependency is built and can be used") From d6004d3337c43275115bfddae289b787ebbf4d55 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Fri, 9 Aug 2024 10:43:18 -0700 Subject: [PATCH 4/4] Use underscore for prebuilt var --- cmake/AwsPrebuildDependency.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/AwsPrebuildDependency.cmake b/cmake/AwsPrebuildDependency.cmake index f48a83f83..66a97b270 100644 --- a/cmake/AwsPrebuildDependency.cmake +++ b/cmake/AwsPrebuildDependency.cmake @@ -7,7 +7,7 @@ # SOURCE_DIR Path to the project. # CMAKE_ARGUMENTS Additional arguments that will be passed to cmake command. # -# Set ${DEPENDENCY_NAME}-PREBUILT variable on success. +# Set ${DEPENDENCY_NAME}_PREBUILT variable on success. function(prebuild_dependency) set(oneValueArgs DEPENDENCY_NAME SOURCE_DIR) set(multiValueArgs CMAKE_ARGUMENTS) @@ -62,7 +62,7 @@ function(prebuild_dependency) list(INSERT CMAKE_PREFIX_PATH 0 ${depInstallDir}/) set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE) - set(${AWS_PREBUILD_DEPENDENCY_NAME}-PREBUILT TRUE CACHE INTERNAL "Indicate that dependency is built and can be used") + set(${AWS_PREBUILD_DEPENDENCY_NAME}_PREBUILT TRUE CACHE INTERNAL "Indicate that dependency is built and can be used") # Generates installation rules for the dependency project. # On installing targets, CMake will just copy this prebuilt version to a designated installation directory.