From f54f5918a164bc522cfc3f05da4662d495669c8b Mon Sep 17 00:00:00 2001 From: Yang Kun <91833768+ikspress@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:47:30 +0800 Subject: [PATCH] Fix compilation issues on Windows --- CMakeLists.txt | 32 ------------- contrib/win32/{nfc.def => libnfc.def.in} | 2 +- contrib/win32/nfc_msvc.def | 57 ------------------------ contrib/win32/stdlib.c | 31 ++++++------- contrib/win32/version.rc.in | 26 +++++------ contrib/windows.h | 40 ++++++++++------- examples/CMakeLists.txt | 10 +++-- libnfc/CMakeLists.txt | 47 +++++++------------ libnfc/drivers/pcsc.c | 4 ++ utils/CMakeLists.txt | 7 ++- 10 files changed, 87 insertions(+), 169 deletions(-) rename contrib/win32/{nfc.def => libnfc.def.in} (97%) delete mode 100644 contrib/win32/nfc_msvc.def diff --git a/CMakeLists.txt b/CMakeLists.txt index a8605fa4..4f2c594d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,30 +102,6 @@ ENDIF(CMAKE_COMPILER_IS_GNUCC) # Workarounds for libusb in C99 ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t) -IF(MINGW) - IF (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") - IF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86") - # force MinGW-w64 in 32bit mode - MESSAGE("Building 32-bit Windows DLL") - #SET(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}") - #SET(CMAKE_MODULE_LINKER_FLAGS "--Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}") - #SET(CMAKE_SHARED_LINKER_FLAGS "--Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}") - #SET(CMAKE_EXE_LINKER_FLAGS "--Wl,--enable-stdcall-fixup ${CMAKE_EXE_LINKER_FLAGS}") - SET(CMAKE_RC_FLAGS "--target=pe-i386 --output-format=coff ${CMAKE_RC_FLAGS}") - ELSEIF((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") OR (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")) - MESSAGE("Building 64-bit Windows DLL") - SET(CMAKE_RC_FLAGS "--target=pe-x86-64 --output-format=coff ${CMAKE_RC_FLAGS}") - ELSE(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86") - MESSAGE(FATAL_ERROR "Unknown Processor: ${CMAKE_SYSTEM_PROCESSOR}") - ENDIF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86") - ENDIF(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") - - FIND_PROGRAM(DLLTOOL dlltool CMAKE_FIND_ROOT_PATH_BOTH) - IF (NOT DLLTOOL) - MESSAGE(FATAL_ERROR "Could not find dlltool command") - ENDIF (NOT DLLTOOL) -ENDIF(MINGW) - IF(NOT WIN32) # Set some pkg-config variables SET(prefix ${CMAKE_INSTALL_PREFIX}) @@ -178,17 +154,9 @@ IF(LIBUSB_INCLUDE_DIRS) SET(LIBUSB_FOUND TRUE) ENDIF(LIBUSB_INCLUDE_DIRS) -# version.rc for Windows IF(WIN32) # Date for filling in rc file information STRING(TIMESTAMP CURRENT_YEAR %Y) - SET(prefix ${CMAKE_INSTALL_PREFIX}) - SET(RC_COMMENT "${PACKAGE_NAME} library") - SET(RC_INTERNAL_NAME "${PACKAGE_NAME} ${WIN32_MODE}") - SET(RC_ORIGINAL_NAME ${PACKAGE_NAME}.dll) - # RC_FILE_TYPE: VFT_DLL - SET(RC_FILE_TYPE 0x00000002L) - CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/windows/libnfc.rc @ONLY) ENDIF(WIN32) ADD_SUBDIRECTORY(libnfc) diff --git a/contrib/win32/nfc.def b/contrib/win32/libnfc.def.in similarity index 97% rename from contrib/win32/nfc.def rename to contrib/win32/libnfc.def.in index 22598172..5f0f6011 100644 --- a/contrib/win32/nfc.def +++ b/contrib/win32/libnfc.def.in @@ -1,5 +1,5 @@ LIBRARY libnfc -VERSION 1.7 +VERSION @VERSION_MAJOR@.@VERSION_MINOR@ EXPORTS nfc_init diff --git a/contrib/win32/nfc_msvc.def b/contrib/win32/nfc_msvc.def deleted file mode 100644 index ec4dc80f..00000000 --- a/contrib/win32/nfc_msvc.def +++ /dev/null @@ -1,57 +0,0 @@ -LIBRARY nfc -VERSION 1.7 - -EXPORTS - nfc_init - nfc_exit - nfc_register_driver - nfc_open - nfc_close - nfc_abort_command - nfc_list_devices - nfc_idle - nfc_initiator_init - nfc_initiator_init_secure_element - nfc_initiator_select_passive_target - nfc_initiator_list_passive_targets - nfc_initiator_poll_target - nfc_initiator_select_dep_target - nfc_initiator_poll_dep_target - nfc_initiator_deselect_target - nfc_initiator_transceive_bytes - nfc_initiator_transceive_bits - nfc_initiator_transceive_bytes_timed - nfc_initiator_transceive_bits_timed - nfc_initiator_target_is_present - nfc_target_init - nfc_target_send_bytes - nfc_target_receive_bytes - nfc_target_send_bits - nfc_target_receive_bits - nfc_strerror - nfc_strerror_r - nfc_perror - nfc_device_get_last_error - nfc_device_get_name - nfc_device_get_connstring - nfc_device_get_supported_modulation - nfc_device_get_supported_baud_rate - nfc_device_get_supported_baud_rate_target_mode - nfc_device_set_property_int - nfc_device_set_property_bool - nfc_emulate_target - iso14443a_crc - iso14443a_crc_append - iso14443b_crc - iso14443b_crc_append - iso14443a_locate_historical_bytes - nfc_free - nfc_version - nfc_device_get_information_about - str_nfc_modulation_type - str_nfc_baud_rate - str_nfc_target - pn53x_transceive - pn532_SAMConfiguration - pn53x_read_register - pn53x_write_register diff --git a/contrib/win32/stdlib.c b/contrib/win32/stdlib.c index 5a1bd560..e1ebe58f 100644 --- a/contrib/win32/stdlib.c +++ b/contrib/win32/stdlib.c @@ -31,27 +31,28 @@ * @brief Windows System compatibility */ -// Handle platform specific includes +#include +#include + #include "contrib/windows.h" -//There is no setenv()and unsetenv() in windows,but we can use putenv() instead. +// Use _putenv_s() as the underlying function to implement setenv() and +// unsetenv() on Windows +// NOTE: unlike POSIX, they return errno instead of -1 when they fail + int setenv(const char *name, const char *value, int overwrite) { - char *env = getenv(name); - if ((env && overwrite) || (!env)) { - char *str[32]; - strcpy(str, name); - strcat(str, "="); - strcat(str, value); - return putenv(str); + if (!overwrite) { + size_t sz; + // Test for existence. + getenv_s(&sz, NULL, 0, name); + if (sz != 0) + return 0; } - return -1; + return _putenv_s(name, value); } -void unsetenv(const char *name) +int unsetenv(const char *name) { - char *str[32]; - strcpy(str, name); - strcat(str, "="); - putenv(str); + return _putenv_s(name, ""); } diff --git a/contrib/win32/version.rc.in b/contrib/win32/version.rc.in index 30f7b838..93d11f2b 100644 --- a/contrib/win32/version.rc.in +++ b/contrib/win32/version.rc.in @@ -1,25 +1,25 @@ +#include + 1 VERSIONINFO FILEVERSION @VERSION_MAJOR@,@VERSION_MINOR@,@VERSION_PATCH@,0 PRODUCTVERSION @VERSION_MAJOR@,@VERSION_MINOR@,@VERSION_PATCH@,0 - FILEFLAGSMASK 0x3fL - FILEFLAGS 0x0L - FILEOS 0x00040004L + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK + FILEFLAGS @RC_FILE_FLAGS@ + FILEOS VOS_NT FILETYPE @RC_FILE_TYPE@ - FILESUBTYPE 0x0L BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040904e4" BEGIN - VALUE "Comments", "@RC_COMMENT@\0" - VALUE "CompanyName", "libnfc.org\0" - VALUE "FileDescription", "\0" - VALUE "FileVersion", "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@.0\0" - VALUE "InternalName", "@RC_INTERNAL_NAME@ @WIN32_MODE@\0" - VALUE "LegalCopyright", "Copyright (C) @CURRENT_YEAR@\0" - VALUE "OriginalFilename", "@RC_ORIGINAL_NAME@\0" - VALUE "ProductName", "@PACKAGE_NAME@ @WIN32_MODE@\0" - VALUE "ProductVersion", "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@.0\0" + VALUE "Comments", "@RC_COMMENT@" + VALUE "CompanyName", "libnfc.org" + VALUE "FileVersion", "@VERSION@" + VALUE "InternalName", "@RC_INTERNAL_NAME@" + VALUE "LegalCopyright", "Copyright (C) @CURRENT_YEAR@" + VALUE "OriginalFilename", "@RC_ORIGINAL_NAME@" + VALUE "ProductName", "@PACKAGE_NAME@ @WIN32_MODE@" + VALUE "ProductVersion", "@VERSION@" END END BLOCK "VarFileInfo" diff --git a/contrib/windows.h b/contrib/windows.h index 3928d6ed..1fb9da54 100644 --- a/contrib/windows.h +++ b/contrib/windows.h @@ -33,32 +33,42 @@ #ifndef __WINDOWS_H__ #define __WINDOWS_H__ -# include -# include -# include "win32/err.h" -# if defined (__MINGW32__) +#define WIN32_LEAN_AND_MEAN +#include +#include + +#if defined(__MINGW32__) + +#if __MINGW64_VERSION_MAJOR < 3 +#include +#define ETIMEDOUT WSAETIMEDOUT +#define ENOTSUP WSAEOPNOTSUPP +#define ECONNABORTED WSAECONNABORTED +#endif + +#if __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) /* * Cheating here on the snprintf to incorporate the format argument * into the VA_ARGS. Else we get MinGW errors regarding number of arguments * if doing a fixed string with no arguments. */ -# define snprintf(S, n, ...) sprintf(S, __VA_ARGS__) -# define pipe(fds) _pipe(fds, 5000, _O_BINARY) -# define ETIMEDOUT WSAETIMEDOUT -# define ENOTSUP WSAEOPNOTSUPP -# define ECONNABORTED WSAECONNABORTED -# else -#ifndef _MSC_VER -# define snprintf sprintf_s +#define snprintf(S, n, ...) sprintf(S, __VA_ARGS__) #endif -# define strdup _strdup -# endif + +#endif + +#if defined(_MSC_VER) && _MSC_VER < 1900 +#define snprintf sprintf_s +#endif + +#define pipe(fds) _pipe(fds, 4096, _O_BINARY) +#define strdup _strdup /* * setenv and unsetenv are not Windows compliant nor implemented in MinGW. * These declarations get rid of the "implicit declaration warning." */ int setenv(const char *name, const char *value, int overwrite); -void unsetenv(const char *name); +int unsetenv(const char *name); #endif diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 01a93c9b..8241c3a0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -24,12 +24,14 @@ FOREACH(source ${EXAMPLES-SOURCES}) SET(RC_COMMENT "${PACKAGE_NAME} example") SET(RC_INTERNAL_NAME ${source}) SET(RC_ORIGINAL_NAME ${source}.exe) - # RC_FILE_TYPE: VFT_APP - SET(RC_FILE_TYPE 0x00000001L) + SET(RC_FILE_TYPE VFT_APP) + SET(RC_FILE_FLAGS 0x0) + IF(LIBNFC_DEBUG_MODE) + SET(RC_FILE_FLAGS VS_FF_DEBUG) + ENDIF(LIBNFC_DEBUG_MODE) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc @ONLY) LIST(APPEND TARGETS ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc) - - + IF(${source} MATCHES "nfc-st25tb") LIST(APPEND TARGETS ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/getopt.c) ENDIF() diff --git a/libnfc/CMakeLists.txt b/libnfc/CMakeLists.txt index d3321778..9f913e11 100644 --- a/libnfc/CMakeLists.txt +++ b/libnfc/CMakeLists.txt @@ -1,13 +1,19 @@ -# Windows MinGW workarounds IF(WIN32) + # Windows workarounds SET(WINDOWS_SOURCES ../contrib/win32/stdlib.c) - INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32) - - # Add in the rc for version information in the dll - LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/../windows/libnfc.rc) - IF (NOT MINGW) - LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/nfc_msvc.def) - ENDIF() + # def and rc files for Windows + SET(RC_COMMENT "${PACKAGE_NAME} library") + SET(RC_INTERNAL_NAME ${PACKAGE_NAME}) + SET(RC_ORIGINAL_NAME ${PACKAGE_NAME}.dll) + SET(RC_FILE_TYPE VFT_DLL) + SET(RC_FILE_FLAGS 0x0) + IF(LIBNFC_DEBUG_MODE) + SET(RC_FILE_FLAGS VS_FF_DEBUG) + ENDIF(LIBNFC_DEBUG_MODE) + CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/libnfc.rc) + CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/libnfc.def.in ${CMAKE_CURRENT_BINARY_DIR}/libnfc.def) + LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.rc) + LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.def) ENDIF(WIN32) # Library's chips @@ -66,9 +72,6 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) IF(LIBNFC_LOG) IF(WIN32) - IF(MINGW) - SET(CMAKE_C_FLAGS "-fgnu89-inline ${CMAKE_C_FLAGS}") - ENDIF(MINGW) LIST(APPEND LIBRARY_SOURCES log.c ../contrib/win32/libnfc/log-internal.c) ELSE(WIN32) LIST(APPEND LIBRARY_SOURCES log.c log-internal.c) @@ -93,24 +96,8 @@ SET_TARGET_PROPERTIES(nfc PROPERTIES SOVERSION 6 VERSION 6.0.0) IF(WIN32) # Libraries that are windows specific TARGET_LINK_LIBRARIES(nfc wsock32) - IF(MINGW) - ADD_CUSTOM_COMMAND( - OUTPUT libnfc.lib - COMMAND ${DLLTOOL} -d ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/nfc.def -l ${CMAKE_CURRENT_BINARY_DIR}/libnfc.lib ${CMAKE_CURRENT_BINARY_DIR}/libnfc.dll - DEPENDS nfc ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/nfc.def - ) - ADD_CUSTOM_TARGET(win32lib ALL DEPENDS libnfc.lib) - ELSE() - ADD_LIBRARY(win32lib ALIAS nfc) - ENDIF() - - # 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) - - # At compile time we need the .LIB file, we place it in the lib directory - INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.lib DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries) -ELSE(WIN32) - INSTALL(TARGETS nfc LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries) + # Ensure consistent lib prefix + SET_TARGET_PROPERTIES(nfc PROPERTIES PREFIX lib) ENDIF(WIN32) +INSTALL(TARGETS nfc LIBRARY COMPONENT libraries) diff --git a/libnfc/drivers/pcsc.c b/libnfc/drivers/pcsc.c index 106d99ac..34d34185 100644 --- a/libnfc/drivers/pcsc.c +++ b/libnfc/drivers/pcsc.c @@ -85,6 +85,10 @@ #define LOG_GROUP NFC_LOG_GROUP_DRIVER #define LOG_CATEGORY "libnfc.driver.pcsc" +#ifndef MAX_ATR_SIZE +#define MAX_ATR_SIZE 33 +#endif + static const char *supported_devices[] = { "ACS ACR122", // ACR122U & Touchatag, last version "ACS ACR 38U-CCID", // Touchatag, early version diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index b9c723ea..03c65e54 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -23,8 +23,11 @@ FOREACH(source ${UTILS-SOURCES}) SET(RC_COMMENT "${PACKAGE_NAME} utility") SET(RC_INTERNAL_NAME ${source}) SET(RC_ORIGINAL_NAME ${source}.exe) - # RC_FILE_TYPE: VFT_APP - SET(RC_FILE_TYPE 0x00000001L) + SET(RC_FILE_TYPE VFT_APP) + SET(RC_FILE_FLAGS 0x0) + IF(LIBNFC_DEBUG_MODE) + SET(RC_FILE_FLAGS VS_FF_DEBUG) + ENDIF(LIBNFC_DEBUG_MODE) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc @ONLY) LIST(APPEND TARGETS ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc) ENDIF(WIN32)