aboutsummaryrefslogtreecommitdiff
path: root/CMakeLists.txt
blob: 13787632ac574352be24ea88faced4dd39ad9aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# cmake -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=include-what-you-use -B build . && cmake --build build -j
cmake_minimum_required(VERSION 3.24)

set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# For whatever reason, the C++ stdlib is broken on my Mac.
#
#   % echo "#include <vector>" | c++ -x c++ -
#   <stdin>:1:10: fatal error: 'vector' file not found
#       1 | #include <vector>
#         |          ^~~~~~~~
#   1 error generated.
if(APPLE)
  set(CMAKE_CXX_FLAGS "-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1")
endif()

project(Webdir)
option(WEBDIR_BUILD_TESTS "Build unit tests" OFF)

include(FetchContent)
FetchContent_Declare(
  libmw
  GIT_REPOSITORY https://github.com/MetroWind/libmw.git
)

FetchContent_Declare(
  cxxopts
  GIT_REPOSITORY https://github.com/jarro2783/cxxopts.git
  GIT_TAG v3.1.1
)

FetchContent_Declare(
  ryml
  GIT_REPOSITORY https://github.com/biojppm/rapidyaml.git
  GIT_TAG
  GIT_SHALLOW FALSE  # ensure submodules are checked out
)

FetchContent_Declare(
  spdlog
  GIT_REPOSITORY https://github.com/gabime/spdlog.git
  GIT_TAG v1.12.0
)

FetchContent_Declare(
  json
  GIT_REPOSITORY https://github.com/nlohmann/json.git
  GIT_TAG v3.11.3
)

FetchContent_Declare(
  inja
  GIT_REPOSITORY https://github.com/pantor/inja.git
  GIT_TAG main
)

set(SPDLOG_USE_STD_FORMAT ON)
set(LIBMW_BUILD_URL ON)
set(LIBMW_BUILD_SQLITE ON)
set(LIBMW_BUILD_HTTP_SERVER ON)
set(INJA_USE_EMBEDDED_JSON FALSE)
set(INJA_BUILD_TESTS FALSE)
FetchContent_MakeAvailable(libmw ryml spdlog cxxopts json inja)

if(WEBDIR_BUILD_TESTS)
  FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
  )
  FetchContent_MakeAvailable(googletest)
endif()

set(SOURCE_FILES
  src/app.cpp
  src/app.hpp
  src/config.cpp
  src/config.hpp
  src/data.cpp
  src/data.hpp
)

set(LIBS
  cxxopts
  mw::mw
  mw::url
  mw::sqlite
  mw::http-server
  ryml::ryml
  spdlog::spdlog
)

set(INCLUDES
  ${libmw_SOURCE_DIR}/includes
  ${json_SOURCE_DIR}/single_include
  ${inja_SOURCE_DIR}/single_include/inja
)

add_executable(webdir ${SOURCE_FILES} src/main.cpp)
set_property(TARGET webdir PROPERTY CXX_STANDARD 23)
set_property(TARGET webdir PROPERTY CXX_EXTENSIONS FALSE)

set_property(TARGET webdir PROPERTY COMPILE_WARNING_AS_ERROR TRUE)
target_compile_options(webdir PRIVATE -Wall -Wextra -Wpedantic)
target_include_directories(webdir PRIVATE ${INCLUDES})
target_link_libraries(webdir PRIVATE ${LIBS})

if(WEBDIR_BUILD_TESTS)
  set(TEST_FILES
    src/data_mock.hpp
    src/data_test.cpp
    src/app_test.cpp
  )

  # ctest --test-dir build
  add_executable(webdir_test ${SOURCE_FILES} ${TEST_FILES})
  set_property(TARGET webdir_test PROPERTY CXX_STANDARD 23)
  set_property(TARGET webdir_test PROPERTY COMPILE_WARNING_AS_ERROR TRUE)
  target_compile_options(webdir_test PRIVATE -Wall -Wextra -Wpedantic)
  target_include_directories(webdir_test PRIVATE
    ${INCLUDES}
    ${googletest_SOURCE_DIR}/googletest/include
    ${googletest_SOURCE_DIR}/googlemock/include
  )

  target_link_libraries(webdir_test PRIVATE
    ${LIBS}
    GTest::gtest_main
    GTest::gmock_main
  )

  enable_testing()
  include(GoogleTest)
  gtest_discover_tests(webdir_test
    # Need this so that the unit tests can find the templates.
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()