CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (c) 2015-2016 The Khronos Group Inc.
  2. #
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Utility functions for pushing & popping variables.
  16. function(push_variable var val)
  17. set("${var}_SAVE_STACK" "${${var}}" "${${var}_SAVE_STACK}" PARENT_SCOPE)
  18. set(${var} ${val} PARENT_SCOPE)
  19. endfunction()
  20. function(pop_variable var)
  21. set(save_stack "${${var}_SAVE_STACK}")
  22. list(GET save_stack 0 val)
  23. list(REMOVE_AT save_stack 0)
  24. set("${var}_SAVE_STACK" "${save_stack}" PARENT_SCOPE)
  25. set(${var} ${val} PARENT_SCOPE)
  26. endfunction()
  27. if (DEFINED SPIRV-Headers_SOURCE_DIR)
  28. # This allows flexible position of the SPIRV-Headers repo.
  29. set(SPIRV_HEADER_DIR ${SPIRV-Headers_SOURCE_DIR})
  30. else()
  31. set(SPIRV_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers)
  32. endif()
  33. if (IS_DIRECTORY ${SPIRV_HEADER_DIR})
  34. # TODO(dneto): We should not be modifying the parent scope.
  35. set(SPIRV_HEADER_INCLUDE_DIR ${SPIRV_HEADER_DIR}/include PARENT_SCOPE)
  36. # Add SPIRV-Headers as a sub-project if it isn't already defined.
  37. # Do this so enclosing projects can use SPIRV-Headers_SOURCE_DIR to find
  38. # headers to include.
  39. if (NOT DEFINED SPIRV-Headers_SOURCE_DIR)
  40. add_subdirectory(${SPIRV_HEADER_DIR})
  41. endif()
  42. else()
  43. message(FATAL_ERROR
  44. "SPIRV-Headers was not found - please checkout a copy at external/spirv-headers.")
  45. endif()
  46. if (NOT ${SPIRV_SKIP_TESTS})
  47. # Find gmock if we can. If it's not already configured, then try finding
  48. # it in external/googletest.
  49. if (TARGET gmock)
  50. message(STATUS "Google Mock already configured")
  51. else()
  52. if (NOT GMOCK_DIR)
  53. set(GMOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
  54. endif()
  55. if(EXISTS ${GMOCK_DIR})
  56. if(MSVC)
  57. # Our tests use ::testing::Combine. Work around a compiler
  58. # detection problem in googletest, where that template is
  59. # accidentally disabled for VS 2017.
  60. # See https://github.com/google/googletest/issues/1352
  61. add_definitions(-DGTEST_HAS_COMBINE=1)
  62. endif()
  63. if(WIN32)
  64. option(gtest_force_shared_crt
  65. "Use shared (DLL) run-time lib even when Google Test is built as static lib."
  66. ON)
  67. endif()
  68. # gtest requires special defines for building as a shared
  69. # library, simply always build as static.
  70. push_variable(BUILD_SHARED_LIBS 0)
  71. add_subdirectory(${GMOCK_DIR} ${CMAKE_CURRENT_BINARY_DIR}/googletest EXCLUDE_FROM_ALL)
  72. pop_variable(BUILD_SHARED_LIBS)
  73. endif()
  74. endif()
  75. if (TARGET gmock)
  76. set(GTEST_TARGETS
  77. gtest
  78. gtest_main
  79. gmock
  80. gmock_main
  81. )
  82. foreach(target ${GTEST_TARGETS})
  83. set_property(TARGET ${target} PROPERTY FOLDER GoogleTest)
  84. endforeach()
  85. endif()
  86. # Find Effcee and RE2, for testing.
  87. # RE2 depends on Abseil. We set absl_SOURCE_DIR if it is not already set, so
  88. # that effcee can find abseil.
  89. if(NOT TARGET absl::base)
  90. if (NOT absl_SOURCE_DIR)
  91. if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/abseil_cpp)
  92. set(absl_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/abseil_cpp" CACHE STRING "Abseil source dir" )
  93. endif()
  94. endif()
  95. endif()
  96. # First find RE2, since Effcee depends on it.
  97. # If already configured, then use that. Otherwise, prefer to find it under 're2'
  98. # in this directory.
  99. if (NOT TARGET re2)
  100. # If we are configuring RE2, then turn off its testing. It takes a long time and
  101. # does not add much value for us. If an enclosing project configured RE2, then it
  102. # has already chosen whether to enable RE2 testing.
  103. set(RE2_BUILD_TESTING OFF CACHE STRING "Run RE2 Tests")
  104. if (NOT RE2_SOURCE_DIR)
  105. if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/re2)
  106. set(RE2_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/re2" CACHE STRING "RE2 source dir" )
  107. endif()
  108. endif()
  109. endif()
  110. if (NOT TARGET effcee)
  111. # Expect to find effcee in this directory.
  112. if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/effcee)
  113. # If we're configuring RE2 (via Effcee), then turn off RE2 testing.
  114. if (NOT TARGET re2)
  115. set(RE2_BUILD_TESTING OFF)
  116. endif()
  117. if (MSVC)
  118. # SPIRV-Tools uses the shared CRT with MSVC. Tell Effcee to do the same.
  119. set(EFFCEE_ENABLE_SHARED_CRT ON)
  120. endif()
  121. set(EFFCEE_BUILD_SAMPLES OFF CACHE BOOL "Do not build Effcee examples")
  122. if (NOT TARGET effcee)
  123. set(EFFCEE_BUILD_TESTING OFF CACHE BOOL "Do not build Effcee test suite")
  124. endif()
  125. push_variable(BUILD_SHARED_LIBS 0) # effcee does not export any symbols for building as a DLL. Always build as static.
  126. add_subdirectory(effcee EXCLUDE_FROM_ALL)
  127. pop_variable(BUILD_SHARED_LIBS)
  128. set_property(TARGET effcee PROPERTY FOLDER Effcee)
  129. # Turn off warnings for effcee and re2
  130. set_property(TARGET effcee APPEND PROPERTY COMPILE_OPTIONS -w)
  131. set_property(TARGET re2 APPEND PROPERTY COMPILE_OPTIONS -w)
  132. endif()
  133. endif()
  134. endif()
  135. if(SPIRV_BUILD_FUZZER)
  136. function(backup_compile_options)
  137. get_property(
  138. SPIRV_TOOLS_BACKUP_EXTERNAL_COMPILE_OPTIONS
  139. DIRECTORY
  140. PROPERTY COMPILE_OPTIONS
  141. )
  142. endfunction()
  143. function(restore_compile_options)
  144. set_property(
  145. DIRECTORY
  146. PROPERTY COMPILE_OPTIONS
  147. ${SPIRV_TOOLS_BACKUP_EXTERNAL_COMPILE_OPTIONS}
  148. )
  149. endfunction()
  150. if(NOT TARGET protobuf::libprotobuf OR NOT TARGET protobuf::protoc)
  151. set(SPIRV_TOOLS_PROTOBUF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/protobuf)
  152. if (NOT IS_DIRECTORY ${SPIRV_TOOLS_PROTOBUF_DIR})
  153. message(
  154. FATAL_ERROR
  155. "protobuf not found - please checkout a copy under external/.")
  156. endif()
  157. set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable protobuf tests")
  158. set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Do not build protobuf static runtime")
  159. backup_compile_options()
  160. if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
  161. add_compile_options(-Wno-inconsistent-missing-override)
  162. endif()
  163. add_subdirectory(${SPIRV_TOOLS_PROTOBUF_DIR} EXCLUDE_FROM_ALL)
  164. restore_compile_options()
  165. endif()
  166. endif()