From ba9134e3d0a4c1fc4afd62a6a368c59f026d68f5 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 25 Aug 2024 16:30:07 +0200 Subject: [PATCH] Support cross build The build system was not designed for cross compiling, so this never worked. One problem is that we run both lopsubgen-stage1 and lopsubgen during the build, so these executables must be built for both the build and the host architecture. This patch enables cross building by setting CC and CC_FOR_BUILD like this: make CC=/usr/bin/arm-linux-gnueabihf-gcc-11 CC_FOR_BUILD=cc Object files and executables for the build system go to build directory while files for the host system stay in the top level directory as before. --- Makefile | 44 ++++++++++++++++++++++++++++---------------- README | 4 ++++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 6fbca9c..5418ef9 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ MAKEFLAGS += -Rr ifeq ("$(origin CC)", "default") CC := cc endif +ifeq ($(CC_FOR_BUILD),) + CC_FOR_BUILD := $(CC) +endif .ONESHELL: .SHELLFLAGS := -ec @@ -48,7 +51,9 @@ ifdef SOURCE_DATE_EPOCH else DATE := $(shell date '$(DATE_FMT)') endif -GIT_VERSION := $(shell ./version-gen.sh) + +B := build +GIT_VERSION := $(shell mkdir -p $(B) && ./version-gen.sh) PLAIN_VERSION := $(firstword $(subst -, , $(GIT_VERSION))) MAJOR_VERSION := $(firstword $(subst ., , $(PLAIN_VERSION))) SONAME := liblopsub.so.$(MAJOR_VERSION) @@ -56,7 +61,7 @@ REALNAME := liblopsub.so.$(PLAIN_VERSION) LINKERNAME:=liblopsub.so m4_man_pages := lopsub-suite.5.gz lopsub.7.gz -all := $(m4_man_pages) $(REALNAME) lopsubgen lopsubgen.1.gz \ +all := $(m4_man_pages) $(REALNAME) lopsubgen $(B)/lopsubgen lopsubgen.1.gz \ lopsubex lopsubex.1.gz all: $(all) @@ -80,20 +85,20 @@ $(m4_man_pages): %.gz: %.m4 version.c $(LEX) -o $@ $< # lopsubgen -lopsubgen.lsg.c lopsubgen.lsg.h: lopsubgen.suite lopsubgen-stage1 \ +lopsubgen.lsg.c lopsubgen.lsg.h: lopsubgen.suite $(B)/lopsubgen-stage1 \ lopsub-internal.h - ./lopsubgen-stage1 < $< + $(B)/lopsubgen-stage1 < $< %.lsg.c: %.suite lopsubgen - ./lopsubgen --gen-c < $< + $(B)/./lopsubgen --gen-c < $< %.lsg.h: %.suite lopsubgen - ./lopsubgen --gen-header < $< + $(B)/./lopsubgen --gen-header < $< %.1.gz: %.suite lopsubgen - ./lopsubgen --gen-man=${@:.gz=} --version-string $(GIT_VERSION) < $< + $(B)/./lopsubgen --gen-man=${@:.gz=} --version-string $(GIT_VERSION) < $< $(GZIP) ${@:.gz=} -# compiling -lsg1_objs := lopsubgen.o lsg1.o version.o +lsg1_objs := $(addprefix $(B)/, lopsubgen.o lsg1.o version.o) lsg_objs := lopsubgen.o lsg.o lopsubgen.lsg.o lopsub.o version.o +blsg_objs := $(addprefix $(B)/, $(lsg_objs)) liblopsub_objs := config_file.o lopsub.o version.o lopsubex_objs := lopsubex.o lopsubex.lsg.o $(liblopsub_objs) @@ -102,18 +107,24 @@ STRICT_CFLAGS := -Wall STRICT_CFLAGS += -Werror-implicit-function-declaration $(lsg_objs) $(liblopsub_objs) $(lopsubex_objs): %.o: %.c +$(blsg_objs): $(B)/%.o: %.c lopsubgen.o config_file.o: $(CC) $(CPPFLAGS) $(LLS_CFLAGS) $(CFLAGS) -c -o $@ ${@:.o=.c} -lsg1.o: lsg.c lsg.h - $(CC) $(CPPFLAGS) $(LLS_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS) -DSTAGE1 -c -o $@ $< +$(B)/lopsubgen.o: lopsubgen.c + $(CC_FOR_BUILD) $(CPPFLAGS) $(LLS_CFLAGS) $(CFLAGS) -c -o $@ $< +$(B)/lsg1.o: lsg.c lsg.h + $(CC_FOR_BUILD) $(CPPFLAGS) $(LLS_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS) -DSTAGE1 -c -o $@ $< %.o: %.c $(CC) -I. $(CPPFLAGS) $(LLS_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS) -c -o $@ $< - -# linking -lopsubgen-stage1: $(lsg1_objs) - $(CC) -Wall -g $(lsg1_objs) -o $@ +$(B)/%.o: %.c + $(CC_FOR_BUILD) -I. $(CPPFLAGS) $(LLS_CFLAGS) $(STRICT_CFLAGS) $(CFLAGS) -c -o $@ $< +$(B)/lopsubgen-stage1: $(lsg1_objs) + $(CC_FOR_BUILD) -Wall -g $(lsg1_objs) -o $@ lopsubgen: $(lsg_objs) $(CC) -Wall -g $(LDFLAGS) -o $@ $(lsg_objs) +$(B)/lopsubgen: $(blsg_objs) + $(CC_FOR_BUILD) -Wall -g $(LDFLAGS) -o $@ $(blsg_objs) + $(REALNAME): $(liblopsub_objs) $(CC) --shared -Wl,-soname,liblopsub.so.$(MAJOR_VERSION) \ $(LDFLAGS) -o $@ $^ @@ -151,8 +162,9 @@ install: $(all) $(INSTALL) -m 644 lopsub.7.gz $(DESTDIR)$(PREFIX)/share/man/man7 clean: - $(RM) $(all) $(html) *.o *.man + $(RM) $(all) $(html) *.o *.man $(B)/*.o distclean: clean $(RM) *.lsg.c *.lsg.h lopsubgen.c config_file.c lopsubgen-stage1 \ lopsub.h lopsub.7 lopsub-suite.5 version.c + $(RM) -r $(B) -include Makefile.local diff --git a/README b/README index 2f0bc51..d9f67ca 100644 --- a/README +++ b/README @@ -34,3 +34,7 @@ compile the source files of your application and -L/somewhere/else/lib for linking. Alternatively, don't run make install at all and specify the path to the top level directory of the repository for both -I and -L. + +To cross-compile the package, run a command like + + make CC=/usr/bin/arm-linux-gnueabihf-gcc-11 CC_FOR_BUILD=cc -- 2.39.5