Debian Codegraph @

crt/aws-c-common/cmake/AwsPrebuildDependency.cmake

raw
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 # Build given dependency project during CMake configuration step and install it into CMAKE_BINARY_DIR.
5 # Arguments:
6 # DEPENDENCY_NAME Project name that should be built and installed.
7 # SOURCE_DIR Path to the project.
8 # CMAKE_ARGUMENTS Additional arguments that will be passed to cmake command.
9 #
10 # Set ${DEPENDENCY_NAME}_PREBUILT variable on success.
11 function(aws_prebuild_dependency)
12 set(oneValueArgs DEPENDENCY_NAME SOURCE_DIR)
13 set(multiValueArgs CMAKE_ARGUMENTS)
14 cmake_parse_arguments(AWS_PREBUILD "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
15
16 if (NOT AWS_PREBUILD_DEPENDENCY_NAME)
17 message(FATAL_ERROR "Missing DEPENDENCY_NAME argument in prebuild_dependency function")
18 endif()
19
20 if (NOT AWS_PREBUILD_SOURCE_DIR)
21 message(FATAL_ERROR "Missing SOURCE_DIR argument in prebuild_dependency function")
22 endif()
23
24 set(depBinaryDir ${CMAKE_BINARY_DIR}/deps/${AWS_PREBUILD_DEPENDENCY_NAME})
25 set(depInstallDir ${depBinaryDir}/install)
26 file(MAKE_DIRECTORY ${depBinaryDir})
27
28 # Convert prefix path from list to escaped string, to be passed on command line
29 string(REPLACE ";" "\\\\;" ESCAPED_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
30 # For execute_process to accept a dynamically constructed command, it should be passed in a list format.
31 set(cmakeCommand "${CMAKE_COMMAND}")
32
33 # Get the list of optional and platform-specific variables that may affect build process.
34 set(cmakeOptionalVariables "")
35 aws_get_variables_for_prebuild_dependency(cmakeOptionalVariables)
36 list(APPEND cmakeCommand ${cmakeOptionalVariables})
37
38 # The following variables should always be used.
39 list(APPEND cmakeCommand -H${AWS_PREBUILD_SOURCE_DIR})
40 list(APPEND cmakeCommand -B${depBinaryDir})
41 list(APPEND cmakeCommand -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
42 list(APPEND cmakeCommand -DCMAKE_PREFIX_PATH=${ESCAPED_PREFIX_PATH})
43 list(APPEND cmakeCommand -DCMAKE_INSTALL_PREFIX=${depInstallDir})
44 list(APPEND cmakeCommand -DCMAKE_INSTALL_RPATH=${CMAKE_INSTALL_RPATH})
45 list(APPEND cmakeCommand -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS})
46 # In case a custom generator was provided via -G option. If we don't propagate it, the default value might
47 # conflict with other cmake options (e.g. CMAKE_MAKE_PROGRAM) or no make program could be found at all.
48 if (CMAKE_GENERATOR)
49 list(APPEND cmakeCommand -G${CMAKE_GENERATOR})
50 endif()
51
52 # Append provided arguments to CMake command.
53 if (AWS_PREBUILD_CMAKE_ARGUMENTS)
54 list(APPEND cmakeCommand ${AWS_PREBUILD_CMAKE_ARGUMENTS})
55 endif()
56
57 message(STATUS "cmake command for dependency ${AWS_PREBUILD_DEPENDENCY_NAME}: ${cmakeCommand}")
58 # Configure dependency project.
59 execute_process(
60 COMMAND ${cmakeCommand}
61 RESULT_VARIABLE result
62 )
63
64 if (NOT ${result} EQUAL 0)
65 message(FATAL_ERROR "Configuration failed for dependency project ${AWS_PREBUILD_DEPENDENCY_NAME}")
66 endif()
67
68 # Build and install dependency project into depInstallDir directory.
69 execute_process(
70 COMMAND ${CMAKE_COMMAND} --build ${depBinaryDir} --target install
71 RESULT_VARIABLE result
72 )
73 if (NOT ${result} EQUAL 0)
74 message(FATAL_ERROR "Build failed for dependency project ${AWS_PREBUILD_DEPENDENCY_NAME}")
75 endif()
76
77 # Make the installation visible for others.
78 list(INSERT CMAKE_PREFIX_PATH 0 ${depInstallDir}/)
79 set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE)
80 if (CMAKE_CROSSCOMPILING)
81 list(INSERT CMAKE_FIND_ROOT_PATH 0 ${depInstallDir})
82 set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} PARENT_SCOPE)
83 endif()
84
85 set(${AWS_PREBUILD_DEPENDENCY_NAME}_PREBUILT TRUE CACHE INTERNAL "Indicate that dependency is built and can be used")
86
87 # Generates installation rules for the dependency project.
88 # On installing targets, CMake will just copy this prebuilt version to a designated installation directory.
89 install(
90 DIRECTORY ${depInstallDir}/
91 DESTINATION ${CMAKE_INSTALL_PREFIX}
92 )
93 endfunction()
94
95 # Get list of optional or platform-specific variables that may affect build process.
96 function(aws_get_variables_for_prebuild_dependency AWS_CMAKE_PREBUILD_ARGS)
97 set(variables "")
98
99
100 # The variables below were chosen based on reading docs and a lot of experimenting on different platforms.
101 # Is the list of variables below exhaustive and guaranteed to cover all cases? Probably not.
102 # So if you are here reading this, then its more than likely that some variable got missed and something
103 # is not building correctly. So don't be afraid to add more variables to the list.
104 # Refer to https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html as a decent starting point for which variables to handle.
105
106 get_cmake_property(vars CACHE_VARIABLES)
107 foreach(var ${vars})
108 # Variables in this block make sense only in cross-compiling mode. The variable list is created from the CMake
109 # documentation on toolchains: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html
110 # NOTE: Some variables are missed here (e.g. CMAKE_SYSROOT) because they can be set via toolchain file only.
111 if (CMAKE_CROSSCOMPILING AND (
112 var STREQUAL "CMAKE_TOOLCHAIN_FILE"
113 OR var STREQUAL "CMAKE_SYSTEM_NAME"
114 OR var STREQUAL "CMAKE_SYSTEM_VERSION"
115 OR var STREQUAL "CMAKE_SYSTEM_PROCESSOR"
116 # Android-specific variables.
117 OR var MATCHES "^(CMAKE_)?ANDROID_"))
118 # To store a list within another list, it needs to be escaped first.
119 string(REPLACE ";" "\\\\;" escapedVar "${${var}}")
120 if (escapedVar)
121 list(APPEND variables "-D${var}=${escapedVar}")
122 endif()
123 endif()
124
125 # Other optional variables applicable both in cross-compiling and non-cross-compiling modes.
126 # Refer to https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html for each variable description.
127 if (var STREQUAL "CMAKE_C_COMPILER"
128 OR var MATCHES "^CMAKE_C_FLAGS(_DEBUG|_RELEASE|_RELWITHDEBINFO|_MINSIZEREL)?"
129 OR var STREQUAL "CMAKE_CXX_COMPILER"
130 OR var MATCHES "^CMAKE_CXX_FLAGS(_DEBUG|_RELEASE|_RELWITHDEBINFO|_MINSIZEREL)?"
131 OR var STREQUAL "CMAKE_LINKER_TYPE"
132 OR var MATCHES "^CMAKE_EXE_LINKER_FLAGS(_DEBUG|_RELEASE|_RELWITHDEBINFO|_MINSIZEREL)?"
133 OR var MATCHES "^CMAKE_MODULE_LINKER_FLAGS(_DEBUG|_RELEASE|_RELWITHDEBINFO|_MINSIZEREL)?"
134 OR var MATCHES "^CMAKE_STATIC_LINKER_FLAGS(_DEBUG|_RELEASE|_RELWITHDEBINFO|_MINSIZEREL)?"
135 OR var MATCHES "^CMAKE_SHARED_LINKER_FLAGS(_DEBUG|_RELEASE|_RELWITHDEBINFO|_MINSIZEREL)?"
136 OR var STREQUAL "CMAKE_MAKE_PROGRAM"
137 OR var MATCHES "^CMAKE_RUNTIME_OUTPUT_DIRECTORY"
138 OR var MATCHES "^CMAKE_ARCHIVE_OUTPUT_DIRECTORY"
139 OR var MATCHES "^CMAKE_LIBRARY_OUTPUT_DIRECTORY"
140 OR var STREQUAL "CMAKE_OSX_ARCHITECTURES"
141 OR var STREQUAL "CMAKE_OSX_DEPLOYMENT_TARGET"
142 OR var STREQUAL "CMAKE_OSX_SYSROOT"
143 OR var MATCHES "^CMAKE_GENERATOR"
144 )
145 # To store a list within another list, it needs to be escaped first.
146 string(REPLACE ";" "\\\\;" escapedVar "${${var}}")
147 if (escapedVar)
148 list(APPEND variables "-D${var}=${escapedVar}")
149 endif()
150 endif()
151 endforeach()
152
153 set(${AWS_CMAKE_PREBUILD_ARGS} ${variables} PARENT_SCOPE)
154 endfunction()