FindV8.cmake 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 the library was built with that change V8's object layout, embedders must use the same
  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. endif()
  60. if(V8_INCLUDE_DIR AND NOT EXISTS "${V8_INCLUDE_DIR}/libplatform/libplatform.h")
  61. message(STATUS "V8 headers in ${V8_INCLUDE_DIR} have no libplatform/libplatform.h")
  62. unset(V8_INCLUDE_DIR CACHE)
  63. endif()
  64. include(FindPackageHandleStandardArgs)
  65. find_package_handle_standard_args(
  66. V8
  67. REQUIRED_VARS V8_LIBRARY V8_INCLUDE_DIR
  68. VERSION_VAR V8_VERSION
  69. REASON_FAILURE_MESSAGE
  70. "V8 comes from Node.js's development package: nodejs24-devel or nodejs22-devel (Fedora), libnode-dev (Debian/Ubuntu), \
  71. the AUR's libnode (Arch). Elsewhere set V8_INCLUDE_DIR (the directory with v8.h) and V8_LIBRARY (libnode.so)."
  72. )
  73. if(V8_FOUND AND NOT TARGET V8::V8)
  74. add_library(V8::V8 UNKNOWN IMPORTED)
  75. set_target_properties(V8::V8 PROPERTIES
  76. IMPORTED_LOCATION "${V8_LIBRARY}"
  77. INTERFACE_INCLUDE_DIRECTORIES "${V8_INCLUDE_DIR}"
  78. INTERFACE_COMPILE_DEFINITIONS "${V8_DEFINITIONS}")
  79. endif()
  80. mark_as_advanced(V8_INCLUDE_DIR V8_LIBRARY)