Skip to content

Commit

Permalink
Fix compilation issues on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ikspress committed Jun 22, 2024
1 parent df0a3be commit 58e2e3d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 171 deletions.
32 changes: 0 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion contrib/win32/nfc.def → contrib/win32/libnfc.def.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
LIBRARY libnfc
VERSION 1.7
VERSION @VERSION_MAJOR@.@VERSION_MINOR@

EXPORTS
nfc_init
Expand Down
57 changes: 0 additions & 57 deletions contrib/win32/nfc_msvc.def

This file was deleted.

31 changes: 16 additions & 15 deletions contrib/win32/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,28 @@
* @brief Windows System compatibility
*/

// Handle platform specific includes
#include <stddef.h>
#include <stdlib.h>

#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, "");
}
26 changes: 13 additions & 13 deletions contrib/win32/version.rc.in
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#include <winver.h>

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@.@[email protected]\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@.@[email protected]\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"
Expand Down
40 changes: 25 additions & 15 deletions contrib/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,42 @@
#ifndef __WINDOWS_H__
#define __WINDOWS_H__

# include <windows.h>
# include <winerror.h>
# include "win32/err.h"
# if defined (__MINGW32__)
#define WIN32_LEAN_AND_MEAN
#include <fcntl.h>
#include <windows.h>

#if defined(__MINGW32__)

#if __MINGW64_VERSION_MAJOR < 3
#include <winerror.h>
#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
10 changes: 6 additions & 4 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
48 changes: 16 additions & 32 deletions libnfc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -88,29 +91,10 @@ IF(LIBRT_FOUND)
TARGET_LINK_LIBRARIES(nfc ${LIBRT_LIBRARIES})
ENDIF(LIBRT_FOUND)

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)
ENDIF(WIN32)

SET_TARGET_PROPERTIES(nfc PROPERTIES PREFIX "lib" SOVERSION 6 VERSION 6.0.0)
INSTALL(TARGETS nfc LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)
4 changes: 4 additions & 0 deletions libnfc/drivers/pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 58e2e3d

Please sign in to comment.