summaryrefslogtreecommitdiff
path: root/Makefile
blob: 59fa3576478009da83afeb442910ab224e0fb48c (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
TARGET_ARCH?=$(shell uname -p)
BUILD_TYPE?=Debug
CROSS_BUILD?=0
HOST_OS?=linux
TARGET_OS?=linux
PARALLEL_BUILD?=1
## TODO: fix obs build break
OBS_BUILD?=OFF
COVERAGE_BUILD?=0
BENCHMARK_ACL_BUILD?=0
OPTIONS?=
UPDATE_MODULE?=1

# make TARGET and TYPE to lowercase
TARGET_ARCH_LC=$(shell echo $(TARGET_ARCH) | tr A-Z a-z)
BUILD_TYPE_LC=$(shell echo $(BUILD_TYPE) | tr A-Z a-z)
# we need base name 'arm` for all arm arch
TARGET_ARCH_BASE=$(TARGET_ARCH_LC)
ifneq (,$(findstring arm64,$(TARGET_ARCH_BASE)))
	# arm64 as target-arch comes from Android
	TARGET_ARCH_BASE=arm64
	ifdef ROOTFS_DIR
		ROOTFS_ARM64=$(ROOTFS_DIR)
		export ROOTFS_ARM64
	endif
	# For now Android is the only option for arm64
	TARGET_OS:=android
else ifneq (,$(findstring arm,$(TARGET_ARCH_BASE)))
	TARGET_ARCH_BASE=arm
	ifdef ROOTFS_DIR
		ROOTFS_ARM=$(ROOTFS_DIR)
		export ROOTFS_ARM
	endif
else ifneq (,$(findstring aarch64,$(TARGET_ARCH_BASE)))
	# aarch64 as target-arch comes from all except for Android
	TARGET_ARCH_BASE=aarch64
	ifdef ROOTFS_DIR
		ROOTFS_ARM64=$(ROOTFS_DIR)
		export ROOTFS_ARM64
	endif
endif
# Todo: we may set CROSS_BUILD=1 when ROOTFS_DIR is given
# the toolchain file, only for cross build
ifeq ($(CROSS_BUILD),1)
	TOOLCHAIN_FILE=cmake/config/config_$(TARGET_ARCH_LC)-$(TARGET_OS).cmake
	OPTION_TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=$(TOOLCHAIN_FILE)
else
	OPTION_TOOLCHAIN=
endif

ifeq ($(COVERAGE_BUILD),1)
	OPTIONS+= -DCOVERAGE_BUILD=1
else
	OPTIONS+= -DCOVERAGE_BUILD=0
endif

ifeq ($(BENCHMARK_ACL_BUILD),1)
	OPTIONS+= -DBUILD_BENCHMARK_ACL=1
endif

ifeq ($(PARALLEL_BUILD),1)
	# Get number of processors (linux only for now)
	ifeq ($(HOST_OS),linux)
		NPROCS?=$(shell grep -c ^processor /proc/cpuinfo)
	endif
endif

NPROCS?=1
WORKHOME=Product
WORKFOLDER=$(TARGET_ARCH_LC)-$(TARGET_OS).$(BUILD_TYPE_LC)
WORKDIR=$(WORKHOME)/$(WORKFOLDER)

BUILD_ROOT=$(WORKDIR)/obj
INSTALL_ROOT=$(WORKDIR)/out

BUILD_FOLDER=$(WORKFOLDER)/obj
INSTALL_FOLDER=$(WORKFOLDER)/out
BUILD_ALIAS=$(WORKHOME)/obj
INSTALL_ALIAS=$(WORKHOME)/out

INSTALL_PATH?=$(CURDIR)/$(WORKDIR)/out

TIMESTAMP_CONFIGURE=$(WORKDIR)/CONFIGURE
TIMESTAMP_BUILD=$(WORKDIR)/BUILD
TIMESTAMP_INSTALL=$(WORKDIR)/INSTALL

#
# for Build Arm Compute Library with SCONS
#
ACL_FOLDER=externals/acl
ACL_COMMAND=scons -j${NPROCS} neon=1 opencl=1 examples=0 embed_kernels=1 os=$(TARGET_OS)
ifeq ($(TARGET_ARCH_LC),armv7l)
	ACL_COMMAND+= arch=armv7a
	ACL_BUILD_OUT=armv7a-$(TARGET_OS)
else ifeq ($(TARGET_ARCH_LC),aarch64)
	ACL_COMMAND+= arch=arm64-v8a
	ACL_BUILD_OUT=arm64-v8a-$(TARGET_OS)
else ifeq ($(TARGET_ARCH_BASE),arm64)
	ACL_COMMAND+= arch=arm64-v8a
	ACL_BUILD_OUT=arm64-v8a-$(TARGET_OS)
endif
ifeq ($(TARGET_OS),android)
	ACL_COMMAND+= Werror=0
	ANDROID_GNUSTL_PATH=$(ROOTFS_ARM64)/bin:$(ROOTFS_ARM64)/aarch64-linux-android/bin:$$PATH
else
	ACL_COMMAND+= Werror=1
endif
ifeq ($(BUILD_TYPE_LC),debug)
	ACL_COMMAND+= debug=1 asserts=1
endif
ACL_FOLDER_NAME=$(ACL_BUILD_OUT).$(BUILD_TYPE_LC)
ACL_COMMAND+= build_dir=$(ACL_FOLDER_NAME)
ACL_FOLDER_BUILD=$(ACL_FOLDER)/build/$(ACL_FOLDER_NAME)
ifneq ($(EXT_ACL_FOLDER),)
	ACL_FOLDER_BUILD=$(EXT_ACL_FOLDER)
endif

ifeq ($(OBS_BUILD),ON)
	UPDATE_MODULE=0
endif

all: build

###
### Command (public)
###
configure: configure_internal

build: build_internal

install: $(TIMESTAMP_INSTALL)

clean:
	rm -rf $(WORKDIR)
	rm -rf externals/*.stamp

acl: internal_acl_build internal_acl_install

check: check_internal

runtime: runtime_build_internal

testbuild: test_build_internal

tflite: tflite_build_internal

###
### Command (internal)
###
configure_internal:
	mkdir -p $(BUILD_ROOT)
	cmake -B$(CURDIR)/$(BUILD_ROOT) -H$(CURDIR) \
		-DCMAKE_INSTALL_PREFIX=$(INSTALL_PATH) \
		-DCMAKE_BUILD_TYPE=$(BUILD_TYPE_LC) -DTARGET_ARCH=$(TARGET_ARCH_LC) \
		-DHOST_OS=$(HOST_OS) \
		-DTARGET_OS=$(TARGET_OS) \
		-DOBS_BUILD=$(OBS_BUILD) \
		$(OPTION_TOOLCHAIN) \
		$(OPTIONS)
	touch $(TIMESTAMP_CONFIGURE)

build_internal: $(BUILD_ROOT)
	cd $(BUILD_ROOT) && make -j $(NPROCS) all
	rm -rf $(BUILD_ALIAS)
	ln -s $(BUILD_FOLDER) $(BUILD_ALIAS)
	touch $(TIMESTAMP_BUILD)

install_internal:
	cd $(BUILD_ROOT) && make install
	rm -rf $(INSTALL_ALIAS)
	ln -s $(INSTALL_FOLDER) $(INSTALL_ALIAS)
	touch $(TIMESTAMP_INSTALL)

internal_acl_build: configure_internal
ifeq ($(EXT_ACL_FOLDER),)
ifeq ($(TARGET_OS),android)
	cd $(ACL_FOLDER) && CXX=clang++ CC=clang PATH=$(ANDROID_GNUSTL_PATH) $(ACL_COMMAND)
else
	cd $(ACL_FOLDER) && $(ACL_COMMAND)
endif
endif

internal_acl_install:
	@echo $(ACL_FOLDER_BUILD)
	@mkdir -vp $(INSTALL_PATH)/lib
	@cp -v $(ACL_FOLDER_BUILD)/libarm_compute_core.so $(INSTALL_PATH)/lib/.
	@cp -v $(ACL_FOLDER_BUILD)/libarm_compute_graph.so $(INSTALL_PATH)/lib/.
	@cp -v $(ACL_FOLDER_BUILD)/libarm_compute.so $(INSTALL_PATH)/lib/.

external_acl:
ifneq ($(EXT_ACL_FOLDER),)
	@mkdir -vp $(INSTALL_PATH)/lib
	@cp -v $(EXT_ACL_FOLDER)/libarm_compute_core.so $(INSTALL_PATH)/lib/.
	@cp -v $(EXT_ACL_FOLDER)/libarm_compute_graph.so $(INSTALL_PATH)/lib/.
	@cp -v $(EXT_ACL_FOLDER)/libarm_compute.so $(INSTALL_PATH)/lib/.
endif

ifeq ($(CROSS_BUILD),1)
check_internal: test_internal_cross
else
check_internal: test_internal
endif

# TODO: Enable tests on cross build
test_internal_cross:
	@echo "Can't do tests on cross build"

test_internal:
	tools/test_driver/test_driver.sh .

build_test_suite: install_internal
	@echo "packaging test suite"
	@rm -rf $(INSTALL_ROOT)/test-suite.tar.gz
	@tar -zcf test-suite.tar.gz tests/ tools/test_driver $(INSTALL_ALIAS) --dereference
	@mv test-suite.tar.gz $(INSTALL_ROOT)/.

build_coverage_suite: install_internal
	@echo "packaging test-coverage suite"
	@rm -rf $(INSTALL_ROOT)/coverage-suite.tar.gz
	@find Product -name "*.gcno" > include_lists.txt
	@tar -zcf coverage-suite.tar.gz tests/ tools/test_driver $(INSTALL_ALIAS) --dereference -T include_lists.txt
	@rm -rf include_lists.txt
	@mv coverage-suite.tar.gz $(INSTALL_ROOT)/.

runtime_build_internal: $(BUILD_ROOT)
	cd $(BUILD_ROOT) && make -j $(NPROCS) nnapi_pure_arm_compute
	rm -rf $(BUILD_ALIAS)
	ln -s $(BUILD_FOLDER) $(BUILD_ALIAS)
	touch $(TIMESTAMP_BUILD)

test_build_internal: $(BUILD_ROOT)
	# Build test
	cd $(BUILD_ROOT) && make -j $(NPROCS) nnapi_test
	cd $(BUILD_ROOT) && make -j $(NPROCS) tflite_benchmark
	# Build unittest
	cd $(BUILD_ROOT) && make -j $(NPROCS) runtime_run_android_nn_test
	rm -rf $(BUILD_ALIAS)
	ln -s $(BUILD_FOLDER) $(BUILD_ALIAS)
	touch $(TIMESTAMP_BUILD)

tflite_build_internal: $(BUILD_ROOT)
	# Build test
	cd $(BUILD_ROOT) && make -j $(NPROCS) tensorflow-lite gtest_main
	rm -rf $(BUILD_ALIAS)
	ln -s $(BUILD_FOLDER) $(BUILD_ALIAS)
	touch $(TIMESTAMP_BUILD)

###
### Timestamps
###
$(WORKDIR):
	mkdir -p $@

$(BUILD_ROOT): $(WORKDIR)
	make configure_internal

$(TIMESTAMP_CONFIGURE):
	make configure_internal

$(TIMESTAMP_BUILD): $(TIMESTAMP_CONFIGURE)
	make build_internal

$(TIMESTAMP_INSTALL): $(TIMESTAMP_BUILD)
	make install_internal