@@ -177,6 +177,94 @@ macro(install_graphar_target target)
177177 )
178178endmacro ()
179179
180+ # Implementations of lisp "car" and "cdr" functions
181+ macro (GRAPHAR_CAR var)
182+ set (${var} ${ARGV1} )
183+ endmacro ()
184+
185+ macro (GRAPHAR_CDR var rest)
186+ set (${var} ${ARGN} )
187+ endmacro ()
188+
189+ # Based on MIT-licensed
190+ # https://gist.github.com/cristianadam/ef920342939a89fae3e8a85ca9459b49
191+ function (graphar_create_merged_static_lib output_target)
192+ set (options )
193+ set (one_value_args NAME ROOT)
194+ set (multi_value_args TO_MERGE)
195+ cmake_parse_arguments (ARG
196+ "${options} "
197+ "${one_value_args} "
198+ "${multi_value_args} "
199+ ${ARGN} )
200+ if (ARG_UNPARSED_ARGUMENTS)
201+ message (SEND_ERROR "Error: unrecognized arguments: ${ARG_UNPARSED_ARGUMENTS} " )
202+ endif ()
203+
204+ set (output_lib_path
205+ ${BUILD_OUTPUT_ROOT_DIRECTORY}${CMAKE_STATIC_LIBRARY_PREFIX}${ARG_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}
206+ )
207+
208+ set (all_library_paths $<TARGET_FILE:${ARG_ROOT} >)
209+ foreach (lib ${ARG_TO_MERGE} )
210+ list (APPEND all_library_paths $<TARGET_FILE:${lib} >)
211+ endforeach ()
212+
213+ if (APPLE )
214+ set (BUNDLE_COMMAND "libtool" "-no_warning_for_no_symbols" "-static" "-o"
215+ ${output_lib_path} ${all_library_paths} )
216+ elseif (CMAKE_CXX_COMPILER_ID MATCHES "^(Clang|GNU|Intel)$" )
217+ set (ar_script_path ${CMAKE_BINARY_DIR} /${ARG_NAME} .ar)
218+
219+ file (WRITE ${ar_script_path} .in "CREATE ${output_lib_path} \n " )
220+ file (APPEND ${ar_script_path} .in "ADDLIB $<TARGET_FILE:${ARG_ROOT} >\n " )
221+
222+ foreach (lib ${ARG_TO_MERGE} )
223+ file (APPEND ${ar_script_path} .in "ADDLIB $<TARGET_FILE:${lib} >\n " )
224+ endforeach ()
225+
226+ file (APPEND ${ar_script_path} .in "SAVE\n END\n " )
227+ file (GENERATE
228+ OUTPUT ${ar_script_path}
229+ INPUT ${ar_script_path} .in)
230+ set (ar_tool ${CMAKE_AR} )
231+
232+ if (CMAKE_INTERPROCEDURAL_OPTIMIZATION)
233+ set (ar_tool ${CMAKE_CXX_COMPILER_AR} )
234+ endif ()
235+
236+ set (BUNDLE_COMMAND ${ar_tool} -M < ${ar_script_path} )
237+
238+ elseif (MSVC )
239+ if (CMAKE_LIBTOOL)
240+ set (BUNDLE_TOOL ${CMAKE_LIBTOOL} )
241+ else ()
242+ find_program (BUNDLE_TOOL lib HINTS "${CMAKE_CXX_COMPILER} /.." )
243+ if (NOT BUNDLE_TOOL)
244+ message (FATAL_ERROR "Cannot locate lib.exe to bundle libraries" )
245+ endif ()
246+ endif ()
247+ set (BUNDLE_COMMAND ${BUNDLE_TOOL} /NOLOGO /OUT:${output_lib_path}
248+ ${all_library_paths} )
249+ else ()
250+ message (FATAL_ERROR "Unknown bundle scenario!" )
251+ endif ()
252+
253+ add_custom_target (${output_target} _merge ALL
254+ ${BUNDLE_COMMAND}
255+ DEPENDS ${ARG_ROOT} ${ARG_TO_MERGE}
256+ BYPRODUCTS ${output_lib_path}
257+ COMMENT "Bundling ${output_lib_path} "
258+ VERBATIM )
259+
260+ message (STATUS "Creating bundled static library target ${output_target} at ${output_lib_path} "
261+ )
262+
263+ add_library (${output_target} STATIC IMPORTED )
264+ set_target_properties (${output_target} PROPERTIES IMPORTED_LOCATION ${output_lib_path} )
265+ add_dependencies (${output_target} ${output_target} _merge)
266+ endfunction ()
267+
180268macro (build_graphar)
181269 file (GLOB_RECURSE CORE_SRC_FILES "src/graphar/*.cc" ${CMAKE_CURRENT_SOURCE_DIR} /thirdparty/mini-yaml/yaml/*.cpp)
182270 if (GRAPHAR_BUILD_STATIC)
@@ -231,21 +319,40 @@ macro(build_graphar_with_arrow_bundled)
231319 target_include_directories (graphar SYSTEM BEFORE PRIVATE ${GAR_ARROW_INCLUDE_DIR} )
232320 target_link_libraries (graphar PRIVATE ${CMAKE_DL_LIBS} )
233321
322+ set (GAR_BUNDLED_DEPS_STATIC_LIBS)
323+ list (APPEND GAR_BUNDLED_DEPS_STATIC_LIBS
324+ gar_arrow_static
325+ gar_parquet_static
326+ gar_dataset_static
327+ gar_acero_static
328+ gar_arrow_bundled_dependencies_static)
329+ graphar_car(_FIRST_LIB ${GAR_BUNDLED_DEPS_STATIC_LIBS} )
330+ graphar_cdr(_OTHER_LIBS ${GAR_BUNDLED_DEPS_STATIC_LIBS} )
331+
332+ graphar_create_merged_static_lib(graphar_bundled_dependencies
333+ NAME
334+ graphar_bundled_dependencies
335+ ROOT
336+ ${_FIRST_LIB}
337+ TO_MERGE
338+ ${_OTHER_LIBS} )
339+ # We can't use install(TARGETS) here because
340+ # graphar_bundled_dependencies is an IMPORTED library.
341+ get_target_property (graphar_bundled_dependencies_path graphar_bundled_dependencies
342+ IMPORTED_LOCATION )
343+ install (FILES ${CMAKE_BINARY_DIR} /${graphar_bundled_dependencies_path} ${INSTALL_IS_OPTIONAL}
344+ DESTINATION ${CMAKE_INSTALL_LIBDIR} )
345+ string (APPEND ARROW_PC_LIBS_PRIVATE " -lgraphar_bundled_dependencies" )
346+ list (INSERT ARROW_STATIC_INSTALL_INTERFACE_LIBS 0 "graphar_bundled_dependencies" )
347+
234348 if (APPLE )
235- message (STATUS "Linking arrow bundled dependencies " ${GAR_PARQUET_STATIC_LIB} ${GAR_ACERO_STATIC_LIB} )
236- target_link_libraries (graphar PRIVATE -Wl,-force_load gar_arrow_static
237- "${GAR_PARQUET_STATIC_LIB} "
238- "${GAR_DATASET_STATIC_LIB} "
239- "${GAR_ARROW_ACERO_STATIC_LIB} "
240- "${GAR_ARROW_BUNDLED_DEPS_STATIC_LIB} "
349+ target_link_libraries (graphar PRIVATE -Wl,-force_load
350+ graphar_bundled_dependencies
241351 "-framework CoreFoundation"
242352 "-framework Security" )
243353 else ()
244- target_link_libraries (graphar PRIVATE -Wl,--exclude -libs,ALL -Wl,--whole-archive gar_arrow_static
245- "${GAR_PARQUET_STATIC_LIB} "
246- "${GAR_DATASET_STATIC_LIB} "
247- "${GAR_ARROW_ACERO_STATIC_LIB} "
248- "${GAR_ARROW_BUNDLED_DEPS_STATIC_LIB} " -Wl,--no -whole-archive)
354+ target_link_libraries (graphar PRIVATE -Wl,--exclude -libs,ALL -Wl,--whole-archive
355+ graphar_bundled_dependencies -Wl,--no -whole-archive)
249356 endif ()
250357
251358 # if OpenSSL library exists, link the OpenSSL library.
@@ -266,7 +373,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/src)
266373include_directories (src)
267374
268375if (BUILD_ARROW_FROM_SOURCE)
269- # the nessary dependencies for building arrow from source
376+ # the necessary dependencies for building arrow from source
270377 find_package (OpenSSL REQUIRED)
271378 if (OPENSSL_FOUND)
272379 if (OPENSSL_VERSION LESS "1.1.0" )
@@ -334,11 +441,11 @@ if (BUILD_EXAMPLES)
334441 if (BUILD_ARROW_FROM_SOURCE)
335442 target_include_directories (${E_NAME} SYSTEM BEFORE PRIVATE ${GAR_ARROW_INCLUDE_DIR} )
336443 if (APPLE )
337- target_link_libraries (${E_NAME} PRIVATE -Wl,-force_load gar_arrow_static
444+ target_link_libraries (${E_NAME} PRIVATE -Wl,-force_load ${GAR_ARROW_STATIC_LIB}
338445 "${GAR_PARQUET_STATIC_LIB} "
339446 "${GAR_ARROW_BUNDLED_DEPS_STATIC_LIB} " )
340447 else ()
341- target_link_libraries (${E_NAME} PRIVATE -Wl,--exclude -libs,ALL -Wl,--whole-archive gar_arrow_static
448+ target_link_libraries (${E_NAME} PRIVATE -Wl,--exclude -libs,ALL -Wl,--whole-archive ${GAR_ARROW_STATIC_LIB}
342449 "${GAR_PARQUET_STATIC_LIB} "
343450 "${GAR_ARROW_BUNDLED_DEPS_STATIC_LIB} " -Wl,--no -whole-archive)
344451 endif ()
0 commit comments