|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +cmake_minimum_required(VERSION 2.8) |
| 17 | + |
| 18 | +set (CMAKE_CXX_STANDARD 11) |
| 19 | + |
| 20 | +project(cppdap C CXX) |
| 21 | + |
| 22 | +########################################################### |
| 23 | +# Options |
| 24 | +########################################################### |
| 25 | +option(CPPDAP_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF) |
| 26 | +option(CPPDAP_BUILD_EXAMPLES "Build example applications" OFF) |
| 27 | +option(CPPDAP_BUILD_TESTS "Build tests" OFF) |
| 28 | +option(CPPDAP_ASAN "Build dap with address sanitizer" OFF) |
| 29 | +option(CPPDAP_MSAN "Build dap with memory sanitizer" OFF) |
| 30 | +option(CPPDAP_TSAN "Build dap with thread sanitizer" OFF) |
| 31 | +option(CPPDAP_INSTALL_VSCODE_EXAMPLES "Build and install dap examples into vscode extensions directory" OFF) |
| 32 | +option(CPPDAP_INSTALL "Create dap install target" OFF) |
| 33 | + |
| 34 | +########################################################### |
| 35 | +# Directories |
| 36 | +########################################################### |
| 37 | +set(CPPDAP_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) |
| 38 | +set(CPPDAP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) |
| 39 | +set(CPPDAP_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party) |
| 40 | +set(JSON_DIR ${CPPDAP_THIRD_PARTY_DIR}/json) |
| 41 | +set(GOOGLETEST_DIR ${CPPDAP_THIRD_PARTY_DIR}/googletest) |
| 42 | + |
| 43 | +########################################################### |
| 44 | +# Submodules |
| 45 | +########################################################### |
| 46 | +if(CPPDAP_BUILD_TESTS) |
| 47 | + if(NOT EXISTS ${CPPDAP_THIRD_PARTY_DIR}/googletest/.git) |
| 48 | + message(WARNING "third_party/googletest submodule missing.") |
| 49 | + message(WARNING "Run: `git submodule update --init` to build tests.") |
| 50 | + set(CPPDAP_BUILD_TESTS OFF) |
| 51 | + endif() |
| 52 | +endif(CPPDAP_BUILD_TESTS) |
| 53 | + |
| 54 | +########################################################### |
| 55 | +# File lists |
| 56 | +########################################################### |
| 57 | +set(CPPDAP_LIST |
| 58 | + ${CPPDAP_SRC_DIR}/content_stream.cpp |
| 59 | + ${CPPDAP_SRC_DIR}/io.cpp |
| 60 | + ${CPPDAP_SRC_DIR}/json_serializer.cpp |
| 61 | + ${CPPDAP_SRC_DIR}/network.cpp |
| 62 | + ${CPPDAP_SRC_DIR}/protocol_events.cpp |
| 63 | + ${CPPDAP_SRC_DIR}/protocol_requests.cpp |
| 64 | + ${CPPDAP_SRC_DIR}/protocol_response.cpp |
| 65 | + ${CPPDAP_SRC_DIR}/protocol_types.cpp |
| 66 | + ${CPPDAP_SRC_DIR}/session.cpp |
| 67 | + ${CPPDAP_SRC_DIR}/socket.cpp |
| 68 | + ${CPPDAP_SRC_DIR}/typeof.cpp |
| 69 | +) |
| 70 | + |
| 71 | +########################################################### |
| 72 | +# OS libraries |
| 73 | +########################################################### |
| 74 | +if(CMAKE_SYSTEM_NAME MATCHES "Windows") |
| 75 | + set(CPPDAP_OS_LIBS WS2_32) |
| 76 | +elseif(CMAKE_SYSTEM_NAME MATCHES "Linux") |
| 77 | + set(CPPDAP_OS_LIBS pthread) |
| 78 | +elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") |
| 79 | + set(CPPDAP_OS_LIBS) |
| 80 | +endif() |
| 81 | + |
| 82 | +########################################################### |
| 83 | +# Functions |
| 84 | +########################################################### |
| 85 | +function(cppdap_set_target_options target) |
| 86 | + # Enable all warnings |
| 87 | + if(MSVC) |
| 88 | + target_compile_options(${target} PRIVATE "-W4") |
| 89 | + else() |
| 90 | + target_compile_options(${target} PRIVATE "-Wall") |
| 91 | + endif() |
| 92 | + |
| 93 | + # Disable specific, pedantic warnings |
| 94 | + if(MSVC) |
| 95 | + target_compile_options(${target} PRIVATE "-D_CRT_SECURE_NO_WARNINGS") |
| 96 | + endif() |
| 97 | + |
| 98 | + # Treat all warnings as errors |
| 99 | + if(CPPDAP_WARNINGS_AS_ERRORS) |
| 100 | + if(MSVC) |
| 101 | + target_compile_options(${target} PRIVATE "/WX") |
| 102 | + else() |
| 103 | + target_compile_options(${target} PRIVATE "-Werror") |
| 104 | + endif() |
| 105 | + endif(CPPDAP_WARNINGS_AS_ERRORS) |
| 106 | + |
| 107 | + if(CPPDAP_ASAN) |
| 108 | + target_compile_options(${target} PUBLIC "-fsanitize=address") |
| 109 | + target_link_libraries(${target} "-fsanitize=address") |
| 110 | + elseif(CPPDAP_MSAN) |
| 111 | + target_compile_options(${target} PUBLIC "-fsanitize=memory") |
| 112 | + target_link_libraries(${target} "-fsanitize=memory") |
| 113 | + elseif(CPPDAP_TSAN) |
| 114 | + target_compile_options(${target} PUBLIC "-fsanitize=thread") |
| 115 | + target_link_libraries(${target} "-fsanitize=thread") |
| 116 | + endif() |
| 117 | + |
| 118 | + # Error on undefined symbols |
| 119 | + # if(NOT MSVC) |
| 120 | + # target_compile_options(${target} PRIVATE "-Wl,--no-undefined") |
| 121 | + # endif() |
| 122 | + |
| 123 | + target_include_directories(${target} PRIVATE ${CPPDAP_INCLUDE_DIR}) |
| 124 | +endfunction(cppdap_set_target_options) |
| 125 | + |
| 126 | +########################################################### |
| 127 | +# Targets |
| 128 | +########################################################### |
| 129 | + |
| 130 | +# dap |
| 131 | +add_library(cppdap STATIC ${CPPDAP_LIST}) |
| 132 | +set_target_properties(cppdap PROPERTIES |
| 133 | + POSITION_INDEPENDENT_CODE 1 |
| 134 | +) |
| 135 | + |
| 136 | +target_include_directories(cppdap PRIVATE "${JSON_DIR}/include/") |
| 137 | + |
| 138 | +cppdap_set_target_options(cppdap) |
| 139 | + |
| 140 | +target_link_libraries(cppdap "${CPPDAP_OS_LIBS}") |
| 141 | + |
| 142 | +# install |
| 143 | +if(CPPDAP_INSTALL) |
| 144 | + include(GNUInstallDirs) |
| 145 | + |
| 146 | + install(DIRECTORY ${CPPDAP_INCLUDE_DIR}/cppdap |
| 147 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 148 | + USE_SOURCE_PERMISSIONS |
| 149 | + ) |
| 150 | + |
| 151 | + install(TARGETS cppdap |
| 152 | + EXPORT cppdap-targets |
| 153 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 154 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 155 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
| 156 | + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 157 | + ) |
| 158 | + |
| 159 | + install(EXPORT cppdap-targets |
| 160 | + FILE cppdap-config.cmake |
| 161 | + NAMESPACE cppdap:: |
| 162 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cppdap |
| 163 | + ) |
| 164 | +endif(CPPDAP_INSTALL) |
| 165 | + |
| 166 | +# tests |
| 167 | +if(CPPDAP_BUILD_TESTS) |
| 168 | + set(DAP_TEST_LIST |
| 169 | + ${CPPDAP_SRC_DIR}/any_test.cpp |
| 170 | + ${CPPDAP_SRC_DIR}/chan_test.cpp |
| 171 | + ${CPPDAP_SRC_DIR}/content_stream_test.cpp |
| 172 | + ${CPPDAP_SRC_DIR}/dap_test.cpp |
| 173 | + ${CPPDAP_SRC_DIR}/json_serializer_test.cpp |
| 174 | + ${CPPDAP_SRC_DIR}/network_test.cpp |
| 175 | + ${CPPDAP_SRC_DIR}/optional_test.cpp |
| 176 | + ${CPPDAP_SRC_DIR}/session_test.cpp |
| 177 | + ${CPPDAP_SRC_DIR}/variant_test.cpp |
| 178 | + ${GOOGLETEST_DIR}/googletest/src/gtest-all.cc |
| 179 | + ) |
| 180 | + |
| 181 | + set(DAP_TEST_INCLUDE_DIR |
| 182 | + ${GOOGLETEST_DIR}/googlemock/include/ |
| 183 | + ${GOOGLETEST_DIR}/googletest/ |
| 184 | + ${GOOGLETEST_DIR}/googletest/include/ |
| 185 | + ${JSON_DIR}/include/ |
| 186 | + ) |
| 187 | + |
| 188 | + add_executable(cppdap-unittests ${DAP_TEST_LIST}) |
| 189 | + |
| 190 | + set_target_properties(cppdap-unittests PROPERTIES |
| 191 | + INCLUDE_DIRECTORIES "${DAP_TEST_INCLUDE_DIR}" |
| 192 | + FOLDER "Tests" |
| 193 | + ) |
| 194 | + |
| 195 | + if(MSVC) |
| 196 | + # googletest emits warning C4244: 'initializing': conversion from 'double' to 'testing::internal::BiggestInt', possible loss of data |
| 197 | + target_compile_options(cppdap-unittests PRIVATE "/wd4244") |
| 198 | + endif() |
| 199 | + |
| 200 | + cppdap_set_target_options(cppdap-unittests) |
| 201 | + |
| 202 | + target_link_libraries(cppdap-unittests cppdap "${CPPDAP_OS_LIBS}") |
| 203 | +endif(CPPDAP_BUILD_TESTS) |
| 204 | + |
| 205 | +# examples |
| 206 | +if(CPPDAP_BUILD_EXAMPLES) |
| 207 | + function(build_example target) |
| 208 | + add_executable(${target} "${CMAKE_CURRENT_SOURCE_DIR}/examples/${target}.cpp") |
| 209 | + set_target_properties(${target} PROPERTIES |
| 210 | + FOLDER "Examples" |
| 211 | + ) |
| 212 | + cppdap_set_target_options(${target}) |
| 213 | + target_link_libraries(${target} cppdap "${CPPDAP_OS_LIBS}") |
| 214 | + |
| 215 | + if(CPPDAP_INSTALL_VSCODE_EXAMPLES) |
| 216 | + set(extroot "$ENV{HOME}/.vscode/extensions") |
| 217 | + if(EXISTS ${extroot}) |
| 218 | + set(extdir "${extroot}/google.cppdap-example-${target}-1.0.0") |
| 219 | + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/examples/vscode/package.json ${extdir}/package.json) |
| 220 | + add_custom_command(TARGET ${target} |
| 221 | + POST_BUILD |
| 222 | + COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${extdir}) |
| 223 | + else() |
| 224 | + message(WARNING "Could not install vscode example extension as '${extroot}' does not exist") |
| 225 | + endif() |
| 226 | + endif(CPPDAP_INSTALL_VSCODE_EXAMPLES) |
| 227 | + endfunction(build_example) |
| 228 | + |
| 229 | + build_example(hello_debugger) |
| 230 | + |
| 231 | +endif(CPPDAP_BUILD_EXAMPLES) |
0 commit comments