Makefile 2.55 KB
Newer Older
nhynes committed
1
SGX_SDK ?= /opt/sgxsdk
nhynes committed
2
RUST_SGX_SDK ?= /opt/rust-sgx-sdk
nhynes committed
3
SGX_MODE ?= SIM
nhynes committed
4 5 6
DEBUG ?= true
NUM_THREADS ?= 4

7
TVM_DIR ?= $(shell git rev-parse --show-toplevel)
nhynes committed
8 9

export
nhynes committed
10 11 12 13 14 15 16 17 18 19 20 21

sgx_edger8r := $(SGX_SDK)/bin/x64/sgx_edger8r
sgx_enclave_signer := $(SGX_SDK)/bin/x64/sgx_sign

ifneq ($(SGX_MODE), HW)
	sgx_sim := _sim
endif
urts_library_name := sgx_urts$(sgx_sim)
trts_library_name := sgx_trts$(sgx_sim)
tservice_library_name := sgx_tservice$(sgx_sim)
uservice_library_name := sgx_uae_service$(sgx_sim)

nhynes committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
pkg_cflags := -std=c++11 -fPIC \
	-I$(SGX_SDK)/include \
	-I$(TVM_DIR)/include \
	-I$(TVM_DIR)/dlpack/include \
	-I$(TVM_DIR)/dmlc-core/include

pkg_ldflags := -L$(TVM_DIR)/build -ltvm_runtime

ifneq ($(DEBUG), false)
	debug := debug
	enclave_cflags += -Og -g
	pkg_cflags += -Og -g
else
	debug := release
	enclave_cflags += -O2
	pkg_cflags += -O2
endif
nhynes committed
39

nhynes committed
40
build_dir := build
nhynes committed
41

nhynes committed
42 43 44 45 46 47 48 49
enclave_cflags := \
	-I$(SGX_SDK)/include \
	-I$(SGX_SDK)/include/tlibc \
	-I$(SGX_SDK)/include/stdport \
	-I$(SGX_SDK)/include/epid \
	-I$(TVM_DIR)/include \
	-I$(TVM_DIR)/dlpack/include \
	-I$(TVM_DIR)/dmlc-core/include
nhynes committed
50 51

enclave_ldflags :=\
nhynes committed
52
	-L$(build_dir) -L$(TVM_DIR)/build \
nhynes committed
53 54 55 56
	-Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_SDK)/lib64\
	-Wl,--whole-archive -l$(trts_library_name) -Wl,--no-whole-archive\
	-Wl,--start-group\
	-lsgx_tstdc -lsgx_tstdcxx -lsgx_tcxx -lsgx_tcrypto -lsgx_tkey_exchange -l$(tservice_library_name)\
nhynes committed
57
	-lenclave -ltvm_t\
nhynes committed
58 59 60
	-Wl,--end-group\
	-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined\
	-Wl,-pie,-eenclave_entry -Wl,--export-dynamic\
nhynes committed
61 62
	-Wl,--defsym,__ImageBase=0 -Wl,--gc-sections\
	-Wl,--version-script=enclave/enclave.lds
nhynes committed
63

nhynes committed
64
.PHONY: enclave clean
nhynes committed
65

nhynes committed
66
enclave: $(build_dir)/enclave.signed.so
nhynes committed
67

nhynes committed
68 69
$(build_dir)/enclave.signed.so: $(build_dir)/enclave.so build/enclave_config.xml enclave/enclave.pem
	$(sgx_enclave_signer) sign -key enclave/enclave.pem -enclave $< -out $@ -config build/enclave_config.xml
nhynes committed
70

nhynes committed
71 72
enclave/enclave.pem:
	curl -sSo $@ 'https://gist.githubusercontent.com/nhynes/8a2d80068a92e672f8b0b7d710ceb404/raw/2d5ae5fbe83198ede49465fdc6535065e093543b/tvm_sgx_demo.pem'
nhynes committed
73

nhynes committed
74 75
build/enclave_config.xml: enclave/enclave_config.xml.in
	cpp $^ -P -o $@ -DNUM_THREADS=$$(( $(NUM_THREADS) + 1 ))
nhynes committed
76

nhynes committed
77 78
$(build_dir)/enclave.so: $(build_dir)/libenclave.a $(TVM_DIR)/build/libtvm_t.a
	$(CXX) $< -o $@ $(enclave_ldflags) $(enclave_cflags) -ltvm_t
nhynes committed
79

nhynes committed
80 81 82
$(build_dir)/libenclave.a: enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a
	@mkdir -p $(@D)
	@cp $< $@
83

nhynes committed
84 85
enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a: enclave/**/*
	$(MAKE) -C enclave
nhynes committed
86 87

clean:
nhynes committed
88 89
	$(MAKE) -s -C enclave clean
	rm -rf build