Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed compilation issues on Windows #718

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 5 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.6)
cmake_minimum_required (VERSION 3.1)

if (NOT DEFINED CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
Expand Down Expand Up @@ -51,10 +51,10 @@ ENDIF(LIBNFC_ENVVARS)

SET(LIBNFC_DEBUG_MODE OFF CACHE BOOL "Debug mode")
IF(LIBNFC_DEBUG_MODE)
ADD_DEFINITIONS(-DDEBUG)
SET(CMAKE_C_FLAGS "-g3 ${CMAKE_C_FLAGS}")
SET(WIN32_MODE "debug")
ADD_DEFINITIONS(-DDEBUG -D_DEBUG)
SET(CMAKE_C_FLAGS "-Wall -Wextra -pedantic ${CMAKE_C_FLAGS}")
SET(CMAKE_RC_FLAGS "-D_DEBUG ${CMAKE_RC_FLAGS}")
SET(WIN32_MODE "debug")
ELSE(LIBNFC_DEBUG_MODE)
SET(WIN32_MODE "release")
ENDIF(LIBNFC_DEBUG_MODE)
Expand Down Expand Up @@ -93,38 +93,9 @@ IF(NOT DEFINED SHARE_INSTALL_PREFIX)
SET(SHARE_INSTALL_PREFIX share)
ENDIF(NOT DEFINED SHARE_INSTALL_PREFIX)

# Additonnal GCC flags
IF(CMAKE_COMPILER_IS_GNUCC)
# Make sure we will not miss some warnings ;)
SET(CMAKE_C_FLAGS "-Wall -pedantic -std=c99 ${CMAKE_C_FLAGS}")
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)
SET(C_STANDARD 99)

IF(NOT WIN32)
# Set some pkg-config variables
Expand Down Expand Up @@ -178,17 +149,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.

75 changes: 60 additions & 15 deletions contrib/win32/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,72 @@
* @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.
// This implementation of setenv() and unsetenv() using _putenv_s() as the
// underlying function.
// NOTE: unlike the POSIX functions, they return errno instead of -1 when they
// fail, and they allow name to contain '=', which is not allowed by the POSIX
// standard.
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);
}

int unsetenv(const char *name)
{
return _putenv_s(name, "");
}

#ifdef USE_LIBNFC_SNPRINTF

#include <stdarg.h>
#include <stdio.h>

// The implementation of libnfc_snprintf() is taken from mingw-w64-crt
int libnfc_vsnprintf(char *s, size_t n, const char *format, va_list arg)
{
int retval;

/* _vsnprintf() does not work with zero length buffer
* so count number of character by _vscprintf() call */
if (n == 0)
return _vscprintf(format, arg);

retval = _vsnprintf(s, n, format, arg);

/* _vsnprintf() does not fill trailing null byte if there is not place for it
*/
if (retval < 0 || (size_t)retval == n)
s[n - 1] = '\0';

/* _vsnprintf() returns negative number if buffer is too small
* so count number of character by _vscprintf() call */
if (retval < 0)
retval = _vscprintf(format, arg);

return retval;
}

void unsetenv(const char *name)
int libnfc_snprintf(char *buffer, size_t n, const char *format, ...)
{
char *str[32];
strcpy(str, name);
strcat(str, "=");
putenv(str);
int retval;
va_list argptr;

va_start(argptr, format);
retval = libnfc_vsnprintf(buffer, n, format, argptr);
va_end(argptr);
return retval;
}

#endif
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@.@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"
Expand Down
41 changes: 21 additions & 20 deletions contrib/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,33 @@
#ifndef __WINDOWS_H__
#define __WINDOWS_H__

# include <windows.h>
# include <winerror.h>
# include "win32/err.h"
# if defined (__MINGW32__)
/*
* 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 WIN32_LEAN_AND_MEAN
#include <fcntl.h>
#include <windows.h>

#if defined(__MINGW32__) && __MINGW64_VERSION_MAJOR < 3
#include <winerror.h>
#define ETIMEDOUT WSAETIMEDOUT
#define ENOTSUP WSAEOPNOTSUPP
#define ECONNABORTED WSAECONNABORTED
#endif

#if defined(_MSC_VER) && _MSC_VER < 1900 || \
defined(__MINGW32__) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT)
#define USE_LIBNFC_SNPRINTF
#define snprintf libnfc_snprintf

int libnfc_snprintf(char *buffer, size_t n, const char *format, ...);
#endif
# define strdup _strdup
# 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
Loading