Skip to content

Enhance Windows Compatibility #457

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

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 13 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -100,26 +100,23 @@ ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t)

IF(MINGW)
IF (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
# force MinGW-w64 in 32bit mode
SET(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}")
SET(CMAKE_MODULE_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_EXE_LINKER_FLAGS}")
SET(CMAKE_RC_FLAGS "--target=pe-i386 --output-format=coff ${CMAKE_RC_FLAGS}")
ELSE(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}")
MESSAGE("Building 32-bit Windows DLL")
SET(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}")
SET(CMAKE_MODULE_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "-m32 -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")
MESSAGE("Building 64-bit Windows DLL")
SET(CMAKE_RC_FLAGS "--target=pe-x86-64 --output-format=coff ${CMAKE_RC_FLAGS}")
MESSAGE("Building 64-bit Windows DLL")
SET(CMAKE_C_FLAGS "-m64 ${CMAKE_C_FLAGS}")
SET(CMAKE_MODULE_LINKER_FLAGS "-m64 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "-m64 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "-m64 -Wl,--enable-stdcall-fixup ${CMAKE_EXE_LINKER_FLAGS}")
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}")
MESSAGE(FATAL_ERROR "Unknown Processor: ${CMAKE_SYSTEM_PROCESSOR}")
ENDIF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
ENDIF(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")

138 changes: 69 additions & 69 deletions README-Windows.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
*-
* Free/Libre Near Field Communication (NFC) library
*
* Libnfc historical contributors:
* Copyright (C) 2009 Roel Verdult
* Copyright (C) 2009-2013 Romuald Conty
* Copyright (C) 2010-2012 Romain Tartière
* Copyright (C) 2010-2013 Philippe Teuwen
* Copyright (C) 2012-2013 Ludovic Rousseau
* Additional contributors of Windows-specific parts:
* Copyright (C) 2010 Glenn Ergeerts
* Copyright (C) 2013 Alex Lian
-*
## Compile for Windows - 64-BIT (x64)

#### Tested using windows 10 pro x64, build 1703.

Install latest libusbk release from: (this installs both libusb and libusbk windows components required for running libnfc applications)
https://sourceforge.net/projects/libusb-win32/files/libusbK-release/
https://sourceforge.net/projects/libusb-win32/files/libusbK-release/libusbK-3.0.7.0-setup.exe

#### install mingw64 toolchain on Windows:

##### Install latest version of: MSYS2 for 64-bit windows.
##### even if msys2 version appears old, the packages it installs are very recent:
http://www.msys2.org/
msys2-x86_64-xxxxxxxx.exe


##### Open the MSYS shell in mingw-64 mode (64-bit mode)
```
#update the MSYS system using pacman:
pacman -Syu

#close the shell, re-open and run:
pacman -Su

#install build tools:
pacman -S git make mingw-w64-$(uname -m)-gcc mingw-w64-$(uname -m)-binutils mingw-w64-$(uname -m)-cmake mingw-w64-$(uname -m)-zlib mingw-w64-$(uname -m)-make
```

#### download and extract: libusb-win32-bin-1.2.6.0.zip
##### todo: (this step shouldn't be needed, as libusb0.dll is already in system32, but the logic is broken in the libusb search that cmake uses in: cmake/modules/FindLIBUSB.cmake, so do this step for now)
https://sourceforge.net/projects/libusb-win32/files/libusb-win32-releases/1.2.6.0/
##### Extract contents to the native program files directory: (not the x86 program files!):
C:\Program Files\libusb-win32

#### Restart msys2 bash - in mingw-64 mode.

```

#download libnfc source files:
cd ~
mkdir libnfc-build-win64
git clone https://github.com/nfc-tools/libnfc.git

# launch windows powershell x64 (non-admin)
# add some paths for current build session:

$env:Path += ";c:\msys64\mingw64\bin;c:\msys64\mingw64\x86_64-w64-mingw32\lib;c:\msys64\mingw64\x86_64-w64-mingw32\include"
cd C:\msys64\home\<your username>\libnfc-build-win64

#then, run "CMD.exe" from within the powershell, (this ensures the PATH set above is available in the build environment)
cmd.exe

# configure the build with cmake:
cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..\libnfc

# run cmake again, same parameters, otherwise errors appear:
cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..\libnfc

# build it!
mingw32-make

```
#### put your "libnfc.conf" into c:\program files (x86)\libnfc\config\libnfc.conf
todo: it is the wrong location currently for a x64 system, but it works and this will be fixed to use c:\programdata\libnfc\config\libnfc.conf at some point.

#### put compiled DLL file to the real system32 directory: (c:\windows\sysnative\libnfc.dll on syswow64 machines)
copy .\libnfc\libnfc.dll c:\windows\system32\libnfc.dll

#### Run the executables found in libnfc\utils or libnfc\examples

Requirements
============

- MinGW-w64 compiler toolchain [1]
- LibUsb-Win32 1.2.5.0 (or greater) [2]
- CMake 2.8 [3]

This was tested on Windows 7 64 bit, but should work on Windows Vista and
Windows XP and 32 bit as well.
Only the ACS ACR122 and the ASK Logo readers are tested at the moment, so any feedback about other devices is very welcome.

Community forum: http://www.libnfc.org/community/

Building
========

To build the distribution the MinGW Makefiles generator of CMake was used. Here
is an example of how to generate a distribution with the above mentioned
requirements fulfilled (it is assumed the CMake binaries are in the system
path, this is optional during installation of CMake):

- Add the following directories to your PATH:

c:\MinGW64\bin;c:\MinGW64\x86_64-w64-mingw32\lib32;c:\MinGW64\x86_64-w64-mingw32\include

- Now it is possible to run CMake and mingw32-make:

C:\dev\libnfc-read-only> mkdir ..\libnfc-build
C:\dev\libnfc-read-only> cd ..\libnfc-build
C:\dev\libnfc-build> cmake-gui .

Now you can configure the build. Press "Configure", specify "MinGW32 Makefiles"
and then you have the opportunity to set some configuration variables. If you
don't want a Debug build change the variable CMAKE_BUILD_TYPE to "Release".

If a non-GUI solution is preferred one can use:

C:\dev\libnfc-build> cmake -G "MinGW Makefiles"
-DCMAKE_BUILD_TYPE=Release ..\libnfc-read-only

Now run mingw32-make to build:

C:\dev\libnfc-read-only\bin> mingw32-make

The build will create a shared library for Windows (nfc.dll) to link your applications against. It will compile
the tools against this shared library.

References
==========
[1] the easiest way is to use the TDM-GCC installer.
Make sure to select MinGW-w64 in the installer, the regular MinGW does not contain headers for PCSC.
http://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm64-gcc-4.5.1.exe/download

[2] http://sourceforge.net/projects/libusb-win32/files/

[3] http://www.cmake.org
4 changes: 2 additions & 2 deletions cmake/modules/FindLIBUSB.cmake
Original file line number Diff line number Diff line change
@@ -27,8 +27,8 @@ ENDIF(CMAKE_SYSTEM_NAME MATCHES FreeBSD)
IF(NOT LIBUSB_FOUND)
IF(WIN32)
FIND_PATH(LIBUSB_INCLUDE_DIRS lusb0_usb.h "$ENV{ProgramFiles}/LibUSB-Win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb PATHS "$ENV{ProgramFiles}/LibUSB-Win32/lib/gcc")
SET(LIBUSB_LIBRARY_DIR "$ENV{ProgramFiles}/LibUSB-Win32/bin/x86/")
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb0 PATHS "$ENV{ProgramFiles}/LibUSB-Win32/bin/amd64")
SET(LIBUSB_LIBRARY_DIR "$ENV{ProgramFiles}/LibUSB-Win32/bin/amd64")
# Must fix up variable to avoid backslashes during packaging
STRING(REGEX REPLACE "\\\\" "/" LIBUSB_LIBRARY_DIR ${LIBUSB_LIBRARY_DIR})
ELSE(WIN32)
4 changes: 4 additions & 0 deletions contrib/win32/nfc.def
Original file line number Diff line number Diff line change
@@ -50,3 +50,7 @@ EXPORTS
str_nfc_modulation_type
str_nfc_baud_rate
str_nfc_target
pn53x_transceive
pn532_SAMConfiguration
pn53x_read_register
pn53x_write_register
5 changes: 5 additions & 0 deletions libnfc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -69,6 +69,11 @@ IF(LIBNFC_LOG)
LIST(APPEND LIBRARY_SOURCES log log-internal)
ENDIF(WIN32)
ENDIF(LIBNFC_LOG)

IF(WIN32)
LIST(APPEND LIBRARY_SOURCES ../contrib/win32/nfc.def)
ENDIF(WIN32)

ADD_LIBRARY(nfc SHARED ${LIBRARY_SOURCES})

IF(PCSC_FOUND)