1find_program (NODE_EXECUTABLE NAMES node nodejs
2    HINTS
3    $ENV{NODE_DIR}
4    PATH_SUFFIXES bin
5    DOC "Node.js interpreter"
6)
7
8include (FindPackageHandleStandardArgs)
9
10find_path (NODE_ROOT_DIR "node/node.h" "src/node.h"
11  PATHS /usr/include/nodejs /usr/local/include/nodejs /usr/local/include)
12
13set (NODE_INCLUDE_DIRS
14  ${NODE_ROOT_DIR}/src
15  ${NODE_ROOT_DIR}/node
16  ${NODE_ROOT_DIR}/deps/v8/include
17  ${NODE_ROOT_DIR}/deps/uv/include
18)
19
20find_package_handle_standard_args (Node DEFAULT_MSG
21    NODE_EXECUTABLE
22    NODE_INCLUDE_DIRS
23)
24
25if (NODE_EXECUTABLE)
26    execute_process(COMMAND ${NODE_EXECUTABLE} --version
27                    OUTPUT_VARIABLE _VERSION
28                    RESULT_VARIABLE _NODE_VERSION_RESULT)
29    execute_process(COMMAND ${NODE_EXECUTABLE} -e "console.log(process.versions.v8)"
30                    OUTPUT_VARIABLE _V8_VERSION
31                    RESULT_VARIABLE _V8_RESULT)
32    if (NOT _NODE_VERSION_RESULT AND NOT _V8_RESULT)
33        string (REPLACE "v" "" NODE_VERSION_STRING "${_VERSION}")
34        string (REPLACE "." ";" _VERSION_LIST "${NODE_VERSION_STRING}")
35        list (GET _VERSION_LIST 0 NODE_VERSION_MAJOR)
36        list (GET _VERSION_LIST 1 NODE_VERSION_MINOR)
37        list (GET _VERSION_LIST 2 NODE_VERSION_PATCH)
38        set (V8_VERSION_STRING ${_V8_VERSION})
39        string (REPLACE "." ";" _V8_VERSION_LIST "${_V8_VERSION}")
40        list (GET _V8_VERSION_LIST 0 V8_VERSION_MAJOR)
41        list (GET _V8_VERSION_LIST 1 V8_VERSION_MINOR)
42        list (GET _V8_VERSION_LIST 2 V8_VERSION_PATCH)
43        # we end up with a nasty newline so strip everything that isn't a number
44        string (REGEX MATCH "^[0-9]*" V8_VERSION_PATCH ${V8_VERSION_PATCH})
45    else ()
46        set (NODE_VERSION_STRING "0.10.30")
47        set (NODE_VERSION_MAJOR "0")
48        set (NODE_VERSION_MINOR "10")
49        set (NODE_VERSION_PATCH "30")
50        set (V8_VERSION_MAJOR "3")
51        set (V8_VERSION_MINOR"14")
52        set (V8_VERSION_PATCH "5")
53        set (V8_VERSION_STRING "3.28.72")
54        message ("defaulted to node 0.10.30")
55    endif ()
56    message ("INFO - Node version is " ${NODE_VERSION_STRING} "INFO - Node using v8 " ${V8_VERSION_STRING})
57endif ()
58
59mark_as_advanced (NODE_EXECUTABLE)
60