draco_helpers.cmake 4.6 KB

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