From 6f72c52a44068ba475412f7ca3107aa0e7007138 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Tue, 6 Feb 2018 14:11:28 -0800 Subject: [PATCH] CMake: Update FindNpm module and usage FindNpm REQUIRE functionality was not provided by FindNpm.cmake. UpdatedUpdated FindNpm to extract version, global node_modules directory, and to fail if REQUIRE was set and npm was not found. Signed-off-by: Noel Eck --- CMakeLists.txt | 5 --- cmake/modules/FindNpm.cmake | 78 ++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c1b83776..73922976 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,11 +151,6 @@ if (BUILDSWIGNODE) find_package (Node REQUIRED) if (BUILDTESTS) find_package (Npm REQUIRED) - if(NPM_EXECUTABLE) - message(STATUS "NPM Executable found at: ${NPM_EXECUTABLE}") - else() - message(FATAL_ERROR "Please install NPM first, you can't run tests without it") - endif() endif (BUILDTESTS) endif (BUILDSWIGNODE) diff --git a/cmake/modules/FindNpm.cmake b/cmake/modules/FindNpm.cmake index 5937b7b8..18cf904a 100644 --- a/cmake/modules/FindNpm.cmake +++ b/cmake/modules/FindNpm.cmake @@ -1,13 +1,71 @@ -# Finding and pointing a variable to the npm executable if found -# Only works on Linux systems as of now +# FindNpm +# -------- +# +# Find npm +# +# This module finds an installed npm. It sets the following variables: +# +# NPM_FOUND - Set to true if npm is found +# NPM_DIR - The directory where npm is installed +# NPM_GLOBAL_NODE_MODULE_DIR - The global node_modules directory +# NPM_EXECUTABLE - The path to the npm executable +# NPM_VERSION - The version number of the npm executable -find_program(NPM_EXECUTABLE NAMES npm - HINTS - /usr -) +find_program(NPM_EXECUTABLE NAMES npm HINTS /usr) -if(NPM_EXECUTABLE) - message(STATUS "NPM Executable found at ${NPM_EXECUTABLE}") +# If npm was found, fill in the rest +if (NPM_EXECUTABLE) + # Set the global node_modules location + execute_process(COMMAND ${NPM_EXECUTABLE} root -g + OUTPUT_VARIABLE NPM_GLOBAL_NODE_MODULE_DIR + ERROR_VARIABLE NPM_root_g_error + RESULT_VARIABLE NPM_root_g_result_code) + # Remove and newlines + string (STRIP ${NPM_GLOBAL_NODE_MODULE_DIR} NPM_GLOBAL_NODE_MODULE_DIR) + if(NPM_root_g_result_code) + if(NPM_FIND_REQUIRED) + message(SEND_ERROR "Command \"${NPM_EXECUTABLE} root -g\" failed with output:\n${NPM_root_g_error}") + else () + message(STATUS "Command \"${NPM_EXECUTABLE} root -g\" failed with output:\n${NPM_root_g_error}") + endif () + endif() + unset(NPM_root_g_error) + unset(NPM_root_g_result_code) + + # Set the NPM dir + if (EXISTS "${NPM_GLOBAL_NODE_MODULE_DIR}/npm") + set(NPM_DIR "${NPM_GLOBAL_NODE_MODULE_DIR}/npm") + endif() + + # Set the VERSION + execute_process(COMMAND ${NPM_EXECUTABLE} -v + OUTPUT_VARIABLE NPM_VERSION + ERROR_VARIABLE NPM_version_error + RESULT_VARIABLE NPM_version_result_code) + + if(NPM_version_result_code) + if(NPM_FIND_REQUIRED) + message(SEND_ERROR "Command \"${NPM_EXECUTABLE} -v\" failed with output:\n${NPM_version_error}") + else() + message(STATUS "Command \"${NPM_EXECUTABLE} -v\" failed with output:\n${NPM_version_error}") + endif () + endif () + unset(NPM_version_error) + unset(NPM_version_result_code) + + # Remove and newlines + string (STRIP ${NPM_VERSION} NPM_VERSION) + + set (NPM_FOUND TRUE) else() - message(ERROR "Unable to find NPM installation, please install NPM") -endif() + # Fail on REQUIRED + if (Npm_FIND_REQUIRED) + message(SEND_ERROR "Failed to find npm executable") + endif() +endif () + +find_package_handle_standard_args(NPM + REQUIRED_VARS NPM_EXECUTABLE NPM_DIR + VERSION_VAR NPM_VERSION ) + +mark_as_advanced(NPM_DIR NPM_GLOBAL_NODE_MODULE_DIR NPM_EXECUTABLE NPM_VERSION)