Makefile 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  2. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. ######## SGX SDK Settings ########
  4. SGX_SDK ?= /opt/intel/sgxsdk
  5. SGX_SSL ?= /opt/intel/sgxssl
  6. SGX_MODE ?= SIM
  7. SGX_ARCH ?= x64
  8. SGX_DEBUG ?= 0
  9. SPEC_TEST ?= 0
  10. # These variables are automatically set by CMakeLists.txt
  11. WAMR_BUILD_SGX_IPFS = 0
  12. WAMR_BUILD_LIB_RATS = 0
  13. WAMR_BUILD_GLOBAL_HEAP_POOL = 0
  14. WAMR_BUILD_GLOBAL_HEAP_SIZE = 10485760
  15. WAMR_BUILD_STATIC_PGO = 0
  16. VMLIB_BUILD_DIR ?= $(CURDIR)/../build
  17. LIB_RATS_SRC ?= $(VMLIB_BUILD_DIR)/_deps/librats-build
  18. LIB_RATS_INSTALL_DIR := $(VMLIB_BUILD_DIR)/librats/lib/librats
  19. LIB_RATS_INCLUDE_DIR := $(VMLIB_BUILD_DIR)/librats/include
  20. ifeq ($(shell getconf LONG_BIT), 32)
  21. SGX_ARCH := x86
  22. else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
  23. SGX_ARCH := x86
  24. endif
  25. ifeq ($(SGX_ARCH), x86)
  26. SGX_COMMON_CFLAGS := -m32
  27. SGX_LIBRARY_PATH := $(SGX_SDK)/lib
  28. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
  29. SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
  30. else
  31. SGX_COMMON_CFLAGS := -m64
  32. SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
  33. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
  34. SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
  35. endif
  36. ifeq ($(SGX_DEBUG), 1)
  37. ifeq ($(SGX_PRERELEASE), 1)
  38. $(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
  39. endif
  40. endif
  41. ifeq ($(SGX_DEBUG), 1)
  42. SGX_COMMON_CFLAGS += -O0 -g
  43. else
  44. SGX_COMMON_CFLAGS += -O2
  45. endif
  46. ######## App Settings ########
  47. ifneq ($(SGX_MODE), HW)
  48. Urts_Library_Name := sgx_urts_sim
  49. else
  50. Urts_Library_Name := sgx_urts
  51. endif
  52. App_Cpp_Files := App/App.cpp
  53. App_Include_Paths := -IApp -I$(SGX_SDK)/include
  54. ifeq ($(WAMR_BUILD_LIB_RATS), 1)
  55. App_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR)
  56. endif
  57. App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO)
  58. # Three configuration modes - Debug, prerelease, release
  59. # Debug - Macro DEBUG enabled.
  60. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  61. # Release - Macro NDEBUG enabled.
  62. ifeq ($(SGX_DEBUG), 1)
  63. App_C_Flags += -DDEBUG -UNDEBUG -UEDEBUG
  64. else ifeq ($(SGX_PRERELEASE), 1)
  65. App_C_Flags += -DNDEBUG -DEDEBUG -UDEBUG
  66. else
  67. App_C_Flags += -DNDEBUG -UEDEBUG -UDEBUG
  68. endif
  69. ifeq ($(SPEC_TEST), 1)
  70. App_C_Flags += -DWASM_ENABLE_SPEC_TEST=1
  71. else
  72. App_C_Flags += -DWASM_ENABLE_SPEC_TEST=0
  73. endif
  74. App_Cpp_Flags := $(App_C_Flags) -std=c++11
  75. App_Link_Flags := $(SGX_COMMON_CFLAGS) libvmlib_untrusted.a -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lpthread
  76. ifneq ($(SGX_MODE), HW)
  77. App_Link_Flags += -lsgx_uae_service_sim
  78. else
  79. App_Link_Flags += -lsgx_uae_service
  80. endif
  81. ifeq ($(WAMR_BUILD_LIB_RATS), 1)
  82. App_Link_Flags += -L$(LIB_RATS_INSTALL_DIR) -L$(SGX_SSL)/lib64 -lrats_u -lsgx_dcap_ql -lsgx_dcap_quoteverify -lsgx_ukey_exchange -lsgx_usgxssl
  83. endif
  84. App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
  85. App_Name := iwasm
  86. ######## Enclave Settings ########
  87. ifneq ($(SGX_MODE), HW)
  88. Trts_Library_Name := sgx_trts_sim
  89. Service_Library_Name := sgx_tservice_sim
  90. else
  91. Trts_Library_Name := sgx_trts
  92. Service_Library_Name := sgx_tservice
  93. endif
  94. ifeq ($(WAMR_BUILD_SGX_IPFS), 1)
  95. Intel_Ipfs_Trusted_Flag = -lsgx_tprotected_fs
  96. App_Link_Flags += -lsgx_uprotected_fs
  97. endif
  98. Crypto_Library_Name := sgx_tcrypto
  99. WAMR_ROOT := $(CURDIR)/../../../../
  100. Enclave_Cpp_Files := Enclave/Enclave.cpp
  101. Enclave_Include_Paths := -IEnclave -I$(WAMR_ROOT)/core/iwasm/include \
  102. -I$(WAMR_ROOT)/core/shared/utils \
  103. -I$(WAMR_ROOT)/core/shared/platform/linux-sgx \
  104. -I$(SGX_SDK)/include \
  105. -I$(SGX_SDK)/include/tlibc \
  106. -I$(SGX_SDK)/include/stlport
  107. ifeq ($(WAMR_BUILD_LIB_RATS), 1)
  108. Enclave_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR) -I$(SGX_SSL)/include
  109. endif
  110. Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) -DWASM_GLOBAL_HEAP_SIZE=$(WAMR_BUILD_GLOBAL_HEAP_SIZE) -DWASM_ENABLE_GLOBAL_HEAP_POOL=$(WAMR_BUILD_GLOBAL_HEAP_POOL) -DWASM_ENABLE_LIB_RATS=$(WAMR_BUILD_LIB_RATS) -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO)
  111. ifeq ($(SPEC_TEST), 1)
  112. Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1
  113. else
  114. Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=0
  115. endif
  116. ifeq ($(WAMR_BUILD_LIB_RATS), 1)
  117. Rats_Lib_Link_Dirs := -L$(LIB_RATS_INSTALL_DIR) -L$(LIB_RATS_INSTALL_DIR)/attesters -L$(LIB_RATS_INSTALL_DIR)/verifiers -L$(SGX_SSL)/lib64
  118. Rats_Lib_W_Link_libs := -lattester_nullattester -lattester_sgx_ecdsa -lattester_sgx_la \
  119. -lverifier_nullverifier -lverifier_sgx_ecdsa -lverifier_sgx_la -lverifier_sgx_ecdsa_qve \
  120. -lrats_lib -lsgx_tsgxssl
  121. Rats_Lib_NW_Link_libs := -lsgx_dcap_tvl -lsgx_tsgxssl_crypto
  122. endif
  123. Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++11 -nostdinc++
  124. Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) ${Rats_Lib_Link_Dirs} \
  125. -Wl,--whole-archive -l$(Trts_Library_Name) ${Rats_Lib_W_Link_libs} $(Intel_Ipfs_Trusted_Flag) -Wl,--no-whole-archive \
  126. -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -lsgx_pthread -lsgx_tkey_exchange -l$(Crypto_Library_Name) -l$(Service_Library_Name) $(Rats_Lib_NW_Link_libs) -Wl,--end-group \
  127. -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  128. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
  129. -Wl,--defsym,__ImageBase=0
  130. Enclave_Edl_Search_Path = --search-path ../Enclave \
  131. --search-path $(SGX_SDK)/include \
  132. --search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx
  133. ifeq ($(WAMR_BUILD_LIB_RATS), 1)
  134. Enclave_Edl_Search_Path += --search-path $(LIB_RATS_INCLUDE_DIR)/librats/edl --search-path $(SGX_SSL)/include
  135. endif
  136. Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
  137. Enclave_Name := enclave.so
  138. Signed_Enclave_Name := enclave.signed.so
  139. Enclave_Config_File := Enclave/Enclave.config.xml
  140. ifeq ($(SGX_MODE), HW)
  141. ifneq ($(SGX_DEBUG), 1)
  142. ifneq ($(SGX_PRERELEASE), 1)
  143. Build_Mode = HW_RELEASE
  144. endif
  145. endif
  146. endif
  147. .PHONY: all run
  148. ifeq ($(Build_Mode), HW_RELEASE)
  149. all: $(App_Name) $(Enclave_Name)
  150. @echo "The project has been built in release hardware mode."
  151. @echo "Please sign the $(Enclave_Name) first with your signing key before you run the $(App_Name) to launch and access the enclave."
  152. @echo "To sign the enclave use the command:"
  153. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <your key> -enclave $(Enclave_Name) -out <$(Signed_Enclave_Name)> -config $(Enclave_Config_File)"
  154. @echo "You can also sign the enclave using an external signing tool. See User's Guide for more details."
  155. @echo "To build the project in simulation mode set SGX_MODE=SIM. To build the project in prerelease mode set SGX_PRERELEASE=1 and SGX_MODE=HW."
  156. else
  157. all: $(App_Name) $(Signed_Enclave_Name)
  158. endif
  159. run: all
  160. ifneq ($(Build_Mode), HW_RELEASE)
  161. @$(CURDIR)/$(App_Name)
  162. @echo "RUN => $(App_Name) [$(SGX_MODE)|$(SGX_ARCH), OK]"
  163. endif
  164. ######## App Objects ########
  165. librats:
  166. ifeq ($(WAMR_BUILD_LIB_RATS), 1)
  167. @cd $(LIB_RATS_SRC) && make install
  168. @echo "librats build success"
  169. endif
  170. App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats
  171. @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path)
  172. @echo "GEN => $@"
  173. App/Enclave_u.o: App/Enclave_u.c
  174. @$(CC) $(App_C_Flags) -c $< -o $@
  175. @echo "CC <= $<"
  176. App/%.o: App/%.cpp
  177. @$(CXX) $(App_Cpp_Flags) -c $< -o $@
  178. @echo "CXX <= $<"
  179. libvmlib_untrusted.a: $(VMLIB_BUILD_DIR)/libvmlib_untrusted.a
  180. @cp $< $@
  181. @echo "CP $@ <= $<"
  182. $(App_Name): App/Enclave_u.o $(App_Cpp_Objects) libvmlib_untrusted.a
  183. @$(CXX) $^ -o $@ $(App_Link_Flags)
  184. @echo "LINK => $@"
  185. ######## Enclave Objects ########
  186. Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats
  187. @cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path)
  188. @echo "GEN => $@"
  189. Enclave/Enclave_t.o: Enclave/Enclave_t.c
  190. @$(CC) $(Enclave_C_Flags) -c $< -o $@
  191. @echo "CC <= $<"
  192. Enclave/%.o: Enclave/%.cpp
  193. @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
  194. @echo "CXX <= $<"
  195. libvmlib.a: $(VMLIB_BUILD_DIR)/libvmlib.a
  196. @cp $< $@
  197. @echo "CP $@ <= $<"
  198. $(Enclave_Name): Enclave/Enclave_t.o $(Enclave_Cpp_Objects) libvmlib.a
  199. @$(CXX) $^ -o $@ $(Enclave_Link_Flags)
  200. @echo "LINK => $@"
  201. $(Signed_Enclave_Name): $(Enclave_Name)
  202. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave/Enclave_private.pem -enclave $(Enclave_Name) -out $@ -config $(Enclave_Config_File)
  203. @echo "SIGN => $@"
  204. .PHONY: clean
  205. clean:
  206. @rm -f $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) App/Enclave_u.* $(Enclave_Cpp_Objects) Enclave/Enclave_t.* libvmlib.a libvmlib_untrusted.a