Makefile 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #
  2. # Copyright (C) 2011-2017 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. ######## Library Settings ########
  63. Trust_Lib_Name := libLocalAttestation_Trusted.a
  64. TrustLib_Cpp_Files := $(wildcard LocalAttestationCode/*.cpp)
  65. TrustLib_Cpp_Objects := $(TrustLib_Cpp_Files:.cpp=.o)
  66. TrustLib_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx -I$(SGX_SDK)/include/epid -I./Include
  67. TrustLib_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(TrustLib_Include_Paths)
  68. TrustLib_Compile_Cxx_Flags := -std=c++11 -nostdinc++
  69. UnTrustLib_Name := libLocalAttestation_unTrusted.a
  70. UnTrustLib_Cpp_Files := $(wildcard Untrusted_LocalAttestation/*.cpp)
  71. UnTrustLib_Cpp_Objects := $(UnTrustLib_Cpp_Files:.cpp=.o)
  72. UnTrustLib_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/ippcp -I./Include -I./LocalAttestationCode
  73. UnTrustLib_Compile_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes -std=c++11 $(UnTrustLib_Include_Paths)
  74. ######## App Settings ########
  75. ifneq ($(SGX_MODE), HW)
  76. Urts_Library_Name := sgx_urts_sim
  77. else
  78. Urts_Library_Name := sgx_urts
  79. endif
  80. App_Cpp_Files := $(wildcard App/*.cpp)
  81. App_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/ippcp -I./Include -I./LocalAttestationCode
  82. App_Compile_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
  83. # Three configuration modes - Debug, prerelease, release
  84. # Debug - Macro DEBUG enabled.
  85. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  86. # Release - Macro NDEBUG enabled.
  87. ifeq ($(SGX_DEBUG), 1)
  88. App_Compile_Flags += -DDEBUG -UNDEBUG -UEDEBUG
  89. else ifeq ($(SGX_PRERELEASE), 1)
  90. App_Compile_Flags += -DNDEBUG -DEDEBUG -UDEBUG
  91. else
  92. App_Compile_Flags += -DNDEBUG -UEDEBUG -UDEBUG
  93. endif
  94. App_Link_Flags := $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -L. -lpthread -lLocalAttestation_unTrusted
  95. ifneq ($(SGX_MODE), HW)
  96. App_Link_Flags += -lsgx_uae_service_sim
  97. else
  98. App_Link_Flags += -lsgx_uae_service
  99. endif
  100. App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
  101. App_Name := app
  102. ######## Enclave Settings ########
  103. Enclave1_Version_Script := Enclave1/Enclave1.lds
  104. Enclave2_Version_Script := Enclave2/Enclave2.lds
  105. Enclave3_Version_Script := Enclave3/Enclave3.lds
  106. ifneq ($(SGX_MODE), HW)
  107. Trts_Library_Name := sgx_trts_sim
  108. Service_Library_Name := sgx_tservice_sim
  109. else
  110. Trts_Library_Name := sgx_trts
  111. Service_Library_Name := sgx_tservice
  112. endif
  113. Crypto_Library_Name := sgx_tcrypto
  114. Enclave_Cpp_Files_1 := $(wildcard Enclave1/*.cpp)
  115. Enclave_Cpp_Files_2 := $(wildcard Enclave2/*.cpp)
  116. Enclave_Cpp_Files_3 := $(wildcard Enclave3/*.cpp)
  117. Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx -I./LocalAttestationCode -I./Include
  118. CC_BELOW_4_9 := $(shell expr "`$(CC) -dumpversion`" \< "4.9")
  119. ifeq ($(CC_BELOW_4_9), 1)
  120. Enclave_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections -fstack-protector
  121. else
  122. Enclave_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections -fstack-protector-strong
  123. endif
  124. Enclave_Compile_Flags += $(Enclave_Include_Paths)
  125. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  126. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  127. # so that the whole content of trts is included in the enclave.
  128. # 2. For other libraries, you just need to pull the required symbols.
  129. # Use `--start-group' and `--end-group' to link these libraries.
  130. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  131. # Otherwise, you may get some undesirable errors.
  132. Common_Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
  133. -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  134. -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l$(Crypto_Library_Name) -L. -lLocalAttestation_Trusted -l$(Service_Library_Name) -Wl,--end-group \
  135. -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  136. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
  137. -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections
  138. Enclave1_Link_Flags := $(Common_Enclave_Link_Flags) -Wl,--version-script=$(Enclave1_Version_Script)
  139. Enclave2_Link_Flags := $(Common_Enclave_Link_Flags) -Wl,--version-script=$(Enclave2_Version_Script)
  140. Enclave3_Link_Flags := $(Common_Enclave_Link_Flags) -Wl,--version-script=$(Enclave3_Version_Script)
  141. Enclave_Cpp_Objects_1 := $(Enclave_Cpp_Files_1:.cpp=.o)
  142. Enclave_Cpp_Objects_2 := $(Enclave_Cpp_Files_2:.cpp=.o)
  143. Enclave_Cpp_Objects_3 := $(Enclave_Cpp_Files_3:.cpp=.o)
  144. Enclave_Name_1 := libenclave1.so
  145. Enclave_Name_2 := libenclave2.so
  146. Enclave_Name_3 := libenclave3.so
  147. ifeq ($(SGX_MODE), HW)
  148. ifeq ($(SGX_DEBUG), 1)
  149. Build_Mode = HW_DEBUG
  150. else ifeq ($(SGX_PRERELEASE), 1)
  151. Build_Mode = HW_PRERELEASE
  152. else
  153. Build_Mode = HW_RELEASE
  154. endif
  155. else
  156. ifeq ($(SGX_DEBUG), 1)
  157. Build_Mode = SIM_DEBUG
  158. else ifeq ($(SGX_PRERELEASE), 1)
  159. Build_Mode = SIM_PRERELEASE
  160. else
  161. Build_Mode = SIM_RELEASE
  162. endif
  163. endif
  164. ifeq ($(Build_Mode), HW_RELEASE)
  165. all: .config_$(Build_Mode)_$(SGX_ARCH) $(Trust_Lib_Name) $(UnTrustLib_Name) Enclave1.so Enclave2.so Enclave3.so $(App_Name)
  166. @echo "The project has been built in release hardware mode."
  167. @echo "Please sign the enclaves (Enclave1.so, Enclave2.so, Enclave3.so) first with your signing keys before you run the $(App_Name) to launch and access the enclave."
  168. @echo "To sign the enclaves use the following commands:"
  169. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <key1> -enclave Enclave1.so -out <$(Enclave_Name_1)> -config Enclave1/Enclave1.config.xml"
  170. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <key2> -enclave Enclave2.so -out <$(Enclave_Name_2)> -config Enclave2/Enclave2.config.xml"
  171. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <key3> -enclave Enclave3.so -out <$(Enclave_Name_3)> -config Enclave3/Enclave3.config.xml"
  172. @echo "You can also sign the enclaves using an external signing tool."
  173. @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."
  174. else
  175. all: .config_$(Build_Mode)_$(SGX_ARCH) $(Trust_Lib_Name) $(UnTrustLib_Name) $(Enclave_Name_1) $(Enclave_Name_2) $(Enclave_Name_3) $(App_Name)
  176. ifeq ($(Build_Mode), HW_DEBUG)
  177. @echo "The project has been built in debug hardware mode."
  178. else ifeq ($(Build_Mode), SIM_DEBUG)
  179. @echo "The project has been built in debug simulation mode."
  180. else ifeq ($(Build_Mode), HW_PRERELEASE)
  181. @echo "The project has been built in pre-release hardware mode."
  182. else ifeq ($(Build_Mode), SIM_PRERELEASE)
  183. @echo "The project has been built in pre-release simulation mode."
  184. else
  185. @echo "The project has been built in release simulation mode."
  186. endif
  187. endif
  188. .config_$(Build_Mode)_$(SGX_ARCH):
  189. @rm -rf .config_* $(App_Name) *.so *.a App/*.o Enclave1/*.o Enclave1/*_t.* Enclave1/*_u.* Enclave2/*.o Enclave2/*_t.* Enclave2/*_u.* Enclave3/*.o Enclave3/*_t.* Enclave3/*_u.* LocalAttestationCode/*.o Untrusted_LocalAttestation/*.o LocalAttestationCode/*_t.*
  190. @touch .config_$(Build_Mode)_$(SGX_ARCH)
  191. ######## Library Objects ########
  192. LocalAttestationCode/LocalAttestationCode_t.c LocalAttestationCode/LocalAttestationCode_t.h : $(SGX_EDGER8R) LocalAttestationCode/LocalAttestationCode.edl
  193. @cd LocalAttestationCode && $(SGX_EDGER8R) --trusted ../LocalAttestationCode/LocalAttestationCode.edl --search-path $(SGX_SDK)/include
  194. @echo "GEN => $@"
  195. LocalAttestationCode/LocalAttestationCode_t.o: LocalAttestationCode/LocalAttestationCode_t.c
  196. @$(CC) $(TrustLib_Compile_Flags) -c $< -o $@
  197. @echo "CC <= $<"
  198. LocalAttestationCode/%.o: LocalAttestationCode/%.cpp LocalAttestationCode/LocalAttestationCode_t.h
  199. @$(CXX) $(TrustLib_Compile_Flags) $(TrustLib_Compile_Cxx_Flags) -c $< -o $@
  200. @echo "CC <= $<"
  201. $(Trust_Lib_Name): LocalAttestationCode/LocalAttestationCode_t.o $(TrustLib_Cpp_Objects)
  202. @$(AR) rcs $@ $^
  203. @echo "GEN => $@"
  204. Untrusted_LocalAttestation/%.o: Untrusted_LocalAttestation/%.cpp
  205. @$(CXX) $(UnTrustLib_Compile_Flags) -c $< -o $@
  206. @echo "CC <= $<"
  207. $(UnTrustLib_Name): $(UnTrustLib_Cpp_Objects)
  208. @$(AR) rcs $@ $^
  209. @echo "GEN => $@"
  210. ######## App Objects ########
  211. Enclave1/Enclave1_u.c Enclave1/Enclave1_u.h: $(SGX_EDGER8R) Enclave1/Enclave1.edl
  212. @cd Enclave1 && $(SGX_EDGER8R) --use-prefix --untrusted ../Enclave1/Enclave1.edl --search-path $(SGX_SDK)/include
  213. @echo "GEN => $@"
  214. App/Enclave1_u.o: Enclave1/Enclave1_u.c
  215. @$(CC) $(App_Compile_Flags) -c $< -o $@
  216. @echo "CC <= $<"
  217. Enclave2/Enclave2_u.c Enclave2/Enclave2_u.h: $(SGX_EDGER8R) Enclave2/Enclave2.edl
  218. @cd Enclave2 && $(SGX_EDGER8R) --use-prefix --untrusted ../Enclave2/Enclave2.edl --search-path $(SGX_SDK)/include
  219. @echo "GEN => $@"
  220. App/Enclave2_u.o: Enclave2/Enclave2_u.c
  221. @$(CC) $(App_Compile_Flags) -c $< -o $@
  222. @echo "CC <= $<"
  223. Enclave3/Enclave3_u.c Enclave3/Enclave3_u.h: $(SGX_EDGER8R) Enclave3/Enclave3.edl
  224. @cd Enclave3 && $(SGX_EDGER8R) --use-prefix --untrusted ../Enclave3/Enclave3.edl --search-path $(SGX_SDK)/include
  225. @echo "GEN => $@"
  226. App/Enclave3_u.o: Enclave3/Enclave3_u.c
  227. @$(CC) $(App_Compile_Flags) -c $< -o $@
  228. @echo "CC <= $<"
  229. App/%.o: App/%.cpp Enclave1/Enclave1_u.h Enclave2/Enclave2_u.h Enclave3/Enclave3_u.h
  230. @$(CXX) $(App_Compile_Flags) -c $< -o $@
  231. @echo "CXX <= $<"
  232. $(App_Name): App/Enclave1_u.o App/Enclave2_u.o App/Enclave3_u.o $(App_Cpp_Objects) $(UnTrustLib_Name)
  233. @$(CXX) $^ -o $@ $(App_Link_Flags)
  234. @echo "LINK => $@"
  235. ######## Enclave Objects ########
  236. Enclave1/Enclave1_t.c Enclave1/Enclave1_t.h: $(SGX_EDGER8R) Enclave1/Enclave1.edl
  237. @cd Enclave1 && $(SGX_EDGER8R) --use-prefix --trusted ../Enclave1/Enclave1.edl --search-path $(SGX_SDK)/include
  238. @echo "GEN => $@"
  239. Enclave1/Enclave1_t.o: Enclave1/Enclave1_t.c
  240. @$(CC) $(Enclave_Compile_Flags) -c $< -o $@
  241. @echo "CC <= $<"
  242. Enclave1/%.o: Enclave1/%.cpp Enclave1/Enclave1_t.h
  243. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  244. @echo "CXX <= $<"
  245. Enclave1.so: Enclave1/Enclave1_t.o $(Enclave_Cpp_Objects_1) $(Trust_Lib_Name)
  246. @$(CXX) Enclave1/Enclave1_t.o $(Enclave_Cpp_Objects_1) -o $@ $(Enclave1_Link_Flags)
  247. @echo "LINK => $@"
  248. $(Enclave_Name_1): Enclave1.so
  249. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave1/Enclave1_private.pem -enclave Enclave1.so -out $@ -config Enclave1/Enclave1.config.xml
  250. @echo "SIGN => $@"
  251. Enclave2/Enclave2_t.c: $(SGX_EDGER8R) Enclave2/Enclave2.edl
  252. @cd Enclave2 && $(SGX_EDGER8R) --use-prefix --trusted ../Enclave2/Enclave2.edl --search-path $(SGX_SDK)/include
  253. @echo "GEN => $@"
  254. Enclave2/Enclave2_t.o: Enclave2/Enclave2_t.c
  255. @$(CC) $(Enclave_Compile_Flags) -c $< -o $@
  256. @echo "CC <= $<"
  257. Enclave2/%.o: Enclave2/%.cpp
  258. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  259. @echo "CXX <= $<"
  260. Enclave2.so: Enclave2/Enclave2_t.o $(Enclave_Cpp_Objects_2) $(Trust_Lib_Name)
  261. @$(CXX) Enclave2/Enclave2_t.o $(Enclave_Cpp_Objects_2) -o $@ $(Enclave2_Link_Flags)
  262. @echo "LINK => $@"
  263. $(Enclave_Name_2): Enclave2.so
  264. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave2/Enclave2_private.pem -enclave Enclave2.so -out $@ -config Enclave2/Enclave2.config.xml
  265. @echo "SIGN => $@"
  266. Enclave3/Enclave3_t.c: $(SGX_EDGER8R) Enclave3/Enclave3.edl
  267. @cd Enclave3 && $(SGX_EDGER8R) --use-prefix --trusted ../Enclave3/Enclave3.edl --search-path $(SGX_SDK)/include
  268. @echo "GEN => $@"
  269. Enclave3/Enclave3_t.o: Enclave3/Enclave3_t.c
  270. @$(CC) $(Enclave_Compile_Flags) -c $< -o $@
  271. @echo "CC <= $<"
  272. Enclave3/%.o: Enclave3/%.cpp
  273. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  274. @echo "CXX <= $<"
  275. Enclave3.so: Enclave3/Enclave3_t.o $(Enclave_Cpp_Objects_3) $(Trust_Lib_Name)
  276. @$(CXX) Enclave3/Enclave3_t.o $(Enclave_Cpp_Objects_3) -o $@ $(Enclave3_Link_Flags)
  277. @echo "LINK => $@"
  278. $(Enclave_Name_3): Enclave3.so
  279. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave3/Enclave3_private.pem -enclave Enclave3.so -out $@ -config Enclave3/Enclave3.config.xml
  280. @echo "SIGN => $@"
  281. ######## Clean ########
  282. .PHONY: clean
  283. clean:
  284. @rm -rf .config_* $(App_Name) *.so *.a App/*.o Enclave1/*.o Enclave1/*_t.* Enclave1/*_u.* Enclave2/*.o Enclave2/*_t.* Enclave2/*_u.* Enclave3/*.o Enclave3/*_t.* Enclave3/*_u.* LocalAttestationCode/*.o Untrusted_LocalAttestation/*.o LocalAttestationCode/*_t.*