comandy/Makefile

62 lines
2.4 KiB
Makefile
Raw Permalink Normal View History

2024-04-15 17:10:06 +09:00
# Tested under Apple M1.
# Edit it if you use different platform.
2024-04-15 02:39:28 +09:00
PROJECT_NAME := comandy
BUILD_PATH := build
GOOS := android
GOARCH := arm64
BUILD_MACHINE := darwin
BUILD_ARCH := x86_64
NDK_VERSION := 26.3.11579264
TARGET_SDK := android23
2024-04-15 17:41:05 +09:00
TARGET_ARCH := aarch64 # optional: armv7a i686 x86_64
2024-04-16 20:59:47 +09:00
TEST_INSTALL_PATH := /usr/local/lib
2024-04-15 02:39:28 +09:00
CGO_ENABLED := 1
2024-04-15 23:58:32 +09:00
GO_SRC := $(shell find . -name '*.go' | grep -v '_test.go$$')
2024-04-16 15:27:06 +09:00
NDK_HOME := ~/Library/Android/sdk/ndk/$(NDK_VERSION)
NDK_TOOLCHAIN := $(NDK_HOME)/toolchains/llvm/prebuilt/$(BUILD_MACHINE)-$(BUILD_ARCH)
2024-04-15 17:41:05 +09:00
CC := $(NDK_TOOLCHAIN)/bin/$(TARGET_ARCH)-linux-$(TARGET_SDK)-clang
2024-04-16 15:27:06 +09:00
TEST_EXPECTED := '{"code":200,'
2024-04-15 02:39:28 +09:00
2024-04-16 15:27:06 +09:00
all:
2024-04-15 17:41:05 +09:00
@BUILD_PATH=$(BUILD_PATH)/aarch64 TARGET_ARCH=aarch64 GOARCH=arm64 $(MAKE) -e shared
@BUILD_PATH=$(BUILD_PATH)/armv7a TARGET_ARCH=armv7a GOARCH=arm TARGET_SDK=androideabi23 $(MAKE) -e shared
@BUILD_PATH=$(BUILD_PATH)/i686 TARGET_ARCH=i686 GOARCH=amd64 $(MAKE) -e shared
@BUILD_PATH=$(BUILD_PATH)/x86_64 TARGET_ARCH=x86_64 GOARCH=386 $(MAKE) -e shared
rm -rf $(BUILD_PATH)/*/*.h
2024-04-15 18:36:34 +09:00
cd $(BUILD_PATH) && gzip -9 -k aarch64/* armv7a/* i686/* x86_64/*
find $(BUILD_PATH) -mindepth 1 -maxdepth 1 -type d -exec mv {}/lib$(PROJECT_NAME).so.gz {}_lib$(PROJECT_NAME).so.gz \;; \
2024-04-15 17:41:05 +09:00
cd $(BUILD_PATH) && zip -r -9 $(BUILD_PATH).zip aarch64 armv7a i686 x86_64
2024-04-15 17:10:06 +09:00
shared: $(GO_SRC) dir tidy
2024-04-15 02:39:28 +09:00
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) NDK_TOOLCHAIN=$(NDK_TOOLCHAIN) CC=$(CC) go build -buildmode=c-shared -o $(BUILD_PATH)/lib$(PROJECT_NAME).so $(GO_SRC)
test: dir
@GOOS=$(BUILD_MACHINE) CC=cc NDK_TOOLCHAIN="" $(MAKE) -e shared
cc -o $(BUILD_PATH)/test $(BUILD_PATH)/test.c -l$(PROJECT_NAME) -L$(BUILD_PATH)
2024-04-16 15:27:06 +09:00
runtest:
@if [ ! -f "$(BUILD_PATH)/test" ]; then \
$(MAKE) -e test; \
fi
2024-04-16 20:47:54 +09:00
@TEST_OUTPUT=$$(cd $(BUILD_PATH) && ./test | head -c 12); \
2024-04-16 15:27:06 +09:00
if [ $$TEST_OUTPUT = $(TEST_EXPECTED) ]; then \
2024-04-15 02:39:28 +09:00
echo "test succeeded."; \
else \
2024-04-16 15:27:06 +09:00
echo "test failed, expected:" $(TEST_EXPECTED) "but got:" $$TEST_OUTPUT; \
2024-04-16 20:43:26 +09:00
exit 1; \
2024-04-15 02:39:28 +09:00
fi
2024-04-15 17:10:06 +09:00
tidy:
go mod tidy
2024-04-15 02:39:28 +09:00
dir:
@if [ ! -d "$(BUILD_PATH)" ]; then mkdir $(BUILD_PATH); fi
2024-04-16 20:59:47 +09:00
install: test
sudo cp $(BUILD_PATH)/lib$(PROJECT_NAME).so $(TEST_INSTALL_PATH)/
sudo ldconfig
2024-04-15 02:39:28 +09:00
clean:
2024-04-15 17:10:06 +09:00
@if [ -d "$(BUILD_PATH)" ]; then \
rm -rf $(BUILD_PATH)/lib$(PROJECT_NAME).*; \
rm -rf $(BUILD_PATH)/test; \
2024-04-15 17:54:02 +09:00
rm -rf $(BUILD_PATH)/*.zip; \
2024-04-15 18:36:34 +09:00
rm -rf $(BUILD_PATH)/*.gz; \
2024-04-15 17:54:02 +09:00
find $(BUILD_PATH) -mindepth 1 -maxdepth 1 -type d -exec rm -rf {} \;; \
2024-04-15 17:10:06 +09:00
fi