github.com/wrgl/wrgl@v0.14.0/Makefile (about)

     1  SHELL = /bin/bash
     2  .DEFAULT_GOAL = all
     3  
     4  GO := go
     5  MD5 := md5
     6  DOCKER := docker
     7  BUILD_DIR := build
     8  MD5_DIR := $(BUILD_DIR)/md5
     9  OS_ARCHS := linux-amd64 darwin-amd64 darwin-arm64
    10  BINARIES := $(foreach osarch,$(OS_ARCHS),$(BUILD_DIR)/wrgl-$(osarch)/bin/wrgl)
    11  WRGL_TAR_FILES := $(foreach osarch,$(OS_ARCHS),$(BUILD_DIR)/wrgl-$(osarch).tar.gz)
    12  COMMON_LDFLAGS = -X github.com/wrgl/wrgl/cmd/wrgl.version=$(VERSION)
    13  
    14  .PHONY: all clean
    15  all: $(WRGL_TAR_FILES)
    16  clean:
    17  	rm -rf $(BUILD_DIR)
    18  
    19  # Check that given variables are set and all have non-empty values,
    20  # die with an error otherwise.
    21  #
    22  # Params:
    23  #   1. Variable name(s) to test.
    24  #   2. (optional) Error message to print.
    25  check_defined = \
    26      $(strip $(foreach 1,$1, \
    27          $(call __check_defined,$1,$(strip $(value 2)))))
    28  __check_defined = \
    29      $(if $(value $1),, \
    30        $(error Undefined $1$(if $2, ($2))))
    31  
    32  $(call check_defined, VERSION)
    33  
    34  define binary_rule =
    35  echo "\$$(BUILD_DIR)/wrgl-$(2)-$(1)/bin/wrgl: \$$(MD5_DIR)/go.sum.md5 \$$(wrgl_SOURCES)" >> $(3) && \
    36  echo -e "\t@-mkdir -p \$$(dir \$$@) 2>/dev/null" >> $(3) && \
    37  (if [ "$(2)" == "linux" ]; then \
    38    echo -e "\tenv CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ GOARCH=amd64 GOOS=linux CGO_ENABLED=1 go build -ldflags \"-linkmode external -extldflags -static \$$(COMMON_LDFLAGS)\" -a -o \$$@ github.com/wrgl/wrgl" >> $(3); \
    39  else \
    40    echo -e "\tCGO_ENABLED=1 GOARCH=$(1) GOOS=$(2) go build -ldflags \"\$$(COMMON_LDFLAGS)\" -a -o \$$@ github.com/wrgl/wrgl" >> $(3); \
    41  fi) && \
    42  echo "" >> $(3)
    43  
    44  endef
    45  
    46  $(BUILD_DIR)/wrgl.d: | $(BUILD_DIR)
    47  	echo "wrgl_SOURCES =" > $@
    48  	echo "$$($(GO) list -deps github.com/wrgl/wrgl | \
    49  		grep github.com/wrgl/wrgl/ | \
    50  		sed -r -e 's/github.com\/wrgl\/wrgl\/(.+)/\1/g' | \
    51  		xargs -n 1 -I {} find {} -maxdepth 1 -name '*.go' \! -name '*_test.go' -print | \
    52  		sed -r -e 's/(.+)/$(subst /,\/,wrgl_SOURCES += $(MD5_DIR))\/\1.md5/g')" >> $@
    53  	echo "" >> $@
    54  	$(foreach osarch,$(OS_ARCHS),$(call binary_rule,$(word 2,$(subst -, ,$(osarch))),$(word 1,$(subst -, ,$(osarch))),$@))
    55  
    56  define license_rule =
    57  $(BUILD_DIR)/wrgl-$(1)/LICENSE: LICENSE
    58  	cp $$< $$@
    59  endef
    60  
    61  define tar_rule =
    62  $(BUILD_DIR)/wrgl-$(1).tar.gz: $(BUILD_DIR)/wrgl-$(1)/bin/wrgl $(BUILD_DIR)/wrgl-$(1)/LICENSE
    63  	cd $(BUILD_DIR) && \
    64  	tar -czvf $$(notdir $$@) wrgl-$(1)
    65  endef
    66  
    67  # calculate md5
    68  $(MD5_DIR)/%.md5: % | $(MD5_DIR)
    69  	@-mkdir -p $(dir $@) 2>/dev/null
    70  	$(if $(filter-out $(shell cat $@ 2>/dev/null),$(shell $(MD5) $<)),$(MD5) $< > $@)
    71  
    72  $(foreach osarch,$(OS_ARCHS),$(eval $(call wrgld_bin_rule,$(osarch))))
    73  
    74  $(foreach osarch,$(OS_ARCHS),$(eval $(call license_rule,$(osarch))))
    75  
    76  $(foreach osarch,$(OS_ARCHS),$(eval $(call tar_rule,$(osarch))))
    77  
    78  $(BUILD_DIR): ; @-mkdir $@ 2>/dev/null
    79  $(MD5_DIR): | $(BUILD_DIR) ; @-mkdir $@ 2>/dev/null
    80  
    81  include $(BUILD_DIR)/wrgl.d