Skip to content

Commit 006f9d9

Browse files
committed
pmed#138 Initial CMake support. Intended to use as cmake subdirectory
1 parent 59c8f5d commit 006f9d9

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(v8pp LANGUAGES CXX)
4+
5+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
6+
7+
# On Ubuntu 20.04 install package libnode-dev because libv8-dev is redirected there
8+
find_package(V8)
9+
10+
# TODO: add warning flags setup
11+
# TODO: add option to skip tests targets
12+
13+
add_subdirectory(v8pp)
14+
add_subdirectory(test)
15+
add_subdirectory(plugins)

FindV8.cmake

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Based on https://raw.githubusercontent.com/nicehash/cpp-ethereum/master/cmake/Findv8.cmake
2+
#
3+
# Find v8
4+
#
5+
# Find the v8 includes and library
6+
#
7+
# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH
8+
#
9+
# This module defines
10+
# V8_INCLUDE_DIRS, where to find header, etc.
11+
# V8_LIBRARIES, the libraries needed to use v8.
12+
# V8_FOUND, If false, do not try to use v8.
13+
14+
# only look in default directories
15+
find_path(V8_INCLUDE_DIR NAMES v8.h PATHS /usr/include/v8 DOC "v8 include dir")
16+
17+
find_library(V8_LIBRARY NAMES v8 PATHS /usr/lib DOC "v8 library")
18+
19+
set(V8_INCLUDE_DIRS ${V8_INCLUDE_DIR})
20+
set(V8_LIBRARIES ${V8_LIBRARY})
21+
22+
# debug library on windows
23+
# same naming convention as in qt (appending debug library with d)
24+
# boost is using the same "hack" as us with "optimized" and "debug"
25+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
26+
find_library(V8_LIBRARY NAMES v8_base DOC "v8 base library")
27+
28+
find_library(
29+
V8_NO_SNAPSHOT_LIBRARY
30+
NAMES v8_nosnapshot
31+
DOC "v8 nosnapshot library"
32+
)
33+
34+
set(V8_LIBRARIES ${V8_LIBRARY} ${V8_NO_SNAPSHOT_LIBRARY})
35+
36+
find_library(V8_LIBRARY_DEBUG NAMES v8_based DOC "v8 base library")
37+
38+
find_library(
39+
V8_NO_SNAPSHOT_LIBRARY_DEBUG
40+
NAMES v8_nosnapshotd
41+
DOC "v8 nosnapshot library"
42+
)
43+
44+
set(V8_LIBRARIES
45+
"ws2_32"
46+
"winmm"
47+
optimized
48+
${V8_LIBRARIES}
49+
debug
50+
${V8_LIBRARY_DEBUG}
51+
${V8_NO_SNAPSHOT_LIBRARY_DEBUG}
52+
)
53+
endif()
54+
55+
# handle the QUIETLY and REQUIRED arguments and set V8_FOUND to TRUE
56+
# if all listed variables are TRUE, hide their existence from configuration view
57+
include(FindPackageHandleStandardArgs)
58+
find_package_handle_standard_args(V8 DEFAULT_MSG V8_INCLUDE_DIR V8_LIBRARY)
59+
mark_as_advanced(V8_INCLUDE_DIR V8_LIBRARY)
60+
61+
if(V8_FOUND)
62+
if(NOT TARGET V8::V8)
63+
add_library(V8::V8 SHARED IMPORTED)
64+
65+
set_target_properties(
66+
V8::V8
67+
PROPERTIES
68+
INTERFACE_INCLUDE_DIRECTORIES "${V8_INCLUDE_DIR}"
69+
IMPORTED_LOCATION "${V8_LIBRARY}"
70+
)
71+
endif()
72+
endif()

plugins/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function(v8pp_add_plugin name)
2+
add_library(plugin_${name} MODULE ${name}.cpp)
3+
4+
set_target_properties(plugin_${name} PROPERTIES OUTPUT_NAME "${name}" PREFIX "")
5+
6+
target_link_libraries(plugin_${name} PRIVATE v8pp::v8pp)
7+
endfunction()
8+
9+
v8pp_add_plugin(console)
10+
v8pp_add_plugin(file)

test/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Test target
2+
file(GLOB v8pp_test_cpp *.cpp)
3+
4+
list(SORT v8pp_test_cpp)
5+
6+
add_executable(v8pp_test ${v8pp_test_cpp})
7+
8+
target_link_libraries(v8pp_test PRIVATE v8pp::v8pp)

v8pp/CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copy all headers to ${CMAKE_CURRENT_BINARY_DIR}/v8pp
2+
# It makes this repo a little bit submodule friendly
3+
set(V8PP_INCLUDES_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
4+
file(MAKE_DIRECTORY "${V8PP_INCLUDES_DIR}/v8pp")
5+
6+
file(GLOB v8pp_hpp RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.hpp)
7+
file(GLOB v8pp_ipp RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.ipp)
8+
9+
foreach(f ${v8pp_hpp};${v8pp_ipp})
10+
set(_original "${CMAKE_CURRENT_LIST_DIR}/${f}")
11+
set(_linkname "${V8PP_INCLUDES_DIR}/v8pp/${f}")
12+
file(CREATE_LINK "${_original}" "${_linkname}" COPY_ON_ERROR)
13+
endforeach()
14+
15+
# Header only library target
16+
add_library(v8pp-header-only INTERFACE)
17+
18+
add_library(v8pp::v8pp-header-only ALIAS v8pp-header-only)
19+
20+
target_include_directories(v8pp-header-only INTERFACE "${V8PP_INCLUDES_DIR}")
21+
22+
target_compile_definitions(v8pp-header-only INTERFACE V8PP_HEADER_ONLY=1)
23+
24+
target_compile_features(v8pp-header-only INTERFACE cxx_std_14)
25+
26+
target_link_libraries(v8pp-header-only INTERFACE V8::V8)
27+
28+
# Static library target
29+
file(GLOB v8pp_cpp *.cpp)
30+
list(SORT v8pp_cpp)
31+
32+
add_library(v8pp STATIC ${v8pp_cpp})
33+
34+
add_library(v8pp::v8pp ALIAS v8pp)
35+
36+
target_include_directories(v8pp PUBLIC "${V8PP_INCLUDES_DIR}")
37+
38+
target_compile_definitions(v8pp PUBLIC V8PP_HEADER_ONLY=0)
39+
40+
target_compile_features(v8pp PUBLIC cxx_std_14)
41+
42+
target_link_libraries(v8pp PUBLIC V8::V8 ${CMAKE_DL_LIBS})
43+
44+
set_property(TARGET v8pp PROPERTY POSITION_INDEPENDENT_CODE ON)

0 commit comments

Comments
 (0)