Skip to content

Commit bd50d6c

Browse files
YarikTHPavel Medvedev
authored and
Pavel Medvedev
committed
modularize CMakeLists.txt files, add FindV8.cmake
See #138 Add initial CMake support
1 parent 38c7485 commit bd50d6c

File tree

6 files changed

+194
-161
lines changed

6 files changed

+194
-161
lines changed

CMakeLists.txt

+11-161
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.12)
22

3-
project(v8pp VERSION 1.7.0 LANGUAGES CXX)
4-
5-
find_package(V8 CONFIG REQUIRED)
3+
project(v8pp
4+
VERSION 1.7.0
5+
DESCRIPTION "Bind C++ functions and classes into V8 JavaScript engine"
6+
HOMEPAGE_URL https://github.com/pmed/v8pp
7+
LANGUAGES CXX
8+
)
69

710
set(CMAKE_CXX_STANDARD 14)
811
set(CMAKE_CXX_EXTENSIONS OFF)
@@ -15,163 +18,10 @@ set(V8PP_HEADER_ONLY 0 CACHE BOOL "Header-only library")
1518
set(V8PP_ISOLATE_DATA_SLOT 0 CACHE STRING "v8::Isolate data slot number, used in v8pp for shared data")
1619
set(V8PP_PLUGIN_INIT_PROC_NAME "v8pp_module_init" CACHE STRING "v8pp plugin initialization procedure name")
1720
set(V8PP_PLUGIN_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX} CACHE STRING "v8pp plugin filename suffix")
18-
set(V8PP_WITH_TESTS 1 CACHE BOOL "build with tests")
19-
20-
configure_file(v8pp/config.hpp.in v8pp/config.hpp)
21-
22-
set(V8PP_HEADERS
23-
${CMAKE_SOURCE_DIR}/v8pp/call_from_v8.hpp
24-
${CMAKE_SOURCE_DIR}/v8pp/call_v8.hpp
25-
${CMAKE_SOURCE_DIR}/v8pp/class.hpp
26-
${CMAKE_BINARY_DIR}/v8pp/config.hpp
27-
${CMAKE_SOURCE_DIR}/v8pp/context.hpp
28-
${CMAKE_SOURCE_DIR}/v8pp/convert.hpp
29-
${CMAKE_SOURCE_DIR}/v8pp/function.hpp
30-
${CMAKE_SOURCE_DIR}/v8pp/json.hpp
31-
${CMAKE_SOURCE_DIR}/v8pp/module.hpp
32-
${CMAKE_SOURCE_DIR}/v8pp/object.hpp
33-
${CMAKE_SOURCE_DIR}/v8pp/property.hpp
34-
${CMAKE_SOURCE_DIR}/v8pp/ptr_traits.hpp
35-
${CMAKE_SOURCE_DIR}/v8pp/throw_ex.hpp
36-
${CMAKE_SOURCE_DIR}/v8pp/utility.hpp
37-
${CMAKE_SOURCE_DIR}/v8pp/version.hpp
38-
)
39-
40-
if(V8PP_HEADER_ONLY)
41-
set(V8PP_SOURCES
42-
${CMAKE_SOURCE_DIR}/v8pp/class.ipp
43-
${CMAKE_SOURCE_DIR}/v8pp/json.ipp
44-
${CMAKE_SOURCE_DIR}/v8pp/throw_ex.ipp
45-
${CMAKE_SOURCE_DIR}/v8pp/version.ipp
46-
)
47-
else()
48-
set(V8PP_SOURCES
49-
${CMAKE_SOURCE_DIR}/v8pp/class.cpp
50-
${CMAKE_SOURCE_DIR}/v8pp/context.cpp
51-
${CMAKE_SOURCE_DIR}/v8pp/convert.cpp
52-
${CMAKE_SOURCE_DIR}/v8pp/json.cpp
53-
${CMAKE_SOURCE_DIR}/v8pp/throw_ex.cpp
54-
${CMAKE_SOURCE_DIR}/v8pp/version.cpp
55-
)
56-
endif()
57-
58-
#set(CMAKE_CXX_RTTI OFF)
59-
if(MSVC)
60-
set(V8PP_COMPILE_OPTIONS /GR- /EHsc /permissive- /W4)
61-
# disable specific warnings
62-
list(APPEND V8PP_COMPILE_OPTIONS /wd4190)
63-
# set warning level 3 for system headers
64-
list(APPEND V8PP_COMPILE_OPTIONS /experimental:external /external:anglebrackets /external:W3)
65-
else()
66-
set(V8PP_COMPILE_OPTIONS -fno-rtti -Wall -Wextra -Wpedantic)
67-
endif()
68-
69-
if(V8PP_HEADER_ONLY)
70-
add_library(v8pp INTERFACE)
71-
target_sources(v8pp INTERFACE ${V8PP_HEADERS} ${V8PP_SOURCES})
72-
target_compile_options(v8pp INTERFACE ${V8PP_COMPILE_OPTIONS})
73-
target_include_directories(v8pp INTERFACE ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
74-
target_include_directories(v8pp SYSTEM INTERFACE ${V8_INCLUDE_DIR})
75-
target_link_libraries(v8pp INTERFACE V8::V8)
76-
else()
77-
add_library(v8pp ${V8PP_HEADERS} ${V8PP_SOURCES})
78-
target_compile_options(v8pp PRIVATE ${V8PP_COMPILE_OPTIONS})
79-
target_include_directories(v8pp PUBLIC ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
80-
target_include_directories(v8pp SYSTEM PUBLIC ${V8_INCLUDE_DIR})
81-
target_link_libraries(v8pp PUBLIC V8::V8)
82-
endif()
83-
84-
#source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${V8PP_HEADERS} ${V8PP_SOURCES})
85-
86-
if(BUILD_SHARED_LIBS)
87-
if (NOT V8PP_HEADER_ONLY)
88-
target_link_libraries(v8pp PUBLIC ${CMAKE_DL_LIBS})
89-
endif()
9021

91-
add_library(console MODULE plugins/console.cpp)
92-
set_target_properties(console PROPERTIES PREFIX "")
93-
target_link_libraries(console v8pp)
94-
95-
add_library(file MODULE plugins/file.cpp)
96-
set_target_properties(file PROPERTIES PREFIX "")
97-
target_link_libraries(file v8pp)
98-
endif()
99-
100-
101-
if(V8PP_WITH_TESTS)
22+
add_subdirectory(v8pp)
23+
add_subdirectory(plugins)
24+
if(BUILD_TESTING)
10225
enable_testing()
103-
add_executable(v8pp_test
104-
test/main.cpp
105-
test/test.hpp
106-
test/test_call_from_v8.cpp
107-
test/test_call_v8.cpp
108-
test/test_class.cpp
109-
test/test_context.cpp
110-
test/test_convert.cpp
111-
test/test_factory.cpp
112-
test/test_function.cpp
113-
test/test_json.cpp
114-
test/test_module.cpp
115-
test/test_object.cpp
116-
test/test_property.cpp
117-
test/test_ptr_traits.cpp
118-
test/test_throw_ex.cpp
119-
test/test_utility.cpp
120-
)
121-
if (V8PP_HEADER_ONLY)
122-
target_sources(v8pp_test PRIVATE v8pp/context.cpp)
123-
endif()
124-
125-
target_link_libraries(v8pp_test v8pp)
126-
if(BUILD_SHARED_LIBS)
127-
target_link_libraries(v8pp_test ${CMAKE_DL_LIBS})
128-
endif()
129-
130-
add_test(v8pp_test v8pp_test --version --run-tests)
131-
set_tests_properties(v8pp_test PROPERTIES ENVIRONMENT LD_LIBRARY_PATH=${V8_LIB_DIR}:$ENV{LD_LIBRARY_PATH})
132-
133-
if(BUILD_SHARED_LIBS)
134-
file(GLOB JS_TESTS test/*.js)
135-
add_test(v8pp_js_test v8pp_test --lib-path ${CMAKE_BINARY_DIR} ${JS_TESTS})
136-
set_tests_properties(v8pp_js_test PROPERTIES ENVIRONMENT LD_LIBRARY_PATH=${V8_LIB_DIR}:$ENV{LD_LIBRARY_PATH})
137-
endif()
26+
add_subdirectory(test)
13827
endif()
139-
140-
# Install
141-
#include(GNUInstallDirs)
142-
#include(CMakePackageConfigHelpers)
143-
144-
#write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" COMPATIBILITY SameMajorVersion)
145-
#export(TARGETS ${PROJECT_NAME} NAMESPACE ${PROJECT_NAME}:: FILE "${PROJECT_NAME}Config.cmake")
146-
#install(DIRECTORY "include" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
147-
#install(TARGETS ${PROJECT_NAME} EXPORT "${PROJECT_NAME}Config" INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
148-
#install(EXPORT "${PROJECT_NAME}Config" NAMESPACE ${PROJECT_NAME}:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
149-
#install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
150-
151-
macro(find_v8_dev)
152-
if(NOT DEFINED V8_INCLUDE_DIR)
153-
set(V8_INCLUDE_DIR ${V8_DIR}/include)
154-
endif()
155-
message(STATUS "V8 include dir: ${V8_INCLUDE_DIR}")
156-
if(NOT EXISTS "${V8_INCLUDE_DIR}")
157-
message(FATAL_ERROR "No V8 include dir found")
158-
endif()
159-
160-
if(NOT DEFINED V8_LIB_DIR)
161-
set(V8_LIB_DIR ${V8_DIR}/lib)
162-
endif()
163-
message(STATUS "V8 lib dir: ${V8_LIB_DIR}")
164-
if(NOT EXISTS "${V8_LIB_DIR}")
165-
message(FATAL_ERROR "No V8 lib dir found")
166-
endif()
167-
168-
if(BUILD_SHARED_LIBS)
169-
file(GLOB V8_LIBRARIES ${V8_LIB_DIR}/*${CMAKE_SHARED_LIBRARY_SUFFIX})
170-
else()
171-
file(GLOB V8_LIBRARIES ${V8_LIB_DIR}/*${CMAKE_STATIC_LIBRARY_SUFFIX})
172-
endif()
173-
message(STATUS "V8 libraries: ${V8_LIBRARIES}")
174-
if(NOT V8_LIBRARIES)
175-
message(FATAL_ERROR "No V8 libraries found")
176-
endif()
177-
endmacro()

Config.cmake.in

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if(NOT TARGET v8pp::v8pp)
2+
# Provide path for scripts
3+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
4+
5+
include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake")
6+
endif()

FindV8.cmake

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_DIRS NAMES v8.h)
16+
17+
find_library(V8_LIB NAMES v8)
18+
find_library(V8_LIBPLATFORM NAMES v8_libplatform)
19+
set(V8_LIBRARIES ${V8_LIB} ${V8_LIBPLATFORM})
20+
21+
# handle the QUIETLY and REQUIRED arguments and set V8_FOUND to TRUE
22+
# if all listed variables are TRUE, hide their existence from configuration view
23+
include(FindPackageHandleStandardArgs)
24+
find_package_handle_standard_args(V8 DEFAULT_MSG V8_INCLUDE_DIRS V8_LIB V8_LIBPLATFORM)
25+
mark_as_advanced(V8_INCLUDE_DIRS V8_LIBRARIES)
26+
27+
if(V8_FOUND)
28+
if(NOT TARGET V8::V8)
29+
add_library(V8::V8 SHARED IMPORTED)
30+
set_target_properties(V8::V8 PROPERTIES IMPORTED_LOCATION "${V8_LIB}")
31+
target_include_directories(V8::V8 INTERFACE "${V8_INCLUDE_DIRS}")
32+
target_link_libraries(V8::V8 INTERFACE "${V8_LIBRARIES}")
33+
endif()
34+
endif()

plugins/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# sample plugin targets
2+
3+
if(BUILD_SHARED_LIBS)
4+
add_library(console MODULE console.cpp)
5+
set_target_properties(console PROPERTIES PREFIX "")
6+
target_link_libraries(console v8pp)
7+
8+
add_library(file MODULE file.cpp)
9+
set_target_properties(file PROPERTIES PREFIX "")
10+
target_link_libraries(file v8pp)
11+
endif()

test/CMakeLists.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# test target
2+
3+
add_executable(v8pp_test
4+
main.cpp
5+
test.hpp
6+
test_call_from_v8.cpp
7+
test_call_v8.cpp
8+
test_class.cpp
9+
test_context.cpp
10+
test_convert.cpp
11+
test_factory.cpp
12+
test_function.cpp
13+
test_json.cpp
14+
test_module.cpp
15+
test_object.cpp
16+
test_property.cpp
17+
test_ptr_traits.cpp
18+
test_throw_ex.cpp
19+
test_utility.cpp
20+
)
21+
22+
if(V8PP_HEADER_ONLY)
23+
target_sources(v8pp_test PRIVATE ${PROJECT_SOURCE_DIR}/v8pp/context.cpp)
24+
endif()
25+
26+
target_link_libraries(v8pp_test v8pp)
27+
28+
add_test(v8pp_test v8pp_test --version --run-tests)
29+
30+
if(BUILD_SHARED_LIBS)
31+
file(GLOB JS_TESTS *.js)
32+
add_test(v8pp_js_test v8pp_test --lib-path ${PROJECT_BINARY_DIR}/plugins ${JS_TESTS})
33+
endif()

v8pp/CMakeLists.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# v8pp target
2+
3+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})
4+
find_package(V8 REQUIRED)
5+
6+
configure_file(config.hpp.in config.hpp)
7+
8+
set(V8PP_DEFINES V8_COMPRESS_POINTERS V8_31BIT_SMIS_ON_64BIT_ARCH)
9+
10+
set(V8PP_HEADERS
11+
config.hpp
12+
call_from_v8.hpp
13+
call_v8.hpp
14+
class.hpp
15+
context.hpp
16+
convert.hpp
17+
factory.hpp
18+
function.hpp
19+
json.hpp
20+
module.hpp
21+
object.hpp
22+
property.hpp
23+
ptr_traits.hpp
24+
throw_ex.hpp
25+
utility.hpp
26+
version.hpp
27+
)
28+
29+
if(V8PP_HEADER_ONLY)
30+
list(APPEND V8PP_HEADERS
31+
class.ipp
32+
json.ipp
33+
throw_ex.ipp
34+
version.ipp
35+
)
36+
set(V8PP_SOURCES)
37+
else()
38+
set(V8PP_SOURCES
39+
class.cpp
40+
context.cpp
41+
convert.cpp
42+
json.cpp
43+
throw_ex.cpp
44+
version.cpp
45+
)
46+
endif()
47+
48+
#set(CMAKE_CXX_RTTI OFF)
49+
if(MSVC)
50+
set(V8PP_COMPILE_OPTIONS /GR- /EHsc /permissive- /W4)
51+
# disable specific warnings
52+
list(APPEND V8PP_COMPILE_OPTIONS /wd4190)
53+
# set warning level 3 for system headers
54+
list(APPEND V8PP_COMPILE_OPTIONS /experimental:external /external:anglebrackets /external:W3)
55+
else()
56+
set(V8PP_COMPILE_OPTIONS -fno-rtti -Wall -Wextra -Wpedantic)
57+
endif()
58+
59+
if(V8PP_HEADER_ONLY)
60+
add_library(v8pp INTERFACE)
61+
target_compile_definitions(v8pp INTERFACE ${V8PP_DEFINES})
62+
target_compile_options(v8pp INTERFACE ${V8PP_COMPILE_OPTIONS})
63+
target_include_directories(v8pp INTERFACE
64+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
65+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
66+
target_link_libraries(v8pp INTERFACE V8::V8)
67+
else()
68+
add_library(v8pp ${V8PP_HEADERS} ${V8PP_SOURCES})
69+
target_compile_definitions(v8pp PUBLIC ${V8PP_DEFINES})
70+
target_compile_options(v8pp PRIVATE ${V8PP_COMPILE_OPTIONS})
71+
target_include_directories(v8pp PUBLIC
72+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
73+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
74+
target_link_libraries(v8pp PUBLIC V8::V8)
75+
if(BUILD_SHARED_LIBS)
76+
target_link_libraries(v8pp PUBLIC ${CMAKE_DL_LIBS})
77+
endif()
78+
endif()
79+
80+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${V8PP_HEADERS} ${V8PP_SOURCES})
81+
82+
# Install
83+
include(GNUInstallDirs)
84+
include(CMakePackageConfigHelpers)
85+
86+
write_basic_package_version_file("${PROJECT_BINARY_DIR}/ConfigVersion.cmake" COMPATIBILITY SameMajorVersion)
87+
configure_package_config_file("${PROJECT_SOURCE_DIR}/Config.cmake.in" "${PROJECT_BINARY_DIR}/Config.cmake"
88+
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/v8pp/cmake)
89+
90+
install(TARGETS v8pp EXPORT v8pp_Targets)
91+
install(EXPORT v8pp_Targets
92+
NAMESPACE v8pp::
93+
FILE v8ppTargets.cmake
94+
DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/v8pp")
95+
96+
install(FILES "${PROJECT_BINARY_DIR}/ConfigVersion.cmake" "${PROJECT_BINARY_DIR}/Config.cmake"
97+
DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/v8pp")
98+
99+
install(FILES ${V8PP_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/v8pp)

0 commit comments

Comments
 (0)