Using cmake.js with google test for testing a node.js addon

188 views Asked by At

I am trying to test a node-js addon (built with cmake-js) with google test. I am building on Mac OS.

The addon target builds and runs ok, but I have problems with the googletest target.

I am getting linking errors related to undefined V8 methods.

In CMakeLists.txt I printed (with message) the variables CMAKE_JS_SRC and CMAKE_JS_LIB and they are both empty. If CMAKE_JS_LIB is empty I don't see how target_link_libraries() should work to add the node/v8 library to my test executable...

If instead of using add_executable() I use add_library() the google_test target builds but of course I can not run it since it is not an executable anymore.

Can you help?

Below is my CMakeLists.txt:

make_minimum_required(VERSION 3.14)

include_directories(my_project)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_FLAGS "-Wno-pointer-sign -fno-signed-char -std=c99 -fpermissive")
set(CMAKE_CXX_FLAGS "-Wno-pointer-sign -fno-signed-char -std=c99 -fpermissive")

project (addon)

include(FetchContent)

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG release-1.12.1
)
FetchContent_MakeAvailable(googletest)
include_directories(${CMAKE_JS_INC} "my_project/include/pkcs11/v2.40/")
file(GLOB SOURCE_FILES "my_project/*.c" "my_project/*.h" "my_project/*.cpp")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} )
## google_test
enable_testing()
file(GLOB TEST_FILES "test/*.cc" "test/utils/*.c")

add_executable(
  my_project_google_test
  ${TEST_FILES)
)
target_include_directories(my_project_google_test
PRIVATE
test/utils/*.h
)

target_link_libraries(
  my_project_google_test
  PRIVATE
  GTest::gtest_main
  ${CMAKE_JS_LIB}
)

include(GoogleTest)
gtest_discover_tests(my_project_google_test)
1

There are 1 answers

1
273K On

CMAKE_JS_LIB is empty everywhere except on Windows.

Install v8 from Homebrew if you haven't done it yet: brew install v8.

Link the app to v8

target_link_libraries(
  my_project_google_test
  PRIVATE
  v8
)

Other v8 libraries can be required, you have not post all errors: v8_libbase v8_libplatform.