Makefile 11 KB
Newer Older
1 2
ROOTDIR = $(CURDIR)

3 4
ifndef config
ifneq ("$(wildcard ./config.mk)","")
5
	config ?= config.mk
6
else
7
	config ?= make/config.mk
8 9 10 11
endif
endif

include $(config)
tqchen committed
12

13
.PHONY: clean install installdev all test doc pylint cpplint lint verilog cython cython2 cython3 web runtime
14

ziheng committed
15 16 17 18 19 20 21 22
ifndef DMLC_CORE_PATH
  DMLC_CORE_PATH = $(ROOTDIR)/dmlc-core
endif

ifndef DLPACK_PATH
  DLPACK_PATH = $(ROOTDIR)/dlpack
endif

23 24 25
UNAME_S := $(shell uname -s)

# The flags
26
LLVM_CFLAGS= -fno-rtti -DDMLC_ENABLE_RTTI=0 -DDMLC_USE_FOPEN64=0
27 28 29
LDFLAGS = -pthread -lm -ldl
INCLUDE_FLAGS = -Iinclude -I$(DLPACK_PATH)/include -I$(DMLC_CORE_PATH)/include -IHalideIR/src -Itopi/include
CFLAGS = -std=c++11 -Wall -O2 $(INCLUDE_FLAGS) -fPIC
30
PKG_LDFLAGS =
31 32
FRAMEWORKS =
OBJCFLAGS = -fno-objc-arc
33 34
EMCC_FLAGS= -std=c++11 -DDMLC_LOG_STACK_TRACE=0\
	-Oz -s RESERVED_FUNCTION_POINTERS=2 -s MAIN_MODULE=1 -s NO_EXIT_RUNTIME=1\
35
	-s TOTAL_MEMORY=1073741824\
36
	-s EXTRA_EXPORTED_RUNTIME_METHODS="['cwrap','getValue','setValue','addFunction']"\
37
	-s USE_GLFW=3 -s USE_WEBGL2=1 -lglfw\
38
	$(INCLUDE_FLAGS)
39 40 41 42 43 44 45 46 47 48
# llvm configuration
ifdef LLVM_CONFIG
	LLVM_VERSION=$(shell $(LLVM_CONFIG) --version| cut -b 1,3)
	LLVM_INCLUDE=$(filter -I%, $(shell $(LLVM_CONFIG) --cxxflags))
	LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags --libs --system-libs)
	LLVM_CFLAGS += $(LLVM_INCLUDE) -DTVM_LLVM_VERSION=$(LLVM_VERSION)
else
	LLVM_VERSION=00
endif

49 50
# The source code dependencies
LIB_HALIDEIR = HalideIR/lib/libHalideIR.a
51

52
CC_SRC = $(filter-out src/contrib/%.cc src/runtime/%.cc src/codgen/llvm/%.cc,\
53
             $(wildcard src/*/*.cc src/*/*/*.cc))
54
LLVM_SRC = $(wildcard src/codegen/llvm/*.cc src/codegen/llvm/*/*.cc)
55
METAL_SRC = $(wildcard src/runtime/metal/*.mm)
56
CUDA_SRC = $(wildcard src/runtime/cuda/*.cc)
57
ROCM_SRC = $(wildcard src/runtime/rocm/*.cc)
58
OPENCL_SRC = $(wildcard src/runtime/opencl/*.cc)
59
OPENGL_SRC = $(wildcard src/runtime/opengl/*.cc)
60
VULKAN_SRC = $(wildcard src/runtime/vulkan/*.cc)
nhynes committed
61
SGX_SRC = $(wildcard src/runtime/sgx/untrusted/*.cc)
62
RPC_SRC = $(wildcard src/runtime/rpc/*.cc)
63
GRAPH_SRC = $(wildcard src/runtime/graph/*.cc)
64
RUNTIME_SRC = $(wildcard src/runtime/*.cc)
65
TOPI_SRC = $(wildcard topi/src/*.cc)
66 67

# Objectives
68 69
LLVM_BUILD = build/llvm${LLVM_VERSION}
LLVM_OBJ = $(patsubst src/%.cc, ${LLVM_BUILD}/%.o, $(LLVM_SRC))
70
METAL_OBJ = $(patsubst src/%.mm, build/%.o, $(METAL_SRC))
71
CUDA_OBJ = $(patsubst src/%.cc, build/%.o, $(CUDA_SRC))
72
ROCM_OBJ = $(patsubst src/%.cc, build/%.o, $(ROCM_SRC))
73
OPENCL_OBJ = $(patsubst src/%.cc, build/%.o, $(OPENCL_SRC))
74
OPENGL_OBJ = $(patsubst src/%.cc, build/%.o, $(OPENGL_SRC))
75
VULKAN_OBJ = $(patsubst src/%.cc, build/%.o, $(VULKAN_SRC))
nhynes committed
76
SGX_OBJ = $(patsubst src/%.cc, build/%.o, $(SGX_SRC)) build/runtime/sgx/untrusted/tvm_u.o
77
RPC_OBJ = $(patsubst src/%.cc, build/%.o, $(RPC_SRC))
78
GRAPH_OBJ = $(patsubst src/%.cc, build/%.o, $(GRAPH_SRC))
79
CC_OBJ = $(patsubst src/%.cc, build/%.o, $(CC_SRC)) $(LLVM_OBJ)
80
RUNTIME_OBJ = $(patsubst src/%.cc, build/%.o, $(RUNTIME_SRC))
81
TOPI_OBJ = $(patsubst topi/%.cc, build/%.o, $(TOPI_SRC))
82
CONTRIB_OBJ =
tqchen committed
83

84 85 86
# Deps
ALL_DEP = $(CC_OBJ) $(CONTRIB_OBJ) $(LIB_HALIDEIR)
RUNTIME_DEP = $(RUNTIME_OBJ)
87
TOPI_DEP = $(TOPI_OBJ)
88

89 90 91 92
ifeq ($(UNAME_S), Darwin)
	PKG_LDFLAGS += -undefined dynamic_lookup
endif

93
# Dependency specific rules
94 95 96
ifdef CUDA_PATH
	NVCC=$(CUDA_PATH)/bin/nvcc
	CFLAGS += -I$(CUDA_PATH)/include
97 98 99 100 101
	ifeq ($(UNAME_S),Darwin)
		LDFLAGS += -L$(CUDA_PATH)/lib
	else
		LDFLAGS += -L$(CUDA_PATH)/lib64
	endif
102 103
endif

104
ifeq ($(USE_CUDA), 1)
105
	CFLAGS += -DTVM_CUDA_RUNTIME=1
106
	LDFLAGS += -lcuda -lcudart -lnvrtc
107
	RUNTIME_DEP += $(CUDA_OBJ)
108 109 110 111
else
	CFLAGS += -DTVM_CUDA_RUNTIME=0
endif

112
ifdef ROCM_PATH
113 114
	CFLAGS += -I$(ROCM_PATH)/include
	LDFLAGS += -L$(ROCM_PATH)/lib
115 116 117
endif

ifeq ($(USE_ROCM), 1)
118
	CFLAGS += -DTVM_ROCM_RUNTIME=1 -D__HIP_PLATFORM_HCC__=1
119 120 121 122 123 124
	LDFLAGS += -lhip_hcc
	RUNTIME_DEP += $(ROCM_OBJ)
else
	CFLAGS += -DTVM_ROCM_RUNTIME=0
endif

125
ifeq ($(USE_OPENCL), 1)
126 127 128 129 130 131
	CFLAGS += -DTVM_OPENCL_RUNTIME=1
	ifeq ($(UNAME_S), Darwin)
		FRAMEWORKS += -framework OpenCL
	else
		LDFLAGS += -lOpenCL
	endif
132
	RUNTIME_DEP += $(OPENCL_OBJ)
133 134 135 136
ifdef OPENCL_PATH
        CFLAGS += -I$(OPENCL_PATH)/include
        LDFLAGS += -L$(OPENCL_PATH)/lib
endif
137 138 139 140
else
	CFLAGS += -DTVM_OPENCL_RUNTIME=0
endif

141 142 143 144 145 146 147 148 149 150 151 152 153 154
ifdef VULKAN_SDK
	CFLAGS += -I$(VULKAN_SDK)/include
	LDFLAGS += -L$(VULKAN_SDK)/lib
	LDFLAGS += -L$(VULKAN_SDK)/lib/spirv-tools
endif

ifeq ($(USE_VULKAN), 1)
	CFLAGS += -DTVM_VULKAN_RUNTIME=1
	LDFLAGS += -lvulkan -lSPIRV-Tools
	RUNTIME_DEP += $(VULKAN_OBJ)
else
	CFLAGS += -DTVM_VULKAN_RUNTIME=0
endif

155 156 157 158 159 160 161 162 163 164 165 166 167
ifeq ($(USE_OPENGL), 1)
	CFLAGS += -DTVM_OPENGL_RUNTIME=1
	EMCC_FLAGS += -DTVM_OPENGL_RUNTIME=1
	ifeq ($(UNAME_S), Darwin)
		FRAMEWORKS += -framework OpenGL
	else
		LDFLAGS += -lGL -lglfw
	endif
	RUNTIME_DEP += $(OPENGL_OBJ)
else
	CFLAGS += -DTVM_OPENGL_RUNTIME=0
endif

168
ifeq ($(USE_METAL), 1)
169
	CFLAGS += -DTVM_METAL_RUNTIME=1
170
	LDFLAGS += -lobjc
171 172 173 174 175 176
	RUNTIME_DEP += $(METAL_OBJ)
	FRAMEWORKS += -framework Metal -framework Foundation
else
	CFLAGS += -DTVM_METAL_RUNTIME=0
endif

nhynes committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190
ifeq ($(USE_SGX), 1)
	EDGER8R = $(SGX_SDK)/bin/x64/sgx_edger8r
	ifneq ($(SGX_MODE), HW)
		sgx_sim := _sim
	endif
	urts_library_name := sgx_urts$(sgx_sim)
	CFLAGS += -DTVM_SGX_RUNTIME=1
	SGX_CFLAGS = -include "build/runtime/sgx/untrusted/tvm_u.h" -I$(SGX_SDK)/include
	LDFLAGS += -L$(SGX_SDK)/lib64 -l$(urts_library_name)
	RUNTIME_DEP += $(SGX_OBJ)
else
	CFLAGS += -DTVM_SGX_RUNTIME=0
endif

191 192 193 194
ifeq ($(USE_RPC), 1)
	RUNTIME_DEP += $(RPC_OBJ)
endif

195 196 197 198
ifeq ($(USE_GRAPH_RUNTIME), 1)
	RUNTIME_DEP += $(GRAPH_OBJ)
endif

199 200 201 202
ifeq ($(USE_GRAPH_RUNTIME_DEBUG), 1)
	CFLAGS += -DTVM_GRAPH_RUNTIME_DEBUG
endif

203
include make/contrib/cblas.mk
204
include make/contrib/sort.mk
205
include make/contrib/random.mk
206
include make/contrib/nnpack.mk
207
include make/contrib/cudnn.mk
208
include make/contrib/miopen.mk
209
include make/contrib/mps.mk
210
include make/contrib/cublas.mk
211
include make/contrib/rocblas.mk
212

213
ifdef ADD_CFLAGS
214 215 216
	CFLAGS += $(ADD_CFLAGS)
endif

217
ifdef ADD_LDFLAGS
218 219
	LDFLAGS += $(ADD_LDFLAGS)
endif
220

221 222
ifeq ($(OS),Windows_NT)
	JVM_PKG_PROFILE := windows
223
	SHARED_LIBRARY_SUFFIX := dll
224 225 226 227
else
	UNAME_S := $(shell uname -s)
	ifeq ($(UNAME_S), Darwin)
		JVM_PKG_PROFILE := osx-x86_64
228
		SHARED_LIBRARY_SUFFIX := dylib
229 230
	else
		JVM_PKG_PROFILE := linux-x86_64
231
		SHARED_LIBRARY_SUFFIX := so
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	endif
endif

JVM_TEST_ARGS := $(if $(JVM_TEST_ARGS),$(JVM_TEST_ARGS),-DskipTests -Dcheckstyle.skip=true)

ifeq ($(USE_CUDA), 1)
	JVM_PKG_PROFILE := $(JVM_PKG_PROFILE)-gpu
else ifeq ($(USE_OPENCL), 1)
	JVM_PKG_PROFILE := $(JVM_PKG_PROFILE)-gpu
else ifeq ($(USE_METAL), 1)
	JVM_PKG_PROFILE := $(JVM_PKG_PROFILE)-gpu
else
	JVM_PKG_PROFILE := $(JVM_PKG_PROFILE)-cpu
endif

247 248 249 250
BUILD_TARGETS ?= lib/libtvm.$(SHARED_LIBRARY_SUFFIX) \
	lib/libtvm_runtime.$(SHARED_LIBRARY_SUFFIX) \
  lib/libtvm_topi.$(SHARED_LIBRARY_SUFFIX)

251 252 253
all: ${BUILD_TARGETS}
runtime: lib/libtvm_runtime.$(SHARED_LIBRARY_SUFFIX)
web: lib/libtvm_web_runtime.js lib/libtvm_web_runtime.bc
254
topi: lib/libtvm_topi.$(SHARED_LIBRARY_SUFFIX)
255

tqchen committed
256 257 258 259
include tests/cpp/unittest.mk

test: $(TEST)

260 261 262
include verilog/verilog.mk
verilog: $(VER_LIBS)

263
# Special rules for LLVM related modules.
264
${LLVM_BUILD}/codegen/llvm/%.o: src/codegen/llvm/%.cc
tqchen committed
265
	@mkdir -p $(@D)
266
	$(CXX) $(CFLAGS) $(LLVM_CFLAGS) -MM -MT ${LLVM_BUILD}/codegen/llvm/$*.o $< >${LLVM_BUILD}/codegen/llvm/$*.d
267
	$(CXX) -c $(CFLAGS) $(LLVM_CFLAGS) -c $< -o $@
tqchen committed
268

269
build/runtime/metal/%.o: src/runtime/metal/%.mm
270
	@mkdir -p $(@D)
Tianqi Chen committed
271
	$(CXX) $(OBJCFLAGS) $(CFLAGS) -MM -MT build/runtime/metal/$*.o $< >build/runtime/metal/$*.d
272 273
	$(CXX) $(OBJCFLAGS) -c $(CFLAGS) -c $< -o $@

nhynes committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
build/runtime/sgx/untrusted/tvm_u.h: src/runtime/sgx/tvm.edl
	@mkdir -p $(@D)
	$(EDGER8R) $< --untrusted --untrusted-dir $(@D) --search-path $(SGX_SDK)/include
	mv $@ $@.in
	awk 'NR==4{print "#include <tvm/runtime/c_runtime_api.h>"}1' $@.in > $@

build/runtime/sgx/untrusted/tvm_u.c: build/runtime/sgx/untrusted/tvm_u.h

build/runtime/sgx/untrusted/tvm_u.o: build/runtime/sgx/untrusted/tvm_u.c
	$(CC) $(CFLAGS) $(SGX_CFLAGS) -c $< -o $@

build/runtime/sgx/untrusted/%.o: src/runtime/sgx/untrusted/%.cc build/runtime/sgx/untrusted/tvm_u.h
	$(CXX) $(CFLAGS) $(SGX_CFLAGS) -MM -MT build/$*.o $< >build/$*.d
	$(CXX) -c $(CFLAGS) $(SGX_CFLAGS) -c $< -o $@

289 290 291 292 293
build/%.o: src/%.cc
	@mkdir -p $(@D)
	$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d
	$(CXX) -c $(CFLAGS) -c $< -o $@

294 295 296 297 298
build/src/%.o: topi/src/%.cc
	@mkdir -p $(@D)
	$(CXX) $(CFLAGS) -MM -MT build/src/$*.o $< >build/src/$*.d
	$(CXX) -c $(CFLAGS) -c $< -o $@

299
lib/libtvm.${SHARED_LIBRARY_SUFFIX}: $(ALL_DEP) $(RUNTIME_DEP)
300 301 302
	@mkdir -p $(@D)
	$(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)

303
lib/libtvm_topi.${SHARED_LIBRARY_SUFFIX}: $(TOPI_DEP)
304
	@mkdir -p $(@D)
305
	$(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS) $(PKG_LDFLAGS)
306

307
lib/libtvm_runtime.${SHARED_LIBRARY_SUFFIX}: $(RUNTIME_DEP)
tqchen committed
308
	@mkdir -p $(@D)
309
	$(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)
tqchen committed
310

tqchen committed
311

312 313 314
lib/libtvm_web_runtime.bc: web/web_runtime.cc
	@mkdir -p build/web
	@mkdir -p $(@D)
315
	emcc $(EMCC_FLAGS) -MM -MT lib/libtvm_web_runtime.bc $< >build/web/web_runtime.d
316 317 318 319 320
	emcc $(EMCC_FLAGS) -o $@ web/web_runtime.cc

lib/libtvm_web_runtime.js: lib/libtvm_web_runtime.bc
	@mkdir -p $(@D)
	emcc $(EMCC_FLAGS) -o $@ lib/libtvm_web_runtime.bc
321

322
$(LIB_HALIDEIR): LIBHALIDEIR
323 324

LIBHALIDEIR:
325
	+ cd HalideIR; make lib/libHalideIR.a DMLC_CORE_PATH=../dmlc-core; cd $(ROOTDIR)
326

327
cpplint:
328
	python dmlc-core/scripts/lint.py topi cpp topi/include;
329 330
	python dmlc-core/scripts/lint.py tvm cpp include src verilog\
	 examples/extension/src examples/graph_executor/src
331 332 333

pylint:
	pylint python/tvm --rcfile=$(ROOTDIR)/tests/lint/pylintrc
334
	pylint topi/python/topi --rcfile=$(ROOTDIR)/tests/lint/pylintrc
335

336
jnilint:
337 338 339
	python dmlc-core/scripts/lint.py tvm4j-jni cpp jvm/native/src

lint: cpplint pylint jnilint
tqchen committed
340

341 342 343
doc:
	doxygen docs/Doxyfile

344 345 346 347 348 349 350 351 352 353 354 355
install: lib/libtvm_runtime.$(SHARED_LIBRARY_SUFFIX)
	mkdir -p $(DESTDIR)$(PREFIX)/include/tvm/runtime
	cp -R include/tvm/runtime/. $(DESTDIR)$(PREFIX)/include/tvm/runtime
	cp lib/libtvm_runtime.$(SHARED_LIBRARY_SUFFIX) $(DESTDIR)$(PREFIX)/lib

installdev: lib/libtvm.$(SHARED_LIBRARY_SUFFIX) lib/libtvm_runtime.$(SHARED_LIBRARY_SUFFIX) lib/libtvm.a
	mkdir -p $(DESTDIR)$(PREFIX)/include
	cp -R include/tvm $(DESTDIR)$(PREFIX)/include
	cp lib/libtvm.$(SHARED_LIBRARY_SUFFIX) $(DESTDIR)$(PREFIX)/lib
	cp lib/libtvm_runtime.$(SHARED_LIBRARY_SUFFIX) $(DESTDIR)$(PREFIX)/lib
	cp lib/libtvm.a $(DESTDIR)$(PREFIX)/lib

356 357 358 359 360 361 362 363 364 365 366
# Cython build
cython:
	cd python; python setup.py build_ext --inplace

cython2:
	cd python; python2 setup.py build_ext --inplace

cython3:
	cd python; python3 setup.py build_ext --inplace

cyclean:
367
	rm -rf python/tvm/*/*/*.so python/tvm/*/*/*.dylib python/tvm/*/*/*.cpp
368

369 370 371 372 373
jvmpkg:
	(cd $(ROOTDIR)/jvm; \
		mvn clean package -P$(JVM_PKG_PROFILE) -Dcxx="$(CXX)" \
			-Dcflags="$(CFLAGS)" -Dldflags="$(LDFLAGS)" \
			-Dcurrent_libdir="$(ROOTDIR)/lib" $(JVM_TEST_ARGS))
Yizhi Liu committed
374 375 376 377 378
jvminstall:
	(cd $(ROOTDIR)/jvm; \
		mvn install -P$(JVM_PKG_PROFILE) -Dcxx="$(CXX)" \
			-Dcflags="$(CFLAGS)" -Dldflags="$(LDFLAGS)" \
			-Dcurrent_libdir="$(ROOTDIR)/lib" $(JVM_TEST_ARGS))
379

tqchen committed
380
clean:
tqchen committed
381
	$(RM) -rf build lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o */*.d */*/*.d */*/*/*.d
382
	cd HalideIR; make clean; cd $(ROOTDIR)
tqchen committed
383 384 385

-include build/*.d
-include build/*/*.d
386
-include build/*/*/*.d
Tianqi Chen committed
387
-include build/*/*/*/*.d