From f3ce0fdb33f5c70b549730093d187d1abb19893e Mon Sep 17 00:00:00 2001 From: lazymio Date: Sat, 30 Apr 2022 00:25:27 +0200 Subject: [PATCH] Generate both shared lib and static archive by default --- CMakeLists.txt | 72 ++++++++++++++++++++++++++++++++++----------- bundle_static.cmake | 4 +-- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3710e80e..c2835bd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,11 @@ set(UNICORN_VERSION_PATCH 0) include(bundle_static.cmake) +# Even though we generate shared lib and static archive at the same time, we still support +# using unicorn as a subdirectory so we have to respect BUILD_SHARED_LIBS. +# +# Also we would like users to link a native cmake target, instead of a custom target for better +# compatability. option(BUILD_SHARED_LIBS "Build shared instead of static library" ${PROJECT_IS_TOP_LEVEL}) option(UNICORN_FUZZ "Enable fuzzing" OFF) option(UNICORN_BUILD_TESTS "Build unicorn tests" ${PROJECT_IS_TOP_LEVEL}) @@ -1157,14 +1162,12 @@ if(NOT MSVC AND NOT ANDROID_ABI) target_link_libraries(unicorn-common PRIVATE pthread) endif() +add_library(unicorn ${UNICORN_SRCS}) +# For static archive if (BUILD_SHARED_LIBS) - set(UNICORN_LIB_NAME "unicorn") -else() - set(UNICORN_LIB_NAME "unicorn-static") # This static lib is useless and it's just an intermediate target + add_library(unicorn_static STATIC ${UNICORN_SRCS}) endif() -add_library(${UNICORN_LIB_NAME} ${UNICORN_SRCS}) - if(BUILD_SHARED_LIBS) if(ANDROID_ABI) file(APPEND ${CMAKE_BINARY_DIR}/adb.sh "adb push ./libunicorn.so /data/local/tmp/build/\n") @@ -1263,10 +1266,17 @@ target_compile_options(unicorn-common PRIVATE ${UNICORN_COMPILE_OPTIONS} ) -target_compile_options(${UNICORN_LIB_NAME} PRIVATE +target_compile_options(unicorn PRIVATE ${UNICORN_COMPILE_OPTIONS} ) +# For static archive +if (BUILD_SHARED_LIBS) + target_compile_options(unicorn_static PRIVATE + ${UNICORN_COMPILE_OPTIONS} + ) +endif() + if(MINGW) set(UNICORN_LINK_LIBRARIES ${UNICORN_LINK_LIBRARIES} pthread) endif() @@ -1277,29 +1287,46 @@ endif() if(MSVC) if(BUILD_SHARED_LIBS) - target_compile_options(${UNICORN_LIB_NAME} PRIVATE + target_compile_options(unicorn PRIVATE -DUNICORN_SHARED ) + + # For static archive + target_link_libraries(unicorn_static PRIVATE + ${UNICORN_LINK_LIBRARIES} + ) endif() - target_link_libraries(${UNICORN_LIB_NAME} PRIVATE + target_link_libraries(unicorn PRIVATE ${UNICORN_LINK_LIBRARIES} ) - set_target_properties(${UNICORN_LIB_NAME} PROPERTIES + set_target_properties(unicorn PROPERTIES VERSION "${UNICORN_VERSION_MAJOR}.${UNICORN_VERSION_MINOR}" ) else() - target_link_libraries(${UNICORN_LIB_NAME} PRIVATE + target_link_libraries(unicorn PRIVATE ${UNICORN_LINK_LIBRARIES} m ) - target_link_libraries(${UNICORN_LIB_NAME} PUBLIC + target_link_libraries(unicorn PUBLIC m ) - set_target_properties(${UNICORN_LIB_NAME} PROPERTIES + # For static archive + if (BUILD_SHARED_LIBS) + target_link_libraries(unicorn_static PUBLIC + m + ) + + target_link_libraries(unicorn_static PRIVATE + ${UNICORN_LINK_LIBRARIES} + m + ) + endif() + + set_target_properties(unicorn PROPERTIES VERSION ${UNICORN_VERSION_MAJOR} SOVERSION ${UNICORN_VERSION_MAJOR} ) @@ -1320,12 +1347,24 @@ else() ) endif() -target_include_directories(${UNICORN_LIB_NAME} PUBLIC +target_include_directories(unicorn PUBLIC include ) -if (NOT BUILD_SHARED_LIBS) - bundle_static_library(${UNICORN_LIB_NAME} unicorn) # Bundle our real unicorn static lib +# For static archive +if (BUILD_SHARED_LIBS) + target_include_directories(unicorn_static PUBLIC + include() + ) +endif() + +# Black magic for generating static archives... +if (BUILD_SHARED_LIBS) + bundle_static_library(unicorn_static unicorn_archive unicorn) +else() + # Rename the "static" lib to avoid filename clash. + set_target_properties(unicorn PROPERTIES OUTPUT_NAME "unicorn-static") + bundle_static_library(unicorn unicorn_archive unicorn) endif() if(UNICORN_FUZZ) @@ -1381,9 +1420,8 @@ if(UNICORN_INSTALL AND NOT MSVC) ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) - else() - install(FILES $ DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() + install(FILES $ DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${UNICORN_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/unicorn) file(WRITE ${CMAKE_BINARY_DIR}/unicorn.pc "Name: unicorn\n\ Description: Unicorn emulator engine\n\ diff --git a/bundle_static.cmake b/bundle_static.cmake index 6288186d..4bae6cf6 100644 --- a/bundle_static.cmake +++ b/bundle_static.cmake @@ -1,5 +1,5 @@ # https://cristianadam.eu/20190501/bundling-together-static-libraries-with-cmake/ -function(bundle_static_library tgt_name bundled_tgt_name) +function(bundle_static_library tgt_name bundled_tgt_name library_name) list(APPEND static_libs ${tgt_name}) set(dep_libs "") @@ -41,7 +41,7 @@ function(bundle_static_library tgt_name bundled_tgt_name) list(REMOVE_DUPLICATES dep_libs) set(bundled_tgt_full_name - ${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX}) + ${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${library_name}${CMAKE_STATIC_LIBRARY_SUFFIX}) if (APPLE) find_program(lib_tool libtool)