From c688145a99005272ebd844d1f8623abf2cf2187e Mon Sep 17 00:00:00 2001 From: Maxat Mansurov Date: Mon, 13 Feb 2023 20:32:44 +0100 Subject: [PATCH] RUBY-29825 add a separate `Makefile.cross` for easier cross-compilation to other platforms. By default, we cross-compile for `aarch64`, but that could be adjusted via `HOST` variable. Also cleaned up the `Makefile` and updated README. Reviewed-by: IJ-CR-102814 GitOrigin-RevId: b65519b2ab6a3461f0e621d5dcc832b083948fef --- native/WslTools/.gitignore | 1 + native/WslTools/Makefile | 10 +++++----- native/WslTools/Makefile.cross | 20 ++++++++++++++++++++ native/WslTools/README.txt | 12 +++++++++++- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 native/WslTools/Makefile.cross diff --git a/native/WslTools/.gitignore b/native/WslTools/.gitignore index fd1db4f45907..3f1419e0f310 100644 --- a/native/WslTools/.gitignore +++ b/native/WslTools/.gitignore @@ -1,5 +1,6 @@ .idea musl +musl-*/ *.tgz *.tar.gz wslproxy diff --git a/native/WslTools/Makefile b/native/WslTools/Makefile index 5e5ede0e1fd3..e2112cbf7744 100644 --- a/native/WslTools/Makefile +++ b/native/WslTools/Makefile @@ -7,23 +7,23 @@ MUSL_HOME := $(HOME)/musl MUSL_CC := $(MUSL_HOME)/bin/musl-gcc CFLAGS = -Wall -Wextra -pedantic -Werror -Os -D_POSIX_SOURCE=1 -D_BSD_SOURCE=1 -LDLIBS = -static +LDFLAGS = -static CC = $(MUSL_CC) all: $(MUSL_CC) wslproxy wslhash -musl: +$(MUSL_DISTR): @echo I will now download musl. If it fails, check you have wget and see README wget https://musl.libc.org/releases/musl-$(MUSL_VER).tar.gz -O musl.tar.gz tar xfvz musl.tar.gz && mv musl-$(MUSL_VER) $(MUSL_DISTR) -$(MUSL_CC): musl +$(MUSL_CC): $(MUSL_DISTR) cd $(MUSL_DISTR) && ./configure --prefix=$(MUSL_HOME) --syslibdir=$(MUSL_HOME)/lib && $(MAKE) && $(MAKE) install -wslproxy: $(SOURCES) | $(MUSL_CC) +wslproxy: wslproxy.c | $(MUSL_CC) wslhash: wslhash.c xxhash.h xxhash.c | $(MUSL_CC) .PHONY: all clean clean: - $(RM) wslproxy wslhash \ No newline at end of file + $(RM) wslproxy wslhash diff --git a/native/WslTools/Makefile.cross b/native/WslTools/Makefile.cross new file mode 100644 index 000000000000..4251be2126cd --- /dev/null +++ b/native/WslTools/Makefile.cross @@ -0,0 +1,20 @@ +BUILD := $(shell uname -m) +HOST := aarch64 + +MUSL_CROSS_VER := 10 +MUSL_CROSS_DISTR := musl-$(HOST) +MUSL_CROSS_CC := ./$(MUSL_CROSS_DISTR)/bin/$(HOST)-linux-musl-gcc + +all: $(MUSL_CROSS_DISTR) $(MUSL_CROSS_CC) + make CC="$(MUSL_CROSS_CC)" $(MAKECMDGOALS) + +$(MUSL_CROSS_DISTR): + @echo I will now download musl cross-compile toolchain. If it fails, check you have wget and see README + wget "https://more.musl.cc/$(MUSL_CROSS_VER)/$(BUILD)-linux-musl/$(HOST)-linux-musl-cross.tgz" -O "$(MUSL_CROSS_DISTR).tgz" + tar xfvz "$(MUSL_CROSS_DISTR).tgz" + mv "$(HOST)-linux-musl-cross" "$(MUSL_CROSS_DISTR)" + +.PHONY: all clean + +clean: + $(RM) wslproxy wslhash diff --git a/native/WslTools/README.txt b/native/WslTools/README.txt index 0e1f88270f79..969af799157b 100644 --- a/native/WslTools/README.txt +++ b/native/WslTools/README.txt @@ -30,4 +30,14 @@ Getting musl manually: See https://wiki.musl-libc.org/getting-started.html for more info Q: I got error opening project in CLion: -A: Make sure you run "make". Then, tools->Makefile->Reload \ No newline at end of file +A: Make sure you run "make". Then, tools->Makefile->Reload + +Q: How do I compile binaries for aarch64 platform? +A: Run "make -f Makefile.cross clean all". The required musl cross-compiler will be downloaded and + all tools will be cross-compiled for aarch64 platform. + +Q: How do I check the target platform of the compiled binary? +A: Check the output of "file YOUR_BINARY". + +Q: How do I test that cross-compiled binary works on aarch64? +A: Run "sudo apt install qemu-user-static" (adjust for your distro). Then run "qemu-aarch64-static YOUR_BINARY".