Skip to content
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

Allow make environment and command-line overriding of pre-defined tools in Makefiles to ease cross-compilations #236

Open
johnsonjh opened this issue Mar 21, 2022 · 3 comments

Comments

@johnsonjh
Copy link

johnsonjh commented Mar 21, 2022

For the DOS port, this allows for easier cross-compilation using a local DJGPP toolchain from UNIX. I do this so I can build from OpenBSD or Linux simply using:

$ DJGPP=/opt/djgpp
$ CC=${DJGPP}/bin/i586-pc-msdosdjgpp-gcc
$ STRIP=${DJGPP}/bin/i586-pc-msdosdjgpp-strip
$ gmake

I also have some wrapper hacks that leverage MS-DOS Player in combination with Wine to run Borland C 5.02 for DOS on Linux as a cross-compiler):

$ BCCL=/usr/local/bcclin/
$ CC=${BCCL}/bcc5
$ STRIP=${BCCL}/bstrip5
$ gmake

It all can work without having to modify the Makefiles at all, with the following changes:

diff --git a/dos/Makefile b/dos/Makefile
index 428e905b..9fd087c2 100644
--- a/dos/Makefile
+++ b/dos/Makefile
@@ -1,49 +1,49 @@
 # GNU Makefile for PDCurses - DOS
 #
 # Usage: [g]make [-f path\Makefile] [DEBUG=Y] [target]
 #
 # where target can be any of:
 # [all|libs|demos|pdcurses.a|testcurs.exe...]
 
 O = o
 E = .exe
-RM = del
+RM ?= del
 
 ifndef PDCURSES_SRCDIR
 	PDCURSES_SRCDIR = ..
 endif
 
 osdir		= $(PDCURSES_SRCDIR)/dos
 common		= $(PDCURSES_SRCDIR)/common
 
 include $(common)/libobjs.mif
 
 PDCURSES_DOS_H	= $(osdir)/pdcdos.h
 
-CC		= gcc
-STRIP	= strip
+CC		?= gcc
+STRIP	?= strip
+AR 	?= ar

-CFLAGS  = -Wall
+CFLAGS  ?= -Wall
 ifeq ($(DEBUG),Y)
 	CFLAGS  += -g -DPDCDEBUG
-	LDFLAGS = -g
+	LDFLAGS ?= -g
 else
 	CFLAGS  += -O2
-	LDFLAGS =
+	LDFLAGS ?=
 endif
 
 CFLAGS += -I$(PDCURSES_SRCDIR)
 
-LINK		= gcc
+LINK		?= $(CC)
 
-LIBEXE		= ar
+LIBEXE		?= $(AR)
 LIBFLAGS	= rcv
 
 LIBCURSES	= pdcurses.a
 
 .PHONY: all libs clean demos dist
 
 all:	libs
 
 libs:	$(LIBCURSES)

Similarly for dosvga, if wanted.


I also propose some changes to Windows ports Makefiles, but it's a bit more complicated, so I hope I did it all right.

PDCursesMod does supports a setting a PREFIX, but still hard-codes gcc, ar, and strip, even thought there are other Windows compilers with compatible front-ends (Midipix, Cygwin, MSVC clang-cl, LLVM mingw-clang, etc.) ... also, assume gcc if PREFIX is set (or CC is set to cc), but don't ever override any user specified CFLAGS/LDFLAGS/PREFIX, and use the PREFIX for calls to ar and strip (since the host versions of those tools might not be compatible with the output formats). (I'm using the Xprefix's when rather than overwriting the variable with itself to get around a bug in some older Windows make ports.)

This allows for cases like env PREFIX=x86_64-pc-cygwin- gmake, env CC=/usr/bin/i686-pc-cygwin-gcc STRIP=/usr/local/bin/my-own-better-strip gmake, and env CC=/opt/wxc/compiler/wxc STRIP=/opt/localbinutils/gstrip gmake to all work sanely.

I don't believe it would break any current use-cases, but I didn't test extensively on Windows. These changes are for wincon but should be applied to wingui as well:

diff --git a/wincon/Makefile b/wincon/Makefile
index 353aebdf..edf47fc6 100644
--- a/wincon/Makefile
+++ b/wincon/Makefile
@@ -14,25 +14,25 @@ ifndef PDCURSES_SRCDIR
 endif
 
 osdir		= $(PDCURSES_SRCDIR)/wincon
 common		= $(PDCURSES_SRCDIR)/common
 
 include $(common)/version.mif
 include $(common)/libobjs.mif
 
 uname_S := $(shell uname -s 2>/dev/null)
 
-CAT		= cat
-PREFIX		=
+CAT		?= cat
+PREFIX		?=
 PATH_SEP		= /
-CP		= cp
-RM		= rm -f
+CP		?= cp
+RM		?= rm -f
 
 # It appears we have three cases:  we're running in Cygwin/MSYS;  or we're
 # running in command.com on Windows with MinGW;  or we're on Linux or BSD
 # or similar system,  cross-compiling with MinGW.
 
 ifneq (,$(findstring CYGWIN,$(uname_S)))
 	#  Insert Cygwin-specific changes here
 	ON_WINDOWS = 1
 endif
 ifneq (,$(findstring MINGW32_NT,$(uname_S)))
@@ -45,78 +45,93 @@ ifneq (,$(findstring MINGW64_NT,$(uname_S)))
 endif
 
 ifeq ($(uname_S),)
 	CAT = type
 	PATH_SEP = \\
 	CP = copy
 	RM = cmd /c del
 	ON_WINDOWS = 1
 endif
 
-# If we aren't on Windows,  assume MinGW on a Linux-like host
-# Only decision is:  are we doing a 64-bit compile (_w64 defined)?
+# If we aren't on Windows, assume MinGW on a Linux-like
+# host if the user has not explictly specified CC. Only
+# decision is: are we doing a 64-bit compile (_w64 defined)?
 
 ifndef ON_WINDOWS
-	ifdef _w64
-	   PREFIX  = x86_64-w64-mingw32-
-	else
-	   PREFIX  = i686-w64-mingw32-
-	endif
+    ifndef PREFIX
+		USE_MINGW ?= 1
+    endif
+endif
+
+ifndef USE_MINGW
+    ifdef _w64
+	   CC	   = gcc
+	   PREFIX  ?= x86_64-w64-mingw32-
+    else
+	   CC	   = gcc
+	   PREFIX  ?= i686-w64-mingw32-
+    endif
 endif
 
 PDCURSES_WIN_H	= $(osdir)/pdcwin.h
 
-CC	= $(PREFIX)gcc
+ifeq ($(CC),cc)
+	CC	= gcc
+endif
+XCC	?= $(PREFIX)$(CC)
+
+AR	?= ar
+XAR	?= $(AR)
 
-AR	= ar
-STRIP	= strip
+STRIP	?= strip
+XSTRIP	?= $(STRIP)
 
-CFLAGS  = -Wall -Wextra -pedantic
+CFLAGS  ?= -Wall -Wextra -pedantic
 ifeq ($(DEBUG),Y)
 	CFLAGS  += -g -DPDCDEBUG
-	LDFLAGS = -g
+	LDFLAGS ?= -g
 else
 	CFLAGS  += -O4
-	LDFLAGS =
+	LDFLAGS ?=
 endif
 
 CFLAGS += -I$(PDCURSES_SRCDIR)
 
 ifdef CHTYPE_32
 	CFLAGS += -DCHTYPE_32
 endif
 
 ifeq ($(WIDE),Y)
 	CFLAGS += -DPDC_WIDE
 endif
 
 ifeq ($(UTF8),Y)
 	CFLAGS += -DPDC_FORCE_UTF8
 endif
 
 ifeq ($(INFOEX),N)
 	PDCCFLAGS += -DHAVE_NO_INFOEX
 endif
 
-LINK	   = $(CC)
+LINK	   = $(XCC)
 
 ifeq ($(DLL),Y)
 	CFLAGS += -DPDC_DLL_BUILD
-	LIBEXE = $(CC)
+	LIBEXE = $(XCC)
 	LIBFLAGS = -Wl,--out-implib,pdcurses.a -shared -o
 	LIBCURSES = pdcurses.dll
 	LIBDEPS = $(LIBOBJS) $(PDCOBJS)
 	LIBSADDED = -lwinmm
 	EXELIBS =
 	CLEAN = $(LIBCURSES) *.a
 else
-	LIBEXE = $(AR)
+	LIBEXE = $(XAR)
 ifeq ($(PREFIX),)
 	LIBFLAGS = rcv
 else
 	LIBFLAGS = rv
 endif
 	LIBCURSES = pdcurses.a
 	LIBDEPS = $(LIBOBJS) $(PDCOBJS)
 	LIBSADDED =
 	EXELIBS = -lwinmm
 	CLEAN = *.a
@@ -129,48 +144,48 @@ all:	libs
 libs:	$(LIBCURSES)
 
 clean:
 	-$(RM) *.o
 	-$(RM) *.exe
 	-$(RM) *.dll
 	-$(RM) $(CLEAN)
 
 demos:	$(DEMOS)
 ifneq ($(DEBUG),Y)
-	$(STRIP) *.exe
+	$(XSTRIP) *.exe
 endif
 
 $(LIBCURSES) : $(LIBDEPS)
 	$(LIBEXE) $(LIBFLAGS) $@ $? $(LIBSADDED)
 	$(CP) pdcurses.a panel.a
 
 $(LIBOBJS) $(PDCOBJS) : $(PDCURSES_HEADERS)
 $(PDCOBJS) : $(PDCURSES_WIN_H)
 $(DEMOS) : $(PDCURSES_CURSES_H) $(LIBCURSES)
 panel.o : $(PANEL_HEADER)
 
 $(LIBOBJS) : %.o: $(srcdir)/%.c
-	$(CC) -c $(CFLAGS) $<
+	$(XCC) -c $(CFLAGS) $<
 
 $(PDCOBJS) : %.o: $(osdir)/%.c
-	$(CC) -c $(CFLAGS) $(PDCCFLAGS) $<
+	$(XCC) -c $(CFLAGS) $(PDCCFLAGS) $<
 
 firework.exe init_col.exe newtest.exe ozdemo.exe picsview.exe \
 ptest.exe rain.exe speed.exe testcurs.exe test_pan.exe \
 version.exe widetest.exe worm.exe xmas.exe: %.exe: $(demodir)/%.c
-	$(CC) $(CFLAGS) -o$@ $< $(LIBCURSES) $(EXELIBS)
+	$(XCC) $(CFLAGS) -o$@ $< $(LIBCURSES) $(EXELIBS)
 
 tuidemo.exe: tuidemo.o tui.o
 	$(LINK) $(LDFLAGS) -o$@ tuidemo.o tui.o $(LIBCURSES) $(EXELIBS)
 
 tui.o: $(demodir)/tui.c $(demodir)/tui.h $(PDCURSES_CURSES_H)
-	$(CC) -c $(CFLAGS) -I$(demodir) -o$@ $<
+	$(XCC) -c $(CFLAGS) -I$(demodir) -o$@ $<
 
 tuidemo.o: $(demodir)/tuidemo.c $(PDCURSES_CURSES_H)
-	$(CC) -c $(CFLAGS) -I$(demodir) -o$@ $<
+	$(XCC) -c $(CFLAGS) -I$(demodir) -o$@ $<
 
 PLATFORM1 = MinGW WinCon
 PLATFORM2 = MinGW for WinCon
 ARCNAME = pdc$(VER)_ming_wcon
-BUILD		= $(CC) $(CFLAGS) -I$(PDCURSES_SRCDIR) -mwindows $(LIBCURSES)
+BUILD		= $(XCC) $(CFLAGS) -I$(PDCURSES_SRCDIR) -mwindows $(LIBCURSES)
 
 include $(PDCURSES_SRCDIR)/demos/nctests.mif
@GitMensch
Copy link
Collaborator

Asking my gut says to stick to CC - allowing its override - instead of internally define an XCC.

@johnsonjh Can you please send a PR for review and possibly easier integration?
Also: Shouldn't this be applied to more ports than wincon?

@johnsonjh
Copy link
Author

Sure - I’ll get a PR together shortly that is more complete

@GitMensch
Copy link
Collaborator

@johnsonjh - any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants