-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
194 lines (165 loc) · 5.54 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
MAKEFLAGS += --warn-undefined-variables
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.DEFAULT_GOAL := all
.DELETE_ON_ERROR:
.SUFFIXES:
-include Makefile.local
CODESIGN_IDENTITY ?=
CRFLAGS ?=
CRYSTAL ?= $(shell which crystal)
HOST_ARCH := $(shell uname -m)
HOST_OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
MESON ?= $(shell which meson)
PREFIX ?= /usr/local
RELEASE ?=
SHARDS ?= $(shell which shards)
SKIP_CODESIGN ?=
SKIP_NOTARIZE ?=
STATIC ?=
STRIP_RPATH ?=
SOURCES := src/*.cr src/**/*.cr
TAG_NAME ?= $(shell git describe --tags)
TARGET_ARCH ?= $(HOST_ARCH)
TARGET_CABI ?=
TARGET_OS ?= $(HOST_OS)
TARGET_TRIPLE ?= $(TARGET_OS)-$(TARGET_ARCH)$(if $(TARGET_CABI),-$(TARGET_CABI),)
TARGET_BUILD_DIR ?= .build/$(TARGET_TRIPLE)
TARGET_CROSS_FILE ?= config/$(TARGET_TRIPLE).ini
TARGET_DIST_PATH ?= dist/mstrap-$(TAG_NAME)-$(subst -,_,$(TARGET_TRIPLE)).zip
# Force static compilation on musl
ifeq ($(TARGET_CABI),musl)
override STATIC=1
endif
MESON_FLAGS ?= \
--prefix=$(PREFIX) \
-Dcrystal=$(CRYSTAL) \
-Dshards=$(SHARDS) \
$(if $(RELEASE),--buildtype=release --strip,--buildtype=debug) \
$(if $(STATIC),--default-library=static,--default-library=shared)
ifeq ($(shell command -v xcodebuild > /dev/null && echo true),true)
XCODE_VERSION := $(shell xcodebuild -version | awk '/Xcode/ {print $$2}' | tr -d '')
# Use legacy linker with xcode 15 for now
ifeq ($(XCODE_VERSION),15.0)
override CRFLAGS += --link-flags=-Wl,-ld_classic
endif
endif
# Add cross-files if target and host are different
ifeq ($(shell [[ "$(TARGET_OS)" != "$(HOST_OS)" || "$(TARGET_ARCH)" != "$(HOST_ARCH)" || ! -z "$(TARGET_CABI)" ]] && echo true),true)
override MESON_FLAGS += --cross-file=$(TARGET_CROSS_FILE)
else
# Do some special stuff on macOS
ifeq ($(shell [[ "$(TARGET_OS)" == "$(HOST_OS)" && "$(TARGET_OS)" == "darwin" ]] && echo true),true)
ifeq ($(shell command -v brew > /dev/null && echo true),true)
ifeq ($(shell brew ls --versions openssl@3 > /dev/null && echo true),true)
export PKG_CONFIG_PATH=$(shell brew --prefix openssl@3)/lib/pkgconfig
override MESON_FLAGS += --pkg-config-path=$(PKG_CONFIG_PATH)
else ifeq ($(shell brew ls --versions openssl@1.1 > /dev/null && echo true),true)
export PKG_CONFIG_PATH=$(shell brew --prefix openssl@1.1)/lib/pkgconfig
override MESON_FLAGS += --pkg-config-path=$(PKG_CONFIG_PATH)
endif
endif
endif
endif
.PHONY: all
all: build
$(TARGET_BUILD_DIR)/build.ninja: meson.build $(TARGET_CROSS_FILE)
@if [ ! -z "$(MESON)" ]; then \
mkdir -p $(TARGET_BUILD_DIR); \
if [ -f "$@" ]; then \
$(MESON) setup $(MESON_FLAGS) --reconfigure $(TARGET_BUILD_DIR); \
else \
$(MESON) setup $(MESON_FLAGS) $(TARGET_BUILD_DIR); \
fi; \
else \
echo "FAIL: meson must be installed"; \
exit 1; \
fi
$(TARGET_BUILD_DIR)/mstrap: $(SOURCES) $(TARGET_BUILD_DIR)/build.ninja
@if [ ! -z "$(MESON)" ]; then \
$(MESON) compile -v -C $(TARGET_BUILD_DIR); \
else \
echo "FAIL: meson must be installed"; \
exit 1; \
fi
bin/mstrap: $(TARGET_BUILD_DIR)/mstrap
mkdir -p bin
cp $(TARGET_BUILD_DIR)/mstrap bin/mstrap
@if [ ! -z "$(STRIP_RPATH)" ] && [ "$(TARGET_OS)" == "linux" ] && readelf -p1 bin/mstrap | grep -q 'linuxbrew'; then \
patchelf --remove-rpath bin/mstrap; \
patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 bin/mstrap; \
fi
dist/mstrap.zip: bin/mstrap codesign
@mkdir -p dist
zip --junk-paths dist/mstrap.zip bin/mstrap
.PHONY: build
build: bin/mstrap
.PHONY: codesign
codesign: bin/mstrap
@if [ "$(TARGET_OS)" == "darwin" ] && [ -z "$(SKIP_CODESIGN)" ]; then \
codesign -f -v \
--timestamp \
--options runtime \
--entitlements "macos-entitlements.plist" \
-s "$(CODESIGN_IDENTITY)" \
bin/mstrap; \
fi
.PHONY: notarize
notarize: dist/mstrap.zip
@if [ "$(TARGET_OS)" == "darwin" ] && [ -z "$(SKIP_NOTARIZE)" ]; then \
xcrun notarytool submit \
--keychain-profile "mstrap" \
--wait \
dist/mstrap.zip; \
fi
.PHONY: deps
deps: shard.yml shard.lock
$(SHARDS) check || $(SHARDS) install
docs: $(SOURCES)
$(CRYSTAL) docs
.PHONY: format
format:
$(CRYSTAL) tool format
.PHONY: lint
lint: deps
$(CRYSTAL) run $(CRFLAGS) bin/ameba.cr
.PHONY: clean
clean:
rm -f ./bin/mstrap*
rm -rf ./dist
rm -rf ./docs
rm -rf ./.build
.PHONY: spec
spec: deps $(SOURCES)
$(CRYSTAL) spec $(CRFLAGS) -Dmt_no_expectations --error-trace
.PHONY: check-formatting
check-formatting: $(SOURCES)
$(CRYSTAL) tool format --check
.PHONY: check-libraries
check-libraries: bin/mstrap
@if [ ! -z "$(STATIC)" ] && [ "$(TARGET_OS)" == "darwin" ] && [ "$$(otool -LX bin/mstrap | awk '{print $$1}' | sort)" != "$$(cat expected.libs.darwin)" ]; then \
echo "FAIL: bin/mstrap has non-allowed dynamic libraries"; \
exit 1; \
else \
echo "OK: bin/mstrap has only allowed dynamic libraries"; \
exit 0; \
fi
.PHONY: check-provisioning
check-provisioning:
cd $(CURDIR)/spec/provisioning && \
(bundle check || bundle install) && \
bundle exec rspec
.PHONY: test
test: check-formatting spec check-libraries
.PHONY: release
release: dist/mstrap.zip notarize
@mv dist/mstrap.zip $(TARGET_DIST_PATH)
echo "Release zip saved to $(TARGET_DIST_PATH)"
.PHONY: smoke-test
smoke-test: $(TARGET_BUILD_DIR)/mstrap
BUILD_DIR=$(TARGET_BUILD_DIR) \
TEST_NAME=$(TEST_NAME) \
$(CURDIR)/spec/provisioning/test.sh
.PHONY: install
install: $(TARGET_BUILD_DIR)/mstrap $(TARGET_BUILD_DIR)/build.ninja meson.build
$(MESON) install -C $(TARGET_BUILD_DIR) --tags runtime