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
18SGX_SDK ?= /opt/sgxsdk
19RUST_SGX_SDK ?= /opt/rust-sgx-sdk
20SGX_MODE ?= SIM
21DEBUG ?= true
22NUM_THREADS ?= 4
23
24TVM_DIR ?= $(shell git rev-parse --show-toplevel)
25
26export
27
28sgx_edger8r := $(SGX_SDK)/bin/x64/sgx_edger8r
29sgx_enclave_signer := $(SGX_SDK)/bin/x64/sgx_sign
30
31ifneq ($(SGX_MODE), HW)
32	sgx_sim := _sim
33endif
34urts_library_name := sgx_urts$(sgx_sim)
35trts_library_name := sgx_trts$(sgx_sim)
36tservice_library_name := sgx_tservice$(sgx_sim)
37uservice_library_name := sgx_uae_service$(sgx_sim)
38
39pkg_cflags := -std=c++11 -fPIC \
40	-I$(SGX_SDK)/include \
41	-I$(TVM_DIR)/include \
42	-I$(TVM_DIR)/dlpack/include \
43	-I$(TVM_DIR)/dmlc-core/include
44
45pkg_ldflags := -L$(TVM_DIR)/build -ltvm_runtime
46
47ifneq ($(DEBUG), false)
48	debug := debug
49	enclave_cflags += -Og -g
50	pkg_cflags += -Og -g
51else
52	debug := release
53	enclave_cflags += -O2
54	pkg_cflags += -O2
55endif
56
57build_dir := build
58
59enclave_cflags := \
60	-I$(SGX_SDK)/include \
61	-I$(SGX_SDK)/include/tlibc \
62	-I$(SGX_SDK)/include/stdport \
63	-I$(SGX_SDK)/include/epid \
64	-I$(TVM_DIR)/include \
65	-I$(TVM_DIR)/dlpack/include \
66	-I$(TVM_DIR)/dmlc-core/include
67
68enclave_ldflags :=\
69	-L$(build_dir) -L$(TVM_DIR)/build \
70	-Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_SDK)/lib64\
71	-Wl,--whole-archive -l$(trts_library_name) -Wl,--no-whole-archive\
72	-Wl,--start-group\
73	-lsgx_tstdc -lsgx_tstdcxx -lsgx_tcxx -lsgx_tcrypto -lsgx_tkey_exchange -l$(tservice_library_name)\
74	-lenclave -ltvm_t\
75	-Wl,--end-group\
76	-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined\
77	-Wl,-pie,-eenclave_entry -Wl,--export-dynamic\
78	-Wl,--defsym,__ImageBase=0 -Wl,--gc-sections\
79	-Wl,--version-script=enclave/enclave.lds
80
81.PHONY: enclave clean
82
83enclave: $(build_dir)/enclave.signed.so
84
85$(build_dir)/enclave.signed.so: $(build_dir)/enclave.so build/enclave_config.xml enclave/enclave.pem
86	$(sgx_enclave_signer) sign -key enclave/enclave.pem -enclave $< -out $@ -config build/enclave_config.xml
87
88enclave/enclave.pem:
89	curl -sSo $@ 'https://gist.githubusercontent.com/nhynes/8a2d80068a92e672f8b0b7d710ceb404/raw/2d5ae5fbe83198ede49465fdc6535065e093543b/tvm_sgx_demo.pem'
90
91build/enclave_config.xml: enclave/enclave_config.xml.in
92	cpp $^ -P -o $@ -DNUM_THREADS=$$(( $(NUM_THREADS) + 1 ))
93
94$(build_dir)/enclave.so: $(build_dir)/libenclave.a $(TVM_DIR)/build/libtvm_t.a
95	$(CXX) $< -o $@ $(enclave_ldflags) $(enclave_cflags) -ltvm_t
96
97$(build_dir)/libenclave.a: enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a
98	@mkdir -p $(@D)
99	@cp $< $@
100
101enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a: enclave/**/*
102	$(MAKE) -C enclave
103
104clean:
105	$(MAKE) -s -C enclave clean
106	rm -rf build
107