···44endif(COMMAND cmake_policy)
5566include(use_ld64)
77+include(CMakeParseArguments)
7889FUNCTION(add_darling_library name)
910 foreach(f IN LISTS ARGN)
···3637 COMPILE_FLAGS " -B ${CMAKE_BINARY_DIR}/src/external/cctools-port/cctools/misc/ -arch i386 -arch x86_64")
3738ENDFUNCTION(make_fat)
38394040+# add_circular(name ...)
4141+#
4242+# Creates a shared library with circular/mutual dependencies on other libraries.
4343+#
4444+# Typical use: circular dependencies between libs in /usr/lib/system
4545+#
4646+# Parameters:
4747+# * SOURCES: Sources to build the library from. This function will internally create an object library for them.
4848+# * OBJECTS: Object libraries to build the library from. (Because we cannot add object libraries into object libraries.)
4949+# * SIBLINGS: Which "circular" libraries we should link against in the final pass.
5050+# All specified libs must also be built with this function!
5151+# * STRONG_SIBLINGS: Which "circular" libraries we should link against in the 1st pass.
5252+# Only use for special cases, e.g. if this library reexports from this strong siblibg
5353+# and another sibling depends on this reexport's existence.
5454+# * DEPENDENCIES: Which standard libraries we should link against.
5555+# * LINK_FLAGS: Extra linker flags, e.g. an alias list
5656+FUNCTION(add_circular name)
5757+ cmake_parse_arguments(CIRCULAR "FAT" "LINK_FLAGS" "SOURCES;OBJECTS;SIBLINGS;STRONG_SIBLINGS;DEPENDENCIES" ${ARGN})
5858+ #message(STATUS "${name} sources: ${CIRCULAR_SOURCES}")
5959+ #message(STATUS "${name} siblings ${CIRCULAR_SIBLINGS}")
6060+ #message(STATUS "${name} deps: ${CIRCULAR_DEPENDENCIES}")
6161+6262+ set(all_objects "${CIRCULAR_OBJECTS}")
6363+6464+ # First create an object library for all sources with the aim to avoid rebuild
6565+ if (CIRCULAR_SOURCES)
6666+ add_library("${name}_obj" OBJECT ${CIRCULAR_SOURCES})
6767+ if (CIRCULAR_FAT)
6868+ make_fat("${name}_obj")
6969+ endif (CIRCULAR_FAT)
7070+ set(all_objects "${all_objects};$<TARGET_OBJECTS:${name}_obj>")
7171+ endif (CIRCULAR_SOURCES)
7272+7373+ # Then create a shared Darling library, while ignoring all dependencies and using flat namespace
7474+ add_darling_library("${name}_firstpass" SHARED ${all_objects})# $<TARGET_OBJECTS:${name}_obj> ${CIRCULAR_OBJECTS})
7575+ set_property(TARGET "${name}_firstpass" APPEND_STRING PROPERTY LINK_FLAGS " ${CIRCULAR_LINK_FLAGS} -Wl,-flat_namespace -Wl,-undefined,suppress")
7676+7777+ foreach(dep ${CIRCULAR_STRONG_SIBLINGS})
7878+ target_link_libraries("${name}_firstpass" PRIVATE "${dep}_firstpass")
7979+ endforeach(dep)
8080+8181+ if (CIRCULAR_FAT)
8282+ make_fat("${name}_firstpass")
8383+ endif (CIRCULAR_FAT)
8484+8585+ # Then build the final product while linking against firstpass libraries
8686+ add_darling_library(${name} SHARED ${all_objects}) #$<TARGET_OBJECTS:${name}_obj> ${CIRCULAR_OBJECTS})
8787+ set_property(TARGET "${name}" APPEND_STRING PROPERTY LINK_FLAGS " ${CIRCULAR_LINK_FLAGS}")
8888+8989+ foreach(dep ${CIRCULAR_SIBLINGS})
9090+ target_link_libraries("${name}" PRIVATE "${dep}_firstpass")
9191+ endforeach(dep)
9292+ target_link_libraries("${name}" PRIVATE ${CIRCULAR_DEPENDENCIES})
9393+9494+ if (CIRCULAR_FAT)
9595+ make_fat(${name})
9696+ endif (CIRCULAR_FAT)
9797+ENDFUNCTION(add_circular)