From 8b61c9dd8f0c0e99bd89389c4f19f186beeef3ef Mon Sep 17 00:00:00 2001
From: Marius Greuel <greuelm@mgtek.com>
Date: Wed, 11 May 2022 20:56:05 +0200
Subject: [PATCH 1/4] CMake: Move MSVC compatibility shim into library

---
 src/CMakeLists.txt | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a8e0262b..7d6771f3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -51,7 +51,7 @@ include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
 add_compile_definitions(CONFIG_DIR=\"${CONFIG_DIR}\")
 
 if(WIN32)
-    set(EXTRA_WINDOWS_SOURCES "${PROJECT_BINARY_DIR}/src/windows.rc")
+    set(EXTRA_WINDOWS_RESOURCES "${PROJECT_BINARY_DIR}/src/windows.rc")
     set(EXTRA_WINDOWS_LIBRARIES setupapi ws2_32)
 endif()
 
@@ -216,6 +216,7 @@ add_library(libavrdude
     xbee.c
     ${FLEX_Parser_OUTPUTS}
     ${BISON_Parser_OUTPUTS}
+    "${EXTRA_WINDOWS_SOURCES}"
     )
     
 set_target_properties(libavrdude PROPERTIES
@@ -253,7 +254,7 @@ add_executable(avrdude
     term.h
     whereami.c
     whereami.h
-    "${EXTRA_WINDOWS_SOURCES}"
+    "${EXTRA_WINDOWS_RESOURCES}"
     )
 
 target_link_libraries(avrdude PUBLIC libavrdude)

From 053c2dcdd3cb24e0778bd54073c7456a66056a68 Mon Sep 17 00:00:00 2001
From: Marius Greuel <greuelm@mgtek.com>
Date: Wed, 11 May 2022 20:57:14 +0200
Subject: [PATCH 2/4] CMake: Include GCC specific options also for MSYS2

---
 src/CMakeLists.txt | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7d6771f3..7577e113 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -55,11 +55,6 @@ if(WIN32)
     set(EXTRA_WINDOWS_LIBRARIES setupapi ws2_32)
 endif()
 
-if(NOT WIN32)
-    set(LIB_MATH m)
-    add_compile_options(-Wall) # -Wextra
-endif()
-
 if(MSVC)
     add_compile_definitions(_CRT_SECURE_NO_WARNINGS=1)
     add_compile_definitions(_CRT_NONSTDC_NO_WARNINGS=1)
@@ -79,6 +74,9 @@ if(MSVC)
     set(EXTRA_WINDOWS_INCLUDES ${EXTRA_WINDOWS_INCLUDES}
         "msvc"
         )
+else()
+    set(LIB_MATH m)
+    add_compile_options(-Wall) # -Wextra
 endif()
 
 # =====================================

From 0ea4b08b2f895ed89dcc0f0557acf9de0bd0dcd3 Mon Sep 17 00:00:00 2001
From: Marius Greuel <greuelm@mgtek.com>
Date: Wed, 11 May 2022 21:02:03 +0200
Subject: [PATCH 3/4] CMake: If installed, use static version of libreadline
 for MSYS2

---
 CMakeLists.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 95c89d2b..cf3623e9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -118,6 +118,7 @@ if(WIN32)
     set(PREFERRED_LIBHIDAPI libhidapi.a libhidapi-libusb.a libhidapi-hidraw.a hidapi hidapi-libusb hidapi-hidraw)
     set(PREFERRED_LIBFTDI libftdi.a ftdi)
     set(PREFERRED_LIBFTDI1 libftdi1.a ftdi1)
+    set(PREFERRED_LIBREADLINE libreadline.a)
 else()
     set(PREFERRED_LIBELF elf)
     set(PREFERRED_LIBUSB usb)
@@ -125,6 +126,7 @@ else()
     set(PREFERRED_LIBHIDAPI hidapi hidapi-libusb hidapi-hidraw)
     set(PREFERRED_LIBFTDI ftdi)
     set(PREFERRED_LIBFTDI1 ftdi1)
+    set(PREFERRED_LIBREADLINE readline)
 endif()
 
 # -------------------------------------
@@ -212,7 +214,7 @@ endif()
 # -------------------------------------
 # Find libreadline
 
-find_library(HAVE_LIBREADLINE NAMES readline)
+find_library(HAVE_LIBREADLINE NAMES ${PREFERRED_LIBREADLINE})
 if(HAVE_LIBREADLINE)
     set(LIB_LIBREADLINE ${HAVE_LIBREADLINE})
 endif()

From 65bb41f8e9bd500d2afe2462a7ff7f93b3c277b7 Mon Sep 17 00:00:00 2001
From: Marius Greuel <greuelm@mgtek.com>
Date: Wed, 11 May 2022 21:08:05 +0200
Subject: [PATCH 4/4] CMake: Add build option to select static or shared
 libraries

---
 CMakeLists.txt | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index cf3623e9..19897c72 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -35,6 +35,13 @@ option(USE_LIBUSBWIN32 "Prefer libusb-win32 over libusb" OFF)
 option(DEBUG_CMAKE "Enable debugging output for this CMake project" OFF)
 option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
 
+if(WIN32)
+    # Prefer static libraries over DLLs on Windows
+    option(USE_STATIC_LIBS "Use static libraries" ON)
+else()
+    option(USE_STATIC_LIBS "Use static libraries" OFF)
+endif()
+
 include(CheckIncludeFile)
 include(CheckSymbolExists)
 include(FetchContent)
@@ -110,8 +117,7 @@ endif()
 # Detect installed libraries
 # =====================================
 
-# Prefer static libraries over DLLs on Windows
-if(WIN32)
+if(USE_STATIC_LIBS)
     set(PREFERRED_LIBELF libelf.a elf)
     set(PREFERRED_LIBUSB libusb.a usb)
     set(PREFERRED_LIBUSB_1_0 libusb-1.0.a usb-1.0)