Makefile 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #
  2. # Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in
  12. # the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Intel Corporation nor the names of its
  15. # contributors may be used to endorse or promote products derived
  16. # from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #
  30. #
  31. ####### SGX SDK Settings ########
  32. SGX_SDK ?= /opt/intel/sgxsdk
  33. SGX_MODE ?= HW
  34. SGX_ARCH ?= x64
  35. SGX_DEBUG ?= 1
  36. ifeq ($(shell getconf LONG_BIT), 32)
  37. SGX_ARCH := x86
  38. else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
  39. SGX_ARCH := x86
  40. endif
  41. ifeq ($(SGX_ARCH), x86)
  42. SGX_COMMON_CFLAGS := -m32
  43. SGX_LIBRARY_PATH := $(SGX_SDK)/lib
  44. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
  45. SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
  46. else
  47. SGX_COMMON_CFLAGS := -m64
  48. SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
  49. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
  50. SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
  51. endif
  52. ifeq ($(SGX_DEBUG), 1)
  53. ifeq ($(SGX_PRERELEASE), 1)
  54. $(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
  55. endif
  56. endif
  57. ifeq ($(SGX_DEBUG), 1)
  58. SGX_COMMON_CFLAGS += -O0 -g
  59. else
  60. SGX_COMMON_CFLAGS += -O2
  61. endif
  62. ######## App Settings ########
  63. ifneq ($(SGX_MODE), HW)
  64. Urts_Library_Name := sgx_urts_sim
  65. else
  66. Urts_Library_Name := sgx_urts
  67. endif
  68. App_Cpp_Files := $(wildcard App/*.cpp)
  69. App_Include_Paths := -I$(SGX_SDK)/include -I./Common
  70. App_Compile_CFlags := -fPIC -Wno-attributes $(App_Include_Paths)
  71. # Three configuration modes - Debug, prerelease, release
  72. # Debug - Macro DEBUG enabled.
  73. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  74. # Release - Macro NDEBUG enabled.
  75. ifeq ($(SGX_DEBUG), 1)
  76. App_Compile_CFlags += -DDEBUG -UNDEBUG -UEDEBUG
  77. else ifeq ($(SGX_PRERELEASE), 1)
  78. App_Compile_CFlags += -DNDEBUG -DEDEBUG -UDEBUG
  79. else
  80. App_Compile_CFlags += -DNDEBUG -UEDEBUG -UDEBUG
  81. endif
  82. App_Compile_CXXFlags := -std=c++11 $(App_Compile_CFlags)
  83. App_Link_Flags := -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lpthread
  84. ifneq ($(SGX_MODE), HW)
  85. App_Link_Flags += -lsgx_uae_service_sim
  86. else
  87. App_Link_Flags += -lsgx_uae_service
  88. endif
  89. Gen_Untrusted_Source := App/Enclave_u.c
  90. Gen_Untrusted_Object := App/Enclave_u.o
  91. App_Objects := $(Gen_Untrusted_Object) $(App_Cpp_Files:.cpp=.o)
  92. App_Name := app
  93. ######## Enclave Settings ########
  94. ifneq ($(SGX_MODE), HW)
  95. Trts_Library_Name := sgx_trts_sim
  96. Service_Library_Name := sgx_tservice_sim
  97. else
  98. Trts_Library_Name := sgx_trts
  99. Service_Library_Name := sgx_tservice
  100. endif
  101. Crypto_Library_Name := sgx_tcrypto
  102. Enclave_Cpp_Files := $(wildcard Enclave/*.cpp)
  103. Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx -I./Common
  104. CC_BELOW_4_9 := $(shell expr "`$(CC) -dumpversion`" \< "4.9")
  105. ifeq ($(CC_BELOW_4_9), 1)
  106. Enclave_Compile_CFlags := -fstack-protector
  107. else
  108. Enclave_Compile_CFlags := -fstack-protector-strong
  109. endif
  110. Enclave_Compile_CFlags += -nostdinc -ffreestanding -fvisibility=hidden -fpie -ffunction-sections -fdata-sections $(Enclave_Include_Paths)
  111. Enclave_Compile_CXXFlags := -nostdinc++ -std=c++11 $(Enclave_Compile_CFlags)
  112. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  113. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  114. # so that the whole content of trts is included in the enclave.
  115. # 2. For other libraries, you just need to pull the required symbols.
  116. # Use `--start-group' and `--end-group' to link these libraries.
  117. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  118. # Otherwise, you may get some undesirable errors.
  119. Enclave_Link_Flags := -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
  120. -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  121. -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
  122. -Wl,--version-script=Enclave/Enclave.lds -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  123. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections
  124. Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
  125. Gen_Trusted_Source := Enclave/Enclave_t.c
  126. Gen_Trusted_Object := Enclave/Enclave_t.o
  127. Enclave_Objects := $(Gen_Trusted_Object) $(Enclave_Cpp_Files:.cpp=.o)
  128. Enclave_Name := libenclave.so
  129. Signed_Enclave_Name := libenclave.signed.so
  130. Enclave_Config_File := Enclave/Enclave.config.xml
  131. ifeq ($(SGX_MODE), HW)
  132. ifeq ($(SGX_DEBUG), 1)
  133. Build_Mode = HW_DEBUG
  134. else ifeq ($(SGX_PRERELEASE), 1)
  135. Build_Mode = HW_PRERELEASE
  136. else
  137. Build_Mode = HW_RELEASE
  138. endif
  139. else
  140. ifeq ($(SGX_DEBUG), 1)
  141. Build_Mode = SIM_DEBUG
  142. else ifeq ($(SGX_PRERELEASE), 1)
  143. Build_Mode = SIM_PRERELEASE
  144. else
  145. Build_Mode = SIM_RELEASE
  146. endif
  147. endif
  148. ifeq ($(Build_Mode), HW_RELEASE)
  149. all: .config_$(Build_Mode)_$(SGX_ARCH) $(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."
  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: .config_$(Build_Mode)_$(SGX_ARCH) $(App_Name) $(Signed_Enclave_Name)
  158. ifeq ($(Build_Mode), HW_DEBUG)
  159. @echo "The project has been built in debug hardware mode."
  160. else ifeq ($(Build_Mode), SIM_DEBUG)
  161. @echo "The project has been built in debug simulation mode."
  162. else ifeq ($(Build_Mode), HW_PRERELEASE)
  163. @echo "The project has been built in pre-release hardware mode."
  164. else ifeq ($(Build_Mode), SIM_PRERELEASE)
  165. @echo "The project has been built in pre-release simulation mode."
  166. else
  167. @echo "The project has been built in release simulation mode."
  168. endif
  169. endif
  170. .config_$(Build_Mode)_$(SGX_ARCH):
  171. @rm -f .config_* $(App_Name) $(App_Objects) $(Enclave_Name) $(Enclave_Objects) App/Enclave_u.* Enclave/Enclave_t.* $(Signed_Enclave_Name)
  172. @touch .config_$(Build_Mode)_$(SGX_ARCH)
  173. ######## App Objects ########
  174. $(Gen_Untrusted_Source): $(SGX_EDGER8R) Enclave/Enclave.edl
  175. @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl --search-path $(SGX_SDK)/include
  176. @echo "GEN => $@"
  177. $(Gen_Untrusted_Object): $(Gen_Untrusted_Source)
  178. @$(CC) $(SGX_COMMON_CFLAGS) $(App_Compile_CFlags) -c $< -o $@
  179. @echo "CC <= $<"
  180. App/%.o: App/%.cpp
  181. @$(CXX) $(SGX_COMMON_CFLAGS) $(App_Compile_CXXFlags) -c $< -o $@
  182. @echo "CXX <= $<"
  183. $(App_Name): $(App_Objects)
  184. @$(CXX) $(SGX_COMMON_CFLAGS) $^ -o $@ $(App_Link_Flags)
  185. @echo "LINK => $@"
  186. ######## Enclave Objects ########
  187. $(Gen_Trusted_Source): $(SGX_EDGER8R) Enclave/Enclave.edl
  188. @cd Enclave && $(SGX_EDGER8R) --trusted Enclave.edl --search-path $(SGX_SDK)/include
  189. @echo "GEN => $@"
  190. $(Gen_Trusted_Object): $(Gen_Trusted_Source)
  191. @$(CC) $(SGX_COMMON_CFLAGS) $(Enclave_Compile_CFlags) -c $< -o $@
  192. @echo "CC <= $<"
  193. Enclave/%.o: Enclave/%.cpp
  194. @$(CXX) $(SGX_COMMON_CFLAGS) $(Enclave_Compile_CXXFlags) -c $< -o $@
  195. @echo "CXX <= $<"
  196. $(Enclave_Name): $(Enclave_Objects)
  197. @$(CXX) $(SGX_COMMON_CFLAGS) $(Enclave_Objects) -o $@ $(Enclave_Link_Flags)
  198. @echo "LINK => $@"
  199. $(Signed_Enclave_Name): $(Enclave_Name)
  200. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave/Enclave_private.pem -enclave $(Enclave_Name) -out $@ -config $(Enclave_Config_File)
  201. @echo "SIGN => $@"
  202. ######### clean up ########
  203. .PHONY: clean
  204. clean:
  205. @rm -f .config_* $(App_Name) $(App_Objects) $(Enclave_Name) $(Enclave_Objects) App/Enclave_u.* Enclave/Enclave_t.* $(Signed_Enclave_Name)