CMakeLists.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (c) 2015-2016 The Khronos Group Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. cmake_minimum_required(VERSION 2.8.12)
  15. if (POLICY CMP0054)
  16. # Avoid dereferencing variables or interpret keywords that have been
  17. # quoted or bracketed.
  18. # https://cmake.org/cmake/help/v3.1/policy/CMP0054.html
  19. cmake_policy(SET CMP0054 NEW)
  20. endif()
  21. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  22. project(spirv-tools)
  23. enable_testing()
  24. set(SPIRV_TOOLS "SPIRV-Tools")
  25. include(GNUInstallDirs)
  26. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  27. if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
  28. add_definitions(-DSPIRV_LINUX)
  29. elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
  30. add_definitions(-DSPIRV_WINDOWS)
  31. elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
  32. add_definitions(-DSPIRV_WINDOWS)
  33. elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
  34. add_definitions(-DSPIRV_MAC)
  35. elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
  36. add_definitions(-DSPIRV_ANDROID)
  37. elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
  38. add_definitions(-DSPIRV_FREEBSD)
  39. else()
  40. message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
  41. endif()
  42. if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
  43. message(STATUS "No build type selected, default to Debug")
  44. set(CMAKE_BUILD_TYPE "Debug")
  45. endif()
  46. option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
  47. if(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
  48. set(ENABLE_SPIRV_TOOLS_INSTALL ON)
  49. endif()
  50. option(SPIRV_WERROR "Enable error on warning" ON)
  51. if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
  52. set(COMPILER_IS_LIKE_GNU TRUE)
  53. endif()
  54. if(${COMPILER_IS_LIKE_GNU})
  55. set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
  56. option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
  57. if(${SPIRV_WARN_EVERYTHING})
  58. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  59. set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
  60. -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
  61. elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  62. set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
  63. else()
  64. message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
  65. "so SPIRV_WARN_EVERYTHING has no effect")
  66. endif()
  67. endif()
  68. if(${SPIRV_WERROR})
  69. set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
  70. endif()
  71. elseif(MSVC)
  72. set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
  73. if(${SPIRV_WERROR})
  74. set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
  75. endif()
  76. endif()
  77. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
  78. option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
  79. if(${SPIRV_COLOR_TERMINAL})
  80. add_definitions(-DSPIRV_COLOR_TERMINAL)
  81. endif()
  82. option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
  83. if(${SPIRV_LOG_DEBUG})
  84. add_definitions(-DSPIRV_LOG_DEBUG)
  85. endif()
  86. function(spvtools_default_compile_options TARGET)
  87. target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
  88. if (${COMPILER_IS_LIKE_GNU})
  89. target_compile_options(${TARGET} PRIVATE
  90. -std=c++11 -fno-exceptions -fno-rtti)
  91. target_compile_options(${TARGET} PRIVATE
  92. -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
  93. -Wno-sign-conversion)
  94. # For good call stacks in profiles, keep the frame pointers.
  95. if(NOT "${SPIRV_PERF}" STREQUAL "")
  96. target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
  97. endif()
  98. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  99. set(SPIRV_USE_SANITIZER "" CACHE STRING
  100. "Use the clang sanitizer [address|memory|thread|...]")
  101. if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
  102. target_compile_options(${TARGET} PRIVATE
  103. -fsanitize=${SPIRV_USE_SANITIZER})
  104. endif()
  105. else()
  106. target_compile_options(${TARGET} PRIVATE
  107. -Wno-missing-field-initializers)
  108. endif()
  109. endif()
  110. if (MSVC)
  111. # Specify /EHs for exception handling. This makes using SPIRV-Tools as
  112. # dependencies in other projects easier.
  113. target_compile_options(${TARGET} PRIVATE /EHs)
  114. endif()
  115. # For MinGW cross compile, statically link to the C++ runtime.
  116. # But it still depends on MSVCRT.dll.
  117. if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  118. if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
  119. set_target_properties(${TARGET} PROPERTIES
  120. LINK_FLAGS -static -static-libgcc -static-libstdc++)
  121. endif()
  122. endif()
  123. endfunction()
  124. if(NOT COMMAND find_host_package)
  125. macro(find_host_package)
  126. find_package(${ARGN})
  127. endmacro()
  128. endif()
  129. if(NOT COMMAND find_host_program)
  130. macro(find_host_program)
  131. find_program(${ARGN})
  132. endmacro()
  133. endif()
  134. find_host_package(PythonInterp)
  135. # Defaults to OFF if the user didn't set it.
  136. option(SPIRV_SKIP_EXECUTABLES
  137. "Skip building the executable and tests along with the library"
  138. ${SPIRV_SKIP_EXECUTABLES})
  139. option(SPIRV_SKIP_TESTS
  140. "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
  141. if ("${SPIRV_SKIP_EXECUTABLES}")
  142. set(SPIRV_SKIP_TESTS ON)
  143. endif()
  144. add_subdirectory(external)
  145. add_subdirectory(source)
  146. add_subdirectory(tools)
  147. add_subdirectory(test)
  148. add_subdirectory(examples)
  149. if(ENABLE_SPIRV_TOOLS_INSTALL)
  150. install(
  151. FILES
  152. ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
  153. ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
  154. ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
  155. DESTINATION
  156. ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
  157. endif(ENABLE_SPIRV_TOOLS_INSTALL)
  158. add_test(NAME spirv-tools-copyrights
  159. COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
  160. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})