AddCMakeProject.cmake 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. # The value of CMAKE_BUILD_TYPE will be used for building each dependency even if the
  12. # generator is multi-config. Use this var to specify build type regardless of the generator.
  13. function(add_cmake_project projectname)
  14. cmake_parse_arguments(P_ARGS "" "INSTALL_DIR;BUILD_COMMAND;INSTALL_COMMAND" "CMAKE_ARGS" ${ARGN})
  15. set(_pcount ${DEP_${projectname}_MAX_THREADS})
  16. if (NOT _pcount)
  17. set(_pcount ${DEP_MAX_THREADS})
  18. endif ()
  19. if (NOT _pcount)
  20. ProcessorCount(_pcount)
  21. endif ()
  22. if (_pcount EQUAL 0)
  23. set(_pcount 1)
  24. endif ()
  25. set(_build_j "-j${_pcount}")
  26. if (CMAKE_GENERATOR MATCHES "Visual Studio")
  27. set(_build_j "-m:${_pcount}")
  28. endif ()
  29. string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type_upper)
  30. set(_configs_line -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE})
  31. if (_is_multi)
  32. set(_configs_line "")
  33. endif ()
  34. set(_verbose_switch "")
  35. if (${PROJECT_NAME}_DEP_BUILD_VERBOSE)
  36. if (CMAKE_GENERATOR MATCHES "Ninja")
  37. set(_verbose_switch "--verbose")
  38. elseif (CMAKE_GENERATOR MATCHES "Visual Studio")
  39. set(_verbose_switch "-v:d")
  40. endif ()
  41. endif ()
  42. ExternalProject_Add(
  43. dep_${projectname}
  44. EXCLUDE_FROM_ALL ON # Not built by default, dep_${projectname} needs to be added to ALL target
  45. INSTALL_DIR ${${PROJECT_NAME}_DEP_INSTALL_PREFIX}
  46. DOWNLOAD_DIR ${${PROJECT_NAME}_DEP_DOWNLOAD_DIR}/${projectname}
  47. BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/builds/${projectname}
  48. CMAKE_ARGS
  49. -DCMAKE_INSTALL_PREFIX:STRING=${${PROJECT_NAME}_DEP_INSTALL_PREFIX}
  50. -DCMAKE_MODULE_PATH:STRING=${CMAKE_MODULE_PATH}
  51. -DCMAKE_PREFIX_PATH:STRING=${${PROJECT_NAME}_DEP_INSTALL_PREFIX}
  52. -DCMAKE_DEBUG_POSTFIX:STRING=${CMAKE_DEBUG_POSTFIX}
  53. -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
  54. -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
  55. -DCMAKE_CXX_FLAGS_${_build_type_upper}:STRING=${CMAKE_CXX_FLAGS_${_build_type_upper}}
  56. -DCMAKE_C_FLAGS_${_build_type_upper}:STRING=${CMAKE_C_FLAGS_${_build_type_upper}}
  57. -DCMAKE_TOOLCHAIN_FILE:STRING=${CMAKE_TOOLCHAIN_FILE}
  58. -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
  59. "${_configs_line}"
  60. ${DEP_CMAKE_OPTS}
  61. ${P_ARGS_CMAKE_ARGS}
  62. ${P_ARGS_UNPARSED_ARGUMENTS}
  63. BUILD_COMMAND ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE} -- ${_build_j} ${_verbose_switch}
  64. INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}
  65. )
  66. endfunction(add_cmake_project)