Skip to content

Commit 9f6d666

Browse files
committed
pmed#138 Add initial CMake support
1 parent 814026b commit 9f6d666

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

CMakeLists.txt

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
# Get version from config.hpp
4+
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/v8pp/config.hpp config_hpp)
5+
if(NOT config_hpp MATCHES "V8PP_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"")
6+
message(FATAL_ERROR "Cannot get V8PP_VERSION from config.hpp.")
7+
endif()
8+
unset(config_hpp)
9+
# Use math to skip leading zeros if any.
10+
math(EXPR V8PP_VERSION_MAJOR ${CMAKE_MATCH_1})
11+
math(EXPR V8PP_VERSION_MINOR ${CMAKE_MATCH_2})
12+
math(EXPR V8PP_VERSION_PATCH ${CMAKE_MATCH_3})
13+
set(V8PP_VERSION ${V8PP_VERSION_MAJOR}.${V8PP_VERSION_MINOR}.${V8PP_VERSION_PATCH})
14+
15+
project(v8pp VERSION ${V8PP_VERSION} LANGUAGES CXX)
16+
17+
# Determine if v8pp is built as a subproject (using add_subdirectory)
18+
# or if it is the master project.
19+
if(NOT DEFINED V8PP_MASTER_PROJECT)
20+
set(V8PP_MASTER_PROJECT OFF)
21+
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
22+
set(V8PP_MASTER_PROJECT ON)
23+
endif()
24+
endif()
25+
26+
# Options that control generation of various targets.
27+
option(V8PP_WARNING "Enable extra warnings and expensive tests." ${V8PP_MASTER_PROJECT})
28+
option(V8PP_WERROR "Halt the compilation with an error on compiler warnings." OFF)
29+
option(V8PP_TEST "Generate the test target." ${V8PP_MASTER_PROJECT})
30+
option(V8PP_INSTALL "Generate install target." ${V8PP_MASTER_PROJECT})
31+
32+
# Warnings setup
33+
set(V8PP_WARNING_OPTIONS)
34+
35+
if(V8PP_WARNING)
36+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
37+
list(APPEND V8PP_WARNING_OPTIONS "-Wall")
38+
list(APPEND V8PP_WARNING_OPTIONS "-Wextra")
39+
elseif(
40+
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
41+
OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
42+
)
43+
list(APPEND V8PP_WARNING_OPTIONS "-Wall")
44+
list(APPEND V8PP_WARNING_OPTIONS "-Wextra")
45+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
46+
set_msvc_warning_flags(WARNING_OPTIONS)
47+
list(APPEND V8PP_WARNING_OPTIONS "/W3")
48+
else()
49+
message(SEND_ERROR "unknown compiler \"${CMAKE_CXX_COMPILER_ID}\"")
50+
endif()
51+
endif()
52+
53+
if(V8PP_WERROR)
54+
if(
55+
CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
56+
OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
57+
OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
58+
)
59+
list(APPEND V8PP_WARNING_OPTIONS "-Werror")
60+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
61+
set_msvc_warning_flags(WARNING_OPTIONS)
62+
list(APPEND V8PP_WARNING_OPTIONS "/WX")
63+
else()
64+
message(SEND_ERROR "unknown compiler \"${CMAKE_CXX_COMPILER_ID}\"")
65+
endif()
66+
endif()
67+
68+
# Find V8 package
69+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
70+
71+
find_package(V8 REQUIRED)
72+
73+
add_subdirectory(v8pp)
74+
75+
if(V8PP_TEST)
76+
add_subdirectory(test)
77+
endif()
78+
79+
if(V8PP_INSTALL)
80+
# cmake code for install is based on https://github.com/onqtam/doctest/blob/master/CMakeLists.txt
81+
82+
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
83+
84+
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
85+
include(GNUInstallDirs)
86+
set(include_install_dir ${CMAKE_INSTALL_INCLUDEDIR})
87+
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
88+
else()
89+
set(include_install_dir "include")
90+
set(config_install_dir "lib/cmake/${PROJECT_NAME}")
91+
endif()
92+
93+
set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
94+
set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
95+
set(targets_export_name "${PROJECT_NAME}Targets")
96+
set(namespace "${PROJECT_NAME}::")
97+
98+
include(CMakePackageConfigHelpers)
99+
100+
write_basic_package_version_file(
101+
"${version_config}"
102+
VERSION ${PROJECT_VERSION}
103+
COMPATIBILITY SameMajorVersion
104+
)
105+
106+
configure_file("Config.cmake.in" "${project_config}" @ONLY)
107+
108+
install(
109+
TARGETS v8pp v8pp-header-only
110+
EXPORT "${targets_export_name}"
111+
INCLUDES DESTINATION "${include_install_dir}"
112+
)
113+
114+
install(
115+
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/v8pp/include/"
116+
DESTINATION "${include_install_dir}/"
117+
)
118+
119+
install(
120+
FILES "${project_config}" "${version_config}"
121+
DESTINATION "${config_install_dir}"
122+
)
123+
124+
install(
125+
EXPORT "${targets_export_name}"
126+
NAMESPACE "${namespace}"
127+
DESTINATION "${config_install_dir}"
128+
)
129+
endif()

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

+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()

test/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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)
9+
10+
target_compile_options(v8pp_test PRIVATE ${V8PP_WARNING_OPTIONS})

v8pp/CMakeLists.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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
21+
$<BUILD_INTERFACE:${V8PP_INCLUDES_DIR}>
22+
$<INSTALL_INTERFACE:include/v8pp>)
23+
24+
target_compile_definitions(v8pp-header-only INTERFACE V8PP_HEADER_ONLY=1)
25+
26+
target_compile_features(v8pp-header-only INTERFACE cxx_std_14)
27+
28+
target_link_libraries(v8pp-header-only INTERFACE V8::V8)
29+
30+
# Static library target
31+
file(GLOB v8pp_cpp *.cpp)
32+
list(SORT v8pp_cpp)
33+
34+
add_library(v8pp STATIC ${v8pp_cpp})
35+
36+
add_library(v8pp::v8pp ALIAS v8pp)
37+
38+
target_include_directories(v8pp PUBLIC
39+
$<BUILD_INTERFACE:${V8PP_INCLUDES_DIR}>
40+
$<INSTALL_INTERFACE:include/v8pp>)
41+
42+
target_compile_definitions(v8pp PUBLIC V8PP_HEADER_ONLY=0)
43+
44+
target_compile_features(v8pp PUBLIC cxx_std_14)
45+
46+
target_link_libraries(v8pp PUBLIC V8::V8 ${CMAKE_DL_LIBS})
47+
48+
target_compile_options(v8pp PRIVATE ${V8PP_WARNING_OPTIONS})
49+
50+
set_property(TARGET v8pp PROPERTY POSITION_INDEPENDENT_CODE ON)

0 commit comments

Comments
 (0)