util.cmake 3.2 KB

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