@@ -319,10 +319,65 @@ if(EMSCRIPTEN)
319319 COMMAND ${CMAKE_COMMAND} -E echo " and can be run using: emrun --browser chrome ${JAVASCRIPT_BINDINGS_DIR} /index.html" )
320320 endif ()
321321else ()
322+ # If we are building a static version of libOpenCOR then we want to extract the object files from our various
323+ # packages and merge them into libOpenCOR. Indeed, a static library doesn't include its dependencies (it "only"
324+ # references them). So, we need to merge those dependencies into libOpenCOR and, sadly, there is no (easy?) way to
325+ # do this using CMake, so we must do it ourselves. Now, one (big!) downside of this approach is that it blows up the
326+ # size of libOpenCOR since it also merges parts of our dependencies that we don't actually need. So much so that we
327+ # cannot generate a debug version of libOpenCOR on Windows since it would result in a static library that is more
328+ # than 4GB in size (and MSVC's linker cannot create static libraries that big). This is clearly far from ideal, but
329+ # this is the only way to have a static version of libOpenCOR that can be used without having to "manually" link
330+ # against all of its dependencies.
331+
332+ if (NOT LIBOPENCOR_SHARED_LIBS)
333+ # Retrieve the path to our various packages.
334+
335+ set (PACKAGES_DIR ${CMAKE_BINARY_DIR} /packages)
336+
337+ file (GLOB_RECURSE PACKAGES "${PREBUILT_DIR} /*${CMAKE_STATIC_LIBRARY_SUFFIX} " )
338+
339+ # Extract the object files from our various packages.
340+
341+ set (PACKAGES_OBJECT_FILES)
342+
343+ foreach (PACKAGE ${PACKAGES} )
344+ get_filename_component (PACKAGE_NAME ${PACKAGE} NAME_WE )
345+
346+ set (PACKAGE_DIR ${PACKAGES_DIR} /${PACKAGE_NAME} )
347+
348+ file (MAKE_DIRECTORY ${PACKAGE_DIR} )
349+
350+ if (WIN32 )
351+ execute_process (COMMAND ${CMAKE_AR} /NOLOGO /LIST ${PACKAGE}
352+ OUTPUT_VARIABLE PACKAGE_FILES
353+ OUTPUT_STRIP_TRAILING_WHITESPACE )
354+
355+ string (REPLACE "\n " ";" PACKAGE_FILES ${PACKAGE_FILES} )
356+
357+ foreach (PACKAGE_FILE ${PACKAGE_FILES} )
358+ string (REGEX MATCH ".*\\ ${CMAKE_CXX_OUTPUT_EXTENSION} $" IS_OBJECT_FILE ${PACKAGE_FILE} )
359+
360+ if (IS_OBJECT_FILE)
361+ execute_process (COMMAND ${CMAKE_AR} /NOLOGO /EXTRACT:${PACKAGE_FILE} ${PACKAGE}
362+ WORKING_DIRECTORY ${PACKAGE_DIR} )
363+ endif ()
364+ endforeach ()
365+ else ()
366+ execute_process (COMMAND ${CMAKE_AR} -x ${PACKAGE}
367+ WORKING_DIRECTORY ${PACKAGE_DIR} )
368+ endif ()
369+
370+ file (GLOB_RECURSE PACKAGE_OBJECT_FILES ${PACKAGE_DIR} /*${CMAKE_CXX_OUTPUT_EXTENSION} )
371+
372+ list (APPEND PACKAGES_OBJECT_FILES ${PACKAGE_OBJECT_FILES} )
373+ endforeach ()
374+ endif ()
375+
322376 add_library (${CMAKE_PROJECT_NAME}
323377 ${API_HEADER_FILES}
324378 ${SOURCE_FILES}
325- ${HEADER_FILES} )
379+ ${HEADER_FILES}
380+ ${PACKAGES_OBJECT_FILES} )
326381
327382 set_target_properties (${CMAKE_PROJECT_NAME} PROPERTIES
328383 CXX_VISIBILITY_PRESET hidden
0 commit comments