Skip to content

Commit 57cfb13

Browse files
cppcoffeedeemar
authored andcommitted
Add cross-compilation support to Makefile
Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
1 parent e11ebae commit 57cfb13

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/Makefile

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ ifeq ($(UNAME), MINGW)
5555
OBJ := $(OBJ) zlog_win.o
5656
endif
5757

58-
# Fallback to gcc when $CC is not in $PATH.
59-
CC:=$(shell sh -c 'type $(CC) >/dev/null 2>/dev/null && echo $(CC) || echo gcc')
58+
CROSS_COMPILE?=
59+
CC:=$(CROSS_COMPILE)$(shell sh -c 'type $(CROSS_COMPILE)$(CC) >/dev/null 2>/dev/null && echo $(CC) || echo gcc')
60+
AR:=$(CROSS_COMPILE)ar
61+
STRIP:=$(CROSS_COMPILE)strip
6062
OPTIMIZATION?=-O2
6163

6264
ifeq ($(UNAME), MINGW)
@@ -77,7 +79,7 @@ DYLIB_MAJOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(ZLOG_MAJOR)
7779
DYLIBNAME=$(LIBNAME).$(DYLIBSUFFIX)
7880
DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS)
7981
STLIBNAME=$(LIBNAME).$(STLIBSUFFIX)
80-
STLIB_MAKE_CMD=ar rcs $(STLIBNAME)
82+
STLIB_MAKE_CMD=$(AR) rcs $(STLIBNAME)
8183

8284
# Installation related variables
8385
PREFIX?=/usr/local
@@ -114,7 +116,7 @@ ifeq ($(uname_S),AIX)
114116
# not to mention dynamic linker .a preference...
115117
DYLIB_MAKE_CMD=$(CC) -shared -Wl,-G,-b64 -maix64 -pthread -o $(DYLIBNAME) $(LDFLAGS)
116118
REAL_CFLAGS+= -maix64
117-
STLIB_MAKE_CMD=OBJECT_MODE=64 ar rcs $(STLIBNAME) $(DYLIB_MAJOR_NAME)
119+
STLIB_MAKE_CMD=OBJECT_MODE=64 $(AR) rcs $(STLIBNAME) $(DYLIB_MAJOR_NAME)
118120
endif
119121

120122
ifeq ($(UNAME), MINGW)
@@ -259,6 +261,20 @@ install: $(DYLIBNAME) $(STLIBNAME) zlog.pc
259261
@echo ""
260262
$(MAKE) CFLAGS="-m32" LDFLAGS="-m32"
261263

264+
# Cross-compilation examples:
265+
# For ARM64: make CROSS_COMPILE=aarch64-linux-gnu-
266+
# For ARM: make CROSS_COMPILE=arm-linux-gnueabihf-
267+
# For MIPS: make CROSS_COMPILE=mips-linux-gnu-
268+
cross-compile-help:
269+
@echo "Cross-compilation usage examples:"
270+
@echo " make CROSS_COMPILE=aarch64-linux-gnu- # For ARM64"
271+
@echo " make CROSS_COMPILE=arm-linux-gnueabihf- # For ARM"
272+
@echo " make CROSS_COMPILE=mips-linux-gnu- # For MIPS"
273+
@echo " make CROSS_COMPILE=x86_64-w64-mingw32- # For Windows x64"
274+
@echo ""
275+
@echo "You can also override individual tools:"
276+
@echo " make CC=clang AR=llvm-ar STRIP=llvm-strip"
277+
262278
gprof:
263279
$(MAKE) CFLAGS="-pg" LDFLAGS="-pg"
264280

@@ -274,4 +290,4 @@ coverage: gcov
274290
noopt:
275291
$(MAKE) OPTIMIZATION=""
276292

277-
.PHONY: all clean dep install 32bit gprof gcov noopt
293+
.PHONY: all clean dep install 32bit gprof gcov noopt cross-compile-help

0 commit comments

Comments
 (0)