cmake: Add MinGW DLL Prefix Workaround

CMake assigns "lib" as the prefix to shared libraries on MinGW
GCC, but MinGW does not expect such a prefix when linking to
DLLs. By setting, CMAKE_SHARED_LIBRARY_PREFIX to "" the
example tests were able to run with the exception of the
calculator_test which segfaults (SIGSEGV) on the setjmp in
test_divide_by_zero. This fault is specific to MinGW and does not
occur within runs on Cygwin or Visual Studio.

calculator_test was enabled only for static linking on MinGW

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4507444..478929d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -44,6 +44,11 @@
 include(ConfigureChecks.cmake)
 configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
 
+# MinGW DLL Naming Workaround
+if (MINGW)
+    set(CMAKE_SHARED_LIBRARY_PREFIX "")
+endif (MINGW)
+
 # check subdirectories
 add_subdirectory(doc)
 add_subdirectory(include)
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index e46a4fc..4951733 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -13,7 +13,10 @@
     PROPERTIES
         COMPILE_DEFINITIONS UNIT_TESTING=1)
 
-if (WIN32)
+
+if (WIN32 OR CYGWIN OR MINGW)
+    set(CMOCKA_DLL_LIB ${CMAKE_BINARY_DIR}/src)
+    file(TO_NATIVE_PATH "${CMOCKA_DLL_PATH}" CMOCKA_DLL_PATH)
     set(DLL_PATH_ENV "${CMOCKA_DLL_PATH};$ENV{PATH}")
 
     #
@@ -22,7 +25,7 @@
     # because of this we must protect the semicolons in the path
     #
     string(REPLACE ";" "\\;" DLL_PATH_ENV "${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
 
 
 ### The most simple test
@@ -30,29 +33,40 @@
 target_link_libraries(simple_test ${CMOCKA_SHARED_LIBRARY})
 
 add_test(simple_test ${CMAKE_CURRENT_BINARY_DIR}/simple_test)
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(simple_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
 
 ### Calulator test
-if (NOT WIN32)
+#TODO investigate dll jmp issue on MinGW
+if (NOT MINGW OR WITH_STATIC_LIB)
     add_executable(calculator_test calculator.c calculator_test.c)
-    target_link_libraries(calculator_test ${CMOCKA_SHARED_LIBRARY})
-
     add_test(calculator_test ${CMAKE_CURRENT_BINARY_DIR}/calculator_test)
-endif (NOT WIN32)
+    if (WIN32 OR CYGWIN)
+        set_tests_properties(calculator_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
+    endif (WIN32 OR CYGWIN)
 
-# FIXME These tests fail on Windows when run with ctest but look correct excuted manually.
-if (NOT WIN32)
+    if (MINGW)
+        target_link_libraries(calculator_test ${CMOCKA_STATIC_LIBRARY})
+    else (MINGW)
+        target_link_libraries(calculator_test ${CMOCKA_SHARED_LIBRARY})
+    endif (MINGW)
+
+    if (WIN32 OR CYGWIN OR MINGW)
+        set_tests_properties(calculator_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
+    endif (WIN32 OR CYGWIN OR MINGW)
+endif (NOT MINGW OR WITH_STATIC_LIB)
+
 ### Allocate module test
 add_executable(allocate_module_test allocate_module.c allocate_module_test.c)
 target_link_libraries(allocate_module_test ${CMOCKA_SHARED_LIBRARY})
 
 # This is a test that should detect leaks and overflows and will fail for that
 add_test(allocate_module_test ${CMAKE_CURRENT_BINARY_DIR}/allocate_module_test)
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(allocate_module_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
+
 set_tests_properties(
     allocate_module_test
         PROPERTIES
@@ -69,9 +83,9 @@
         PROPERTIES
         WILL_FAIL 1
 )
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(assert_macro_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
 
 ### Assert module test
 add_executable(assert_module_test assert_module.c assert_module_test.c)
@@ -83,28 +97,27 @@
         PROPERTIES
         WILL_FAIL 1
 )
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(assert_module_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
-endif (NOT WIN32) # FIXME FAIL
+endif (WIN32 OR CYGWIN OR MINGW)
 
 ### Customer database test
 add_executable(customer_database_test customer_database.c customer_database_test.c)
 target_link_libraries(customer_database_test ${CMOCKA_SHARED_LIBRARY})
 
 add_test(customer_database_test ${CMAKE_CURRENT_BINARY_DIR}/customer_database_test)
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(customer_database_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
 
 ### Key Value Test
 add_executable(key_value_test key_value.c key_value_test.c)
 target_link_libraries(key_value_test ${CMOCKA_SHARED_LIBRARY})
 
 add_test(key_value_test ${CMAKE_CURRENT_BINARY_DIR}/key_value_test)
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(key_value_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
 
 ### Product database test
 add_executable(product_database_test product_database.c product_database_test.c)
@@ -117,9 +130,9 @@
         PASS_REGULAR_EXPRESSION
         "\\[  FAILED  \\] 2 test"
 )
-if (WIN32)
+if (WIN32 OR CYGWIN OR MINGW)
     set_tests_properties(product_database_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR CYGWIN OR MINGW)
 
 # TODO Execute "$CMAKE_LINKER --help" and check for --wrap
 if (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)" AND NOT APPLE)
diff --git a/example/chef_wrap/CMakeLists.txt b/example/chef_wrap/CMakeLists.txt
index 5902438..68afec0 100644
--- a/example/chef_wrap/CMakeLists.txt
+++ b/example/chef_wrap/CMakeLists.txt
@@ -15,6 +15,6 @@
         PROPERTIES
         LINK_FLAGS  "-Wl,--wrap=chef_cook"
 )
-if (WIN32)
+if (WIN32 OR MINGW OR CYGWIN)
     set_tests_properties(waiter_test_wrap PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
-endif (WIN32)
+endif (WIN32 OR MINGW OR CYGWIN)