FindV8.cmake 3.8 KB

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