Skip to content

Commit

Permalink
#138 Add initial CMake support
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikTH committed Sep 5, 2021
1 parent d62a0d5 commit 44ffb9f
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 0 deletions.
131 changes: 131 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
cmake_minimum_required(VERSION 3.14)
# using file(CREATE_LINK) that was added in 3.14

# Get version from config.hpp
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/v8pp/config.hpp config_hpp)
if(NOT config_hpp MATCHES "V8PP_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"")
message(FATAL_ERROR "Cannot get V8PP_VERSION from config.hpp.")
endif()
unset(config_hpp)
# Use math to skip leading zeros if any.
math(EXPR V8PP_VERSION_MAJOR ${CMAKE_MATCH_1})
math(EXPR V8PP_VERSION_MINOR ${CMAKE_MATCH_2})
math(EXPR V8PP_VERSION_PATCH ${CMAKE_MATCH_3})
set(V8PP_VERSION ${V8PP_VERSION_MAJOR}.${V8PP_VERSION_MINOR}.${V8PP_VERSION_PATCH})

project(v8pp VERSION ${V8PP_VERSION} LANGUAGES CXX)

# Determine if v8pp is built as a subproject (using add_subdirectory)
# or if it is the master project.
if(NOT DEFINED V8PP_MASTER_PROJECT)
set(V8PP_MASTER_PROJECT OFF)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(V8PP_MASTER_PROJECT ON)
endif()
endif()

# Options that control generation of various targets.
option(V8PP_WARNING "Enable extra warnings and expensive tests." ${V8PP_MASTER_PROJECT})
option(V8PP_WERROR "Halt the compilation with an error on compiler warnings." OFF)
option(V8PP_TEST "Generate the test target." ${V8PP_MASTER_PROJECT})
option(V8PP_INSTALL "Generate install target." ${V8PP_MASTER_PROJECT})

# Warnings setup
set(V8PP_WARNING_OPTIONS)

if(V8PP_WARNING)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND V8PP_WARNING_OPTIONS "-Wall")
list(APPEND V8PP_WARNING_OPTIONS "-Wextra")
list(APPEND V8PP_WARNING_OPTIONS "-Wno-unused-function")
elseif(
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
)
list(APPEND V8PP_WARNING_OPTIONS "-Wall")
list(APPEND V8PP_WARNING_OPTIONS "-Wextra")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_msvc_warning_flags(WARNING_OPTIONS)
list(APPEND V8PP_WARNING_OPTIONS "/W3")
else()
message(SEND_ERROR "unknown compiler \"${CMAKE_CXX_COMPILER_ID}\"")
endif()
endif()

if(V8PP_WERROR)
if(
CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
)
list(APPEND V8PP_WARNING_OPTIONS "-Werror")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_msvc_warning_flags(WARNING_OPTIONS)
list(APPEND V8PP_WARNING_OPTIONS "/WX")
else()
message(SEND_ERROR "unknown compiler \"${CMAKE_CXX_COMPILER_ID}\"")
endif()
endif()

# Find V8 package
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

find_package(V8 REQUIRED)

add_subdirectory(v8pp)

if(V8PP_TEST)
add_subdirectory(test)
endif()

if(V8PP_INSTALL)
# cmake code for install is based on https://github.com/onqtam/doctest/blob/master/CMakeLists.txt

set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
include(GNUInstallDirs)
set(include_install_dir ${CMAKE_INSTALL_INCLUDEDIR})
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
else()
set(include_install_dir "include")
set(config_install_dir "lib/cmake/${PROJECT_NAME}")
endif()

set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
set(targets_export_name "${PROJECT_NAME}Targets")
set(namespace "${PROJECT_NAME}::")

include(CMakePackageConfigHelpers)

write_basic_package_version_file(
"${version_config}"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)

configure_file("Config.cmake.in" "${project_config}" @ONLY)

install(
TARGETS v8pp v8pp-header-only
EXPORT "${targets_export_name}"
INCLUDES DESTINATION "${include_install_dir}"
)

install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/v8pp/include/"
DESTINATION "${include_install_dir}/"
)

install(
FILES "${project_config}" "${version_config}"
DESTINATION "${config_install_dir}"
)

install(
EXPORT "${targets_export_name}"
NAMESPACE "${namespace}"
DESTINATION "${config_install_dir}"
)
endif()
6 changes: 6 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if(NOT TARGET v8pp::v8pp)
# Provide path for scripts
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
endif()
72 changes: 72 additions & 0 deletions FindV8.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Based on https://raw.githubusercontent.com/nicehash/cpp-ethereum/master/cmake/Findv8.cmake
#
# Find v8
#
# Find the v8 includes and library
#
# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH
#
# This module defines
# V8_INCLUDE_DIRS, where to find header, etc.
# V8_LIBRARIES, the libraries needed to use v8.
# V8_FOUND, If false, do not try to use v8.

# only look in default directories
find_path(V8_INCLUDE_DIR NAMES v8.h PATHS /usr/include/v8 DOC "v8 include dir")

find_library(V8_LIBRARY NAMES v8 PATHS /usr/lib DOC "v8 library")

set(V8_INCLUDE_DIRS ${V8_INCLUDE_DIR})
set(V8_LIBRARIES ${V8_LIBRARY})

# debug library on windows
# same naming convention as in qt (appending debug library with d)
# boost is using the same "hack" as us with "optimized" and "debug"
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
find_library(V8_LIBRARY NAMES v8_base DOC "v8 base library")

find_library(
V8_NO_SNAPSHOT_LIBRARY
NAMES v8_nosnapshot
DOC "v8 nosnapshot library"
)

set(V8_LIBRARIES ${V8_LIBRARY} ${V8_NO_SNAPSHOT_LIBRARY})

find_library(V8_LIBRARY_DEBUG NAMES v8_based DOC "v8 base library")

find_library(
V8_NO_SNAPSHOT_LIBRARY_DEBUG
NAMES v8_nosnapshotd
DOC "v8 nosnapshot library"
)

set(V8_LIBRARIES
"ws2_32"
"winmm"
optimized
${V8_LIBRARIES}
debug
${V8_LIBRARY_DEBUG}
${V8_NO_SNAPSHOT_LIBRARY_DEBUG}
)
endif()

# handle the QUIETLY and REQUIRED arguments and set V8_FOUND to TRUE
# if all listed variables are TRUE, hide their existence from configuration view
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(V8 DEFAULT_MSG V8_INCLUDE_DIR V8_LIBRARY)
mark_as_advanced(V8_INCLUDE_DIR V8_LIBRARY)

if(V8_FOUND)
if(NOT TARGET V8::V8)
add_library(V8::V8 SHARED IMPORTED)

set_target_properties(
V8::V8
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${V8_INCLUDE_DIR}"
IMPORTED_LOCATION "${V8_LIBRARY}"
)
endif()
endif()
10 changes: 10 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Test target
file(GLOB v8pp_test_cpp *.cpp)

list(SORT v8pp_test_cpp)

add_executable(v8pp_test ${v8pp_test_cpp})

target_link_libraries(v8pp_test PRIVATE v8pp::v8pp)

target_compile_options(v8pp_test PRIVATE ${V8PP_WARNING_OPTIONS})
50 changes: 50 additions & 0 deletions v8pp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copy all headers to ${CMAKE_CURRENT_BINARY_DIR}/v8pp
# It makes this repo a little bit submodule friendly
set(V8PP_INCLUDES_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
file(MAKE_DIRECTORY "${V8PP_INCLUDES_DIR}/v8pp")

file(GLOB v8pp_hpp RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.hpp)
file(GLOB v8pp_ipp RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.ipp)

foreach(f ${v8pp_hpp};${v8pp_ipp})
set(_original "${CMAKE_CURRENT_LIST_DIR}/${f}")
set(_linkname "${V8PP_INCLUDES_DIR}/v8pp/${f}")
file(CREATE_LINK "${_original}" "${_linkname}" COPY_ON_ERROR)
endforeach()

# Header only library target
add_library(v8pp-header-only INTERFACE)

add_library(v8pp::v8pp-header-only ALIAS v8pp-header-only)

target_include_directories(v8pp-header-only INTERFACE
$<BUILD_INTERFACE:${V8PP_INCLUDES_DIR}>
$<INSTALL_INTERFACE:include/v8pp>)

target_compile_definitions(v8pp-header-only INTERFACE V8PP_HEADER_ONLY=1)

target_compile_features(v8pp-header-only INTERFACE cxx_std_14)

target_link_libraries(v8pp-header-only INTERFACE V8::V8)

# Static library target
file(GLOB v8pp_cpp *.cpp)
list(SORT v8pp_cpp)

add_library(v8pp STATIC ${v8pp_cpp})

add_library(v8pp::v8pp ALIAS v8pp)

target_include_directories(v8pp PUBLIC
$<BUILD_INTERFACE:${V8PP_INCLUDES_DIR}>
$<INSTALL_INTERFACE:include/v8pp>)

target_compile_definitions(v8pp PUBLIC V8PP_HEADER_ONLY=0)

target_compile_features(v8pp PUBLIC cxx_std_14)

target_link_libraries(v8pp PUBLIC V8::V8 ${CMAKE_DL_LIBS})

target_compile_options(v8pp PRIVATE ${V8PP_WARNING_OPTIONS})

set_property(TARGET v8pp PROPERTY POSITION_INDEPENDENT_CODE ON)

0 comments on commit 44ffb9f

Please sign in to comment.