Skip to content

Commit 44ffb9f

Browse files
committed
pmed#138 Add initial CMake support
1 parent d62a0d5 commit 44ffb9f

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

CMakeLists.txt

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