Makefile 3.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

nhynes committed
18
SGX_SDK ?= /opt/sgxsdk
nhynes committed
19
RUST_SGX_SDK ?= /opt/rust-sgx-sdk
nhynes committed
20
SGX_MODE ?= SIM
nhynes committed
21 22 23
DEBUG ?= true
NUM_THREADS ?= 4

24
TVM_DIR ?= $(shell git rev-parse --show-toplevel)
nhynes committed
25 26

export
nhynes committed
27 28 29 30 31 32 33 34 35 36 37 38

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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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
56

nhynes committed
57
build_dir := build
nhynes committed
58

nhynes committed
59 60 61 62 63 64 65 66
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
67 68

enclave_ldflags :=\
nhynes committed
69
	-L$(build_dir) -L$(TVM_DIR)/build \
nhynes committed
70 71 72 73
	-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
74
	-lenclave -ltvm_t\
nhynes committed
75 76 77
	-Wl,--end-group\
	-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined\
	-Wl,-pie,-eenclave_entry -Wl,--export-dynamic\
nhynes committed
78 79
	-Wl,--defsym,__ImageBase=0 -Wl,--gc-sections\
	-Wl,--version-script=enclave/enclave.lds
nhynes committed
80

nhynes committed
81
.PHONY: enclave clean
nhynes committed
82

nhynes committed
83
enclave: $(build_dir)/enclave.signed.so
nhynes committed
84

nhynes committed
85 86
$(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
87

nhynes committed
88 89
enclave/enclave.pem:
	curl -sSo $@ 'https://gist.githubusercontent.com/nhynes/8a2d80068a92e672f8b0b7d710ceb404/raw/2d5ae5fbe83198ede49465fdc6535065e093543b/tvm_sgx_demo.pem'
nhynes committed
90

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

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

nhynes committed
97 98 99
$(build_dir)/libenclave.a: enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a
	@mkdir -p $(@D)
	@cp $< $@
100

nhynes committed
101 102
enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a: enclave/**/*
	$(MAKE) -C enclave
nhynes committed
103 104

clean:
nhynes committed
105 106
	$(MAKE) -s -C enclave clean
	rm -rf build