Skip to content

Instantly share code, notes, and snippets.

@vitorio
Last active January 16, 2021 08:26
Show Gist options
  • Save vitorio/d50fad417bbc2e3d055474046669b358 to your computer and use it in GitHub Desktop.
Save vitorio/d50fad417bbc2e3d055474046669b358 to your computer and use it in GitHub Desktop.
Compiling libnfc natively on OS X for use in any working directory

These instructions compile a native OS X version of libnfc, and adjust its internal paths and those of its dependencies to allow you to use it from any arbitrary working directory, which is normally restricted in versions of OS X with System Integrity Protection.

Assumes OS X with Xcode or Xcode command-line tools, not macports or homebrew

To install Xcode command-line tools in recent versions of OS X, open Terminal, type xcode-select --install and click "Install"

Older versions of OS X may need to manually download disk images of older versions of Xcode and/or the Xcode command-line tools, as various certificates may have expired (e.g. 10.7.4 requires a manual install from the Xcode 4.6.2 command-line tools image)

Download latest pkg-config (tested with 0.29.1) from https://pkg-config.freedesktop.org/releases/?C=M;O=D

Download latest libusb (tested with 1.0.20) from https://sourceforge.net/projects/libusb/files/latest/download?source=files

Download latest libusb-compat (tested with 0.1.15) from https://sourceforge.net/projects/libusb/files/libusb-compat-0.1/

Download latest libnfc (tested with 1.7.1) from https://bintray.com/nfc-tools/sources/libnfc

Create a directory to put the compiled stuff in, you can delete it later

$ mkdir ~/securitoys

Compile pkg-config, libusb, libusb-compat, libnfc

$ cd ~/Downloads/
$ cd pkg-config-0.29.1
$ ./configure --with-internal-glib --prefix=$HOME/securitoys/
$ make
$ make install
$ cd ..
$ cd libusb-1.0.20
$ ./configure --prefix=$HOME/securitoys/
$ make
$ make install
$ cd ..
$ cd libusb-compat-0.1.5
$ PKG_CONFIG=$HOME/securitoys/bin/pkg-config ./configure --prefix=$HOME/securitoys/
$ make
$ make install
$ cd ..
$ cd libnfc-1.7.1
$ PKG_CONFIG=$HOME/securitoys/bin/pkg-config ./configure --prefix=$HOME/securitoys/
$ make
$ make install

Rename the absolute paths in libnfc

$ cd ~/securitoys/lib/
$ otool -L libnfc.5.dylib
libnfc.5.dylib:
	/Users/username/securitoys/lib/libnfc.5.dylib (compatibility version 6.0.0, current version 6.1.0)
	/Users/username/securitoys/lib/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/Users/username/securitoys/lib/libusb-0.1.4.dylib (compatibility version 9.0.0, current version 9.4.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
$ install_name_tool -change /Users/username/securitoys/lib/libusb-1.0.0.dylib @loader_path/libusb-1.0.0.dylib libnfc.5.dylib 
$ install_name_tool -change /Users/username/securitoys/lib/libusb-0.1.4.dylib @loader_path/libusb-0.1.4.dylib libnfc.5.dylib 
$ install_name_tool -id libnfc.dylib libnfc.5.dylib 
$ mv libnfc.5.dylib libnfc.dylib
$ otool -L libnfc.dylib 
libnfc.dylib:
	libnfc.dylib (compatibility version 6.0.0, current version 6.1.0)
	@loader_path/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	@loader_path/libusb-0.1.4.dylib (compatibility version 9.0.0, current version 9.4.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

Rename the absolute paths in libusb

$ otool -L libusb-1.0.0.dylib 
libusb-1.0.0.dylib:
	/Users/username/securitoys/lib/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1258.1.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
$ install_name_tool -id libusb-1.0.0.dylib libusb-1.0.0.dylib
$ otool -L libusb-1.0.0.dylib 
libusb-1.0.0.dylib:
	libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1258.1.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

Rename the absolute paths in libusb-compat

$ otool -L libusb-0.1.4.dylib 
libusb-0.1.4.dylib:
	/Users/username/securitoys/lib/libusb-0.1.4.dylib (compatibility version 9.0.0, current version 9.4.0)
	/Users/username/securitoys/lib/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
$ install_name_tool -change /Users/username/securitoys/lib/libusb-1.0.0.dylib @loader_path/libusb-1.0.0.dylib libusb-0.1.4.dylib 
$ install_name_tool -id libusb-0.1.4.dylib libusb-0.1.4.dylib 
$ otool -L libusb-0.1.4.dylib 
libusb-0.1.4.dylib:
	libusb-0.1.4.dylib (compatibility version 9.0.0, current version 9.4.0)
	@loader_path/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

Now, you can copy libnfc.dylib and its two dependencies, libusb-1.0.0.dylib and libusb-0.1.4.dylib wherever you need them, even on OS X systems with System Integrity Protection (10.11 El Capitan and later).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment