draco_options.cmake 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. if(DRACO_CMAKE_DRACO_OPTIONS_CMAKE_)
  2. return()
  3. endif() # DRACO_CMAKE_DRACO_OPTIONS_CMAKE_
  4. set(DRACO_CMAKE_DRACO_OPTIONS_CMAKE_)
  5. set(draco_features_file_name "${draco_build}/draco/draco_features.h")
  6. set(draco_features_list)
  7. # Simple wrapper for CMake's builtin option command that tracks draco's build
  8. # options in the list variable $draco_options.
  9. macro(draco_option)
  10. unset(option_NAME)
  11. unset(option_HELPSTRING)
  12. unset(option_VALUE)
  13. unset(optional_args)
  14. unset(multi_value_args)
  15. set(single_value_args NAME HELPSTRING VALUE)
  16. cmake_parse_arguments(option "${optional_args}" "${single_value_args}"
  17. "${multi_value_args}" ${ARGN})
  18. if(NOT (option_NAME AND option_HELPSTRING AND DEFINED option_VALUE))
  19. message(FATAL_ERROR "draco_option: NAME HELPSTRING and VALUE required.")
  20. endif()
  21. option(${option_NAME} ${option_HELPSTRING} ${option_VALUE})
  22. if(DRACO_VERBOSE GREATER 2)
  23. message("--------- draco_option ---------\n" "option_NAME=${option_NAME}\n"
  24. "option_HELPSTRING=${option_HELPSTRING}\n"
  25. "option_VALUE=${option_VALUE}\n"
  26. "------------------------------------------\n")
  27. endif()
  28. list(APPEND draco_options ${option_NAME})
  29. list(REMOVE_DUPLICATES draco_options)
  30. endmacro()
  31. # Dumps the $draco_options list via CMake message command.
  32. macro(draco_dump_options)
  33. foreach(option_name ${draco_options})
  34. message("${option_name}: ${${option_name}}")
  35. endforeach()
  36. endmacro()
  37. # Set default options.
  38. macro(draco_set_default_options)
  39. draco_option(NAME DRACO_FAST HELPSTRING "Try to build faster libs." VALUE OFF)
  40. draco_option(NAME DRACO_JS_GLUE HELPSTRING
  41. "Enable JS Glue and JS targets when using Emscripten." VALUE ON)
  42. draco_option(NAME DRACO_IE_COMPATIBLE HELPSTRING
  43. "Enable support for older IE builds when using Emscripten." VALUE
  44. OFF)
  45. draco_option(NAME DRACO_MESH_COMPRESSION HELPSTRING "Enable mesh compression."
  46. VALUE ON)
  47. draco_option(NAME DRACO_POINT_CLOUD_COMPRESSION HELPSTRING
  48. "Enable point cloud compression." VALUE ON)
  49. draco_option(NAME DRACO_PREDICTIVE_EDGEBREAKER HELPSTRING
  50. "Enable predictive edgebreaker." VALUE ON)
  51. draco_option(NAME DRACO_STANDARD_EDGEBREAKER HELPSTRING
  52. "Enable stand edgebreaker." VALUE ON)
  53. draco_option(NAME DRACO_BACKWARDS_COMPATIBILITY HELPSTRING
  54. "Enable backwards compatibility." VALUE ON)
  55. draco_option(NAME DRACO_DECODER_ATTRIBUTE_DEDUPLICATION HELPSTRING
  56. "Enable attribute deduping." VALUE OFF)
  57. draco_option(NAME DRACO_TESTS HELPSTRING "Enables tests." VALUE OFF)
  58. draco_option(NAME DRACO_WASM HELPSTRING "Enables WASM support." VALUE OFF)
  59. draco_option(NAME DRACO_UNITY_PLUGIN HELPSTRING
  60. "Build plugin library for Unity." VALUE OFF)
  61. draco_option(NAME DRACO_ANIMATION_ENCODING HELPSTRING "Enable animation."
  62. VALUE OFF)
  63. draco_option(NAME DRACO_GLTF HELPSTRING "Support GLTF." VALUE OFF)
  64. draco_option(NAME DRACO_MAYA_PLUGIN HELPSTRING
  65. "Build plugin library for Maya." VALUE OFF)
  66. draco_check_deprecated_options()
  67. endmacro()
  68. # Warns when a deprecated option is used and sets the option that replaced it.
  69. macro(draco_handle_deprecated_option)
  70. unset(option_OLDNAME)
  71. unset(option_NEWNAME)
  72. unset(optional_args)
  73. unset(multi_value_args)
  74. set(single_value_args OLDNAME NEWNAME)
  75. cmake_parse_arguments(option "${optional_args}" "${single_value_args}"
  76. "${multi_value_args}" ${ARGN})
  77. if("${${option_OLDNAME}}")
  78. message(WARNING "${option_OLDNAME} is deprecated. Use ${option_NEWNAME}.")
  79. set(${option_NEWNAME} ${${option_OLDNAME}})
  80. endif()
  81. endmacro()
  82. # Checks for use of deprecated options.
  83. macro(draco_check_deprecated_options)
  84. draco_handle_deprecated_option(OLDNAME ENABLE_EXTRA_SPEED NEWNAME DRACO_FAST)
  85. draco_handle_deprecated_option(OLDNAME ENABLE_JS_GLUE NEWNAME DRACO_JS_GLUE)
  86. draco_handle_deprecated_option(OLDNAME ENABLE_MESH_COMPRESSION NEWNAME
  87. DRACO_MESH_COMPRESSION)
  88. draco_handle_deprecated_option(OLDNAME ENABLE_POINT_CLOUD_COMPRESSION NEWNAME
  89. DRACO_POINT_CLOUD_COMPRESSION)
  90. draco_handle_deprecated_option(OLDNAME ENABLE_PREDICTIVE_EDGEBREAKER NEWNAME
  91. DRACO_PREDICTIVE_EDGEBREAKER)
  92. draco_handle_deprecated_option(OLDNAME ENABLE_STANDARD_EDGEBREAKER NEWNAME
  93. DRACO_STANDARD_EDGEBREAKER)
  94. draco_handle_deprecated_option(OLDNAME ENABLE_BACKWARDS_COMPATIBILITY NEWNAME
  95. DRACO_BACKWARDS_COMPATIBILITY)
  96. draco_handle_deprecated_option(OLDNAME ENABLE_DECODER_ATTRIBUTE_DEDUPLICATION
  97. NEWNAME DRACO_DECODER_ATTRIBUTE_DEDUPLICATION)
  98. draco_handle_deprecated_option(OLDNAME ENABLE_TESTS NEWNAME DRACO_TESTS)
  99. draco_handle_deprecated_option(OLDNAME ENABLE_WASM NEWNAME DRACO_WASM)
  100. draco_handle_deprecated_option(OLDNAME BUILD_UNITY_PLUGIN NEWNAME
  101. DRACO_UNITY_PLUGIN)
  102. draco_handle_deprecated_option(OLDNAME BUILD_ANIMATION_ENCODING NEWNAME
  103. DRACO_ANIMATION_ENCODING)
  104. draco_handle_deprecated_option(OLDNAME BUILD_FOR_GLTF NEWNAME DRACO_GLTF)
  105. draco_handle_deprecated_option(OLDNAME BUILD_MAYA_PLUGIN NEWNAME
  106. DRACO_MAYA_PLUGIN)
  107. draco_handle_deprecated_option(OLDNAME BUILD_USD_PLUGIN NEWNAME
  108. BUILD_SHARED_LIBS)
  109. endmacro()
  110. # Macro for setting Draco features based on user configuration. Features enabled
  111. # by this macro are Draco global.
  112. macro(draco_set_optional_features)
  113. if(DRACO_GLTF)
  114. # Override settings when building for GLTF.
  115. draco_enable_feature(FEATURE "DRACO_MESH_COMPRESSION_SUPPORTED")
  116. draco_enable_feature(FEATURE "DRACO_NORMAL_ENCODING_SUPPORTED")
  117. draco_enable_feature(FEATURE "DRACO_STANDARD_EDGEBREAKER_SUPPORTED")
  118. else()
  119. if(DRACO_POINT_CLOUD_COMPRESSION)
  120. draco_enable_feature(FEATURE "DRACO_POINT_CLOUD_COMPRESSION_SUPPORTED")
  121. endif()
  122. if(DRACO_MESH_COMPRESSION)
  123. draco_enable_feature(FEATURE "DRACO_MESH_COMPRESSION_SUPPORTED")
  124. draco_enable_feature(FEATURE "DRACO_NORMAL_ENCODING_SUPPORTED")
  125. if(DRACO_STANDARD_EDGEBREAKER)
  126. draco_enable_feature(FEATURE "DRACO_STANDARD_EDGEBREAKER_SUPPORTED")
  127. endif()
  128. if(DRACO_PREDICTIVE_EDGEBREAKER)
  129. draco_enable_feature(FEATURE "DRACO_PREDICTIVE_EDGEBREAKER_SUPPORTED")
  130. endif()
  131. endif()
  132. if(DRACO_BACKWARDS_COMPATIBILITY)
  133. draco_enable_feature(FEATURE "DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED")
  134. endif()
  135. if(NOT EMSCRIPTEN)
  136. # For now, enable deduplication for both encoder and decoder.
  137. # TODO(ostava): Support for disabling attribute deduplication for the C++
  138. # decoder is planned in future releases.
  139. draco_enable_feature(FEATURE
  140. DRACO_ATTRIBUTE_INDICES_DEDUPLICATION_SUPPORTED)
  141. draco_enable_feature(FEATURE
  142. DRACO_ATTRIBUTE_VALUES_DEDUPLICATION_SUPPORTED)
  143. endif()
  144. endif()
  145. if(DRACO_UNITY_PLUGIN)
  146. draco_enable_feature(FEATURE "DRACO_UNITY_PLUGIN")
  147. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  148. endif()
  149. if(DRACO_MAYA_PLUGIN)
  150. draco_enable_feature(FEATURE "DRACO_MAYA_PLUGIN")
  151. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  152. endif()
  153. endmacro()
  154. # Macro that handles tracking of Draco preprocessor symbols for the purpose of
  155. # producing draco_features.h.
  156. #
  157. # ~~~
  158. # draco_enable_feature(FEATURE <feature_name> [TARGETS <target_name>])
  159. # ~~~
  160. #
  161. # FEATURE is required. It should be a Draco preprocessor symbol. TARGETS is
  162. # optional. It can be one or more draco targets.
  163. #
  164. # When the TARGETS argument is not present the preproc symbol is added to
  165. # draco_features.h. When it is draco_features.h is unchanged, and
  166. # target_compile_options() is called for each target specified.
  167. macro(draco_enable_feature)
  168. set(def_flags)
  169. set(def_single_arg_opts FEATURE)
  170. set(def_multi_arg_opts TARGETS)
  171. cmake_parse_arguments(DEF "${def_flags}" "${def_single_arg_opts}"
  172. "${def_multi_arg_opts}" ${ARGN})
  173. if("${DEF_FEATURE}" STREQUAL "")
  174. message(FATAL_ERROR "Empty FEATURE passed to draco_enable_feature().")
  175. endif()
  176. # Do nothing/return early if $DEF_FEATURE is already in the list.
  177. list(FIND draco_features_list ${DEF_FEATURE} df_index)
  178. if(NOT df_index EQUAL -1)
  179. return()
  180. endif()
  181. list(LENGTH DEF_TARGETS df_targets_list_length)
  182. if(${df_targets_list_length} EQUAL 0)
  183. list(APPEND draco_features_list ${DEF_FEATURE})
  184. else()
  185. foreach(target ${DEF_TARGETS})
  186. target_compile_definitions(${target} PRIVATE ${DEF_FEATURE})
  187. endforeach()
  188. endif()
  189. endmacro()
  190. # Function for generating draco_features.h.
  191. function(draco_generate_features_h)
  192. file(WRITE "${draco_features_file_name}.new"
  193. "// GENERATED FILE -- DO NOT EDIT\n\n" "#ifndef DRACO_FEATURES_H_\n"
  194. "#define DRACO_FEATURES_H_\n\n")
  195. foreach(feature ${draco_features_list})
  196. file(APPEND "${draco_features_file_name}.new" "#define ${feature}\n")
  197. endforeach()
  198. file(APPEND "${draco_features_file_name}.new"
  199. "\n#endif // DRACO_FEATURES_H_")
  200. # Will replace ${draco_features_file_name} only if the file content has
  201. # changed. This prevents forced Draco rebuilds after CMake runs.
  202. configure_file("${draco_features_file_name}.new"
  203. "${draco_features_file_name}")
  204. file(REMOVE "${draco_features_file_name}.new")
  205. endfunction()
  206. # Sets default options for the build and processes user controlled options to
  207. # compute enabled features.
  208. macro(draco_setup_options)
  209. draco_set_default_options()
  210. draco_set_optional_features()
  211. endmacro()