|
| 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 | +######## SGX SDK Settings ######## |
| 19 | + |
| 20 | +SGX_SDK ?= /opt/intel/sgxsdk |
| 21 | +SGX_MODE ?= HW |
| 22 | +SGX_ARCH ?= x64 |
| 23 | + |
| 24 | +TOP_DIR := ../.. |
| 25 | +include $(TOP_DIR)/buildenv.mk |
| 26 | + |
| 27 | +ifeq ($(shell getconf LONG_BIT), 32) |
| 28 | + SGX_ARCH := x86 |
| 29 | +else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32) |
| 30 | + SGX_ARCH := x86 |
| 31 | +endif |
| 32 | + |
| 33 | +ifeq ($(SGX_ARCH), x86) |
| 34 | + SGX_COMMON_CFLAGS := -m32 |
| 35 | + SGX_LIBRARY_PATH := $(SGX_SDK)/lib |
| 36 | + SGX_BIN_PATH := $(SGX_SDK)/bin/x86 |
| 37 | +else |
| 38 | + SGX_COMMON_CFLAGS := -m64 |
| 39 | + SGX_LIBRARY_PATH := $(SGX_SDK)/lib64 |
| 40 | + SGX_BIN_PATH := $(SGX_SDK)/bin/x64 |
| 41 | +endif |
| 42 | + |
| 43 | +ifeq ($(SGX_DEBUG), 1) |
| 44 | + SGX_COMMON_CFLAGS += -O0 -g |
| 45 | + Rust_Build_Flags := |
| 46 | + Rust_Build_Out := debug |
| 47 | +else |
| 48 | + SGX_COMMON_CFLAGS += -O2 |
| 49 | + Rust_Build_Flags := --release |
| 50 | + Rust_Build_Out := release |
| 51 | +endif |
| 52 | + |
| 53 | +SGX_EDGER8R := $(SGX_BIN_PATH)/sgx_edger8r |
| 54 | +ifneq ($(SGX_MODE), HYPER) |
| 55 | + SGX_ENCLAVE_SIGNER := $(SGX_BIN_PATH)/sgx_sign |
| 56 | +else |
| 57 | + SGX_ENCLAVE_SIGNER := $(SGX_BIN_PATH)/sgx_sign_hyper |
| 58 | + SGX_EDGER8R_MODE := --sgx-mode $(SGX_MODE) |
| 59 | +endif |
| 60 | + |
| 61 | +######## CUSTOM Settings ######## |
| 62 | + |
| 63 | +CUSTOM_LIBRARY_PATH := ./lib |
| 64 | +CUSTOM_BIN_PATH := ./bin |
| 65 | +CUSTOM_SYSROOT_PATH := ./sysroot |
| 66 | +CUSTOM_EDL_PATH := $(ROOT_DIR)/sgx_edl/edl |
| 67 | +CUSTOM_COMMON_PATH := $(ROOT_DIR)/common |
| 68 | + |
| 69 | +######## EDL Settings ######## |
| 70 | + |
| 71 | +Enclave_EDL_Files := enclave/enclave_t.c enclave/enclave_t.h app/enclave_u.c app/enclave_u.h |
| 72 | + |
| 73 | +######## APP Settings ######## |
| 74 | + |
| 75 | +App_Rust_Flags := $(Rust_Build_Flags) |
| 76 | +App_Src_Files := $(shell find app/ -type f -name '*.rs') $(shell find app/ -type f -name 'Cargo.toml') |
| 77 | +App_Include_Paths := -I ./app -I$(SGX_SDK)/include -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_EDL_PATH) |
| 78 | +App_C_Flags := $(CFLAGS) $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) |
| 79 | + |
| 80 | +App_Rust_Path := ./target/$(Rust_Build_Out) |
| 81 | +App_Enclave_u_Object := $(CUSTOM_LIBRARY_PATH)/libenclave_u.a |
| 82 | +App_Name := $(CUSTOM_BIN_PATH)/app |
| 83 | + |
| 84 | +######## Enclave Settings ######## |
| 85 | + |
| 86 | +# BUILD_STD=no use no_std |
| 87 | +# BUILD_STD=cargo use cargo-std-aware |
| 88 | +# BUILD_STD=xargo use xargo |
| 89 | +# BUILD_STD ?= cargo |
| 90 | + |
| 91 | +Rust_Build_Target := x86_64-unknown-linux-sgx |
| 92 | +Rust_Target_Path := $(ROOT_DIR)/rustlib |
| 93 | + |
| 94 | +Rust_Build_Std := $(Rust_Build_Flags) -Zbuild-std=core,alloc |
| 95 | +Rust_Std_Features := |
| 96 | +Rust_Target_Flags := --target $(Rust_Target_Path)/$(Rust_Build_Target).json |
| 97 | +Rust_Sysroot_Path := $(CURDIR)/sysroot |
| 98 | +Rust_Sysroot_Flags := RUSTFLAGS="--sysroot $(Rust_Sysroot_Path)" |
| 99 | + |
| 100 | +RustEnclave_Build_Flags := $(Rust_Build_Flags) |
| 101 | +RustEnclave_Src_Files := $(shell find enclave/ -type f -name '*.rs') $(shell find enclave/ -type f -name 'Cargo.toml') |
| 102 | +RustEnclave_Include_Paths := -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_COMMON_PATH)/inc/tlibc -I$(CUSTOM_EDL_PATH) |
| 103 | + |
| 104 | +RustEnclave_Link_Libs := -L$(CUSTOM_LIBRARY_PATH) -lenclave |
| 105 | +RustEnclave_C_Flags := $(CFLAGS) $(ENCLAVE_CFLAGS) $(SGX_COMMON_CFLAGS) $(RustEnclave_Include_Paths) |
| 106 | +RustEnclave_Link_Flags := -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles \ |
| 107 | + -Wl,--start-group $(RustEnclave_Link_Libs) -Wl,--end-group \ |
| 108 | + -Wl,--version-script=enclave/enclave.lds \ |
| 109 | + $(ENCLAVE_LDFLAGS) |
| 110 | + |
| 111 | +# RustEnclave_Out_Path := ./enclave/target/$(Rust_Build_Target)/$(Rust_Build_Out) |
| 112 | +# RustEnclave_Out_Path := ./enclave/target/$(Rust_Build_Out) |
| 113 | +RustEnclave_Out_Path := ./target/$(Rust_Build_Out) |
| 114 | +Rust_Out_Path := ./target/$(Rust_Build_Target)/$(Rust_Build_Out) |
| 115 | + |
| 116 | +RustEnclave_Lib_Name := $(RustEnclave_Out_Path)/libenclave.a |
| 117 | +# RustEnclave_Lib_Name := $(Rust_Out_Path)/libenclave.a |
| 118 | +RustEnclave_Name := $(CUSTOM_BIN_PATH)/enclave.so |
| 119 | +RustEnclave_Signed_Name := $(CUSTOM_BIN_PATH)/enclave.signed.so |
| 120 | + |
| 121 | +.PHONY: all |
| 122 | +# all: $(RustEnclave_Signed_Name) |
| 123 | +all: app enclave sign |
| 124 | + |
| 125 | +######## EDL Objects ######## |
| 126 | + |
| 127 | + |
| 128 | +$(Enclave_EDL_Files): $(SGX_EDGER8R) edl/enclave.edl |
| 129 | + $(SGX_EDGER8R) $(SGX_EDGER8R_MODE) --trusted edl/enclave.edl --search-path $(CUSTOM_COMMON_PATH)/inc --search-path $(CUSTOM_EDL_PATH) --trusted-dir enclave |
| 130 | + $(SGX_EDGER8R) $(SGX_EDGER8R_MODE) --untrusted edl/enclave.edl --search-path $(CUSTOM_COMMON_PATH)/inc --search-path $(CUSTOM_EDL_PATH) --untrusted-dir app |
| 131 | + @echo "GEN => $(Enclave_EDL_Files)" |
| 132 | + |
| 133 | +######## App Objects ######## |
| 134 | + |
| 135 | +# SYMBOLS := $(shell nm $@ | awk '$$2 == "t" && $$3 ~ /^enclave_.*_ocall$$/ {print $$3}') |
| 136 | + |
| 137 | +app/enclave_u.o: $(Enclave_EDL_Files) |
| 138 | + @$(CC) $(App_C_Flags) -c app/enclave_u.c -o $@ |
| 139 | + @$(OBJCOPY) --strip-symbol=ocall_table_enclave $@ $@ |
| 140 | + @nm $@ | awk '/enclave_.*_ocall/{print $$3}' | while read sym; do \ |
| 141 | + $(OBJCOPY) --globalize-symbol=$$sym $@; \ |
| 142 | + done |
| 143 | + |
| 144 | +$(App_Enclave_u_Object): app/enclave_u.o |
| 145 | + @mkdir -p $(CUSTOM_LIBRARY_PATH) |
| 146 | + @$(AR) rcsD $@ $^ |
| 147 | + |
| 148 | +$(App_Name): $(App_Enclave_u_Object) app |
| 149 | + @mkdir -p $(CUSTOM_BIN_PATH) |
| 150 | + @cp $(App_Rust_Path)/app $(CUSTOM_BIN_PATH)/app |
| 151 | + @echo "LINK => $@" |
| 152 | + |
| 153 | +######## Enclave Objects ######## |
| 154 | + |
| 155 | + |
| 156 | +enclave/enclave_t.o: $(Enclave_EDL_Files) |
| 157 | + @$(CC) $(RustEnclave_C_Flags) -c enclave/enclave_t.c -o $@ |
| 158 | + # @$(OBJCOPY) --localize-symbol=g_ecall_table --strip-symbol=g_ecall_table $@ $@ |
| 159 | + @$(OBJCOPY) --localize-symbol=g_ecall_table --localize-symbol=g_dyn_entry_table $@ $@ |
| 160 | + |
| 161 | +# $(RustEnclave_Name): enclave/enclave_t.o enclave |
| 162 | +$(RustEnclave_Name): enclave/enclave_t.o enclave |
| 163 | + @mkdir -p $(CUSTOM_LIBRARY_PATH) |
| 164 | + @mkdir -p $(CUSTOM_BIN_PATH) |
| 165 | + @cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a |
| 166 | + # @$(CXX) -o $@ $(RustEnclave_Link_Flags) |
| 167 | + @$(CXX) enclave/enclave_t.o -o $@ $(RustEnclave_Link_Flags) |
| 168 | + @echo "LINK => $@" |
| 169 | + |
| 170 | +$(RustEnclave_Signed_Name): $(RustEnclave_Name) enclave/config.xml |
| 171 | + @$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $@ -config enclave/config.xml |
| 172 | + @echo "SIGN => $@" |
| 173 | + |
| 174 | +######## Sign Enclave ######## |
| 175 | +.PHONY: enclave/config.xml |
| 176 | + @$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $@ -config enclave/config.xml |
| 177 | + |
| 178 | +######## Build App ######## |
| 179 | + |
| 180 | +.PHONY: app |
| 181 | +app: $(App_Enclave_u_Object) |
| 182 | + @cd app && SGX_SDK=$(SGX_SDK) cargo build $(App_Rust_Flags) |
| 183 | + @mkdir -p $(CUSTOM_BIN_PATH) |
| 184 | + @cp $(App_Rust_Path)/app $(CUSTOM_BIN_PATH)/app |
| 185 | + |
| 186 | +######## Build Enclave ######## |
| 187 | +.PHONY: enclave |
| 188 | +enclave: enclave/enclave_t.o |
| 189 | + @mkdir -p $(CUSTOM_LIBRARY_PATH) |
| 190 | + @mkdir -p $(CUSTOM_BIN_PATH) |
| 191 | + @cd enclave && cargo build $(RustEnclave_Build_Flags) |
| 192 | + @cp $(RustEnclave_Lib_Name) $(CUSTOM_LIBRARY_PATH)/libenclave.a |
| 193 | + $(CXX) enclave/enclave_t.o -o $(RustEnclave_Name) $(RustEnclave_Link_Flags) |
| 194 | + @echo "LINK => $@" |
| 195 | + |
| 196 | + |
| 197 | +.PHONY: enclave_std |
| 198 | +enclave_std: |
| 199 | + @mkdir -p $(CUSTOM_BIN_PATH) |
| 200 | + @cd $(Rust_Target_Path)/std && cargo build $(Rust_Build_Std) $(Rust_Target_Flags) $(Rust_Std_Features) |
| 201 | + |
| 202 | + @rm -rf $(Rust_Sysroot_Path) |
| 203 | + @mkdir -p $(Rust_Sysroot_Path)/lib/rustlib/$(Rust_Build_Target)/lib |
| 204 | + @cp -r $(Rust_Target_Path)/std/target/$(Rust_Build_Target)/$(Rust_Build_Out)/deps/* $(Rust_Sysroot_Path)/lib/rustlib/$(Rust_Build_Target)/lib |
| 205 | + |
| 206 | + @cd enclave && $(Rust_Sysroot_Flags) cargo build $(Rust_Target_Flags) $(RustEnclave_Build_Flags) |
| 207 | + |
| 208 | + |
| 209 | +######## Sign Enclave ######## |
| 210 | +sign: enclave/config.xml |
| 211 | + @$(SGX_ENCLAVE_SIGNER) sign -key enclave/private.pem -enclave $(RustEnclave_Name) -out $(RustEnclave_Signed_Name) -config enclave/config.xml |
| 212 | + |
| 213 | +######## Run Enclave ######## |
| 214 | + |
| 215 | +.PHONY: run |
| 216 | +run: $(App_Name) $(RustEnclave_Signed_Name) |
| 217 | + @echo -e '\n===== Run Enclave =====\n' |
| 218 | + @cd bin && ./app |
| 219 | + |
| 220 | +.PHONY: clean |
| 221 | +clean: |
| 222 | + @rm -f $(App_Name) $(RustEnclave_Name) $(RustEnclave_Signed_Name) enclave/*_t.* app/*_u.* |
| 223 | + @cd enclave && cargo clean |
| 224 | + @cd app && cargo clean |
| 225 | + @cd $(Rust_Target_Path)/std && cargo clean |
| 226 | + @rm -rf $(CUSTOM_BIN_PATH) $(CUSTOM_LIBRARY_PATH) $(CUSTOM_SYSROOT_PATH) |
0 commit comments