InstallPackage.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #[[
  2. Warning: This module is private, may be modified or removed in the future, please use with caution.
  3. ]] #
  4. if(NOT DEFINED QMSETUP_PACKAGE_BUILD_TYPE)
  5. set(QMSETUP_PACKAGE_BUILD_TYPE "Release")
  6. endif()
  7. include_guard(DIRECTORY)
  8. #[[
  9. Install external package at configuration phase.
  10. qm_install_package(<name>
  11. [SOURCE_DIR <dir>]
  12. [BUILD_TREE_DIR <dir>]
  13. [INSTALL_DIR <dir>]
  14. [CMAKE_PACKAGE_SUBDIR <subdir>]
  15. [BUILD_TYPE <type>]
  16. [CONFIGURE_ARGS <arg...>]
  17. [RESULT_PATH <VAR>]
  18. )
  19. ]] #
  20. function(qm_install_package _name)
  21. set(options)
  22. set(oneValueArgs SOURCE_DIR BUILD_TREE_DIR INSTALL_DIR CMAKE_PACKAGE_SUBDIR BUILD_TYPE RESULT_PATH)
  23. set(multiValueArgs CONFIGURE_ARGS)
  24. cmake_parse_arguments(FUNC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  25. # Directories
  26. if(NOT FUNC_SOURCE_DIR)
  27. message(FATAL_ERROR "pre_install_package: SOURCE_DIR is not spefified")
  28. endif()
  29. set(_src_dir ${FUNC_SOURCE_DIR})
  30. if(FUNC_BUILD_TREE_DIR)
  31. set(_build_tree_dir ${FUNC_BUILD_TREE_DIR})
  32. else()
  33. set(_build_tree_dir ${CMAKE_BINARY_DIR}/_build)
  34. endif()
  35. if(FUNC_INSTALL_DIR)
  36. set(_install_dir ${FUNC_INSTALL_DIR})
  37. else()
  38. set(_install_dir ${CMAKE_BINARY_DIR}/_install)
  39. endif()
  40. if(FUNC_CMAKE_PACKAGE_SUBDIR)
  41. set(_cmake_subdir ${FUNC_CMAKE_PACKAGE_SUBDIR})
  42. else()
  43. include(GNUInstallDirs)
  44. set(_cmake_subdir "${CMAKE_INSTALL_LIBDIR}/cmake/${_name}")
  45. endif()
  46. # Build types
  47. if(FUNC_BUILD_TYPE)
  48. set(_build_type -DCMAKE_BUILD_TYPE=${FUNC_BUILD_TYPE})
  49. elseif(CMAKE_BUILD_TYPE)
  50. set(_build_type -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
  51. elseif(NOT CMAKE_CONFIGURATION_TYPES)
  52. set(_build_type -DCMAKE_BUILD_TYPE=${QMSETUP_PACKAGE_BUILD_TYPE})
  53. else()
  54. set(_build_type)
  55. endif()
  56. if(FUNC_BUILD_TYPE)
  57. set(_build_types ${FUNC_BUILD_TYPE})
  58. else()
  59. set(_build_types ${QMSETUP_PACKAGE_BUILD_TYPE})
  60. endif()
  61. # Do it
  62. set(_install_cmake_dir ${_install_dir}/${_cmake_subdir})
  63. set(_build_dir ${_build_tree_dir}/${_name})
  64. if(NOT IS_DIRECTORY ${_install_cmake_dir})
  65. # Determine generator
  66. set(_extra_args)
  67. if(CMAKE_GENERATOR)
  68. set(_extra_args -G "${CMAKE_GENERATOR}")
  69. endif()
  70. if(CMAKE_GENERATOR_PLATFORM)
  71. set(_extra_args -A "${CMAKE_GENERATOR_PLATFORM}")
  72. endif()
  73. # Remove old build directory
  74. if(IS_DIRECTORY ${_build_dir})
  75. file(REMOVE_RECURSE ${_build_dir})
  76. endif()
  77. file(MAKE_DIRECTORY ${_build_tree_dir})
  78. # Configure
  79. message(STATUS "Configuring ${_name}...")
  80. set(_log_file ${_build_tree_dir}/${_name}_configure.log)
  81. execute_process(
  82. COMMAND ${CMAKE_COMMAND} -S ${_src_dir} -B ${_build_dir}
  83. ${_extra_args} ${_build_type}
  84. "-DCMAKE_INSTALL_PREFIX=${_install_dir}" ${FUNC_CONFIGURE_ARGS}
  85. OUTPUT_FILE ${_log_file}
  86. ERROR_FILE ${_log_file}
  87. RESULT_VARIABLE _code
  88. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  89. )
  90. if(NOT ${_code} EQUAL 0)
  91. message(FATAL_ERROR "Configure failed, check \"${_log_file}\"")
  92. endif()
  93. # Build
  94. foreach(_item IN LISTS _build_types)
  95. message(STATUS "Building ${_name} (${_item})...")
  96. set(_log_file ${_build_tree_dir}/${_name}_build-${_item}.log)
  97. execute_process(
  98. COMMAND ${CMAKE_COMMAND} --build ${_build_dir} --config ${_item} --parallel
  99. OUTPUT_FILE ${_log_file}
  100. ERROR_FILE ${_log_file}
  101. RESULT_VARIABLE _code
  102. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  103. )
  104. if(NOT ${_code} EQUAL 0)
  105. message(FATAL_ERROR "Build failed, check \"${_log_file}\"")
  106. endif()
  107. endforeach()
  108. # Install
  109. foreach(_item IN LISTS _build_types)
  110. message(STATUS "Installing ${_name} (${_item})...")
  111. set(_log_file ${_build_tree_dir}/${_name}_install-${_item}.log)
  112. execute_process(
  113. COMMAND ${CMAKE_COMMAND} --build ${_build_dir} --config ${_item} --target install
  114. OUTPUT_FILE ${_log_file}
  115. ERROR_FILE ${_log_file}
  116. RESULT_VARIABLE _code
  117. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  118. )
  119. if(NOT ${_code} EQUAL 0)
  120. message(FATAL_ERROR "Install failed, check \"${_log_file}\"")
  121. endif()
  122. endforeach()
  123. endif()
  124. if(FUNC_RESULT_PATH)
  125. set(${FUNC_RESULT_PATH} ${_install_cmake_dir} PARENT_SCOPE)
  126. endif()
  127. endfunction()