Makefile 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. ######## App Settings ########
  62. ifneq ($(SGX_MODE), HW)
  63. Urts_Library_Name := sgx_urts_sim
  64. else
  65. Urts_Library_Name := sgx_urts
  66. endif
  67. App_Cpp_Files := $(wildcard App/*.cpp)
  68. App_Include_Paths := -I$(SGX_SDK)/include -I./Common
  69. App_Compile_CFlags := -fPIC -Wno-attributes $(App_Include_Paths)
  70. # Three configuration modes - Debug, prerelease, release
  71. # Debug - Macro DEBUG enabled.
  72. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  73. # Release - Macro NDEBUG enabled.
  74. ifeq ($(SGX_DEBUG), 1)
  75. App_Compile_CFlags += -DDEBUG -UNDEBUG -UEDEBUG
  76. else ifeq ($(SGX_PRERELEASE), 1)
  77. App_Compile_CFlags += -DNDEBUG -DEDEBUG -UDEBUG
  78. else
  79. App_Compile_CFlags += -DNDEBUG -UEDEBUG -UDEBUG
  80. endif
  81. App_Compile_CXXFlags := -std=c++0x $(App_Compile_CFlags)
  82. App_Link_Flags := -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lpthread
  83. ifneq ($(SGX_MODE), HW)
  84. App_Link_Flags += -lsgx_uae_service_sim
  85. else
  86. App_Link_Flags += -lsgx_uae_service
  87. endif
  88. Gen_Untrusted_Source := App/Enclave_u.c
  89. Gen_Untrusted_Object := App/Enclave_u.o
  90. App_Objects := $(Gen_Untrusted_Object) $(App_Cpp_Files:.cpp=.o)
  91. App_Name := app
  92. ######## Enclave Settings ########
  93. ifneq ($(SGX_MODE), HW)
  94. Trts_Library_Name := sgx_trts_sim
  95. Service_Library_Name := sgx_tservice_sim
  96. else
  97. Trts_Library_Name := sgx_trts
  98. Service_Library_Name := sgx_tservice
  99. endif
  100. Crypto_Library_Name := sgx_tcrypto
  101. Enclave_Cpp_Files := $(wildcard Enclave/*.cpp)
  102. Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport -I./Common
  103. Enclave_Compile_CFlags := -nostdinc -ffreestanding -fvisibility=hidden -fpie \
  104. $(Enclave_Include_Paths)
  105. Enclave_Compile_CXXFlags := -nostdinc++ -std=c++03 $(Enclave_Compile_CFlags)
  106. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  107. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  108. # so that the whole content of trts is included in the enclave.
  109. # 2. For other libraries, you just need to pull the required symbols.
  110. # Use `--start-group' and `--end-group' to link these libraries.
  111. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  112. # Otherwise, you may get some undesirable errors.
  113. Enclave_Link_Flags := -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
  114. -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  115. -Wl,--start-group -lsgx_tstdc -lsgx_tstdcxx -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
  116. -Wl,--version-script=Enclave/Enclave.lds -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  117. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0
  118. Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
  119. Gen_Trusted_Source := Enclave/Enclave_t.c
  120. Gen_Trusted_Object := Enclave/Enclave_t.o
  121. Enclave_Objects := $(Gen_Trusted_Object) $(Enclave_Cpp_Files:.cpp=.o)
  122. Enclave_Name := libenclave.so
  123. Signed_Enclave_Name := libenclave.signed.so
  124. Enclave_Config_File := Enclave/Enclave.config.xml
  125. ifeq ($(SGX_MODE), HW)
  126. ifneq ($(SGX_DEBUG), 1)
  127. ifneq ($(SGX_PRERELEASE), 1)
  128. Build_Mode = HW_RELEASE
  129. endif
  130. endif
  131. endif
  132. ifeq ($(Build_Mode), HW_RELEASE)
  133. all: $(App_Name) $(Enclave_Name)
  134. @echo "The project has been built in release hardware mode."
  135. @echo "Please sign the $(Enclave_Name) first with your signing key before you run the $(App_Name) to launch and access the enclave."
  136. @echo "To sign the enclave use the command:"
  137. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <your key> -enclave $(Enclave_Name) -out <$(Signed_Enclave_Name)> -config $(Enclave_Config_File)"
  138. @echo "You can also sign the enclave using an external signing tool."
  139. @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."
  140. else
  141. all: $(App_Name) $(Signed_Enclave_Name)
  142. @echo "Build $(App_Name) [$(SGX_MODE)|$(SGX_ARCH)] Success!"
  143. @echo "Please RUN the project with command:"
  144. @echo " $(App_Name)"
  145. endif
  146. ######## App Objects ########
  147. $(Gen_Untrusted_Source): $(SGX_EDGER8R) Enclave/Enclave.edl
  148. @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl --search-path $(SGX_SDK)/include
  149. @echo "GEN => $@"
  150. $(Gen_Untrusted_Object): $(Gen_Untrusted_Source)
  151. @$(CC) $(SGX_COMMON_CFLAGS) $(App_Compile_CFlags) -c $< -o $@
  152. @echo "CC <= $<"
  153. App/%.o: App/%.cpp
  154. @$(CXX) $(SGX_COMMON_CFLAGS) $(App_Compile_CXXFlags) -c $< -o $@
  155. @echo "CXX <= $<"
  156. $(App_Name): $(App_Objects)
  157. @$(CXX) $(SGX_COMMON_CFLAGS) $^ -o $@ $(App_Link_Flags)
  158. @echo "LINK => $@"
  159. ######## Enclave Objects ########
  160. $(Gen_Trusted_Source): $(SGX_EDGER8R) Enclave/Enclave.edl
  161. @cd Enclave && $(SGX_EDGER8R) --trusted Enclave.edl --search-path $(SGX_SDK)/include
  162. @echo "GEN => $@"
  163. $(Gen_Trusted_Object): $(Gen_Trusted_Source)
  164. @$(CC) $(SGX_COMMON_CFLAGS) $(Enclave_Compile_CFlags) -c $< -o $@
  165. @echo "CC <= $<"
  166. Enclave/%.o: Enclave/%.cpp
  167. @$(CXX) $(SGX_COMMON_CFLAGS) $(Enclave_Compile_CXXFlags) -c $< -o $@
  168. @echo "CXX <= $<"
  169. $(Enclave_Name): $(Enclave_Objects)
  170. @$(CXX) $(SGX_COMMON_CFLAGS) $(Enclave_Objects) -o $@ $(Enclave_Link_Flags)
  171. @echo "LINK => $@"
  172. $(Signed_Enclave_Name): $(Enclave_Name)
  173. @$(SGX_ENCLAVE_SIGNER) sign -key Enclave/Enclave_private.pem -enclave $(Enclave_Name) -out $@ -config $(Enclave_Config_File)
  174. @echo "SIGN => $@"
  175. ######### clean up ########
  176. .PHONY: clean
  177. clean:
  178. @rm -f $(App_Name) $(App_Objects) $(Enclave_Name) $(Enclave_Objects) App/Enclave_u.* Enclave/Enclave_t.* $(Signed_Enclave_Name)