DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Back | Log | Files | Refs | Issues

makefile (10586B)


      1 # PC32 ColorForth - builds on ANY x86-64 Linux with no multilib and no root, for Linux and Windows.
      2 # Everything it needs is fetched into this directory (nothing is installed system-wide):
      3 #
      4 #   sysroot32/        32-bit headers + libs for the Linux build   (tools/mk32root.py; build-time only)
      5 #   mingw/            MinGW-w64 cross toolchain for the Windows build   (tools/mkmingw.py; build-time only)
      6 #   third_party/glfw  GLFW 3.3.8 source, compiled here with plain make (no cmake) and linked statically
      7 #   lib32/            32-bit runtime bundle for Linux (libc, loader, X11, Mesa libGL + software driver)
      8 #   build/            object files
      9 #
     10 # Linux                                         Windows (see makefile.win, WINDOWS_PORT.md)
     11 #   make             a.out + lib32/               make win         PC32.exe (one static 32-bit exe)
     12 #   make run         build + run (run.sh)         make windist     PC32-win32.zip (exe + data.blk + MC.bin)
     13 #   make appimage    PC32-x86_64.AppImage         make winslim     delete mingw/ (build-time only)
     14 #   make slim        delete sysroot32/            make winclean    remove PC32.exe + objects
     15 #
     16 #   make dist        AppImage + Windows zip       make help        this list
     17 #   make clean       remove objects and a.out     make distclean   remove everything generated
     18 #
     19 # data.blk (and data/MC.bin) live next to the Linux binary/AppImage, not inside it; the AppImage seeds them on first run.
     20 
     21 SYSROOT ?= $(CURDIR)/sysroot32
     22 # Fetch the sysroot only when a recipe that needs it actually runs.  (Not an order-only prerequisite:
     23 # make builds those whenever they are missing, which would re-download after `make slim`.)
     24 NEEDSYS = @test -f $(SYSROOT)/.done || python3 -I tools/mk32root.py $(SYSROOT)
     25 
     26 # make install: PC32 command + program in PREFIX (no root needed with the default)
     27 PREFIX   ?= $(HOME)/.local
     28 BINDIR   ?= $(PREFIX)/bin
     29 SHAREDIR ?= $(PREFIX)/share/PC32
     30 CC      = gcc
     31 NASM    = $(shell command -v nasm 2>/dev/null || echo $(SYSROOT)/host/usr/bin/nasm)
     32 MULTI   = $(SYSROOT)/usr/lib/i386-linux-gnu
     33 LIB32   = $(SYSROOT)/lib/i386-linux-gnu
     34 GCC32   = $(wildcard $(SYSROOT)/usr/lib/gcc/i686-linux-gnu/*/)
     35 HOSTINC = $(shell $(CC) -print-file-name=include)
     36 
     37 SYSINC = -nostdinc -isystem $(HOSTINC) -isystem $(HOSTINC)-fixed \
     38          -isystem $(SYSROOT)/usr/include/i386-linux-gnu -isystem $(SYSROOT)/usr/include
     39 
     40 # ---- GLFW from source (X11 backend) ----
     41 GLFWDIR  = third_party/glfw
     42 GLFWSRC  = context init input monitor vulkan window x11_init x11_monitor x11_window \
     43            xkb_unicode posix_time posix_thread glx_context egl_context osmesa_context linux_joystick
     44 GLFWOBJ  = $(addprefix build/glfw/,$(addsuffix .o,$(GLFWSRC)))
     45 GLFWFLAGS = -m32 -fno-pie -O2 -w -D_GLFW_X11 -D_DEFAULT_SOURCE $(SYSINC) -I$(GLFWDIR)/include
     46 
     47 CFLAGS = -w -g -O0 -Wno-format -m32 -fno-pie $(SYSINC) -I$(GLFWDIR)/include
     48 
     49 # static: glfw, GLU, mikmod, libstdc++ (GLU needs it).  dynamic (bundled in lib32): GL, X11, libc.
     50 # the program is linked against the sysroot but run by lib32/ld-linux.so.2 (see run.sh)
     51 LDFLAGS = -m32 -no-pie -Wl,--sysroot=$(SYSROOT) -B$(MULTI)/ -B$(GCC32) -L$(MULTI) -L$(LIB32) -L$(GCC32) \
     52           -Wl,-dynamic-linker,/lib/ld-linux.so.2 -Wl,-rpath-link,$(MULTI):$(LIB32)
     53 LIBS = build/libglfw.a -Wl,-Bstatic -lGLU -lmikmod -lstdc++ -Wl,-Bdynamic \
     54        -lGL -lXrandr -lXinerama -lXcursor -lXi -lXxf86vm -lXext -lX11 -ldl -lm -lpthread
     55 
     56 # files cf.s incbin's (alloc/padmem/vmem are reserved inside cf.s with resb)
     57 DATA = data/sine.bin data/fnt.bin
     58 
     59 build: a.out lib32/.done
     60 	@echo "built a.out + lib32/  ->  ./run.sh        (all targets: make help)"
     61 
     62 a.out: $(DATA) cf.o main.o build/libglfw.a
     63 	$(CC) $(LDFLAGS) -o a.out main.o cf.o $(LIBS)
     64 
     65 lib32/.done: a.out tools/bundle32.py
     66 	$(NEEDSYS)
     67 	python3 -I tools/bundle32.py $(SYSROOT) a.out lib32
     68 
     69 $(SYSROOT)/.done sysroot:
     70 	python3 -I tools/mk32root.py $(SYSROOT)
     71 
     72 build/libglfw.a: $(GLFWOBJ)
     73 	ar rcs $@ $^
     74 
     75 build/glfw/%.o: $(GLFWDIR)/src/%.c
     76 	$(NEEDSYS)
     77 	@mkdir -p build/glfw
     78 	$(CC) $(GLFWFLAGS) -c $< -o $@
     79 
     80 main.o: main.c debuglog.h
     81 	$(NEEDSYS)
     82 	$(CC) $(CFLAGS) -c main.c
     83 
     84 cf.o asm: cf.s $(DATA)
     85 	$(NEEDSYS)
     86 	$(NASM) -g -felf32 cf.s -l cf_asm.txt -o cf.o
     87 
     88 data/sine.bin:
     89 	@mkdir -p data; if [ -f editor/data/sine.bin ]; then cp editor/data/sine.bin $@; else truncate -s 512 $@; fi
     90 # the font is built into the binary; if data/fnt.bin is missing, take the one from the project root
     91 data/fnt.bin:
     92 	@mkdir -p data; cp fnt.bin $@
     93 
     94 run: build
     95 	./run.sh
     96 
     97 debug: build
     98 	ddd a.out
     99 
    100 clean:
    101 	rm -rf main.o cf.o build a.out
    102 
    103 distclean: clean winclean
    104 	rm -rf sysroot32 lib32 mingw PC32-x86_64.AppImage PC32-win32.zip PC32-linux-src.zip PC32_debug.log PC32_crash.log
    105 
    106 # the sysroot is only needed to build; the program runs from a.out + lib32/ alone
    107 slim: build
    108 	rm -rf $(SYSROOT)
    109 	@echo "sysroot removed; it is re-fetched automatically if you change a source file"
    110 
    111 # ---- AppImage: 32-bit a.out + lib32 bundle in one file; data.blk/data.res stay external ----
    112 APPTOOL = tools/appimage/appimagetool-x86_64.AppImage
    113 APPRT   = tools/appimage/runtime-x86_64
    114 
    115 $(APPTOOL):
    116 	curl -fL -o $@ https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage
    117 	chmod +x $@
    118 $(APPRT):
    119 	curl -fL -o $@ https://github.com/AppImage/type2-runtime/releases/download/continuous/runtime-x86_64
    120 
    121 # the AppDir is staged in a temp dir (appimagetool needs symlinks, which e.g. VirtualBox
    122 # shared folders lack); only the finished AppImage is written back here, the temp dir is removed
    123 appimage: build data.blk data/MC.bin $(APPTOOL) $(APPRT)
    124 	set -e; T=$$(mktemp -d); trap 'rm -rf "$$T"' EXIT; D=$$T/AppDir; \
    125 	mkdir -p $$D/usr/bin $$D/usr/share/PC32; \
    126 	cp a.out $$D/usr/bin/; cp -r lib32 $$D/usr/lib32; \
    127 	cp data.blk data/MC.bin $$D/usr/share/PC32/; \
    128 	cp tools/appimage/AppRun tools/appimage/PC32.desktop $$D/; \
    129 	python3 -I tools/appimage/mkicon.py $$D/PC32.png; \
    130 	chmod -R a+rX $$D; chmod a+rx $$D/AppRun; \
    131 	ARCH=x86_64 $(APPTOOL) --appimage-extract-and-run --runtime-file $(APPRT) $$D $$T/PC32-x86_64.AppImage; \
    132 	cp $$T/PC32-x86_64.AppImage PC32-x86_64.AppImage
    133 
    134 # ---- install: `PC32` command that runs in the current folder (so any folder's data.blk works) ----
    135 # default PREFIX=$(HOME)/.local (no root); for all users: make && sudo make install PREFIX=/usr/local
    136 install: build data.blk data/MC.bin tools/PC32.in
    137 	install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(SHAREDIR)
    138 	rm -rf $(DESTDIR)$(SHAREDIR)/lib32
    139 	cp -r lib32 $(DESTDIR)$(SHAREDIR)/lib32
    140 	cp --sparse=always a.out $(DESTDIR)$(SHAREDIR)/a.out 2>/dev/null || cp a.out $(DESTDIR)$(SHAREDIR)/a.out
    141 	cp data.blk data/MC.bin $(DESTDIR)$(SHAREDIR)/
    142 	chmod -R a+rX $(DESTDIR)$(SHAREDIR); chmod a+rx $(DESTDIR)$(SHAREDIR)/a.out
    143 	sed 's|@SHAREDIR@|$(SHAREDIR)|' tools/PC32.in > $(DESTDIR)$(BINDIR)/PC32
    144 	chmod 755 $(DESTDIR)$(BINDIR)/PC32
    145 	@echo "installed: $(BINDIR)/PC32  (program in $(SHAREDIR))"
    146 	@case ":$$PATH:" in *":$(BINDIR):"*) ;; *) echo "note: $(BINDIR) is not in your PATH - add it to run 'PC32' from anywhere";; esac
    147 	@echo "usage: cd <folder with data.blk> && PC32      (or 'PC32 --new' to start a fresh folder)"
    148 
    149 uninstall:
    150 	rm -f $(DESTDIR)$(BINDIR)/PC32
    151 	rm -rf $(DESTDIR)$(SHAREDIR)
    152 	@echo "removed $(BINDIR)/PC32 and $(SHAREDIR)"
    153 
    154 # ---- Windows (cross-compiled with MinGW-w64, see makefile.win / WINDOWS_PORT.md) ----
    155 win:
    156 	$(MAKE) -f makefile.win
    157 
    158 winclean:
    159 	$(MAKE) -f makefile.win winclean
    160 
    161 # the zip to distribute: exe + a copy of the current data.blk + data/MC.bin (for the examples).
    162 # Everything else the program needs (font, sine table, memory) is built into the exe.
    163 windist: win data.blk data/MC.bin
    164 	python3 -I tools/mkzip.py PC32-win32.zip \
    165 		PC32.exe data.blk data/MC.bin \
    166 		tools/dist/README-windows.txt:README.txt
    167 
    168 # the cross toolchain is only needed to build
    169 winslim: win
    170 	rm -rf mingw
    171 	@echo "mingw removed; it is re-fetched automatically if you change a source file"
    172 
    173 # source package for Linux users: unzip, `make` (needs gcc, make, python3, network).  Only the build
    174 # inputs are included: code, makefiles, vendored GLFW, data.blk, the files cf.s embeds, the PDF manual
    175 # (docs/PC32_Manual.pdf only, not its generator or the ODT) and tools/
    176 # (your tools plus the build scripts; compiled a.out files, the downloaded AppImage tools,
    177 # tools/_deprecated and the test data.res are skipped).
    178 linuxdist: $(DATA) data.blk data/MC.bin docs/PC32_Manual.pdf
    179 	python3 -I tools/mkzip.py PC32-linux-src.zip --prefix PC32/ \
    180 		--exclude a.out --exclude appimagetool-x86_64.AppImage --exclude runtime-x86_64 --exclude __pycache__ \
    181 		--exclude _deprecated --exclude data.res \
    182 		makefile makefile.win cf.s main.c win32_compat.h debuglog.h run.sh README.md WINDOWS_PORT.md \
    183 		data.blk data/fnt.bin data/sine.bin data/MC.bin docs/PC32_Manual.pdf third_party/glfw tools
    184 
    185 dist: appimage windist linuxdist
    186 
    187 define HELPTEXT
    188 PC32 ColorForth - make targets
    189 
    190 Linux (32-bit program, no multilib needed)
    191   make              build ./a.out and the runtime bundle lib32/   (default)
    192   make run          build, then run ./run.sh
    193   make debug        build, then open a.out in ddd
    194   make asm          assemble cf.s only (cf.o + listing cf_asm.txt)
    195   make sysroot      (re)create sysroot32/ (32-bit headers + libs, fetched into this folder)
    196   make slim         build, then delete sysroot32/ (~600 MB, only needed to build)
    197   make appimage     PC32-x86_64.AppImage (a.out + lib32/ in one file; data files stay outside)
    198   make install      install the PC32 command (default PREFIX=~/.local, no root): run it in any folder
    199                     that has a data.blk; PREFIX=/usr/local with sudo for all users
    200   make uninstall    remove it again
    201 
    202 Windows (cross-compiled here with MinGW-w64, see makefile.win)
    203   make win          PC32.exe: one static 32-bit exe for Windows 7 or later
    204   make windist      PC32-win32.zip: exe + current data.blk + data/MC.bin + readme
    205   make winslim      build, then delete mingw/ (~370 MB, only needed to build)
    206   make winclean     remove PC32.exe and its objects
    207 
    208 Both
    209   make linuxdist    PC32-linux-src.zip: source package (code, makefiles, tools/, PDF manual, trimmed data/)
    210   make dist         AppImage + Windows zip + Linux source zip
    211   make clean        remove objects and a.out
    212   make distclean    remove everything generated, incl. sysroot32/, mingw/, lib32/, packages, logs
    213   make help         this list
    214 
    215 Run with PC32_DEBUG=1 to get PC32_debug.log.  More: README.md, WINDOWS_PORT.md
    216 endef
    217 export HELPTEXT
    218 
    219 help:
    220 	@echo "$$HELPTEXT"
    221 
    222 
    223 .PHONY: build run debug sysroot clean distclean asm slim appimage install uninstall win winclean windist winslim linuxdist dist help