FindV8.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Finds V8 the way distributions ship it: inside Node.js's shared library (Fedora nodejs<N>-devel, Debian/Ubuntu
  2. # libnode-dev), headers under include/node. V8_INCLUDE_DIR / V8_LIBRARY can point anywhere else.
  3. #
  4. # This module defines:
  5. # V8_FOUND
  6. # V8_INCLUDE_DIR
  7. # V8_LIBRARY
  8. # V8_VERSION
  9. # V8_DEFINITIONS - defines that change V8's object layout, plus V8_ICU_DEFAULT_DATA_DIR when node has one
  10. # V8::V8 - imported target carrying all of the above
  11. find_package(PkgConfig QUIET)
  12. if(PKG_CONFIG_FOUND)
  13. pkg_search_module(PC_NODE QUIET nodejs-26 nodejs-25 nodejs-24 nodejs-23 nodejs-22 nodejs-21 nodejs-20 libnode)
  14. endif()
  15. find_library(
  16. V8_LIBRARY
  17. NAMES node v8
  18. HINTS ${PC_NODE_LIBRARY_DIRS}
  19. )
  20. # the headers have to be the library's own: a Node.js tarball in /usr/local brings include/node but no libnode, and
  21. # its V8 version differs from the distribution's
  22. set(_V8_PREFIX_HINTS)
  23. if(V8_LIBRARY)
  24. get_filename_component(_V8_LIBRARY_DIR "${V8_LIBRARY}" DIRECTORY)
  25. list(APPEND _V8_PREFIX_HINTS "${_V8_LIBRARY_DIR}/../include" "${_V8_LIBRARY_DIR}/../../include")
  26. endif()
  27. find_path(
  28. V8_INCLUDE_DIR
  29. NAMES v8.h
  30. HINTS ${PC_NODE_INCLUDE_DIRS} ${_V8_PREFIX_HINTS}
  31. PATH_SUFFIXES node nodejs/deps/v8/include v8
  32. )
  33. set(V8_DEFINITIONS)
  34. if(V8_INCLUDE_DIR AND EXISTS "${V8_INCLUDE_DIR}/v8-version.h")
  35. file(STRINGS "${V8_INCLUDE_DIR}/v8-version.h" _V8_VERSION_LINES REGEX "#define V8_(MAJOR_VERSION|MINOR_VERSION|BUILD_NUMBER)")
  36. foreach(_part MAJOR_VERSION MINOR_VERSION BUILD_NUMBER)
  37. string(REGEX MATCH "V8_${_part} +([0-9]+)" _match "${_V8_VERSION_LINES}")
  38. set(_V8_${_part} "${CMAKE_MATCH_1}")
  39. endforeach()
  40. set(V8_VERSION "${_V8_MAJOR_VERSION}.${_V8_MINOR_VERSION}.${_V8_BUILD_NUMBER}")
  41. # node writes its build configuration next to the headers (config.gypi, Fedora's multilib config-<arch>.gypi);
  42. # these switches are what node's own common.gypi turns into defines for addons
  43. file(GLOB _V8_GYPI_FILES "${V8_INCLUDE_DIR}/config*.gypi")
  44. set(_V8_CONFIG "")
  45. foreach(_file ${_V8_GYPI_FILES})
  46. file(READ "${_file}" _content)
  47. string(APPEND _V8_CONFIG "${_content}")
  48. endforeach()
  49. macro(_v8_flag variable)
  50. string(REGEX MATCH "['\"]${variable}['\"]: *['\"]?(1|true)" _enabled "${_V8_CONFIG}")
  51. if(_enabled)
  52. list(APPEND V8_DEFINITIONS ${ARGN})
  53. endif()
  54. endmacro()
  55. _v8_flag(v8_enable_pointer_compression V8_COMPRESS_POINTERS)
  56. _v8_flag(v8_enable_pointer_compression_shared_cage V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
  57. _v8_flag(v8_enable_31bit_smis_on_64bit_arch V8_31BIT_SMIS_ON_64BIT_ARCH)
  58. _v8_flag(v8_enable_sandbox V8_ENABLE_SANDBOX)
  59. # Fedora's node embeds a small ICU and loads the full data (nodejs<N>-full-i18n) from here
  60. string(REGEX MATCH "['\"]icu_default_data['\"]: *['\"]([^'\"]+)['\"]" _icu_default_data "${_V8_CONFIG}")
  61. if(CMAKE_MATCH_1)
  62. list(APPEND V8_DEFINITIONS "V8_ICU_DEFAULT_DATA_DIR=\"${CMAKE_MATCH_1}\"")
  63. endif()
  64. endif()
  65. if(V8_INCLUDE_DIR AND NOT EXISTS "${V8_INCLUDE_DIR}/libplatform/libplatform.h")
  66. message(STATUS "V8 headers in ${V8_INCLUDE_DIR} have no libplatform/libplatform.h")
  67. unset(V8_INCLUDE_DIR CACHE)
  68. endif()
  69. include(FindPackageHandleStandardArgs)
  70. find_package_handle_standard_args(
  71. V8
  72. REQUIRED_VARS V8_LIBRARY V8_INCLUDE_DIR
  73. VERSION_VAR V8_VERSION
  74. REASON_FAILURE_MESSAGE
  75. "V8 comes from Node.js's development package: nodejs24-devel or nodejs22-devel (Fedora), libnode-dev (Debian/Ubuntu), \
  76. the AUR's libnode (Arch). Elsewhere set V8_INCLUDE_DIR (the directory with v8.h) and V8_LIBRARY (libnode.so)."
  77. )
  78. if(V8_FOUND AND NOT TARGET V8::V8)
  79. add_library(V8::V8 UNKNOWN IMPORTED)
  80. set_target_properties(V8::V8 PROPERTIES
  81. IMPORTED_LOCATION "${V8_LIBRARY}"
  82. INTERFACE_INCLUDE_DIRECTORIES "${V8_INCLUDE_DIR}"
  83. INTERFACE_COMPILE_DEFINITIONS "${V8_DEFINITIONS}")
  84. endif()
  85. mark_as_advanced(V8_INCLUDE_DIR V8_LIBRARY)