|
| 1 | +cmake_minimum_required(VERSION 3.15) |
| 2 | + |
| 3 | +set(BASE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) |
| 4 | + |
| 5 | +set(USE_ASAN OFF CACHE BOOL "Build with Address Sanitizer") |
| 6 | + |
| 7 | +# common compiler settings |
| 8 | + |
| 9 | +# NOTE: MBEDTLS_CONFIG_FILE is not only required to compile the mbedtls subset in others, but also |
| 10 | +# when their headers are included while compiling libModSecurity |
| 11 | +add_compile_definitions(WIN32 _CRT_SECURE_NO_WARNINGS MBEDTLS_CONFIG_FILE="mbed-tls-config.h") |
| 12 | + |
| 13 | +# set standards conformance preprocessor & compiler to align with cross-compiled codebase |
| 14 | +# NOTE: otherwise visual c++'s default compiler/preprocessor behaviour generates C4067 warnings |
| 15 | +# (unexpected tokens following preprocessor directive - expected a newline) |
| 16 | +add_compile_options(/Zc:preprocessor /permissive-) |
| 17 | + |
| 18 | +if(USE_ASAN) |
| 19 | + add_compile_options(/fsanitize=address) |
| 20 | + add_link_options(/INFERASANLIBS /INCREMENTAL:no) |
| 21 | +endif() |
| 22 | + |
| 23 | +# libinjection |
| 24 | + |
| 25 | +project(libinjection C) |
| 26 | + |
| 27 | +add_library(libinjection STATIC ${BASE_DIR}/others/libinjection/src/libinjection_sqli.c ${BASE_DIR}/others/libinjection/src/libinjection_xss.c ${BASE_DIR}/others/libinjection/src/libinjection_html5.c) |
| 28 | + |
| 29 | +# mbedtls |
| 30 | + |
| 31 | +project(mbedtls C) |
| 32 | + |
| 33 | +add_library(mbedtls STATIC ${BASE_DIR}/others/mbedtls/base64.c ${BASE_DIR}/others/mbedtls/sha1.c ${BASE_DIR}/others/mbedtls/md5.c) |
| 34 | + |
| 35 | +target_include_directories(mbedtls PRIVATE ${BASE_DIR}/others) |
| 36 | + |
| 37 | +# |
| 38 | +# libModSecurity |
| 39 | +# |
| 40 | + |
| 41 | +project(libModSecurity |
| 42 | + VERSION |
| 43 | + 3.0.12 |
| 44 | + LANGUAGES |
| 45 | + CXX |
| 46 | +) |
| 47 | + |
| 48 | +set(CMAKE_CXX_STANDARD 17) |
| 49 | +set(CMAKE_CXX_STANDARD_REQUIRED On) |
| 50 | +set(CMAKE_CXX_EXTENSIONS Off) |
| 51 | + |
| 52 | +set(PACKAGE_BUGREPORT "security@modsecurity.org") |
| 53 | +set(PACKAGE_NAME "modsecurity") |
| 54 | +set(PACKAGE_VERSION "${PROJECT_VERSION}") |
| 55 | +set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") |
| 56 | +set(PACKAGE_TARNAME "${PACKAGE_NAME}") |
| 57 | + |
| 58 | +set(HAVE_GEOIP 0) # should always be zero, no conan package available |
| 59 | +set(HAVE_LMDB 1) |
| 60 | +set(HAVE_LUA 1) |
| 61 | +set(HAVE_LIBXML2 1) |
| 62 | +set(HAVE_MAXMIND 1) |
| 63 | +set(HAVE_SSDEEP 0) # should always be zero, no conan package available |
| 64 | +set(HAVE_YAJL 1) # should always be one, mandatory dependency |
| 65 | +set(HAVE_CURL 1) |
| 66 | + |
| 67 | +include(${CMAKE_CURRENT_LIST_DIR}/ConfigureChecks.cmake) |
| 68 | + |
| 69 | +configure_file(config.h.cmake ${BASE_DIR}/src/config.h) |
| 70 | + |
| 71 | +find_package(PCRE2 REQUIRED) |
| 72 | +find_package(PThreads4W REQUIRED) |
| 73 | +find_package(Poco REQUIRED) |
| 74 | +find_package(dirent REQUIRED) # used only by tests (check dirent::dirent refernces) |
| 75 | + |
| 76 | +macro(include_package package flag) |
| 77 | + if(${flag}) |
| 78 | + find_package(${package} REQUIRED) |
| 79 | + endif() |
| 80 | +endmacro() |
| 81 | + |
| 82 | +include_package(yajl HAVE_YAJL) |
| 83 | +include_package(libxml2 HAVE_LIBXML2) |
| 84 | +include_package(lua HAVE_LUA) |
| 85 | +include_package(CURL HAVE_CURL) |
| 86 | +include_package(lmdb HAVE_LMDB) |
| 87 | +include_package(maxminddb HAVE_MAXMIND) |
| 88 | + |
| 89 | +# library |
| 90 | +# |
| 91 | + |
| 92 | +# NOTE: required to generate libModSecurity's import library (libModSecurity.lib), used by tests to link with shared library |
| 93 | +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) |
| 94 | + |
| 95 | +file(GLOB_RECURSE libModSecuritySources ${BASE_DIR}/src/*.cc) |
| 96 | + |
| 97 | +add_library(libModSecurity SHARED ${libModSecuritySources}) |
| 98 | + |
| 99 | +target_compile_definitions(libModSecurity PRIVATE WITH_PCRE2) |
| 100 | +target_include_directories(libModSecurity PRIVATE ${BASE_DIR} ${BASE_DIR}/headers ${BASE_DIR}/others) |
| 101 | +target_link_libraries(libModSecurity PRIVATE pcre2::pcre2 pthreads4w::pthreads4w libinjection mbedtls Poco::Poco Iphlpapi.lib) |
| 102 | + |
| 103 | +macro(add_package_dependency project compile_definition link_library flag) |
| 104 | + if(${flag}) |
| 105 | + target_compile_definitions(${project} PRIVATE ${compile_definition}) |
| 106 | + target_link_libraries(${project} PRIVATE ${link_library}) |
| 107 | + endif() |
| 108 | +endmacro() |
| 109 | + |
| 110 | +add_package_dependency(libModSecurity WITH_YAJL yajl::yajl HAVE_YAJL) |
| 111 | +add_package_dependency(libModSecurity WITH_LIBXML2 LibXml2::LibXml2 HAVE_LIBXML2) |
| 112 | +add_package_dependency(libModSecurity WITH_LUA lua::lua HAVE_LUA) |
| 113 | +if(HAVE_LUA) |
| 114 | + target_compile_definitions(libModSecurity PRIVATE WITH_LUA_5_4) |
| 115 | +endif() |
| 116 | +add_package_dependency(libModSecurity MSC_WITH_CURL CURL::libcurl HAVE_CURL) |
| 117 | +add_package_dependency(libModSecurity WITH_LMDB lmdb::lmdb HAVE_LMDB) |
| 118 | +add_package_dependency(libModSecurity WITH_MAXMIND maxminddb::maxminddb HAVE_MAXMIND) |
| 119 | + |
| 120 | +# tests |
| 121 | +# |
| 122 | + |
| 123 | +project(libModSecurityTests) |
| 124 | + |
| 125 | +function(setTestTargetProperties executable) |
| 126 | + target_compile_definitions(${executable} PRIVATE WITH_PCRE2) |
| 127 | + target_include_directories(${executable} PRIVATE ${BASE_DIR} ${BASE_DIR}/headers) |
| 128 | + target_link_libraries(${executable} PRIVATE libModSecurity pcre2::pcre2 dirent::dirent) |
| 129 | + add_package_dependency(${executable} WITH_YAJL yajl::yajl HAVE_YAJL) |
| 130 | +endfunction() |
| 131 | + |
| 132 | +# unit tests |
| 133 | +file(GLOB unitTestSources ${BASE_DIR}/test/unit/*.cc) |
| 134 | +add_executable(unit_tests ${unitTestSources}) |
| 135 | +setTestTargetProperties(unit_tests) |
| 136 | +target_compile_options(unit_tests PRIVATE /wd4805) |
| 137 | + |
| 138 | +# regression tests |
| 139 | +file(GLOB regressionTestsSources ${BASE_DIR}/test/regression/*.cc) |
| 140 | +add_executable(regression_tests ${regressionTestsSources}) |
| 141 | +setTestTargetProperties(regression_tests) |
| 142 | + |
| 143 | +macro(add_regression_test_capability compile_definition flag) |
| 144 | + if(${flag}) |
| 145 | + target_compile_definitions(regression_tests PRIVATE ${compile_definition}) |
| 146 | + endif() |
| 147 | +endmacro() |
| 148 | + |
| 149 | +add_regression_test_capability(WITH_LUA HAVE_LUA) |
| 150 | +add_regression_test_capability(WITH_CURL HAVE_CURL) |
| 151 | +add_regression_test_capability(WITH_LMDB HAVE_LMDB) |
| 152 | +add_regression_test_capability(WITH_MAXMIND HAVE_MAXMIND) |
| 153 | + |
| 154 | +# benchmark |
| 155 | +add_executable(benchmark ${BASE_DIR}/test/benchmark/benchmark.cc) |
| 156 | +setTestTargetProperties(benchmark) |
| 157 | + |
| 158 | +# rules_optimization |
| 159 | +add_executable(rules_optimization ${BASE_DIR}/test/optimization/optimization.cc) |
| 160 | +setTestTargetProperties(rules_optimization) |
| 161 | + |
| 162 | + |
| 163 | +# examples |
| 164 | +# |
| 165 | + |
| 166 | +project(libModSecurityExamples) |
| 167 | + |
| 168 | +function(setExampleTargetProperties executable) |
| 169 | + target_include_directories(${executable} PRIVATE ${BASE_DIR} ${BASE_DIR}/headers) |
| 170 | + target_link_libraries(${executable} PRIVATE libModSecurity) |
| 171 | +endfunction() |
| 172 | + |
| 173 | +# simple_example_using_c |
| 174 | +add_executable(simple_example_using_c ${BASE_DIR}/examples/simple_example_using_c/test.c) |
| 175 | +setExampleTargetProperties(simple_example_using_c) |
| 176 | + |
| 177 | +# using_bodies_in_chunks |
| 178 | +add_executable(using_bodies_in_chunks ${BASE_DIR}/examples/using_bodies_in_chunks/simple_request.cc) |
| 179 | +setExampleTargetProperties(using_bodies_in_chunks) |
| 180 | + |
| 181 | +# reading_logs_via_rule_message |
| 182 | +add_executable(reading_logs_via_rule_message ${BASE_DIR}/examples/reading_logs_via_rule_message/simple_request.cc) |
| 183 | +setExampleTargetProperties(reading_logs_via_rule_message) |
| 184 | +target_link_libraries(reading_logs_via_rule_message PRIVATE libModSecurity pthreads4w::pthreads4w) |
| 185 | + |
| 186 | +# reading_logs_with_offset |
| 187 | +add_executable(reading_logs_with_offset ${BASE_DIR}/examples/reading_logs_with_offset/read.cc) |
| 188 | +setExampleTargetProperties(reading_logs_with_offset) |
| 189 | + |
| 190 | +# tools |
| 191 | +# |
| 192 | + |
| 193 | +# rules_check |
| 194 | +add_executable(rules_check ${BASE_DIR}/tools/rules-check/rules-check.cc) |
| 195 | +target_include_directories(rules_check PRIVATE ${BASE_DIR} ${BASE_DIR}/headers) |
| 196 | +target_link_libraries(rules_check PRIVATE libModSecurity) |
0 commit comments