AddCMakeProject.cmake 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. include(ExternalProject)
  2. include(ProcessorCount)
  3. set(${PROJECT_NAME}_DEP_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/destdir/usr/local" CACHE PATH "Destination directory")
  4. set(${PROJECT_NAME}_DEP_DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/downloads CACHE PATH "Path for downloaded source packages.")
  5. option(${PROJECT_NAME}_DEP_BUILD_VERBOSE "Use verbose output for each dependency build" OFF)
  6. get_property(_is_multi GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  7. if (NOT CMAKE_BUILD_TYPE)
  8. set(CMAKE_BUILD_TYPE Release)
  9. message(STATUS "Forcing CMAKE_BUILD_TYPE to Release as it was not specified.")
  10. endif ()
  11. # This is a wrapper function around ExternalProject_Add to simplify adding
  12. # CMake based external projects. It will forward common build configuration from the parent
  13. # project and set up multithreaded build for various generators.
  14. # The function signiture is identical to that of ExternalProject_Add, except that the
  15. # BUILD_COMMAND, INSTALL_COMMAND and INSTALL_DIR arguments are ignored.
  16. # The value of CMAKE_BUILD_TYPE will be used for building each dependency even if the
  17. # generator is multi-config. Use this var to specify build type regardless of the generator.
  18. function(add_cmake_project projectname)
  19. cmake_parse_arguments(P_ARGS "" "INSTALL_DIR;BUILD_COMMAND;INSTALL_COMMAND" "CMAKE_ARGS" ${ARGN})
  20. set(_pcount ${DEP_${projectname}_MAX_THREADS})
  21. if (NOT _pcount)
  22. set(_pcount ${DEP_MAX_THREADS})
  23. endif ()
  24. if (NOT _pcount)
  25. ProcessorCount(_pcount)
  26. endif ()
  27. if (_pcount EQUAL 0)
  28. set(_pcount 1)
  29. endif ()
  30. set(_build_j "-j${_pcount}")
  31. if (CMAKE_GENERATOR MATCHES "Visual Studio")
  32. set(_build_j "-m:${_pcount}")
  33. endif ()
  34. string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type_upper)
  35. set(_configs_line -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE})
  36. if (_is_multi)
  37. set(_configs_line "")
  38. endif ()
  39. set(_verbose_switch "")
  40. if (${PROJECT_NAME}_DEP_BUILD_VERBOSE)
  41. if (CMAKE_GENERATOR MATCHES "Ninja")
  42. set(_verbose_switch "--verbose")
  43. elseif (CMAKE_GENERATOR MATCHES "Visual Studio")
  44. set(_verbose_switch "-v:d")
  45. endif ()
  46. endif ()
  47. ExternalProject_Add(
  48. dep_${projectname}
  49. INSTALL_DIR ${${PROJECT_NAME}_DEP_INSTALL_PREFIX}
  50. DOWNLOAD_DIR ${${PROJECT_NAME}_DEP_DOWNLOAD_DIR}/${projectname}
  51. BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/builds/${projectname}
  52. CMAKE_ARGS
  53. -DCMAKE_INSTALL_PREFIX:STRING=${${PROJECT_NAME}_DEP_INSTALL_PREFIX}
  54. -DCMAKE_MODULE_PATH:STRING=${CMAKE_MODULE_PATH}
  55. -DCMAKE_PREFIX_PATH:STRING=${${PROJECT_NAME}_DEP_INSTALL_PREFIX}
  56. -DCMAKE_DEBUG_POSTFIX:STRING=${CMAKE_DEBUG_POSTFIX}
  57. -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
  58. -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
  59. -DCMAKE_CXX_FLAGS_${_build_type_upper}:STRING=${CMAKE_CXX_FLAGS_${_build_type_upper}}
  60. -DCMAKE_C_FLAGS_${_build_type_upper}:STRING=${CMAKE_C_FLAGS_${_build_type_upper}}
  61. -DCMAKE_TOOLCHAIN_FILE:STRING=${CMAKE_TOOLCHAIN_FILE}
  62. -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
  63. "${_configs_line}"
  64. ${DEP_CMAKE_OPTS}
  65. ${P_ARGS_CMAKE_ARGS}
  66. ${P_ARGS_UNPARSED_ARGUMENTS}
  67. BUILD_COMMAND ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE} -- ${_build_j} ${_verbose_switch}
  68. INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}
  69. )
  70. endfunction(add_cmake_project)