1- cmake_minimum_required (VERSION 3.1 )
1+ cmake_minimum_required (VERSION 3.21 )
22project (aws-crt-kotlin C)
33message (STATUS "CMake ${CMAKE_VERSION} " )
4-
54option (BUILD_DEPS "Builds aws common runtime dependencies as part of build. Turn off if you want to control your dependency chain." ON )
6- option (BUILD_SHARED_LIBS "Build shared library for FFI: default: ON" ON )
5+ option (BUILD_SHARED_LIBS "Build shared library for FFI: default: OFF" OFF )
76
87if (POLICY CMP0069)
98 cmake_policy (SET CMP0069 NEW) # Enable LTO/IPO if available in the compiler
@@ -21,12 +20,20 @@ if (UNIX AND NOT APPLE)
2120 set (FIND_LIBRARY_USE_LIB64_PATHS true )
2221endif ()
2322
23+ # Multi-config generators like Xcode, Visual Studio, and Ninja don't always respect
24+ # CMAKE_BUILD_TYPE. Set the available configuration to the build type.
25+ if (${CMAKE_BUILD_TYPE} STREQUAL "Release" )
26+ set (CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} )
27+ message (STATUS "Setting CMAKE_CONFIGURATION_TYPES = ${CMAKE_CONFIGURATION_TYPES} " )
28+ endif ()
29+
2430# This is required in order to append /lib/cmake to each element in CMAKE_PREFIX_PATH
2531set (AWS_MODULE_DIR "/${CMAKE_INSTALL_LIBDIR} /cmake" )
2632string (REPLACE ";" "${AWS_MODULE_DIR} ;" AWS_MODULE_PATH "${CMAKE_PREFIX_PATH}${AWS_MODULE_DIR} " )
2733# Append that generated list to the module search path
2834list (APPEND CMAKE_MODULE_PATH ${AWS_MODULE_PATH} )
2935
36+ set (ADDITIONAL_DEPS "" )
3037
3138if (BUILD_DEPS)
3239 list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR} /crt/aws-c-common/cmake" )
@@ -60,6 +67,7 @@ if (BUILD_DEPS)
6067 set (DISABLE_GO ON )
6168 set (SEARCH_LIBCRYPTO OFF )
6269 set (BUILD_LIBSSL OFF )
70+ list (APPEND ADDITIONAL_DEPS crypto)
6371 add_subdirectory (crt/aws-lc)
6472 else ()
6573 set (SEARCH_LIBCRYPTO ON )
@@ -74,6 +82,7 @@ if (BUILD_DEPS)
7482 endif ()
7583 endif ()
7684 add_subdirectory (crt/s2n)
85+ list (APPEND ADDITIONAL_DEPS s2n)
7786 endif ()
7887 add_subdirectory (crt/aws-c-sdkutils)
7988 add_subdirectory (crt/aws-c-io)
8796 set (IN_SOURCE_BUILD OFF )
8897endif ()
8998
90- # Restore BUILD_SHARED_LIBS for this project
91- set (BUILD_SHARED_LIBS ${SHARED_FFI_LIB} )
92-
9399include (AwsCFlags)
94100include (AwsSharedLibSetup)
95101include (AwsSanitizers)
@@ -103,7 +109,80 @@ aws_use_package(aws-c-http)
103109aws_use_package(aws-c-auth)
104110aws_use_package(aws-checksums)
105111
106- # TODO - bundling static libraries into single static library composed of all the static libraries
107- # to enable IPO/LTO. See also:
108- # * https://cristianadam.eu/20190501/bundling-together-static-libraries-with-cmake/
109- # * https://cmake.org/cmake/help/latest/module/CheckIPOSupported.html
112+ if (XCODE)
113+ # we only ever build for a single device/simulator and turning this on leaks into generator expressions like `$<TARGET_FILE:${tgt}>`
114+ # and breaks for ios/tvos/watchos builds
115+ set_property (GLOBAL PROPERTY XCODE_EMIT_EFFECTIVE_PLATFORM_NAME OFF )
116+ endif ()
117+
118+ # Create a single static library that combines all the individual libs
119+ # We do this to work around issues with link order when including multiple
120+ # `staticLibraries` in the cinterop file.
121+ function (bundle_static_library name deps)
122+ set (tgt_full_name ${CMAKE_BINARY_DIR} /${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX} )
123+
124+ if (XCODE)
125+ find_program (lib_tool libtool)
126+ foreach (tgt IN LISTS deps)
127+ list (APPEND static_libs_full_names $<TARGET_FILE:${tgt} >)
128+ endforeach ()
129+
130+ add_custom_command (
131+ COMMAND
132+ ${lib_tool} -static -o ${tgt_full_name} ${static_libs_full_names}
133+ OUTPUT
134+ ${tgt_full_name}
135+ COMMENT
136+ "Bundling ${name} "
137+ VERBATIM
138+ COMMAND_EXPAND_LISTS
139+ )
140+
141+ elseif (CMAKE_C_COMPILER_ID MATCHES "^(Clang|GNU)$" )
142+ # archive tool needs to support -M flag
143+ file (WRITE ${CMAKE_BINARY_DIR} /${name} .ar.in "CREATE ${tgt_full_name} \n " )
144+
145+ foreach (tgt IN LISTS deps)
146+ message ("tgt=${tgt} " )
147+ file (APPEND ${CMAKE_BINARY_DIR} /${name} .ar.in "ADDLIB $<TARGET_FILE:${tgt} >\n " )
148+ endforeach ()
149+
150+ file (APPEND ${CMAKE_BINARY_DIR} /${name} .ar.in "SAVE\n " )
151+ file (APPEND ${CMAKE_BINARY_DIR} /${name} .ar.in "END\n " )
152+
153+ file (
154+ GENERATE
155+ OUTPUT ${CMAKE_BINARY_DIR} /${name} .ar
156+ INPUT ${CMAKE_BINARY_DIR} /${name} .ar.in
157+ )
158+
159+ add_custom_command (
160+ COMMAND ${CMAKE_AR} -M < ${CMAKE_BINARY_DIR} /${name} .ar
161+ OUTPUT ${tgt_full_name}
162+ COMMENT "Bundling ${name} "
163+ VERBATIM
164+ )
165+ else ()
166+ message (FATAL_ERROR "Unknown bundling!" )
167+ endif ()
168+
169+ add_custom_target (bundling_target ALL DEPENDS ${tgt_full_name} )
170+ foreach (tgt IN LISTS deps)
171+ message ("adding dependency on ${tgt} " )
172+ add_dependencies (bundling_target ${tgt} )
173+ endforeach ()
174+
175+ add_library (${name} STATIC IMPORTED )
176+ set_target_properties (${name}
177+ PROPERTIES
178+ IMPORTED_LOCATION ${tgt_full_name}
179+ INTERFACE_INCLUDE_DIRECTORIES $<TARGET_PROPERTY:${name} ,INTERFACE_INCLUDE_DIRECTORIES >)
180+ add_dependencies (${name} bundling_target)
181+
182+ install (FILES ${tgt_full_name} TYPE LIB)
183+ endfunction ()
184+
185+ list (APPEND STATIC_LIBS ${DEP_AWS_LIBS} ${ADDITIONAL_DEPS} )
186+ bundle_static_library(aws-crt-kotlin "${STATIC_LIBS} " )
187+
188+ message ("CMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID} " )
0 commit comments