Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. SGX_PCL ?= 1
  37. ifeq ($(shell getconf LONG_BIT), 32)
  38. SGX_ARCH := x86
  39. else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
  40. SGX_ARCH := x86
  41. endif
  42. ifeq ($(SGX_ARCH), x86)
  43. ifneq ($(SGX_PCL), 0)
  44. $(error SGX PCL feature is not supported for 32bit mode!)
  45. else
  46. SGX_COMMON_CFLAGS := -m32
  47. SGX_LIBRARY_PATH := $(SGX_SDK)/lib
  48. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
  49. SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
  50. endif
  51. else
  52. SGX_COMMON_CFLAGS := -m64
  53. SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
  54. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
  55. SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
  56. endif
  57. ifeq ($(SGX_DEBUG), 1)
  58. ifeq ($(SGX_PRERELEASE), 1)
  59. $(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
  60. endif
  61. endif
  62. ifeq ($(SGX_DEBUG), 1)
  63. SGX_COMMON_CFLAGS += -O0 -g
  64. Encryption_Tool_Flags := -d
  65. else
  66. SGX_COMMON_CFLAGS += -O2
  67. endif
  68. ######## App Settings ########
  69. ifneq ($(SGX_MODE), HW)
  70. Urts_Library_Name := sgx_urts_sim
  71. else
  72. Urts_Library_Name := sgx_urts
  73. endif
  74. App_Cpp_Files := App/App.cpp $(wildcard App/Edger8rSyntax/*.cpp) $(wildcard App/TrustedLibrary/*.cpp)
  75. App_Include_Paths := -IInclude -IApp -I$(SGX_SDK)/include
  76. App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
  77. # Three configuration modes - Debug, prerelease, release
  78. # Debug - Macro DEBUG enabled.
  79. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  80. # Release - Macro NDEBUG enabled.
  81. ifeq ($(SGX_DEBUG), 1)
  82. App_C_Flags += -DDEBUG -UNDEBUG -UEDEBUG
  83. else ifeq ($(SGX_PRERELEASE), 1)
  84. App_C_Flags += -DNDEBUG -DEDEBUG -UDEBUG
  85. else
  86. App_C_Flags += -DNDEBUG -UEDEBUG -UDEBUG
  87. endif
  88. ifneq ($(SGX_PCL), 0)
  89. App_C_Flags += -DSGX_USE_PCL -I../Seal
  90. endif
  91. App_Cpp_Flags := $(App_C_Flags) -std=c++11
  92. App_Link_Flags := $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lpthread
  93. ifneq ($(SGX_MODE), HW)
  94. App_Link_Flags += -lsgx_uae_service_sim
  95. else
  96. App_Link_Flags += -lsgx_uae_service
  97. endif
  98. App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
  99. App_Name := app
  100. ######## Enclave Settings ########
  101. ifneq ($(SGX_MODE), HW)
  102. Trts_Library_Name := sgx_trts_sim
  103. Service_Library_Name := sgx_tservice_sim
  104. else
  105. Trts_Library_Name := sgx_trts
  106. Service_Library_Name := sgx_tservice
  107. endif
  108. Crypto_Library_Name := sgx_tcrypto
  109. Enclave_Cpp_Files := Enclave/Enclave.cpp $(wildcard Enclave/Edger8rSyntax/*.cpp) $(wildcard Enclave/TrustedLibrary/*.cpp)
  110. Enclave_Include_Paths := -IInclude -IEnclave -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx
  111. CC_BELOW_4_9 := $(shell expr "`$(CC) -dumpversion`" \< "4.9")
  112. ifeq ($(CC_BELOW_4_9), 1)
  113. Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections -fstack-protector
  114. else
  115. Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections -fstack-protector-strong
  116. endif
  117. Enclave_C_Flags += $(Enclave_Include_Paths)
  118. Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++11 -nostdinc++
  119. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  120. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  121. # so that the whole content of trts is included in the enclave.
  122. # 2. For other libraries, you just need to pull the required symbols.
  123. # Use `--start-group' and `--end-group' to link these libraries.
  124. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  125. # Otherwise, you may get some undesirable errors.
  126. Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
  127. -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  128. -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
  129. -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  130. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
  131. -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections \
  132. -Wl,--version-script=Enclave/Enclave.lds
  133. Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
  134. Enclave_Name := enclave.so
  135. Signed_Enclave_Name := enclave.signed.so
  136. Enclave_Config_File := Enclave/Enclave.config.xml
  137. Unsigned_Enclave_Name := $(Enclave_Name)
  138. Encrypted_Enclave_Name := $(Enclave_Name).enc
  139. Seal_Enclave_Name := Seal.so
  140. Signed_Seal_Enclave_Name := Seal.signed.so
  141. Seal_Enclave_Cpp_Files := Seal/Seal.cpp
  142. Seal_Enclave_Cpp_Objects := $(Seal_Enclave_Cpp_Files:.cpp=.o)
  143. Seal_Enclave_Config_File := Seal/Seal.config.xml
  144. PCL_LINK_FLAGS :=
  145. ifneq ($(SGX_PCL),0)
  146. Pcl_Encryption_Tool := $(SGX_SDK)/bin/x64/sgx_encrypt
  147. PCL_KEY := ./debug_mock_key.bin
  148. ifneq ($(SGX_MODE), HW)
  149. PCL_LIB_NAME := sgx_pclsim
  150. else # ifneq ($(SGX_MODE), HW)
  151. PCL_LIB_NAME := sgx_pcl
  152. endif # ifneq ($(SGX_MODE), HW)
  153. PCL_LINK_FLAGS = -Wl,--whole-archive -l$(PCL_LIB_NAME) -Wl,--no-whole-archive
  154. endif # ifneq ($(SGX_PCL),0)
  155. ifeq ($(SGX_MODE), HW)
  156. ifeq ($(SGX_DEBUG), 1)
  157. Build_Mode = HW_DEBUG
  158. else ifeq ($(SGX_PRERELEASE), 1)
  159. Build_Mode = HW_PRERELEASE
  160. else
  161. Build_Mode = HW_RELEASE
  162. endif
  163. else
  164. ifeq ($(SGX_DEBUG), 1)
  165. Build_Mode = SIM_DEBUG
  166. else ifeq ($(SGX_PRERELEASE), 1)
  167. Build_Mode = SIM_PRERELEASE
  168. else
  169. Build_Mode = SIM_RELEASE
  170. endif
  171. endif
  172. .PHONY: all run
  173. ifeq ($(Build_Mode), HW_RELEASE)
  174. all: .config_$(Build_Mode)_$(SGX_ARCH)$(SGX_PCL) $(App_Name) $(Enclave_Name) $(Seal_Enclave_Name)
  175. @echo "The project has been built in release hardware mode."
  176. @echo "Please sign the $(Enclave_Name) first with your signing key before you run the $(App_Name) to launch and access the enclave."
  177. @echo "To sign the enclave use the command:"
  178. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <your key> -enclave $(Enclave_Name) -out <$(Signed_Enclave_Name)> -config $(Enclave_Config_File)"
  179. @echo "You can also sign the enclave using an external signing tool."
  180. @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."
  181. else
  182. all: .config_$(Build_Mode)_$(SGX_ARCH)$(SGX_PCL) $(Signed_Seal_Enclave_Name) $(App_Name) $(Signed_Enclave_Name)
  183. ifeq ($(Build_Mode), HW_DEBUG)
  184. @echo "The project has been built in debug hardware mode."
  185. else ifeq ($(Build_Mode), SIM_DEBUG)
  186. @echo "The project has been built in debug simulation mode."
  187. else ifeq ($(Build_Mode), HW_PRERELEASE)
  188. @echo "The project has been built in pre-release hardware mode."
  189. else ifeq ($(Build_Mode), SIM_PRERELEASE)
  190. @echo "The project has been built in pre-release simulation mode."
  191. else
  192. @echo "The project has been built in release simulation mode."
  193. endif
  194. endif
  195. run: all
  196. ifneq ($(Build_Mode), HW_RELEASE)
  197. @$(CURDIR)/$(App_Name)
  198. @echo "RUN => $(App_Name) [$(SGX_MODE)|$(SGX_ARCH), OK]"
  199. endif
  200. ######## App Objects ########
  201. App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl
  202. @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl --search-path ../Enclave --search-path $(SGX_SDK)/include
  203. @echo "GEN => $@"
  204. App/Enclave_u.o: App/Enclave_u.c
  205. @$(CC) $(App_C_Flags) -c $< -o $@
  206. @echo "CC <= $<"
  207. App/Seal_u.c: $(SGX_EDGER8R) Seal/Seal.edl
  208. @cd App && $(SGX_EDGER8R) --untrusted ../Seal/Seal.edl --search-path ../Seal --search-path $(SGX_SDK)/include
  209. @echo "GEN => $@"
  210. App/Seal_u.o: App/Seal_u.c
  211. @$(CC) $(App_C_Flags) -c $< -o $@
  212. @echo "CC <= $<"
  213. App/%.o: App/%.cpp
  214. @$(CXX) $(App_Cpp_Flags) -c $< -o $@
  215. @echo "CXX <= $<"
  216. $(App_Name): App/Seal_u.o App/Enclave_u.o $(App_Cpp_Objects)
  217. @$(CXX) $^ -o $@ $(App_Link_Flags)
  218. @echo "LINK => $@"
  219. .config_$(Build_Mode)_$(SGX_ARCH)$(SGX_PCL):
  220. @rm -f .config_* $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) App/Enclave_u.* $(Enclave_Cpp_Objects) Enclave/Enclave_t.* $(Encrypted_Enclave_Name) $(Seal_Enclave_Name) $(Signed_Seal_Enclave_Name) App/Seal_u.* $(Seal_Enclave_Cpp_Objects) Seal/Seal_t.* sealed_key.bin
  221. @touch .config_$(Build_Mode)_$(SGX_ARCH)$(SGX_PCL)
  222. ######## Sealing enclave Objects ########
  223. Seal/Seal_t.c: $(SGX_EDGER8R) Seal/Seal.edl
  224. @cd Seal && $(SGX_EDGER8R) --trusted ../Seal/Seal.edl --search-path ../Seal --search-path $(SGX_SDK)/include
  225. @echo "GEN => $@"
  226. Seal/Seal_t.o: Seal/Seal_t.c
  227. @$(CC) $(Enclave_C_Flags) -c $< -o $@
  228. @echo "CC <= $<"
  229. Seal/%.o: Seal/%.cpp
  230. @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
  231. @echo "CXX <= $<"
  232. $(Seal_Enclave_Name): Seal/Seal_t.o $(Seal_Enclave_Cpp_Objects)
  233. @$(CXX) $^ -o $@ $(Enclave_Link_Flags)
  234. @echo "LINK => $@"
  235. $(Signed_Seal_Enclave_Name): $(Seal_Enclave_Name)
  236. @$(SGX_ENCLAVE_SIGNER) sign -key Seal/Seal_private.pem -enclave $(Seal_Enclave_Name) -out $@ -config $(Seal_Enclave_Config_File)
  237. @echo "SIGN => $@"
  238. ######## Enclave Objects ########
  239. Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl
  240. @cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl --search-path ../Enclave --search-path $(SGX_SDK)/include
  241. @echo "GEN => $@"
  242. Enclave/Enclave_t.o: Enclave/Enclave_t.c
  243. @$(CC) $(Enclave_C_Flags) -c $< -o $@
  244. @echo "CC <= $<"
  245. Enclave/%.o: Enclave/%.cpp
  246. @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
  247. @echo "CXX <= $<"
  248. $(Enclave_Name): Enclave/Enclave_t.o $(Enclave_Cpp_Objects)
  249. @$(CXX) $^ -o $@ $(Enclave_Link_Flags) $(PCL_LINK_FLAGS)
  250. @echo "LINK => $@"
  251. # encrypted enclave required only for PCL build
  252. ifneq ($(SGX_PCL),0)
  253. $(Encrypted_Enclave_Name): $(Enclave_Name) $(Pcl_Encryption_Tool)
  254. @$(Pcl_Encryption_Tool) -i $< -o $@ -k $(PCL_KEY) $(Encryption_Tool_Flags)
  255. @echo "Encrypted => $@"
  256. endif # ifneq ($(SGX_PCL),0)
  257. ifneq ($(SGX_PCL), 0)
  258. $(Signed_Enclave_Name): $(Encrypted_Enclave_Name)
  259. else
  260. $(Signed_Enclave_Name): $(Enclave_Name)
  261. endif
  262. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave/Enclave_private.pem -enclave $< -out $@ -config $(Enclave_Config_File)
  263. @echo "SIGN => $@"
  264. .PHONY: clean
  265. clean:
  266. @rm -f .config_* $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) App/Enclave_u.* $(Enclave_Cpp_Objects) Enclave/Enclave_t.* $(Encrypted_Enclave_Name) $(Seal_Enclave_Name) $(Signed_Seal_Enclave_Name) App/Seal_u.* $(Seal_Enclave_Cpp_Objects) Seal/Seal_t.* sealed_key.bin