draco_options.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. # Copyright 2021 The Draco Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not
  4. # use this file except in compliance with the License. You may obtain a copy of
  5. # 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, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations under
  13. # the License.
  14. if(DRACO_CMAKE_DRACO_OPTIONS_CMAKE_)
  15. return()
  16. endif() # DRACO_CMAKE_DRACO_OPTIONS_CMAKE_
  17. set(DRACO_CMAKE_DRACO_OPTIONS_CMAKE_)
  18. set(draco_features_file_name "${draco_build}/draco/draco_features.h")
  19. set(draco_features_list)
  20. # Simple wrapper for CMake's builtin option command that tracks draco's build
  21. # options in the list variable $draco_options.
  22. macro(draco_option)
  23. unset(option_NAME)
  24. unset(option_HELPSTRING)
  25. unset(option_VALUE)
  26. unset(optional_args)
  27. unset(multi_value_args)
  28. set(single_value_args NAME HELPSTRING VALUE)
  29. cmake_parse_arguments(option "${optional_args}" "${single_value_args}"
  30. "${multi_value_args}" ${ARGN})
  31. if(NOT
  32. (option_NAME
  33. AND option_HELPSTRING
  34. AND DEFINED option_VALUE))
  35. message(FATAL_ERROR "draco_option: NAME HELPSTRING and VALUE required.")
  36. endif()
  37. option(${option_NAME} ${option_HELPSTRING} ${option_VALUE})
  38. if(DRACO_VERBOSE GREATER 2)
  39. message(
  40. "--------- draco_option ---------\n"
  41. "option_NAME=${option_NAME}\n"
  42. "option_HELPSTRING=${option_HELPSTRING}\n"
  43. "option_VALUE=${option_VALUE}\n"
  44. "------------------------------------------\n")
  45. endif()
  46. list(APPEND draco_options ${option_NAME})
  47. list(REMOVE_DUPLICATES draco_options)
  48. endmacro()
  49. # Dumps the $draco_options list via CMake message command.
  50. macro(draco_dump_options)
  51. foreach(option_name ${draco_options})
  52. message("${option_name}: ${${option_name}}")
  53. endforeach()
  54. endmacro()
  55. # Set default options.
  56. macro(draco_set_default_options)
  57. draco_option(
  58. NAME DRACO_FAST
  59. HELPSTRING "Try to build faster libs."
  60. VALUE OFF)
  61. draco_option(
  62. NAME DRACO_JS_GLUE
  63. HELPSTRING "Enable JS Glue and JS targets when using Emscripten."
  64. VALUE ON)
  65. draco_option(
  66. NAME DRACO_IE_COMPATIBLE
  67. HELPSTRING "Enable support for older IE builds when using Emscripten."
  68. VALUE OFF)
  69. draco_option(
  70. NAME DRACO_MESH_COMPRESSION
  71. HELPSTRING "Enable mesh compression."
  72. VALUE ON)
  73. draco_option(
  74. NAME DRACO_POINT_CLOUD_COMPRESSION
  75. HELPSTRING "Enable point cloud compression."
  76. VALUE ON)
  77. draco_option(
  78. NAME DRACO_PREDICTIVE_EDGEBREAKER
  79. HELPSTRING "Enable predictive edgebreaker."
  80. VALUE ON)
  81. draco_option(
  82. NAME DRACO_STANDARD_EDGEBREAKER
  83. HELPSTRING "Enable stand edgebreaker."
  84. VALUE ON)
  85. draco_option(
  86. NAME DRACO_BACKWARDS_COMPATIBILITY
  87. HELPSTRING "Enable backwards compatibility."
  88. VALUE ON)
  89. draco_option(
  90. NAME DRACO_DECODER_ATTRIBUTE_DEDUPLICATION
  91. HELPSTRING "Enable attribute deduping."
  92. VALUE OFF)
  93. draco_option(
  94. NAME DRACO_TESTS
  95. HELPSTRING "Enables tests."
  96. VALUE OFF)
  97. draco_option(
  98. NAME DRACO_WASM
  99. HELPSTRING "Enables WASM support."
  100. VALUE OFF)
  101. draco_option(
  102. NAME DRACO_UNITY_PLUGIN
  103. HELPSTRING "Build plugin library for Unity."
  104. VALUE OFF)
  105. draco_option(
  106. NAME DRACO_ANIMATION_ENCODING
  107. HELPSTRING "Enable animation."
  108. VALUE OFF)
  109. draco_option(
  110. NAME DRACO_GLTF_BITSTREAM
  111. HELPSTRING "Draco GLTF extension bitstream specified features only."
  112. VALUE OFF)
  113. draco_option(
  114. NAME DRACO_MAYA_PLUGIN
  115. HELPSTRING "Build plugin library for Maya."
  116. VALUE OFF)
  117. draco_option(
  118. NAME DRACO_TRANSCODER_SUPPORTED
  119. HELPSTRING "Enable the Draco transcoder."
  120. VALUE OFF)
  121. draco_option(
  122. NAME DRACO_DEBUG_COMPILER_WARNINGS
  123. HELPSTRING "Turn on more warnings."
  124. VALUE OFF)
  125. draco_check_deprecated_options()
  126. endmacro()
  127. # Warns when a deprecated option is used and sets the option that replaced it.
  128. macro(draco_handle_deprecated_option)
  129. unset(option_OLDNAME)
  130. unset(option_NEWNAME)
  131. unset(optional_args)
  132. unset(multi_value_args)
  133. set(single_value_args OLDNAME NEWNAME)
  134. cmake_parse_arguments(option "${optional_args}" "${single_value_args}"
  135. "${multi_value_args}" ${ARGN})
  136. if("${${option_OLDNAME}}")
  137. message(WARNING "${option_OLDNAME} is deprecated. Use ${option_NEWNAME}.")
  138. set(${option_NEWNAME} ${${option_OLDNAME}})
  139. endif()
  140. endmacro()
  141. # Checks for use of deprecated options.
  142. macro(draco_check_deprecated_options)
  143. draco_handle_deprecated_option(OLDNAME ENABLE_EXTRA_SPEED NEWNAME DRACO_FAST)
  144. draco_handle_deprecated_option(OLDNAME ENABLE_JS_GLUE NEWNAME DRACO_JS_GLUE)
  145. draco_handle_deprecated_option(OLDNAME ENABLE_MESH_COMPRESSION NEWNAME
  146. DRACO_MESH_COMPRESSION)
  147. draco_handle_deprecated_option(OLDNAME ENABLE_POINT_CLOUD_COMPRESSION NEWNAME
  148. DRACO_POINT_CLOUD_COMPRESSION)
  149. draco_handle_deprecated_option(OLDNAME ENABLE_PREDICTIVE_EDGEBREAKER NEWNAME
  150. DRACO_PREDICTIVE_EDGEBREAKER)
  151. draco_handle_deprecated_option(OLDNAME ENABLE_STANDARD_EDGEBREAKER NEWNAME
  152. DRACO_STANDARD_EDGEBREAKER)
  153. draco_handle_deprecated_option(OLDNAME ENABLE_BACKWARDS_COMPATIBILITY NEWNAME
  154. DRACO_BACKWARDS_COMPATIBILITY)
  155. draco_handle_deprecated_option(OLDNAME ENABLE_DECODER_ATTRIBUTE_DEDUPLICATION
  156. NEWNAME DRACO_DECODER_ATTRIBUTE_DEDUPLICATION)
  157. draco_handle_deprecated_option(OLDNAME ENABLE_TESTS NEWNAME DRACO_TESTS)
  158. draco_handle_deprecated_option(OLDNAME ENABLE_WASM NEWNAME DRACO_WASM)
  159. draco_handle_deprecated_option(OLDNAME BUILD_UNITY_PLUGIN NEWNAME
  160. DRACO_UNITY_PLUGIN)
  161. draco_handle_deprecated_option(OLDNAME BUILD_ANIMATION_ENCODING NEWNAME
  162. DRACO_ANIMATION_ENCODING)
  163. draco_handle_deprecated_option(OLDNAME BUILD_FOR_GLTF NEWNAME DRACO_GLTF)
  164. draco_handle_deprecated_option(OLDNAME BUILD_MAYA_PLUGIN NEWNAME
  165. DRACO_MAYA_PLUGIN)
  166. draco_handle_deprecated_option(OLDNAME BUILD_USD_PLUGIN NEWNAME
  167. BUILD_SHARED_LIBS)
  168. draco_handle_deprecated_option(OLDNAME DRACO_GLTF NEWNAME
  169. DRACO_GLTF_BITSTREAM)
  170. endmacro()
  171. # Macro for setting Draco features based on user configuration. Features enabled
  172. # by this macro are Draco global.
  173. macro(draco_set_optional_features)
  174. if(DRACO_GLTF_BITSTREAM)
  175. # Enable only the features included in the Draco GLTF bitstream spec.
  176. draco_enable_feature(FEATURE "DRACO_MESH_COMPRESSION_SUPPORTED")
  177. draco_enable_feature(FEATURE "DRACO_NORMAL_ENCODING_SUPPORTED")
  178. draco_enable_feature(FEATURE "DRACO_STANDARD_EDGEBREAKER_SUPPORTED")
  179. else()
  180. if(DRACO_POINT_CLOUD_COMPRESSION)
  181. draco_enable_feature(FEATURE "DRACO_POINT_CLOUD_COMPRESSION_SUPPORTED")
  182. endif()
  183. if(DRACO_MESH_COMPRESSION)
  184. draco_enable_feature(FEATURE "DRACO_MESH_COMPRESSION_SUPPORTED")
  185. draco_enable_feature(FEATURE "DRACO_NORMAL_ENCODING_SUPPORTED")
  186. if(DRACO_STANDARD_EDGEBREAKER)
  187. draco_enable_feature(FEATURE "DRACO_STANDARD_EDGEBREAKER_SUPPORTED")
  188. endif()
  189. if(DRACO_PREDICTIVE_EDGEBREAKER)
  190. draco_enable_feature(FEATURE "DRACO_PREDICTIVE_EDGEBREAKER_SUPPORTED")
  191. endif()
  192. endif()
  193. if(DRACO_BACKWARDS_COMPATIBILITY)
  194. draco_enable_feature(FEATURE "DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED")
  195. endif()
  196. if(NOT EMSCRIPTEN)
  197. # For now, enable deduplication for both encoder and decoder.
  198. # TODO(ostava): Support for disabling attribute deduplication for the C++
  199. # decoder is planned in future releases.
  200. draco_enable_feature(FEATURE
  201. DRACO_ATTRIBUTE_INDICES_DEDUPLICATION_SUPPORTED)
  202. draco_enable_feature(FEATURE
  203. DRACO_ATTRIBUTE_VALUES_DEDUPLICATION_SUPPORTED)
  204. endif()
  205. endif()
  206. if(DRACO_UNITY_PLUGIN)
  207. draco_enable_feature(FEATURE "DRACO_UNITY_PLUGIN")
  208. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  209. endif()
  210. if(DRACO_MAYA_PLUGIN)
  211. draco_enable_feature(FEATURE "DRACO_MAYA_PLUGIN")
  212. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  213. endif()
  214. if(DRACO_TRANSCODER_SUPPORTED)
  215. draco_enable_feature(FEATURE "DRACO_TRANSCODER_SUPPORTED")
  216. endif()
  217. endmacro()
  218. # Macro that handles tracking of Draco preprocessor symbols for the purpose of
  219. # producing draco_features.h.
  220. #
  221. # ~~~
  222. # draco_enable_feature(FEATURE <feature_name> [TARGETS <target_name>])
  223. # ~~~
  224. #
  225. # FEATURE is required. It should be a Draco preprocessor symbol. TARGETS is
  226. # optional. It can be one or more draco targets.
  227. #
  228. # When the TARGETS argument is not present the preproc symbol is added to
  229. # draco_features.h. When it is draco_features.h is unchanged, and
  230. # target_compile_options() is called for each target specified.
  231. macro(draco_enable_feature)
  232. set(def_flags)
  233. set(def_single_arg_opts FEATURE)
  234. set(def_multi_arg_opts TARGETS)
  235. cmake_parse_arguments(DEF "${def_flags}" "${def_single_arg_opts}"
  236. "${def_multi_arg_opts}" ${ARGN})
  237. if("${DEF_FEATURE}" STREQUAL "")
  238. message(FATAL_ERROR "Empty FEATURE passed to draco_enable_feature().")
  239. endif()
  240. # Do nothing/return early if $DEF_FEATURE is already in the list.
  241. list(FIND draco_features_list ${DEF_FEATURE} df_index)
  242. if(NOT df_index EQUAL -1)
  243. return()
  244. endif()
  245. list(LENGTH DEF_TARGETS df_targets_list_length)
  246. if(${df_targets_list_length} EQUAL 0)
  247. list(APPEND draco_features_list ${DEF_FEATURE})
  248. else()
  249. foreach(target ${DEF_TARGETS})
  250. target_compile_definitions(${target} PRIVATE ${DEF_FEATURE})
  251. endforeach()
  252. endif()
  253. endmacro()
  254. # Function for generating draco_features.h.
  255. function(draco_generate_features_h)
  256. file(WRITE "${draco_features_file_name}.new"
  257. "// GENERATED FILE -- DO NOT EDIT\n\n" "#ifndef DRACO_FEATURES_H_\n"
  258. "#define DRACO_FEATURES_H_\n\n")
  259. foreach(feature ${draco_features_list})
  260. file(APPEND "${draco_features_file_name}.new" "#define ${feature}\n")
  261. endforeach()
  262. if(MSVC)
  263. if(NOT DRACO_DEBUG_COMPILER_WARNINGS)
  264. file(APPEND "${draco_features_file_name}.new"
  265. "// Enable DRACO_DEBUG_COMPILER_WARNINGS at CMake generation \n"
  266. "// time to remove these pragmas.\n")
  267. # warning C4018: '<operator>': signed/unsigned mismatch.
  268. file(APPEND "${draco_features_file_name}.new"
  269. "#pragma warning(disable:4018)\n")
  270. # warning C4146: unary minus operator applied to unsigned type, result
  271. # still unsigned
  272. file(APPEND "${draco_features_file_name}.new"
  273. "#pragma warning(disable:4146)\n")
  274. # warning C4244: 'return': conversion from '<type>' to '<type>', possible
  275. # loss of data.
  276. file(APPEND "${draco_features_file_name}.new"
  277. "#pragma warning(disable:4244)\n")
  278. # warning C4267: 'initializing' conversion from '<type>' to '<type>',
  279. # possible loss of data.
  280. file(APPEND "${draco_features_file_name}.new"
  281. "#pragma warning(disable:4267)\n")
  282. # warning C4305: 'context' : truncation from 'type1' to 'type2'.
  283. file(APPEND "${draco_features_file_name}.new"
  284. "#pragma warning(disable:4305)\n")
  285. # warning C4661: 'identifier' : no suitable definition provided for
  286. # explicit template instantiation request.
  287. file(APPEND "${draco_features_file_name}.new"
  288. "#pragma warning(disable:4661)\n")
  289. # warning C4800: Implicit conversion from 'type' to bool. Possible
  290. # information loss.
  291. # Also, in older MSVC releases:
  292. # warning C4800: 'type' : forcing value to bool 'true' or 'false'
  293. # (performance warning).
  294. file(APPEND "${draco_features_file_name}.new"
  295. "#pragma warning(disable:4800)\n")
  296. # warning C4804: '<operator>': unsafe use of type '<type>' in operation.
  297. file(APPEND "${draco_features_file_name}.new"
  298. "#pragma warning(disable:4804)\n")
  299. endif()
  300. endif()
  301. file(APPEND "${draco_features_file_name}.new"
  302. "\n#endif // DRACO_FEATURES_H_\n")
  303. # Will replace ${draco_features_file_name} only if the file content has
  304. # changed. This prevents forced Draco rebuilds after CMake runs.
  305. configure_file("${draco_features_file_name}.new"
  306. "${draco_features_file_name}")
  307. file(REMOVE "${draco_features_file_name}.new")
  308. endfunction()
  309. # Sets default options for the build and processes user controlled options to
  310. # compute enabled features.
  311. macro(draco_setup_options)
  312. draco_set_default_options()
  313. draco_set_optional_features()
  314. endmacro()