From 21302147a9d6716aa098a2064132ee0ed8f834d9 Mon Sep 17 00:00:00 2001
From: Alex Lian <alian@alum.mit.edu>
Date: Sat, 26 Jan 2013 18:43:58 -0500
Subject: [PATCH] Add CMake options for Win32 to require PCRE

---
 CMakeLists.txt               |  9 ++++++++
 cmake/modules/FindPCRE.cmake | 40 ++++++++++++++++++++++++++++++++++++
 cmake/modules/Makefile.am    |  1 +
 libnfc/CMakeLists.txt        | 14 +++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 cmake/modules/FindPCRE.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1bce778..8e3aa50 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -88,6 +88,15 @@ IF(NOT WIN32)
   INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
 ENDIF(NOT WIN32)
 
+# Require PCRE for Win32
+IF (WIN32)
+  FIND_PACKAGE(PCRE REQUIRED)
+  IF(PCRE_INCLUDE_DIRS)
+    INCLUDE_DIRECTORIES(${PCRE_INCLUDE_DIRS})
+    LINK_DIRECTORIES(${PCRE_LIBRARY_DIRS})
+  ENDIF(PCRE_INCLUDE_DIRS)
+ENDIF(WIN32)
+
 IF(PCSC_INCLUDE_DIRS)
   INCLUDE_DIRECTORIES(${PCSC_INCLUDE_DIRS})
   LINK_DIRECTORIES(${PCSC_LIBRARY_DIRS})
diff --git a/cmake/modules/FindPCRE.cmake b/cmake/modules/FindPCRE.cmake
new file mode 100644
index 0000000..0b83def
--- /dev/null
+++ b/cmake/modules/FindPCRE.cmake
@@ -0,0 +1,40 @@
+# This CMake script wants to use pcre functionality needed for windows 
+# compilation. However, since PCRE isn't really a default install location
+# there isn't much to search.
+#
+# Operating Systems Supported:
+# - Windows (requires MinGW)
+#   Tested with Windows XP/Windows 7
+#
+# This should work for both 32 bit and 64 bit systems.
+#
+# Author: A. Lian <alex.lian@gmail.com>
+#
+
+IF(WIN32)
+  IF(NOT PCRE_FOUND)
+    FIND_PATH(PCRE_INCLUDE_DIRS regex.h)
+    FIND_LIBRARY(PCRE_LIBRARIES NAMES PCRE pcre)
+
+    IF(PCRE_INCLUDE_DIRS AND PCRE_LIBRARIES)
+       SET(PCRE_FOUND TRUE)
+    ENDIF(PCRE_INCLUDE_DIRS AND PCRE_LIBRARIES)
+  ENDIF(NOT PCRE_FOUND)
+
+  IF(PCRE_FOUND)
+    IF(NOT PCRE_FIND_QUIETLY)
+      MESSAGE(STATUS "Found PCRE: ${PCRE_LIBRARIES} ${PCRE_INCLUDE_DIRS}")
+    ENDIF (NOT PCRE_FIND_QUIETLY)
+  ELSE(PCRE_FOUND)
+    IF(PCRE_FIND_REQUIRED)
+      MESSAGE(FATAL_ERROR "Could not find PCRE")
+    ENDIF(PCRE_FIND_REQUIRED)
+  ENDIF(PCRE_FOUND)
+
+  INCLUDE(FindPackageHandleStandardArgs)
+  FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE DEFAULT_MSG
+    PCRE_LIBRARIES
+    PCRE_INCLUDE_DIRS
+  )
+
+ENDIF(WIN32)
diff --git a/cmake/modules/Makefile.am b/cmake/modules/Makefile.am
index 52fd92d..55334ca 100644
--- a/cmake/modules/Makefile.am
+++ b/cmake/modules/Makefile.am
@@ -2,5 +2,6 @@ EXTRA_DIST = \
 	COPYING-CMAKE-SCRIPTS \
 	FindLIBUSB.cmake \
 	FindPCSC.cmake \
+	FindPCRE.cmake \
 	UseDoxygen.cmake \
 	LibnfcDrivers.cmake
diff --git a/libnfc/CMakeLists.txt b/libnfc/CMakeLists.txt
index 491f324..9edac93 100644
--- a/libnfc/CMakeLists.txt
+++ b/libnfc/CMakeLists.txt
@@ -8,6 +8,16 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/buses)
 
 INCLUDE(LibnfcDrivers)
 
+IF(WIN32)
+  # Windows now requires regex, so we utilize PCRE
+  # since Windows doesn't get the benefit of finding in CMake
+  # it has to be added manually
+  IF(PCRE_FOUND)
+    INCLUDE_DIRECTORIES(${PCRE_INCLUDE_DIRS})
+    LINK_DIRECTORIES(${PCRE_LIBRARY_DIRS})
+  ENDIF(PCRE_FOUND)
+ENDIF(WIN32)
+
 IF(PCSC_FOUND)
   INCLUDE_DIRECTORIES(${PCSC_INCLUDE_DIRS})
   LINK_DIRECTORIES(${PCSC_LIBRARY_DIRS})
@@ -40,6 +50,10 @@ ENDIF(WIN32)
 SET_TARGET_PROPERTIES(nfc PROPERTIES SOVERSION 0)
 
 IF(WIN32)
+  IF(PCRE_FOUND)
+    TARGET_LINK_LIBRARIES(nfc ${PCRE_LIBRARIES})
+  ENDIF(PCRE_FOUND)
+
   # On Windows the shared (runtime) library should be either in the same 
   # directory as the excutables or in the path, we add it to same directory
   INSTALL(TARGETS nfc RUNTIME DESTINATION bin COMPONENT libraries)