Mislav Novakovic | a827b5f | 2016-02-29 11:47:22 +0100 | [diff] [blame^] | 1 | # Macro to add directory to NODEJS_INCLUDE_DIRS if it exists and is not /usr/include |
| 2 | macro(add_include_dir dir) |
| 3 | if (IS_DIRECTORY ${dir} AND NOT ${dir} STREQUAL "/usr/include") |
| 4 | set(NODEJS_INCLUDE_DIRS ${NODEJS_INCLUDE_DIRS} ${dir}) |
| 5 | endif() |
| 6 | endmacro() |
| 7 | |
| 8 | |
| 9 | find_program (NODEJS_EXECUTABLE NAMES node nodejs |
| 10 | HINTS |
| 11 | $ENV{NODE_DIR} |
| 12 | PATH_SUFFIXES bin |
| 13 | DOC "Node.js interpreter" |
| 14 | ) |
| 15 | |
| 16 | include (FindPackageHandleStandardArgs) |
| 17 | |
| 18 | # If compat-libuv package exists, it must be at start of include path |
| 19 | find_path (UV_ROOT_DIR "uv.h" PATHS /usr/include/compat-libuv010 NO_DEFAULT_PATH) |
| 20 | if (UV_ROOT_DIR) |
| 21 | # set (NODEJS_INCLUDE_DIRS ${UV_ROOT_DIR}) |
| 22 | add_include_dir(${UV_ROOT_DIR}) |
| 23 | endif() |
| 24 | |
| 25 | # Now look for node. Flag an error if not found |
| 26 | find_path (NODE_ROOT_DIR "node/node.h" "src/node.h" |
| 27 | PATHS /usr/include/nodejs /usr/local/include/nodejs /usr/local/include) |
| 28 | if (NODE_ROOT_DIR) |
| 29 | add_include_dir(${NODE_ROOT_DIR}/src) |
| 30 | add_include_dir(${NODE_ROOT_DIR}/node) |
| 31 | add_include_dir(${NODE_ROOT_DIR}/deps/v8/include) |
| 32 | add_include_dir(${NODE_ROOT_DIR}/deps/uv/include) |
| 33 | else() |
| 34 | unset(NODEJS_INCLUDE_DIRS) |
| 35 | message(ERROR " - node.h not found") |
| 36 | endif() |
| 37 | |
| 38 | # Check that v8.h is in NODEJS_INCLUDE_DIRS |
| 39 | find_path (V8_ROOT_DIR "v8.h" PATHS ${NODEJS_INCLUDE_DIRS}) |
| 40 | if (NOT V8_ROOT_DIR) |
| 41 | unset(NODEJS_INCLUDE_DIRS) |
| 42 | message(ERROR " - v8.h not found") |
| 43 | endif() |
| 44 | |
| 45 | # Check that uv.h is in NODEJS_INCLUDE_DIRS |
| 46 | find_path (UV_ROOT_DIR "uv.h" PATHS ${NODEJS_INCLUDE_DIRS}) |
| 47 | if (NOT UV_ROOT_DIR) |
| 48 | unset(NODEJS_INCLUDE_DIRS) |
| 49 | message(ERROR " - uv.h not found") |
| 50 | endif() |
| 51 | |
| 52 | find_package_handle_standard_args (Nodejs DEFAULT_MSG |
| 53 | NODEJS_EXECUTABLE |
| 54 | NODEJS_INCLUDE_DIRS |
| 55 | ) |
| 56 | |
| 57 | if (NODEJS_EXECUTABLE) |
| 58 | execute_process(COMMAND ${NODEJS_EXECUTABLE} --version |
| 59 | OUTPUT_VARIABLE _VERSION |
| 60 | RESULT_VARIABLE _NODE_VERSION_RESULT) |
| 61 | execute_process(COMMAND ${NODEJS_EXECUTABLE} -e "console.log(process.versions.v8)" |
| 62 | OUTPUT_VARIABLE _V8_VERSION |
| 63 | RESULT_VARIABLE _V8_RESULT) |
| 64 | if (NOT _NODE_VERSION_RESULT AND NOT _V8_RESULT) |
| 65 | string (REPLACE "v" "" NODE_VERSION_STRING "${_VERSION}") |
| 66 | string (REPLACE "." ";" _VERSION_LIST "${NODE_VERSION_STRING}") |
| 67 | list (GET _VERSION_LIST 0 NODE_VERSION_MAJOR) |
| 68 | list (GET _VERSION_LIST 1 NODE_VERSION_MINOR) |
| 69 | list (GET _VERSION_LIST 2 NODE_VERSION_PATCH) |
| 70 | set (V8_VERSION_STRING ${_V8_VERSION}) |
| 71 | string (REPLACE "." ";" _V8_VERSION_LIST "${_V8_VERSION}") |
| 72 | list (GET _V8_VERSION_LIST 0 V8_VERSION_MAJOR) |
| 73 | list (GET _V8_VERSION_LIST 1 V8_VERSION_MINOR) |
| 74 | list (GET _V8_VERSION_LIST 2 V8_VERSION_PATCH) |
| 75 | # we end up with a nasty newline so strip everything that isn't a number |
| 76 | string (REGEX MATCH "^[0-9]*" V8_VERSION_PATCH ${V8_VERSION_PATCH}) |
| 77 | else () |
| 78 | set (NODE_VERSION_STRING "0.10.30") |
| 79 | set (NODE_VERSION_MAJOR "0") |
| 80 | set (NODE_VERSION_MINOR "10") |
| 81 | set (NODE_VERSION_PATCH "30") |
| 82 | set (V8_VERSION_MAJOR "3") |
| 83 | set (V8_VERSION_MINOR"14") |
| 84 | set (V8_VERSION_PATCH "5") |
| 85 | set (V8_VERSION_STRING "3.28.72") |
| 86 | message ("defaulted to node 0.10.30") |
| 87 | endif () |
| 88 | string (REGEX REPLACE "\n" "" NODE_VERSION_STRING ${NODE_VERSION_STRING}) |
| 89 | string (REGEX REPLACE "\n" "" V8_VERSION_STRING ${V8_VERSION_STRING}) |
| 90 | message ("INFO - Node version is " ${NODE_VERSION_STRING}) |
| 91 | message ("INFO - Node using v8 " ${V8_VERSION_STRING}) |
| 92 | mark_as_advanced (NODEJS_EXECUTABLE) |
| 93 | endif () |
| 94 | |