Skip to content

Commit 558c878

Browse files
committed
Support for cross-compilation for MacOS using libftdi and libusb is added.
1 parent 83ead4e commit 558c878

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

Makefile.MacOS_libftdi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CXX = x86_64-apple-darwin15-g++
2+
CXXFLAGS = -c -g -O2 -Wno-narrowing -std=c++98 -mmacosx-version-min=10.8 -v -DUSE_FTDI
3+
INCPATH = -I ../libftdi1-1.5/src
4+
LINK = x86_64-apple-darwin15-g++
5+
LFLAGS = -g -O2 -v -static-libgcc -mmacosx-version-min=10.8 -lobjc -framework IOKit -framework CoreFoundation
6+
LIBS = -lftdi1 -lusb-1.0 -L ../libftdi1-1.5/src -L ../libusb-1.0.23/prefix/lib
7+
VPATH = src
8+
SRC_FILES = butterfly.c jtag.c iobase.c ioftdi.c bitfile.c tools.c devicedb.c progalgspi.c progalgxc3s.c
9+
OBJ_FILES = $(patsubst %.c, %.o, $(addprefix $(VPATH)/, $(SRC_FILES)))
10+
DEP_FILES = $(OBJ_FILES:.o=.d)
11+
12+
fireprog: $(OBJ_FILES)
13+
$(LINK) $(LFLAGS) $(LIBS) $^ -o $@
14+
15+
$(OBJ_FILES): %.o: %.cpp
16+
$(CXX) $(CXXFLAGS) $(INCPATH) -MMD -MF $(patsubst %.o,%.d,$@) -o $@ $<
17+
18+
clean:
19+
rm -f $ $(OBJ_FILES) $(DEP_FILES) fireprog

README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Check the cross-compiler:
147147
Thread model: posix
148148
gcc version 9.2.0 (GCC)
149149

150+
### Cross-compilation on Linux for MacOS using official libftd2xx library
150151
Once the toolchain is ready, download and unpack FTD2XX drivers for MacOS:
151152

152153
cd <path of the "fireprog" repository>
@@ -159,7 +160,7 @@ Once the toolchain is ready, download and unpack FTD2XX drivers for MacOS:
159160
7z x D2XX1.4.16.dmg release/D2XX
160161
mv release/D2XX .
161162

162-
Rename the shared library file by adding some prefix:
163+
Rename the shared library file so that its name does not start with "lib":
163164

164165
mv D2XX/libftd2xx.1.4.16.dylib D2XX/backup_libftd2xx.1.4.16.dylib
165166
cd ..
@@ -214,3 +215,118 @@ The output executable should be statically linked with no dynamic dependencies s
214215
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1253.0.0)
215216
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
216217

218+
### Cross-compilation on Linux for MacOS using libftdi and libusb libraries
219+
Download and unpack *libusb* library:
220+
221+
cd <path of the "fireprog" repository>
222+
# Change one directory up (important!)
223+
cd ..
224+
wget -vc https://github.com/libusb/libusb/releases/download/v1.0.23/libusb-1.0.23.tar.bz2
225+
# Unpack archive
226+
tar -xvf libusb-1.0.23.tar.bz2
227+
228+
Create a local installation directory:
229+
230+
cd libusb-1.0.23
231+
mkdir prefix
232+
cd ..
233+
234+
Configure the build environment, compile and install the library locally:
235+
236+
./configure --host=x86_64-apple-darwin15 --prefix="$(pwd)/prefix" CC=x86_64-apple-darwin15-clang
237+
make
238+
make install
239+
240+
Check for newly created library files:
241+
242+
tree ./prefix/
243+
./prefix
244+
├── include
245+
│   └── libusb-1.0
246+
│   └── libusb.h
247+
└── lib
248+
├── libusb-1.0.0.dylib
249+
├── libusb-1.0.a
250+
├── libusb-1.0.dylib -> libusb-1.0.0.dylib
251+
├── libusb-1.0.la
252+
└── pkgconfig
253+
└── libusb-1.0.pc
254+
255+
Rename the shared library files so that their names do not start with "lib":
256+
257+
mv ./prefix/lib/libusb-1.0.0.dylib ./prefix/lib/backup_libusb-1.0.0.dylib
258+
mv ./prefix/lib/libusb-1.0.dylib ./prefix/lib/backup_libusb-1.0.dylib
259+
260+
This is needed to **enforce static linking**, so that static library *libusb-1.0.a* instead of *libusb-1.0.dylib* is used by the linker.
261+
262+
Download and unpack *libftdi* library:
263+
264+
cd <path of the "fireprog" repository>
265+
# Change one directory up (important!)
266+
cd ..
267+
wget -vc https://www.intra2net.com/en/developer/libftdi/download/libftdi1-1.5.tar.bz2
268+
# Unpack archive
269+
tar -xvf libftdi1-1.5.tar.bz2
270+
cd libftdi1-1.5
271+
272+
There are only two files which need to be compiled, configuring CMake to support cross-compilation is more difficult than compiling these two files.
273+
274+
In order to create header *ftdi_version_i.h* from template *ftdi_version_i.h.in* manually, one needs to learn the versions of the library:
275+
276+
MAJOR_VERSION=`grep -P -o "(?<=set\(MAJOR_VERSION)\s*\d+(?=\))" CMakeLists.txt`
277+
echo ${MAJOR_VERSION}
278+
1
279+
280+
MINOR_VERSION=`grep -P -o "(?<=set\(MINOR_VERSION)\s*\d+(?=\))" CMakeLists.txt`
281+
echo ${MINOR_VERSION}
282+
5
283+
284+
grep -P -o "(?<=set\(VERSION_STRING)\s*.+(?=\))" CMakeLists.txt
285+
${MAJOR_VERSION}.${MINOR_VERSION}
286+
287+
VERSION_STRING_TEMPLATE=`grep -P -o "(?<=set\(VERSION_STRING)\s*.+(?=\))" CMakeLists.txt`
288+
VERSION_STRING=$(eval echo ${VERSION_STRING_TEMPLATE}|sed "s/\s//g")
289+
echo $VERSION_STRING
290+
1.5
291+
292+
After setting MAJOR_VERSION, MINOR_VERSION, VERSION_STRING environment variables, create *ftdi_version_i.h* from the template:
293+
294+
cd src
295+
sed "s/@MAJOR_VERSION@/${MAJOR_VERSION}/g" ftdi_version_i.h.in > ftdi_version_i.h
296+
sed -i "s/@MINOR_VERSION@/${MINOR_VERSION}/g" ftdi_version_i.h
297+
sed -i "s/@VERSION_STRING@/${VERSION_STRING}/g" ftdi_version_i.h
298+
sed -i "s/@SNAPSHOT_VERSION@/unknown/g" ftdi_version_i.h
299+
300+
Compile files:
301+
302+
x86_64-apple-darwin15-g++ -c ftdi.c -o ftdi.o -I ../../libusb-1.0.23/libusb/ -I .
303+
x86_64-apple-darwin15-g++ -c ftdi_stream.c -o ftdi_stream.o -I ../../libusb-1.0.23/libusb/ -I . -Wno-narrowing -fpermissive
304+
305+
Bind object files into a static library:
306+
307+
x86_64-apple-darwin15-libtool -static ftdi.o ftdi_stream.o -o libftdi1.a
308+
309+
The corresponding Makefile is written according to this directory structure:
310+
311+
├── fireprog
312+
├── libftdi1-1.5
313+
└── libusb-1.0.23
314+
315+
Now compile the "fireprog":
316+
317+
cd <path of the fireprog repository>
318+
make -f Makefile.MacOS_libftdi
319+
320+
Check the output binary:
321+
322+
file fireprog
323+
fireprog: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|WEAK_DEFINES|BINDS_TO_WEAK|PIE>
324+
325+
The output executable should be statically linked with no dynamic dependencies such as FT2XX:
326+
327+
x86_64-apple-darwin15-otool -L fireprog
328+
fireprog:
329+
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
330+
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
331+
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1253.0.0)
332+
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)

src/ioftdi.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
2727
#include "ftd2xx.h"
2828
#define USE_FTD2XX
2929
#elif defined(__APPLE__)
30-
#include "ftd2xx.h"
31-
#define USE_FTD2XX
30+
#ifdef USE_FTDI
31+
#include "ftdi.h"
32+
#else
33+
#include "ftd2xx.h"
34+
#define USE_FTD2XX
35+
#endif
3236
#else
3337
#include <ftdi.h>
3438
#endif

0 commit comments

Comments
 (0)