Makefile 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. ######## 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 DRM_app/*.cpp)
  69. App_Include_Paths := -I$(SGX_SDK)/include
  70. App_C_Flags := $(SGX_COMMON_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_C_Flags += -DDEBUG -UNDEBUG -UEDEBUG
  77. else ifeq ($(SGX_PRERELEASE), 1)
  78. App_C_Flags += -DNDEBUG -DEDEBUG -UDEBUG
  79. else
  80. App_C_Flags += -DNDEBUG -UEDEBUG -UDEBUG
  81. endif
  82. App_Cpp_Flags := $(App_C_Flags) -std=c++11
  83. App_Link_Flags := $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -L. -lpthread -Wl,-rpath=$(CURDIR)/sample_libcrypto -Wl,-rpath=$(CURDIR)
  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. App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
  90. App_Name := app
  91. ######## Enclave Settings ########
  92. ifneq ($(SGX_MODE), HW)
  93. Trts_Library_Name := sgx_trts_sim
  94. Service_Library_Name := sgx_tservice_sim
  95. else
  96. Trts_Library_Name := sgx_trts
  97. Service_Library_Name := sgx_tservice
  98. endif
  99. Crypto_Library_Name := sgx_tcrypto
  100. Enclave_Cpp_Files := DRM_enclave/DRM_enclave.cpp
  101. Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport
  102. Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths)
  103. Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++03 -nostdinc++
  104. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  105. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  106. # so that the whole content of trts is included in the enclave.
  107. # 2. For other libraries, you just need to pull the required symbols.
  108. # Use `--start-group' and `--end-group' to link these libraries.
  109. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  110. # Otherwise, you may get some undesirable errors.
  111. Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
  112. -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  113. -Wl,--start-group -lsgx_tstdc -lsgx_tstdcxx -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
  114. -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
  115. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
  116. -Wl,--defsym,__ImageBase=0 \
  117. -Wl,--version-script=DRM_enclave/DRM_enclave.lds
  118. Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
  119. Enclave_Name := DRM_enclave.so
  120. Signed_Enclave_Name := DRM_enclave.signed.so
  121. Enclave_Config_File := DRM_enclave/DRM_enclave.config.xml
  122. ifeq ($(SGX_MODE), HW)
  123. ifeq ($(SGX_DEBUG), 1)
  124. Build_Mode = HW_DEBUG
  125. else ifeq ($(SGX_PRERELEASE), 1)
  126. Build_Mode = HW_PRERELEASE
  127. else
  128. Build_Mode = HW_RELEASE
  129. endif
  130. else
  131. ifeq ($(SGX_DEBUG), 1)
  132. Build_Mode = SIM_DEBUG
  133. else ifeq ($(SGX_PRERELEASE), 1)
  134. Build_Mode = SIM_PRERELEASE
  135. else
  136. Build_Mode = SIM_RELEASE
  137. endif
  138. endif
  139. .PHONY: all run
  140. ifeq ($(Build_Mode), HW_RELEASE)
  141. all: .config_$(Build_Mode)_$(SGX_ARCH) $(App_Name) $(Enclave_Name)
  142. @echo "The project has been built in release hardware mode."
  143. @echo "Please sign the $(Enclave_Name) first with your signing key before you run the $(App_Name) to launch and access the enclave."
  144. @echo "To sign the enclave use the command:"
  145. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <your key> -enclave $(Enclave_Name) -out <$(Signed_Enclave_Name)> -config $(Enclave_Config_File)"
  146. @echo "You can also sign the enclave using an external signing tool."
  147. @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."
  148. else
  149. all: .config_$(Build_Mode)_$(SGX_ARCH) $(App_Name) $(Signed_Enclave_Name)
  150. ifeq ($(Build_Mode), HW_DEBUG)
  151. @echo "The project has been built in debug hardware mode."
  152. else ifeq ($(Build_Mode), SIM_DEBUG)
  153. @echo "The project has been built in debug simulation mode."
  154. else ifeq ($(Build_Mode), HW_PRERELEASE)
  155. @echo "The project has been built in pre-release hardware mode."
  156. else ifeq ($(Build_Mode), SIM_PRERELEASE)
  157. @echo "The project has been built in pre-release simulation mode."
  158. else
  159. @echo "The project has been built in release simulation mode."
  160. endif
  161. endif
  162. run: all
  163. ifneq ($(Build_Mode), HW_RELEASE)
  164. @$(CURDIR)/$(App_Name)
  165. @echo "RUN => $(App_Name) [$(SGX_MODE)|$(SGX_ARCH), OK]"
  166. endif
  167. .config_$(Build_Mode)_$(SGX_ARCH):
  168. @rm -f .config_* $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) DRM_app/DRM_enclave_u.* $(Enclave_Cpp_Objects) DRM_enclave/DRM_enclave_t.*
  169. @touch .config_$(Build_Mode)_$(SGX_ARCH)
  170. ######## App Objects ########
  171. DRM_app/DRM_enclave_u.c: $(SGX_EDGER8R) DRM_enclave/DRM_enclave.edl
  172. @cd DRM_app && $(SGX_EDGER8R) --untrusted ../DRM_enclave/DRM_enclave.edl --search-path ../DRM_enclave --search-path $(SGX_SDK)/include
  173. @echo "GEN => $@"
  174. DRM_app/DRM_enclave_u.o: DRM_app/DRM_enclave_u.c
  175. @$(CC) $(App_C_Flags) -c $< -o $@
  176. @echo "CC <= $<"
  177. DRM_app/%.o: DRM_app/%.cpp
  178. @$(CXX) $(App_Cpp_Flags) -c $< -o $@
  179. @echo "CXX <= $<"
  180. $(App_Name): DRM_app/DRM_enclave_u.o $(App_Cpp_Objects)
  181. @$(CXX) $^ -o $@ $(App_Link_Flags)
  182. @echo "LINK => $@"
  183. ######## Enclave Objects ########
  184. DRM_enclave/DRM_enclave_t.c: $(SGX_EDGER8R) DRM_enclave/DRM_enclave.edl
  185. @cd DRM_enclave && $(SGX_EDGER8R) --trusted ../DRM_enclave/DRM_enclave.edl --search-path ../DRM_enclave --search-path $(SGX_SDK)/include
  186. @echo "GEN => $@"
  187. DRM_enclave/DRM_enclave_t.o: DRM_enclave/DRM_enclave_t.c
  188. @$(CC) $(Enclave_C_Flags) -c $< -o $@
  189. @echo "CC <= $<"
  190. DRM_enclave/%.o: DRM_enclave/%.cpp
  191. @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
  192. @echo "CXX <= $<"
  193. $(Enclave_Name): DRM_enclave/DRM_enclave_t.o $(Enclave_Cpp_Objects)
  194. @$(CXX) $^ -o $@ $(Enclave_Link_Flags)
  195. @echo "LINK => $@"
  196. $(Signed_Enclave_Name): $(Enclave_Name)
  197. @$(SGX_ENCLAVE_SIGNER) sign -key DRM_enclave/DRM_enclave_private.pem -enclave $(Enclave_Name) -out $@ -config $(Enclave_Config_File)
  198. @echo "SIGN => $@"
  199. .PHONY: clean
  200. clean:
  201. @rm -f .config_* $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) DRM_app/DRM_enclave_u.* $(Enclave_Cpp_Objects) DRM_enclave/DRM_enclave_t.*