Misc.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. include(CheckCXXCompilerFlag)
  2. # Flatten our header structure
  3. function(flatify target headers)
  4. foreach(header ${headers})
  5. get_filename_component(tgt ${header} NAME)
  6. configure_file(${header} ${target}/${tgt} COPYONLY)
  7. endforeach(header ${headers})
  8. endfunction()
  9. function(add_optional_compile_options flags)
  10. foreach(flag ${flags})
  11. check_cxx_compiler_flag(-${flag} have-compiler-flag:-${flag})
  12. if (have-compiler-flag:-${flag})
  13. add_compile_options("-${flag}")
  14. endif()
  15. endforeach()
  16. endfunction()
  17. # Substitute vendor references in a file
  18. function(add_vendor_file)
  19. set(options)
  20. set(oneValueArgs OUTPUT INPUT MODE)
  21. set(multiValueArgs VARIABLES)
  22. cmake_parse_arguments(AVF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  23. set(in ${CMAKE_CURRENT_SOURCE_DIR}/${AVF_INPUT})
  24. set(out ${CMAKE_CURRENT_BINARY_DIR}/${AVF_OUTPUT})
  25. add_custom_command(
  26. OUTPUT ${out}
  27. COMMAND ${CMAKE_COMMAND} -DPROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
  28. "-DVARS=${AVF_VARIABLES}"
  29. -DCURRENT_VENDOR=${CURRENT_VENDOR}
  30. -DIN=${in}
  31. -DOUT=${out}
  32. -P ${PROJECT_SOURCE_DIR}/CMake/vendor_substitute.cmake
  33. COMMAND chmod ${AVF_MODE} ${out}
  34. DEPENDS ${in}
  35. ${PROJECT_SOURCE_DIR}/doc/apt-verbatim.ent
  36. ${PROJECT_SOURCE_DIR}/vendor/${CURRENT_VENDOR}/apt-vendor.ent
  37. ${PROJECT_SOURCE_DIR}/vendor/getinfo
  38. ${PROJECT_SOURCE_DIR}/CMake/vendor_substitute.cmake
  39. VERBATIM
  40. )
  41. # Woud like to use ${AVF_OUTPUT} as target name, but then ninja gets
  42. # cycles.
  43. add_custom_target(vendor-${AVF_OUTPUT} ALL DEPENDS ${out})
  44. endfunction()
  45. # Add symbolic links to a file
  46. function(add_slaves destination master)
  47. set(slaves "")
  48. foreach(slave ${ARGN})
  49. add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${slave}
  50. COMMAND ${CMAKE_COMMAND} -E create_symlink ${master} ${CMAKE_CURRENT_BINARY_DIR}/${slave})
  51. list(APPEND slaves ${CMAKE_CURRENT_BINARY_DIR}/${slave})
  52. endforeach()
  53. STRING(REPLACE "/" "-" master "${master}")
  54. add_custom_target(${master}-slaves ALL DEPENDS ${slaves})
  55. install(FILES ${slaves} DESTINATION ${destination})
  56. endfunction()
  57. # Generates a simple version script versioning everything with current SOVERSION
  58. function(add_version_script target)
  59. #get_target_property(soversion ${target} SOVERSION)
  60. #set(script "${CMAKE_CURRENT_BINARY_DIR}/${target}.versionscript")
  61. #string(REPLACE "-" "" name "${target}_${soversion}")
  62. #string(TOUPPER "${name}" name)
  63. #add_custom_command(OUTPUT "${script}"
  64. # COMMAND echo "${name} {global: *; };" > "${script}"
  65. # VERBATIM )
  66. #add_custom_target(${target}-versionscript DEPENDS "${script}")
  67. #target_link_libraries(${target} PRIVATE -Wl,-version-script="${script}")
  68. #add_dependencies(${target} ${target}-versionscript)
  69. endfunction()
  70. function(path_join out path1 path2)
  71. string(SUBSTRING ${path2} 0 1 init_char)
  72. if ("${init_char}" STREQUAL "/")
  73. set(${out} "${path2}" PARENT_SCOPE)
  74. else()
  75. set(${out} "${path1}/${path2}" PARENT_SCOPE)
  76. endif()
  77. endfunction()
  78. # install_empty_directories(path ...)
  79. #
  80. # Creates empty directories in the install destination dir. Paths may be
  81. # absolute or relative; in the latter case, the value of CMAKE_INSTALL_PREFIX
  82. # is prepended.
  83. function(install_empty_directories)
  84. foreach(path ${ARGN})
  85. path_join(full_path "${CMAKE_INSTALL_PREFIX}" "${path}")
  86. INSTALL(CODE "MESSAGE(STATUS \"Creating directory: \$ENV{DESTDIR}${full_path}\")"
  87. CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${full_path})")
  88. endforeach()
  89. endfunction()