From f391b7138b51e8e72a6b04432b8f7689c7757c46 Mon Sep 17 00:00:00 2001
From: Marius Greuel <greuelm@mgtek.com>
Date: Sun, 26 Dec 2021 16:40:28 +0100
Subject: [PATCH 1/2] Improve CMake project for FreeBSD builds

---
 src/CMakeLists.txt    | 15 +++++++++++++--
 src/cmake_config.h.in |  2 --
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 56f73091..7860d281 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -48,15 +48,19 @@ set(AVRDUDE_EXTERNAL_PATH "${PROJECT_SOURCE_DIR}/external")
 find_package(FLEX)
 if(FLEX_FOUND)
     FLEX_TARGET(Parser lexer.l "${PROJECT_BINARY_DIR}/lexer.c")
+
+    if (FLEX_VERSION VERSION_GREATER_EQUAL 2.5.9)
+        set(HAVE_YYLEX_DESTROY 1)
+    endif()
 else()
-    message(SEND_ERROR "This CMake project requires flex, which is not installed on your system." )
+    message(SEND_ERROR "This CMake project requires 'flex', which is not installed on your system." )
 endif()
 
 find_package(BISON)
 if(BISON_FOUND)
     BISON_TARGET(Parser config_gram.y "${PROJECT_BINARY_DIR}/config_gram.c" DEFINES_FILE "${PROJECT_BINARY_DIR}/config_gram.h")
 else()
-    message(SEND_ERROR "This CMake project requires bison, which is not installed on your system." )
+    message(SEND_ERROR "This CMake project requires 'bison', which is not installed on your system." )
 endif()
 
 # =====================================
@@ -121,6 +125,11 @@ if(HAVE_LIBUSB_1_0)
     set(LIB_LIBUSB_1_0 ${HAVE_LIBUSB_1_0})
 endif()
 
+# FreeBSD's library 'libusb' supports both the libusb-0.1 and libusb-1.0 API.
+if (HAVE_LIBUSB AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+    set(HAVE_LIBUSB_1_0 ${HAVE_LIBUSB})
+endif()
+
 find_library(HAVE_LIBUSB_WIN32 NAMES libusb0.a usb0)
 
 if(HAVE_LIBUSB OR HAVE_LIBUSB_1_0 OR HAVE_LIBUSB_WIN32)
@@ -182,6 +191,8 @@ endif()
 # Setup target specific options
 # =====================================
 
+add_compile_definitions(CONFIG_DIR=\"${CONFIG_DIR}\")
+
 if(WIN32)
     set(HAVE_LIBWS2_32 1)
     set(EXTRA_WINDOWS_SOURCES "${PROJECT_BINARY_DIR}/windows.rc")
diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in
index 533c8475..9643546e 100644
--- a/src/cmake_config.h.in
+++ b/src/cmake_config.h.in
@@ -22,8 +22,6 @@
 
 #define VERSION "@PROJECT_VERSION@"
 
-#define CONFIG_DIR "${CONFIG_DIR}"
-
 /* Options */
 
 /* Linux sysfs GPIO support enabled */

From 3840468e36bc24ea7092cc3762c06e880dbdaed6 Mon Sep 17 00:00:00 2001
From: Marius Greuel <greuelm@mgtek.com>
Date: Sun, 26 Dec 2021 16:46:15 +0100
Subject: [PATCH 2/2] Improve CMake project to filter conditional sections

---
 src/CMakeLists.txt | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7860d281..6dfebd28 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -329,8 +329,22 @@ message(STATUS "----------------------")
 # Configure files
 # =====================================
 
+macro(configure_option option)
+    if(${${option}})
+        string(REGEX REPLACE "(.*)@${option}_BEGIN@(.*)@${option}_END@(.*)" "\\1\\2\\3" conf_file "${conf_file}")
+    else()
+        string(REGEX REPLACE "(.*)@${option}_BEGIN@(.*)@${option}_END@(.*)" "\\1\\3" conf_file "${conf_file}")
+    endif()
+endmacro()
+
+file(READ avrdude.conf.in conf_file)
+configure_option(HAVE_PARPORT)
+configure_option(HAVE_LINUXGPIO)
+configure_option(HAVE_LINUXSPI)
+file(WRITE "${PROJECT_BINARY_DIR}/avrdude.conf.in" "${conf_file}")
+
 configure_file(cmake_config.h.in ac_cfg.h)
-configure_file(avrdude.conf.in avrdude.conf)
+configure_file("${PROJECT_BINARY_DIR}/avrdude.conf.in" avrdude.conf)
 configure_file(avrdude.spec.in avrdude.spec)
 if(WIN32)
     configure_file(windows.rc.in windows.rc)