Makefile 7.7 KB

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