util.cmake 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. if(DRACO_CMAKE_UTIL_CMAKE_)
  2. return()
  3. endif()
  4. set(DRACO_CMAKE_UTIL_CMAKE_ 1)
  5. # Creates dummy source file in $draco_build_dir named $basename.$extension and
  6. # returns the full path to the dummy source file via the $out_file_path
  7. # parameter.
  8. function(create_dummy_source_file basename extension out_file_path)
  9. set(dummy_source_file "${draco_build_dir}/${basename}.${extension}")
  10. file(WRITE "${dummy_source_file}.new"
  11. "// Generated file. DO NOT EDIT!\n"
  12. "// ${target_name} needs a ${extension} file to force link language, \n"
  13. "// or to silence a harmless CMake warning: Ignore me.\n"
  14. "void ${target_name}_dummy_function(void) {}\n")
  15. # Will replace ${dummy_source_file} only if the file content has changed.
  16. # This prevents forced Draco rebuilds after CMake runs.
  17. configure_file("${dummy_source_file}.new" "${dummy_source_file}")
  18. file(REMOVE "${dummy_source_file}.new")
  19. set(${out_file_path} ${dummy_source_file} PARENT_SCOPE)
  20. endfunction()
  21. # Convenience function for adding a dummy source file to $target_name using
  22. # $extension as the file extension. Wraps create_dummy_source_file().
  23. function(add_dummy_source_file_to_target target_name extension)
  24. create_dummy_source_file("${target_name}" "${extension}" "dummy_source_file")
  25. target_sources(${target_name} PRIVATE ${dummy_source_file})
  26. endfunction()
  27. # Extracts the version number from $version_file and returns it to the user via
  28. # $version_string_out_var. This is achieved by finding the first instance of the
  29. # kDracoVersion variable and then removing everything but the string literal
  30. # assigned to the variable. Quotes and semicolon are stripped from the returned
  31. # string.
  32. function(extract_version_string version_file version_string_out_var)
  33. file(STRINGS "${version_file}" draco_version REGEX "kDracoVersion")
  34. list(GET draco_version 0 draco_version)
  35. string(REPLACE "static const char kDracoVersion[] = " "" draco_version
  36. "${draco_version}")
  37. string(REPLACE ";" "" draco_version "${draco_version}")
  38. string(REPLACE "\"" "" draco_version "${draco_version}")
  39. set("${version_string_out_var}" "${draco_version}" PARENT_SCOPE)
  40. endfunction()
  41. # Sets CMake compiler launcher to $launcher_name when $launcher_name is found in
  42. # $PATH. Warns user about ignoring build flag $launcher_flag when $launcher_name
  43. # is not found in $PATH.
  44. function(set_compiler_launcher launcher_flag launcher_name)
  45. find_program(launcher_path "${launcher_name}")
  46. if(launcher_path)
  47. set(CMAKE_C_COMPILER_LAUNCHER "${launcher_path}" PARENT_SCOPE)
  48. set(CMAKE_CXX_COMPILER_LAUNCHER "${launcher_path}" PARENT_SCOPE)
  49. message("--- Using ${launcher_name} as compiler launcher.")
  50. else()
  51. message(
  52. WARNING "--- Cannot find ${launcher_name}, ${launcher_flag} ignored.")
  53. endif()
  54. endfunction()
  55. # Terminates CMake execution when $var_name is unset in the environment. Sets
  56. # CMake variable to the value of the environment variable when the variable is
  57. # present in the environment.
  58. macro(require_variable var_name)
  59. if("$ENV{${var_name}}" STREQUAL "")
  60. message(FATAL_ERROR "${var_name} must be set in environment.")
  61. endif()
  62. set_variable_if_unset(${var_name} "")
  63. endmacro()
  64. # Sets $var_name to $default_value if not already set.
  65. macro(set_variable_if_unset var_name default_value)
  66. if(NOT "$ENV{${var_name}}" STREQUAL "")
  67. set(${var_name} $ENV{${var_name}})
  68. elseif(NOT ${var_name})
  69. set(${var_name} ${default_value})
  70. endif()
  71. endmacro()