Simpler cross-compiling for multiple platforms

For a project of mine I use the following Makefile code to create binaries for different platforms:

VERSION=$(shell cat VERSION)

TARGET ?= x86_64-linux
NICE_TARGET ?= ${TARGET}
VM ?= cs
# Both options are allowed for Posix and Windows.
TARGET_OPTS ?= --orig-exe --embed-dlls
# `raco exe` appends `.exe` for Windows automatically.
EXE_NAME := sudoku-solver-${VERSION}-${NICE_TARGET}
EXE_PATH := build/${NICE_TARGET}/${EXE_NAME}

# Build standalone binary.
.PHONY : build
build:
	mkdir -p "build/${NICE_TARGET}"
	# Ignore uninstalled package.
	-raco cross --target "${TARGET}" --vm ${VM} pkg remove sudoku-solver
	# Implicitly install dependencies.
	raco cross --target "${TARGET}" --vm ${VM} -j 4 pkg install --deps search-auto
	raco cross --target "${TARGET}" --vm ${VM} -j 4 exe ${TARGET_OPTS} \
		-o "${EXE_PATH}" games/sudoku-solver.rkt

.PHONY : build-x86_64-linux
build-x86_64-linux: build

.PHONY : build-x86_64-win32
build-x86_64-win32:
	TARGET=x86_64-win32 NICE_TARGET=x86_64-windows make build

.PHONY : build-x86_64-macosx
build-x86_64-macosx:
	TARGET=x86_64-macosx make build

.PHONY : build-aarch64-macosx
build-aarch64-macosx:
	TARGET=aarch64-macosx make build

# Create standalone binaries for different platforms.
.PHONY : build-all
build-all: build-x86_64-linux build-x86_64-win32 build-x86_64-macosx \
	build-aarch64-macosx

I run make build-all from the Git working directory root of my package.

I think it would make sense to make this more standalone, i.e. usable without Make, and of course independent of a specific hardcoded project and version. I could write a command line tool in Racket for this, but before I do it, I'd like to ask if you know of something like this that exists already. I'd like to avoid double work. :slight_smile:

As for the packaging, I guess the above is too specialized to be a part of raco cross or even a raco subcommand. So probably this should be a raco pkg install-able standalone script.

What do you think?

3 Likes

I think it could be raco pkg install-able raco subcommand, but if that isn't desirable for some reason a standalone script works too.
I am not aware of something that does this already, so far it has been on the back of my mind as something I might need eventually.

1 Like

I think a way to easily generate standalone binaries also for other platforms is pretty important. Since Racket is used so little compared with mainstream languages, I assume it's unlikely that a non-Racketeer will install Racket for a command line program unless the program is extremely interesting. :wink:

1 Like

I think I'll start with a standalone script and probably convert that to a raco subcommand later. I'm still not sure how to call the subcommand, though.

  • build-all would relate to the use in the makefile, but it's easy to expect that the command will literally build all, for example a tarball and documentation.
  • exe-all would be nice to hint at the similarity to raco exe, but my subcommand would actually do more, so the name might be misleading. But then, the functionality would be very similar to raco exe.
  • exe-multitarget? This is a bit long, but I like that it's more explicit.
  • Any other suggestions?