Makefile 6.2 KB

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