Makefile 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #
  2. # Copyright (C) 2011-2016 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 ?= SIM
  34. SGX_ARCH ?= x64
  35. ifeq ($(shell getconf LONG_BIT), 32)
  36. SGX_ARCH := x86
  37. else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
  38. SGX_ARCH := x86
  39. endif
  40. ifeq ($(SGX_ARCH), x86)
  41. SGX_COMMON_CFLAGS := -m32
  42. SGX_LIBRARY_PATH := $(SGX_SDK)/lib
  43. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
  44. SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
  45. else
  46. SGX_COMMON_CFLAGS := -m64
  47. SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
  48. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
  49. SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
  50. endif
  51. ifeq ($(SGX_DEBUG), 1)
  52. ifeq ($(SGX_PRERELEASE), 1)
  53. $(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
  54. endif
  55. endif
  56. ifeq ($(SGX_DEBUG), 1)
  57. SGX_COMMON_CFLAGS += -O0 -g
  58. else
  59. SGX_COMMON_CFLAGS += -O2
  60. endif
  61. ifeq ($(SUPPLIED_KEY_DERIVATION), 1)
  62. SGX_COMMON_CFLAGS += -DSUPPLIED_KEY_DERIVATION
  63. endif
  64. ######## App Settings ########
  65. ifneq ($(SGX_MODE), HW)
  66. Urts_Library_Name := sgx_urts_sim
  67. else
  68. Urts_Library_Name := sgx_urts
  69. endif
  70. App_Cpp_Files := isv_app/isv_app.cpp
  71. App_Include_Paths := -Iservice_provider -I$(SGX_SDK)/include
  72. App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
  73. # Three configuration modes - Debug, prerelease, release
  74. # Debug - Macro DEBUG enabled.
  75. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  76. # Release - Macro NDEBUG enabled.
  77. ifeq ($(SGX_DEBUG), 1)
  78. App_C_Flags += -DDEBUG -UNDEBUG -UEDEBUG
  79. else ifeq ($(SGX_PRERELEASE), 1)
  80. App_C_Flags += -DNDEBUG -DEDEBUG -UDEBUG
  81. else
  82. App_C_Flags += -DNDEBUG -UEDEBUG -UDEBUG
  83. endif
  84. App_Cpp_Flags := $(App_C_Flags) -std=c++11
  85. App_Link_Flags := $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -L. -lsgx_ukey_exchange -lpthread -lservice_provider -Wl,-rpath=$(CURDIR)/sample_libcrypto -Wl,-rpath=$(CURDIR)
  86. ifneq ($(SGX_MODE), HW)
  87. App_Link_Flags += -lsgx_uae_service_sim
  88. else
  89. App_Link_Flags += -lsgx_uae_service
  90. endif
  91. App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
  92. App_Name := app
  93. ######## Service Provider Settings ########
  94. ServiceProvider_Cpp_Files := service_provider/ecp.cpp service_provider/network_ra.cpp service_provider/service_provider.cpp service_provider/ias_ra.cpp
  95. ServiceProvider_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport -Isample_libcrypto
  96. ServiceProvider_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes -I$(SGX_SDK)/include -Isample_libcrypto
  97. ServiceProvider_Cpp_Flags := $(ServiceProvider_C_Flags) -std=c++11
  98. ServiceProvider_Link_Flags := -shared $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -lsample_libcrypto -Lsample_libcrypto
  99. ServiceProvider_Cpp_Objects := $(ServiceProvider_Cpp_Files:.cpp=.o)
  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 := isv_enclave/isv_enclave.cpp
  110. Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport
  111. Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths)
  112. Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++03 -nostdinc++
  113. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  114. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  115. # so that the whole content of trts is included in the enclave.
  116. # 2. For other libraries, you just need to pull the required symbols.
  117. # Use `--start-group' and `--end-group' to link these libraries.
  118. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  119. # Otherwise, you may get some undesirable errors.
  120. Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
  121. -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  122. -Wl,--start-group -lsgx_tstdc -lsgx_tstdcxx -lsgx_tkey_exchange -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
  123. -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  124. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
  125. -Wl,--defsym,__ImageBase=0 \
  126. -Wl,--version-script=isv_enclave/isv_enclave.lds
  127. Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
  128. Enclave_Name := isv_enclave.so
  129. Signed_Enclave_Name := isv_enclave.signed.so
  130. Enclave_Config_File := isv_enclave/isv_enclave.config.xml
  131. ifeq ($(SGX_MODE), HW)
  132. ifneq ($(SGX_DEBUG), 1)
  133. ifneq ($(SGX_PRERELEASE), 1)
  134. Build_Mode = HW_RELEASE
  135. endif
  136. endif
  137. endif
  138. .PHONY: all run
  139. ifeq ($(Build_Mode), HW_RELEASE)
  140. all: libservice_provider.so $(App_Name) $(Enclave_Name)
  141. @echo "The project has been built in release hardware mode."
  142. @echo "Please sign the $(Enclave_Name) first with your signing key before you run the $(App_Name) to launch and access the enclave."
  143. @echo "To sign the enclave use the command:"
  144. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <your key> -enclave $(Enclave_Name) -out <$(Signed_Enclave_Name)> -config $(Enclave_Config_File)"
  145. @echo "You can also sign the enclave using an external signing tool."
  146. @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."
  147. else
  148. all: libservice_provider.so $(App_Name) $(Signed_Enclave_Name)
  149. endif
  150. run: all
  151. ifneq ($(Build_Mode), HW_RELEASE)
  152. @$(CURDIR)/$(App_Name)
  153. @echo "RUN => $(App_Name) [$(SGX_MODE)|$(SGX_ARCH), OK]"
  154. endif
  155. ######## App Objects ########
  156. isv_app/isv_enclave_u.c: $(SGX_EDGER8R) isv_enclave/isv_enclave.edl
  157. @cd isv_app && $(SGX_EDGER8R) --untrusted ../isv_enclave/isv_enclave.edl --search-path ../isv_enclave --search-path $(SGX_SDK)/include
  158. @echo "GEN => $@"
  159. isv_app/isv_enclave_u.o: isv_app/isv_enclave_u.c
  160. @$(CC) $(App_C_Flags) -c $< -o $@
  161. @echo "CC <= $<"
  162. isv_app/%.o: isv_app/%.cpp
  163. @$(CXX) $(App_Cpp_Flags) -c $< -o $@
  164. @echo "CXX <= $<"
  165. $(App_Name): isv_app/isv_enclave_u.o $(App_Cpp_Objects)
  166. @$(CXX) $^ -o $@ $(App_Link_Flags)
  167. @echo "LINK => $@"
  168. ######## Service Provider Objects ########
  169. service_provider/%.o: service_provider/%.cpp
  170. @$(CXX) $(ServiceProvider_Cpp_Flags) -c $< -o $@
  171. @echo "CXX <= $<"
  172. libservice_provider.so: $(ServiceProvider_Cpp_Objects)
  173. @$(CXX) $^ -o $@ $(ServiceProvider_Link_Flags)
  174. @echo "LINK => $@"
  175. ######## Enclave Objects ########
  176. isv_enclave/isv_enclave_t.c: $(SGX_EDGER8R) isv_enclave/isv_enclave.edl
  177. @cd isv_enclave && $(SGX_EDGER8R) --trusted ../isv_enclave/isv_enclave.edl --search-path ../isv_enclave --search-path $(SGX_SDK)/include
  178. @echo "GEN => $@"
  179. isv_enclave/isv_enclave_t.o: isv_enclave/isv_enclave_t.c
  180. @$(CC) $(Enclave_C_Flags) -c $< -o $@
  181. @echo "CC <= $<"
  182. isv_enclave/%.o: isv_enclave/%.cpp
  183. @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
  184. @echo "CXX <= $<"
  185. $(Enclave_Name): isv_enclave/isv_enclave_t.o $(Enclave_Cpp_Objects)
  186. @$(CXX) $^ -o $@ $(Enclave_Link_Flags)
  187. @echo "LINK => $@"
  188. $(Signed_Enclave_Name): $(Enclave_Name)
  189. @$(SGX_ENCLAVE_SIGNER) sign -key isv_enclave/isv_enclave_private.pem -enclave $(Enclave_Name) -out $@ -config $(Enclave_Config_File)
  190. @echo "SIGN => $@"
  191. .PHONY: clean
  192. clean:
  193. @rm -f $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) isv_app/isv_enclave_u.* $(Enclave_Cpp_Objects) isv_enclave/isv_enclave_t.* libservice_provider.* $(ServiceProvider_Cpp_Objects)