Skip to content

Commit 087e24e

Browse files
committed
Fix compilation issues on Windows
1 parent df0a3be commit 087e24e

File tree

10 files changed

+87
-169
lines changed

10 files changed

+87
-169
lines changed

CMakeLists.txt

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,6 @@ ENDIF(CMAKE_COMPILER_IS_GNUCC)
102102
# Workarounds for libusb in C99
103103
ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t)
104104

105-
IF(MINGW)
106-
IF (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
107-
IF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
108-
# force MinGW-w64 in 32bit mode
109-
MESSAGE("Building 32-bit Windows DLL")
110-
#SET(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}")
111-
#SET(CMAKE_MODULE_LINKER_FLAGS "--Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
112-
#SET(CMAKE_SHARED_LINKER_FLAGS "--Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
113-
#SET(CMAKE_EXE_LINKER_FLAGS "--Wl,--enable-stdcall-fixup ${CMAKE_EXE_LINKER_FLAGS}")
114-
SET(CMAKE_RC_FLAGS "--target=pe-i386 --output-format=coff ${CMAKE_RC_FLAGS}")
115-
ELSEIF((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") OR (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64"))
116-
MESSAGE("Building 64-bit Windows DLL")
117-
SET(CMAKE_RC_FLAGS "--target=pe-x86-64 --output-format=coff ${CMAKE_RC_FLAGS}")
118-
ELSE(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
119-
MESSAGE(FATAL_ERROR "Unknown Processor: ${CMAKE_SYSTEM_PROCESSOR}")
120-
ENDIF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
121-
ENDIF(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
122-
123-
FIND_PROGRAM(DLLTOOL dlltool CMAKE_FIND_ROOT_PATH_BOTH)
124-
IF (NOT DLLTOOL)
125-
MESSAGE(FATAL_ERROR "Could not find dlltool command")
126-
ENDIF (NOT DLLTOOL)
127-
ENDIF(MINGW)
128-
129105
IF(NOT WIN32)
130106
# Set some pkg-config variables
131107
SET(prefix ${CMAKE_INSTALL_PREFIX})
@@ -178,17 +154,9 @@ IF(LIBUSB_INCLUDE_DIRS)
178154
SET(LIBUSB_FOUND TRUE)
179155
ENDIF(LIBUSB_INCLUDE_DIRS)
180156

181-
# version.rc for Windows
182157
IF(WIN32)
183158
# Date for filling in rc file information
184159
STRING(TIMESTAMP CURRENT_YEAR %Y)
185-
SET(prefix ${CMAKE_INSTALL_PREFIX})
186-
SET(RC_COMMENT "${PACKAGE_NAME} library")
187-
SET(RC_INTERNAL_NAME "${PACKAGE_NAME} ${WIN32_MODE}")
188-
SET(RC_ORIGINAL_NAME ${PACKAGE_NAME}.dll)
189-
# RC_FILE_TYPE: VFT_DLL
190-
SET(RC_FILE_TYPE 0x00000002L)
191-
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/windows/libnfc.rc @ONLY)
192160
ENDIF(WIN32)
193161

194162
ADD_SUBDIRECTORY(libnfc)

contrib/win32/nfc.def renamed to contrib/win32/libnfc.def.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
LIBRARY libnfc
2-
VERSION 1.7
2+
VERSION @VERSION_MAJOR@.@VERSION_MINOR@
33

44
EXPORTS
55
nfc_init

contrib/win32/nfc_msvc.def

Lines changed: 0 additions & 57 deletions
This file was deleted.

contrib/win32/stdlib.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,28 @@
3131
* @brief Windows System compatibility
3232
*/
3333

34-
// Handle platform specific includes
34+
#include <stddef.h>
35+
#include <stdlib.h>
36+
3537
#include "contrib/windows.h"
3638

37-
//There is no setenv()and unsetenv() in windows,but we can use putenv() instead.
39+
// Use _putenv_s() as the underlying function to implement setenv() and
40+
// unsetenv() on Windows
41+
// NOTE: unlike POSIX, they return errno instead of -1 when they fail
42+
3843
int setenv(const char *name, const char *value, int overwrite)
3944
{
40-
char *env = getenv(name);
41-
if ((env && overwrite) || (!env)) {
42-
char *str[32];
43-
strcpy(str, name);
44-
strcat(str, "=");
45-
strcat(str, value);
46-
return putenv(str);
45+
if (!overwrite) {
46+
size_t sz;
47+
// Test for existence.
48+
getenv_s(&sz, NULL, 0, name);
49+
if (sz != 0)
50+
return 0;
4751
}
48-
return -1;
52+
return _putenv_s(name, value);
4953
}
5054

51-
void unsetenv(const char *name)
55+
int unsetenv(const char *name)
5256
{
53-
char *str[32];
54-
strcpy(str, name);
55-
strcat(str, "=");
56-
putenv(str);
57+
return _putenv_s(name, "");
5758
}

contrib/win32/version.rc.in

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
#include <winver.h>
2+
13
1 VERSIONINFO
24
FILEVERSION @VERSION_MAJOR@,@VERSION_MINOR@,@VERSION_PATCH@,0
35
PRODUCTVERSION @VERSION_MAJOR@,@VERSION_MINOR@,@VERSION_PATCH@,0
4-
FILEFLAGSMASK 0x3fL
5-
FILEFLAGS 0x0L
6-
FILEOS 0x00040004L
6+
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
7+
FILEFLAGS @RC_FILE_FLAGS@
8+
FILEOS VOS_NT
79
FILETYPE @RC_FILE_TYPE@
8-
FILESUBTYPE 0x0L
910
BEGIN
1011
BLOCK "StringFileInfo"
1112
BEGIN
1213
BLOCK "040904e4"
1314
BEGIN
14-
VALUE "Comments", "@RC_COMMENT@\0"
15-
VALUE "CompanyName", "libnfc.org\0"
16-
VALUE "FileDescription", "\0"
17-
VALUE "FileVersion", "@VERSION_MAJOR@.@VERSION_MINOR@.@[email protected]\0"
18-
VALUE "InternalName", "@RC_INTERNAL_NAME@ @WIN32_MODE@\0"
19-
VALUE "LegalCopyright", "Copyright (C) @CURRENT_YEAR@\0"
20-
VALUE "OriginalFilename", "@RC_ORIGINAL_NAME@\0"
21-
VALUE "ProductName", "@PACKAGE_NAME@ @WIN32_MODE@\0"
22-
VALUE "ProductVersion", "@VERSION_MAJOR@.@VERSION_MINOR@.@[email protected]\0"
15+
VALUE "Comments", "@RC_COMMENT@"
16+
VALUE "CompanyName", "libnfc.org"
17+
VALUE "FileVersion", "@VERSION@"
18+
VALUE "InternalName", "@RC_INTERNAL_NAME@"
19+
VALUE "LegalCopyright", "Copyright (C) @CURRENT_YEAR@"
20+
VALUE "OriginalFilename", "@RC_ORIGINAL_NAME@"
21+
VALUE "ProductName", "@PACKAGE_NAME@ @WIN32_MODE@"
22+
VALUE "ProductVersion", "@VERSION@"
2323
END
2424
END
2525
BLOCK "VarFileInfo"

contrib/windows.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,42 @@
3333
#ifndef __WINDOWS_H__
3434
#define __WINDOWS_H__
3535

36-
# include <windows.h>
37-
# include <winerror.h>
38-
# include "win32/err.h"
39-
# if defined (__MINGW32__)
36+
#define WIN32_LEAN_AND_MEAN
37+
#include <fcntl.h>
38+
#include <windows.h>
39+
40+
#if defined(__MINGW32__)
41+
42+
#if __MINGW64_VERSION_MAJOR < 3
43+
#include <winerror.h>
44+
#define ETIMEDOUT WSAETIMEDOUT
45+
#define ENOTSUP WSAEOPNOTSUPP
46+
#define ECONNABORTED WSAECONNABORTED
47+
#endif
48+
49+
#if __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT)
4050
/*
4151
* Cheating here on the snprintf to incorporate the format argument
4252
* into the VA_ARGS. Else we get MinGW errors regarding number of arguments
4353
* if doing a fixed string with no arguments.
4454
*/
45-
# define snprintf(S, n, ...) sprintf(S, __VA_ARGS__)
46-
# define pipe(fds) _pipe(fds, 5000, _O_BINARY)
47-
# define ETIMEDOUT WSAETIMEDOUT
48-
# define ENOTSUP WSAEOPNOTSUPP
49-
# define ECONNABORTED WSAECONNABORTED
50-
# else
51-
#ifndef _MSC_VER
52-
# define snprintf sprintf_s
55+
#define snprintf(S, n, ...) sprintf(S, __VA_ARGS__)
5356
#endif
54-
# define strdup _strdup
55-
# endif
57+
58+
#endif
59+
60+
#if defined(_MSC_VER) && _MSC_VER < 1900
61+
#define snprintf sprintf_s
62+
#endif
63+
64+
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
65+
#define strdup _strdup
5666

5767
/*
5868
* setenv and unsetenv are not Windows compliant nor implemented in MinGW.
5969
* These declarations get rid of the "implicit declaration warning."
6070
*/
6171
int setenv(const char *name, const char *value, int overwrite);
62-
void unsetenv(const char *name);
72+
int unsetenv(const char *name);
6373

6474
#endif

examples/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ FOREACH(source ${EXAMPLES-SOURCES})
2424
SET(RC_COMMENT "${PACKAGE_NAME} example")
2525
SET(RC_INTERNAL_NAME ${source})
2626
SET(RC_ORIGINAL_NAME ${source}.exe)
27-
# RC_FILE_TYPE: VFT_APP
28-
SET(RC_FILE_TYPE 0x00000001L)
27+
SET(RC_FILE_TYPE VFT_APP)
28+
SET(RC_FILE_FLAGS 0x0)
29+
IF(LIBNFC_DEBUG_MODE)
30+
SET(RC_FILE_FLAGS VS_FF_DEBUG)
31+
ENDIF(LIBNFC_DEBUG_MODE)
2932
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc @ONLY)
3033
LIST(APPEND TARGETS ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc)
31-
32-
34+
3335
IF(${source} MATCHES "nfc-st25tb")
3436
LIST(APPEND TARGETS ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/getopt.c)
3537
ENDIF()

libnfc/CMakeLists.txt

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
# Windows MinGW workarounds
21
IF(WIN32)
2+
# Windows workarounds
33
SET(WINDOWS_SOURCES ../contrib/win32/stdlib.c)
4-
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32)
5-
6-
# Add in the rc for version information in the dll
7-
LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/../windows/libnfc.rc)
8-
IF (NOT MINGW)
9-
LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/nfc_msvc.def)
10-
ENDIF()
4+
# def and rc files for Windows
5+
SET(RC_COMMENT "${PACKAGE_NAME} library")
6+
SET(RC_INTERNAL_NAME ${PACKAGE_NAME})
7+
SET(RC_ORIGINAL_NAME ${PACKAGE_NAME}.dll)
8+
SET(RC_FILE_TYPE VFT_DLL)
9+
SET(RC_FILE_FLAGS 0x0)
10+
IF(LIBNFC_DEBUG_MODE)
11+
SET(RC_FILE_FLAGS VS_FF_DEBUG)
12+
ENDIF(LIBNFC_DEBUG_MODE)
13+
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/libnfc.rc)
14+
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/libnfc.def.in ${CMAKE_CURRENT_BINARY_DIR}/libnfc.def)
15+
LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.rc)
16+
LIST(APPEND WINDOWS_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.def)
1117
ENDIF(WIN32)
1218

1319
# Library's chips
@@ -66,9 +72,6 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
6672

6773
IF(LIBNFC_LOG)
6874
IF(WIN32)
69-
IF(MINGW)
70-
SET(CMAKE_C_FLAGS "-fgnu89-inline ${CMAKE_C_FLAGS}")
71-
ENDIF(MINGW)
7275
LIST(APPEND LIBRARY_SOURCES log.c ../contrib/win32/libnfc/log-internal.c)
7376
ELSE(WIN32)
7477
LIST(APPEND LIBRARY_SOURCES log.c log-internal.c)
@@ -93,24 +96,8 @@ SET_TARGET_PROPERTIES(nfc PROPERTIES SOVERSION 6 VERSION 6.0.0)
9396
IF(WIN32)
9497
# Libraries that are windows specific
9598
TARGET_LINK_LIBRARIES(nfc wsock32)
96-
IF(MINGW)
97-
ADD_CUSTOM_COMMAND(
98-
OUTPUT libnfc.lib
99-
COMMAND ${DLLTOOL} -d ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/nfc.def -l ${CMAKE_CURRENT_BINARY_DIR}/libnfc.lib ${CMAKE_CURRENT_BINARY_DIR}/libnfc.dll
100-
DEPENDS nfc ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/nfc.def
101-
)
102-
ADD_CUSTOM_TARGET(win32lib ALL DEPENDS libnfc.lib)
103-
ELSE()
104-
ADD_LIBRARY(win32lib ALIAS nfc)
105-
ENDIF()
106-
107-
# On Windows the shared (runtime) library should be either in the same
108-
# directory as the excutables or in the path, we add it to same directory
109-
INSTALL(TARGETS nfc RUNTIME DESTINATION bin COMPONENT libraries)
110-
111-
# At compile time we need the .LIB file, we place it in the lib directory
112-
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libnfc.lib DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)
113-
ELSE(WIN32)
114-
INSTALL(TARGETS nfc LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)
99+
# Ensure consistent lib prefix
100+
SET_TARGET_PROPERTIES(nfc PROPERTIES PREFIX lib)
115101
ENDIF(WIN32)
116102

103+
INSTALL(TARGETS nfc LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)

libnfc/drivers/pcsc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
#define LOG_GROUP NFC_LOG_GROUP_DRIVER
8686
#define LOG_CATEGORY "libnfc.driver.pcsc"
8787

88+
#ifndef MAX_ATR_SIZE
89+
#define MAX_ATR_SIZE 33
90+
#endif
91+
8892
static const char *supported_devices[] = {
8993
"ACS ACR122", // ACR122U & Touchatag, last version
9094
"ACS ACR 38U-CCID", // Touchatag, early version

utils/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ FOREACH(source ${UTILS-SOURCES})
2323
SET(RC_COMMENT "${PACKAGE_NAME} utility")
2424
SET(RC_INTERNAL_NAME ${source})
2525
SET(RC_ORIGINAL_NAME ${source}.exe)
26-
# RC_FILE_TYPE: VFT_APP
27-
SET(RC_FILE_TYPE 0x00000001L)
26+
SET(RC_FILE_TYPE VFT_APP)
27+
SET(RC_FILE_FLAGS 0x0)
28+
IF(LIBNFC_DEBUG_MODE)
29+
SET(RC_FILE_FLAGS VS_FF_DEBUG)
30+
ENDIF(LIBNFC_DEBUG_MODE)
2831
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/../contrib/win32/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc @ONLY)
2932
LIST(APPEND TARGETS ${CMAKE_CURRENT_BINARY_DIR}/../windows/${source}.rc)
3033
ENDIF(WIN32)

0 commit comments

Comments
 (0)