From c990334e17e81c9d2dbac842ea60bf1b9bcb2541 Mon Sep 17 00:00:00 2001 From: mcuee Date: Tue, 25 Oct 2022 13:48:54 +0800 Subject: [PATCH] Try pull request #960 for static and shared build Try the changes from https://github.com/avrdudes/avrdude/pull/960 --- src/CMakeLists.txt | 106 ++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 29 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ca6cb64f5..4ea58726c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,9 +47,13 @@ endif() # Setup target specific options # ===================================== -include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) add_compile_definitions(CONFIG_DIR=\"${CONFIG_DIR}\") +include_directories( + BEFORE ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} + ) + if(WIN32) set(EXTRA_WINDOWS_RESOURCES "${PROJECT_BINARY_DIR}/src/windows.rc") set(EXTRA_WINDOWS_LIBRARIES setupapi ws2_32) @@ -132,7 +136,25 @@ add_custom_target(conf ALL DEPENDS avrdude.conf) # Project # ===================================== -add_library(libavrdude +set(includes + "${PROJECT_SOURCE_DIR}" + "${PROJECT_BINARY_DIR}" + "${LIBUSB_COMPAT_DIR}" + "${EXTRA_WINDOWS_INCLUDES}" + ) +set(deplibs + ${LIB_MATH} + ${LIB_LIBELF} + ${LIB_LIBUSB} + ${LIB_LIBUSB_1_0} + ${LIB_LIBHID} + ${LIB_LIBHIDAPI} + ${LIB_LIBFTDI} + ${LIB_LIBFTDI1} + ${LIB_LIBREADLINE} + ${EXTRA_WINDOWS_LIBRARIES} + ) +set(sources ac_cfg.h arduino.h arduino.c @@ -242,35 +264,57 @@ add_library(libavrdude "${EXTRA_WINDOWS_SOURCES}" ) -set_target_properties(libavrdude PROPERTIES - PREFIX "" - PUBLIC_HEADER "libavrdude.h" - VERSION 1.0.0 - SOVERSION 1 - ) +if (MSVC) + set(BUILT_LIBS staticlib) + add_library(staticlib STATIC ${sources}) + set_target_properties(staticlib PROPERTIES + PREFIX "" + OUTPUT_NAME "libavrdude" + PUBLIC_HEADER "libavrdude.h" + ) + target_include_directories(staticlib PUBLIC ${includes}) + target_link_libraries(staticlib PUBLIC ${deplibs}) +else() + # build objects + add_library(objlib OBJECT ${sources}) + target_include_directories(objlib PUBLIC ${includes}) + set_target_properties(objlib PROPERTIES + POSITION_INDEPENDENT_CODE true) -target_include_directories(libavrdude - PUBLIC - "${PROJECT_SOURCE_DIR}" - "${PROJECT_BINARY_DIR}" - "${LIBUSB_COMPAT_DIR}" - "${EXTRA_WINDOWS_INCLUDES}" - ) + # staticlib + set(BUILT_LIBS staticlib) + add_library(staticlib STATIC $) + set_target_properties(staticlib PROPERTIES + PREFIX "" + OUTPUT_NAME "libavrdude" + PUBLIC_HEADER "libavrdude.h" + ) + target_include_directories(staticlib PUBLIC ${includes}) + target_link_libraries(staticlib PUBLIC ${deplibs}) -target_link_libraries(libavrdude - PUBLIC - ${LIB_MATH} - ${LIB_LIBELF} - ${LIB_LIBUSB} - ${LIB_LIBUSB_1_0} - ${LIB_LIBHID} - ${LIB_LIBHIDAPI} - ${LIB_LIBFTDI} - ${LIB_LIBFTDI1} - ${LIB_LIBREADLINE} - ${EXTRA_WINDOWS_LIBRARIES} - ) + # sharedlib + if(NOT WIN32) # DLL cannot have undefined references + set(BUILT_LIBS staticlib sharedlib) + add_library(sharedlib SHARED $) + set_target_properties(sharedlib PROPERTIES + PREFIX "" + OUTPUT_NAME "libavrdude" + PUBLIC_HEADER "libavrdude.h" + VERSION 1.0.0 + SOVERSION 1 + ) + target_include_directories(sharedlib PUBLIC ${includes}) + target_link_libraries(sharedlib PUBLIC ${deplibs}) + if (APPLE) + target_link_options(sharedlib + PRIVATE -undefined + PRIVATE dynamic_lookup + ) + endif() + endif() +endif() +# executable add_executable(avrdude main.c term.c @@ -285,7 +329,11 @@ add_executable(avrdude "${EXTRA_WINDOWS_RESOURCES}" ) -target_link_libraries(avrdude PUBLIC libavrdude) +if (sharedlib IN_LIST BUILT_LIBS) + target_link_libraries(avrdude PUBLIC sharedlib) +else() + target_link_libraries(avrdude PUBLIC staticlib) +endif() # ===================================== # Install