1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18# Makefile Example to bundle TVM modules.
19
20# Setup build environment
21TVM_ROOT=$(shell cd ../..; pwd)
22CRT_ROOT ?= ../../build/standalone_crt
23ifeq ($(shell ls -lhd $(CRT_ROOT)),)
24$(error "CRT not found. Ensure you have built the standalone_crt target and try again")
25endif
26
27ENABLE_TVM_PLATFORM_ABORT_BACKTRACE ?= 1
28
29DMLC_CORE=${TVM_ROOT}/3rdparty/dmlc-core
30PKG_COMPILE_OPTS = -g -Wall -O2 -fPIC
31PKG_CXXFLAGS = ${PKG_COMPILE_OPTS} -std=c++14 \
32	-I${TVM_ROOT}/include \
33	-I${DMLC_CORE}/include \
34	-I${TVM_ROOT}/3rdparty/dlpack/include \
35	-Icrt_config
36PKG_CFLAGS = ${PKG_COMPILE_OPTS} \
37	-I${TVM_ROOT}/include \
38	-I${DMLC_CORE}/include \
39	-I${TVM_ROOT}/3rdparty/dlpack/include \
40	-Icrt_config
41
42PKG_LDFLAGS = -pthread
43
44build_dir := build
45
46BACKTRACE_SRCS =
47BACKTRACE_LDFLAGS =
48BACKTRACE_CFLAGS =
49$(ifeq ENABLE_TVM_PLATFORM_ABORT_BACKTRACE,1)
50BACKTRACE_SRCS += backtrace.c
51BACKTRACE_LDFLAGS += -ldl
52BACKTRACE_CFLAGS += -DENABLE_TVM_PLATFORM_ABORT_BACKTRACE
53$(endif)
54
55BACKTRACE_OBJS = $(patsubst %.c,$(build_dir)/%.o,$(BACKTRACE_SRCS))
56
57$(ifeq VERBOSE,1)
58QUIET ?=
59$(else)
60QUIET ?= @
61$(endif)
62
63CRT_SRCS = $(shell find $(CRT_ROOT))
64
65demo_dynamic: $(build_dir)/demo_dynamic $(build_dir)/bundle.so $(build_dir)/bundle_c.so $(build_dir)/bundle.so $(build_dir)/graph_cpp.json $(build_dir)/graph_c.json $(build_dir)/params_cpp.bin $(build_dir)/params_c.bin $(build_dir)/cat.bin
66	$(QUIET)TVM_NUM_THREADS=1 $(build_dir)/demo_dynamic $(build_dir)/bundle.so $(build_dir)/graph_cpp.json $(build_dir)/params_cpp.bin $(build_dir)/cat.bin
67	$(QUIET)TVM_NUM_THREADS=1 $(build_dir)/demo_dynamic $(build_dir)/bundle_c.so $(build_dir)/graph_c.json $(build_dir)/params_c.bin $(build_dir)/cat.bin
68
69test_dynamic: $(build_dir)/test_dynamic $(build_dir)/test_bundle.so $(build_dir)/test_bundle_c.so $(build_dir)/test_data_c.bin $(build_dir)/test_output_c.bin $(build_dir)/test_data_cpp.bin $(build_dir)/test_output_cpp.bin
70	$(QUIET)TVM_NUM_THREADS=1 $(build_dir)/test_dynamic $(build_dir)/test_bundle.so $(build_dir)/test_data_cpp.bin $(build_dir)/test_output_cpp.bin $(build_dir)/test_graph_cpp.json $(build_dir)/test_params_cpp.bin
71	$(QUIET)TVM_NUM_THREADS=1 $(build_dir)/test_dynamic $(build_dir)/test_bundle_c.so $(build_dir)/test_data_c.bin $(build_dir)/test_output_c.bin $(build_dir)/test_graph_c.json $(build_dir)/test_params_c.bin
72
73demo_static: $(build_dir)/demo_static $(build_dir)/cat.bin
74	$(QUIET)TVM_NUM_THREADS=1 $(build_dir)/demo_static $(build_dir)/cat.bin
75
76test_static: $(build_dir)/test_static $(build_dir)/test_data_c.bin $(build_dir)/test_output_c.bin
77	$(QUIET)TVM_NUM_THREADS=1 $(build_dir)/test_static $(build_dir)/test_data_c.bin $(build_dir)/test_output_c.bin $(build_dir)/test_graph_c.json $(build_dir)/test_params_c.bin
78
79$(build_dir)/crt/libgraph_runtime.a: $(CRT_SRCS)
80	$(QUIET)cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath $(build_dir))/crt CRT_CONFIG=$(abspath crt_config/crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" graph_runtime
81
82$(build_dir)/crt/libcommon.a: $(CRT_SRCS)
83	$(QUIET)cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath $(build_dir))/crt CRT_CONFIG=$(abspath crt_config/crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" common
84
85$(build_dir)/demo_dynamic: demo.cc
86	$(QUIET)mkdir -p $(@D)
87	$(QUIET)g++ $(PKG_CXXFLAGS) -o $@ demo.cc $(BACKTRACE_LDFLAGS)
88
89$(build_dir)/test_dynamic: test.cc ${build_dir}/test_graph_c.json ${build_dir}/test_params_c.bin $(BACKTRACE_OBJS)
90	$(QUIET)mkdir -p $(@D)
91	$(QUIET)g++ $(PKG_CXXFLAGS) -o $@ test.cc $(BACKTRACE_OBJS) $(BACKTRACE_LDFLAGS)
92
93$(build_dir)/demo_static: demo_static.c ${build_dir}/bundle_static.o ${build_dir}/model_c.o ${build_dir}/crt/libgraph_runtime.a ${build_dir}/crt/libcommon.a ${build_dir}/graph_c.json.c ${build_dir}/params_c.bin.c $(BACKTRACE_OBJS)
94	$(QUIET)mkdir -p $(@D)
95	$(QUIET)gcc $(PKG_CFLAGS) -o $@ $^ $(BACKTRACE_CFLAGS)
96
97$(build_dir)/test_static: test_static.c ${build_dir}/bundle_static.o ${build_dir}/test_model_c.o ${build_dir}/crt/libgraph_runtime.a ${build_dir}/crt/libcommon.a $(BACKTRACE_OBJS)
98	$(QUIET)mkdir -p $(@D)
99	$(QUIET)gcc $(PKG_CFLAGS) -o $@ $^ $(BACKTRACE_LDFLAGS)
100
101$(build_dir)/backtrace.o: backtrace.c
102	$(QUIET)mkdir -p $(@D)
103	$(QUIET)gcc -c $(PKG_CFLAGS) -o $@ $^ $(BACKTRACE_CFLAGS)
104
105# Serialize our graph.json file.
106$(build_dir)/graph_cpp.json.c: $(build_dir)/graph_cpp.json
107	$(QUIET)xxd -i $^  > $@
108
109$(build_dir)/graph_c.json.c: $(build_dir)/graph_c.json
110	$(QUIET)xxd -i $^  > $@
111
112# Serialize our params.bin file.
113$(build_dir)/params_c.bin.c: $(build_dir)/params_c.bin
114	$(QUIET)xxd -i $^  > $@
115
116$(build_dir)/params_cpp.bin.c: $(build_dir)/params_cpp.bin
117	$(QUIET)xxd -i $^  > $@
118
119$(build_dir)/model_c.o $(build_dir)/graph_c.json $(build_dir)/model_cpp.o $(build_dir)/graph_cpp.json $(build_dir)/params.bin $(build_dir)/cat.bin: build_model.py
120	$(QUIET)python3 $< -o $(build_dir)
121
122$(build_dir)/test_model_c.o $(build_dir)/test_graph_c.json $(build_dir)/test_params_c.bin $(build_dir)/test_data_c.bin $(build_dir)/test_output_c.bin $(build_dir)/test_model_cpp.o $(build_dir)/test_graph_cpp.json $(build_dir)/test_params_cpp.bin $(build_dir)/test_data_cpp.bin $(build_dir)/test_output_cpp.bin: build_model.py
123	$(QUIET)python3 $< -o $(build_dir) --test
124
125# Build our bundle against the serialized bundle.c API, the runtime.cc API, and
126# the serialized graph.json and params.bin
127$(build_dir)/bundle.so: bundle.cc runtime.cc $(build_dir)/model_cpp.o
128	$(QUIET)mkdir -p $(@D)
129	$(QUIET)g++ -shared $(PKG_CXXFLAGS) -fvisibility=hidden -o $@  $^ $(PKG_LDFLAGS)
130
131$(build_dir)/bundle_c.so: bundle.c $(build_dir)/model_c.o ${build_dir}/crt/libgraph_runtime.a ${build_dir}/crt/libcommon.a $(BACKTRACE_OBJS)
132	$(QUIET)mkdir -p $(@D)
133	$(QUIET)gcc -shared $(PKG_CFLAGS) -fvisibility=hidden -o $@  $^ $(PKG_LDFLAGS) $(BACKTRACE_LDFLAGS) $(BACKTRACE_CFLAGS)
134
135$(build_dir)/test_bundle.so: bundle.cc runtime.cc $(build_dir)/test_model_cpp.o
136	$(QUIET)mkdir -p $(@D)
137	$(QUIET)g++ -shared $(PKG_CXXFLAGS) -fvisibility=hidden -o $@  $^ $(PKG_LDFLAGS)
138
139$(build_dir)/test_bundle_c.so: bundle.c $(build_dir)/test_model_c.o ${build_dir}/crt/libgraph_runtime.a ${build_dir}/crt/libcommon.a $(BACKTRACE_OBJS)
140	$(QUIET)mkdir -p $(@D)
141	$(QUIET)gcc -shared $(PKG_CFLAGS) -fvisibility=hidden -o $@  $^ $(PKG_LDFLAGS) $(BACKTRACE_LDFLAGS) $(BACKTRACE_CFLAGS)
142
143$(build_dir)/bundle_static.o: bundle_static.c
144	$(QUIET)mkdir -p $(@D)
145	$(QUIET)gcc -c $(PKG_CFLAGS) -o $@  $^ $(BACKTRACE_CFLAGS)
146
147clean:
148	$(QUIET)rm -rf $(build_dir)/bundle.so $(build_dir)/bundle_c.so $(build_dir)/test_bundle.so $(build_dir)/test_bundle_c.so $(build_dir)/crt
149
150cleanall:
151	$(QUIET)rm -rf $(build_dir)
152
153# Don't define implicit rules; they tend to match on logical target names that aren't targets (i.e. bundle_static)
154.SUFFIXES:
155
156.DEFAULT: demo_static demo_dynamic
157
158test: test_static test_dynamic
159.PHONY: test
160