draco_helpers.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. if(DRACO_CMAKE_DRACO_HELPERS_CMAKE_)
  2. return()
  3. endif() # DRACO_CMAKE_DRACO_HELPERS_CMAKE_
  4. set(DRACO_CMAKE_DRACO_HELPERS_CMAKE_ 1)
  5. # Kills build generation using message(FATAL_ERROR) and outputs all data passed
  6. # to the console via use of $ARGN.
  7. macro(draco_die)
  8. message(FATAL_ERROR ${ARGN})
  9. endmacro()
  10. # Converts semi-colon delimited list variable(s) to string. Output is written to
  11. # variable supplied via the DEST parameter. Input is from an expanded variable
  12. # referenced by SOURCE and/or variable(s) referenced by SOURCE_VARS.
  13. macro(draco_set_and_stringify)
  14. set(optional_args)
  15. set(single_value_args DEST SOURCE_VAR)
  16. set(multi_value_args SOURCE SOURCE_VARS)
  17. cmake_parse_arguments(sas "${optional_args}" "${single_value_args}"
  18. "${multi_value_args}" ${ARGN})
  19. if(NOT sas_DEST OR NOT (sas_SOURCE OR sas_SOURCE_VARS))
  20. draco_die("draco_set_and_stringify: DEST and at least one of SOURCE "
  21. "SOURCE_VARS required.")
  22. endif()
  23. unset(${sas_DEST})
  24. if(sas_SOURCE)
  25. # $sas_SOURCE is one or more expanded variables, just copy the values to
  26. # $sas_DEST.
  27. set(${sas_DEST} "${sas_SOURCE}")
  28. endif()
  29. if(sas_SOURCE_VARS)
  30. # $sas_SOURCE_VARS is one or more variable names. Each iteration expands a
  31. # variable and appends it to $sas_DEST.
  32. foreach(source_var ${sas_SOURCE_VARS})
  33. set(${sas_DEST} "${${sas_DEST}} ${${source_var}}")
  34. endforeach()
  35. # Because $sas_DEST can be empty when entering this scope leading whitespace
  36. # can be introduced to $sas_DEST on the first iteration of the above loop.
  37. # Remove it:
  38. string(STRIP "${${sas_DEST}}" ${sas_DEST})
  39. endif()
  40. # Lists in CMake are simply semicolon delimited strings, so stringification is
  41. # just a find and replace of the semicolon.
  42. string(REPLACE ";" " " ${sas_DEST} "${${sas_DEST}}")
  43. if(DRACO_VERBOSE GREATER 1)
  44. message("draco_set_and_stringify: ${sas_DEST}=${${sas_DEST}}")
  45. endif()
  46. endmacro()
  47. # Creates a dummy source file in $DRACO_GENERATED_SOURCES_DIRECTORY and adds it
  48. # to the specified target. Optionally adds its path to a list variable.
  49. #
  50. # draco_create_dummy_source_file(<TARGET <target> BASENAME <basename of file>>
  51. # [LISTVAR <list variable>])
  52. macro(draco_create_dummy_source_file)
  53. set(optional_args)
  54. set(single_value_args TARGET BASENAME LISTVAR)
  55. set(multi_value_args)
  56. cmake_parse_arguments(cdsf "${optional_args}" "${single_value_args}"
  57. "${multi_value_args}" ${ARGN})
  58. if(NOT cdsf_TARGET OR NOT cdsf_BASENAME)
  59. draco_die("draco_create_dummy_source_file: TARGET and BASENAME required.")
  60. endif()
  61. if(NOT DRACO_GENERATED_SOURCES_DIRECTORY)
  62. set(DRACO_GENERATED_SOURCES_DIRECTORY "${draco_build}/gen_src")
  63. endif()
  64. set(dummy_source_dir "${DRACO_GENERATED_SOURCES_DIRECTORY}")
  65. set(dummy_source_file
  66. "${dummy_source_dir}/draco_${cdsf_TARGET}_${cdsf_BASENAME}.cc")
  67. set(dummy_source_code
  68. "// Generated file. DO NOT EDIT!\n"
  69. "// C++ source file created for target ${cdsf_TARGET}.\n"
  70. "void draco_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void)\;\n"
  71. "void draco_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void) {}\n")
  72. file(WRITE "${dummy_source_file}" ${dummy_source_code})
  73. target_sources(${cdsf_TARGET} PRIVATE ${dummy_source_file})
  74. if(cdsf_LISTVAR)
  75. list(APPEND ${cdsf_LISTVAR} "${dummy_source_file}")
  76. endif()
  77. endmacro()
  78. # Loads the version string from $draco_source/draco/version.h and sets
  79. # $DRACO_VERSION.
  80. macro(draco_load_version_info)
  81. file(STRINGS "${draco_src_root}/core/draco_version.h" version_file_strings)
  82. foreach(str ${version_file_strings})
  83. if(str MATCHES "char kDracoVersion")
  84. string(FIND "${str}" "\"" open_quote_pos)
  85. string(FIND "${str}" ";" semicolon_pos)
  86. math(EXPR open_quote_pos "${open_quote_pos} + 1")
  87. math(EXPR close_quote_pos "${semicolon_pos} - 1")
  88. math(EXPR version_string_length "${close_quote_pos} - ${open_quote_pos}")
  89. string(SUBSTRING "${str}" ${open_quote_pos} ${version_string_length}
  90. DRACO_VERSION)
  91. break()
  92. endif()
  93. endforeach()
  94. endmacro()