Skip to content
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

Forward CMake variables to prebuilding dependencies #1161

Merged
merged 31 commits into from
Nov 26, 2024
Merged
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2098e1c
Add cmake function to gather cmd arguments
sfodagain Oct 23, 2024
b107785
Merge branch 'main' into add-cmake-cmd-args
sfod Oct 29, 2024
917e24c
Add VARS_TO_IGNORE
sfodagain Nov 4, 2024
ceed439
Merge branch 'main' into add-cmake-cmd-args
sfodagain Nov 4, 2024
3b06022
Add aws_get_cmd_arguments_for_prebuild_dependency
sfodagain Nov 7, 2024
d1fa8fa
fixup
sfodagain Nov 7, 2024
921950a
Debugging
sfodagain Nov 7, 2024
f2cd1e7
Simplify aws_get_cmd_arguments_for_prebuild_dependency
sfodagain Nov 8, 2024
70ec6a2
Add CMAKE_C_FLAGS, remove COMMAND_ECHO
sfodagain Nov 8, 2024
4e855e9
Restore passing AWS_PREBUILD_CMAKE_ARGUMENTS
sfodagain Nov 8, 2024
4c034b6
Remove separate aws_get_cmd_arguments
sfodagain Nov 8, 2024
058fdaa
Pass CMAKE_GENERATOR
sfodagain Nov 8, 2024
a13540b
Set CMAKE_FIND_ROOT_PATH
sfodagain Nov 8, 2024
9344a01
Rephrase comment
sfodagain Nov 9, 2024
cdb20c2
Do not parse cmd vars
sfodagain Nov 13, 2024
31095b8
Merge branch 'main' into add-cmake-cmd-args
sfodagain Nov 13, 2024
154317e
Merge branch 'main' into add-cmake-cmd-args
sfod Nov 13, 2024
b567baf
Cleanup
sfodagain Nov 14, 2024
f4771da
Check CMAKE_CROSSOMPILING
sfodagain Nov 14, 2024
625ba4f
Check platform
sfodagain Nov 14, 2024
a30b5f5
Check if CMAKE_GENERATOR is set
sfodagain Nov 14, 2024
aeb891e
Rephrase comment
sfodagain Nov 14, 2024
c6cb3cd
Extract cross-compiling vars
sfodagain Nov 14, 2024
2022b24
Fix comment
sfodagain Nov 14, 2024
3944483
fixup
sfodagain Nov 14, 2024
867d0a5
Update cmake/AwsPrebuildDependency.cmake
sfod Nov 25, 2024
9f55dc9
Remove excess var
sfodagain Nov 25, 2024
4e3efd4
Merge branch 'main' into add-cmake-cmd-args
sfodagain Nov 25, 2024
23bae32
Add linker flags
sfodagain Nov 25, 2024
2daea27
Add non-empty vars
sfodagain Nov 25, 2024
8f8067c
Merge branch 'main' into add-cmake-cmd-args
sfod Nov 26, 2024
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
72 changes: 69 additions & 3 deletions cmake/AwsPrebuildDependency.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ function(aws_prebuild_dependency)
set(multiValueArgs CMAKE_ARGUMENTS)
cmake_parse_arguments(AWS_PREBUILD "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(NOT AWS_PREBUILD_DEPENDENCY_NAME)
if (NOT AWS_PREBUILD_DEPENDENCY_NAME)
message(FATAL_ERROR "Missing DEPENDENCY_NAME argument in prebuild_dependency function")
endif()

if(NOT AWS_PREBUILD_SOURCE_DIR)
if (NOT AWS_PREBUILD_SOURCE_DIR)
message(FATAL_ERROR "Missing SOURCE_DIR argument in prebuild_dependency function")
endif()

Expand All @@ -29,18 +29,31 @@ function(aws_prebuild_dependency)
string(REPLACE ";" "\\\\;" ESCAPED_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
# For execute_process to accept a dynamically constructed command, it should be passed in a list format.
set(cmakeCommand "${CMAKE_COMMAND}")

# Get the list of optional and platform-specific variables that may affect build process.
set(cmakeOptionalVariables "")
aws_get_variables_for_prebuild_dependency(cmakeOptionalVariables)
list(APPEND cmakeCommand ${cmakeOptionalVariables})

# The following variables should always be used.
list(APPEND cmakeCommand ${AWS_PREBUILD_SOURCE_DIR})
list(APPEND cmakeCommand -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
list(APPEND cmakeCommand -DCMAKE_PREFIX_PATH=${ESCAPED_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})
# In case a custom generator was provided via -G option. If we don't propagate it, the default value might
# conflict with other cmake options (e.g. CMAKE_MAKE_PROGRAM) or no make program could be found at all.
if (CMAKE_GENERATOR)
list(APPEND cmakeCommand -G${CMAKE_GENERATOR})
endif()

# Append provided arguments to CMake command.
if(AWS_PREBUILD_CMAKE_ARGUMENTS)
if (AWS_PREBUILD_CMAKE_ARGUMENTS)
list(APPEND cmakeCommand ${AWS_PREBUILD_CMAKE_ARGUMENTS})
endif()

message(STATUS "cmake command for dependency ${AWS_PREBUILD_DEPENDENCY_NAME}: ${cmakeCommand}")
# Configure dependency project.
execute_process(
COMMAND ${cmakeCommand}
Expand All @@ -65,6 +78,10 @@ function(aws_prebuild_dependency)
# Make the installation visible for others.
list(INSERT CMAKE_PREFIX_PATH 0 ${depInstallDir}/)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE)
if (CMAKE_CROSSCOMPILING)
list(INSERT CMAKE_FIND_ROOT_PATH 0 ${depInstallDir})
set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} PARENT_SCOPE)
endif()

set(${AWS_PREBUILD_DEPENDENCY_NAME}_PREBUILT TRUE CACHE INTERNAL "Indicate that dependency is built and can be used")

Expand All @@ -75,3 +92,52 @@ function(aws_prebuild_dependency)
DESTINATION ${CMAKE_INSTALL_PREFIX}
)
endfunction()

# Get list of optional or platform-specific variables that may affect build process.
function(aws_get_variables_for_prebuild_dependency AWS_CMAKE_PREBUILD_ARGS)
set(variables "")
set(variablesToIgnore CMAKE_INSTALL_PREFIX)
sfod marked this conversation as resolved.
Show resolved Hide resolved

# The CMake variables below were chosen for Linux, BSD, and Android platforms. If you want to use the prebuild logic
# on other platforms, the chances are you have to handle additional variables (like CMAKE_OSX_SYSROOT). Refer to
# https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html to update the list of handled variables, and
sfod marked this conversation as resolved.
Show resolved Hide resolved
# then you can enable a new platform here.
if (NOT UNIX OR APPLE)
sfod marked this conversation as resolved.
Show resolved Hide resolved
message(FATAL_ERROR "aws_get_variables_for_prebuild_dependency is called for unsupported platform")
endif()

get_cmake_property(vars CACHE_VARIABLES)
foreach(var ${vars})
# Variables in this block make sense only in cross-compiling mode. The variable list is created from the CMake
# documentation on toolchains: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html
# NOTE: Some variables are missed here (e.g. CMAKE_SYSROOT) because they can be set via toolchain file only.
sfod marked this conversation as resolved.
Show resolved Hide resolved
if (CMAKE_CROSSCOMPILING AND (
var STREQUAL "CMAKE_TOOLCHAIN_FILE"
OR var STREQUAL "CMAKE_SYSTEM_NAME"
sfod marked this conversation as resolved.
Show resolved Hide resolved
OR var STREQUAL "CMAKE_SYSTEM_VERSION"
OR var STREQUAL "CMAKE_SYSTEM_PROCESSOR"
# Android-specific variables.
OR var MATCHES "^(CMAKE_)?ANDROID_"))
set(escaped_var ${${var}})
# To store a list within another list, it needs to be escaped first.
string(REPLACE ";" "\\\\;" escapedVar "${${var}}")
list(APPEND variables "-D${var}=${escapedVar}")
endif()

# Other optional variables applicable both in cross-compiling and non-cross-compiling modes.
if (var STREQUAL "CMAKE_C_COMPILER"
OR var STREQUAL "CMAKE_CXX_COMPILER"
OR var STREQUAL "CMAKE_C_FLAGS"
sfod marked this conversation as resolved.
Show resolved Hide resolved
OR var STREQUAL "CMAKE_MAKE_PROGRAM"
OR var STREQUAL "CMAKE_RUNTIME_OUTPUT_DIRECTORY"
OR var STREQUAL "CMAKE_ARCHIVE_OUTPUT_DIRECTORY"
OR var STREQUAL "CMAKE_LIBRARY_OUTPUT_DIRECTORY")
set(escaped_var ${${var}})
# To store a list within another list, it needs to be escaped first.
string(REPLACE ";" "\\\\;" escapedVar "${${var}}")
list(APPEND variables "-D${var}=${escapedVar}")
endif()
endforeach()

set(${AWS_CMAKE_PREBUILD_ARGS} ${variables} PARENT_SCOPE)
endfunction()
Loading