From b11072a66d45567c0b9d97b39835adeb10e28e3d Mon Sep 17 00:00:00 2001
From: Dmitriy Nesterov <dim.nesterov2015@gmail.com>
Date: Sun, 25 Sep 2022 22:44:22 +0300
Subject: [PATCH] cmake: add dependencies for LuaJIT and SQL fuzzers

Added Google's 'libprotobuf-mutator' and 'protobuf' libraries
for developing grammar-based LuaJIT and SQL fuzzers based on
LibFuzzer.

It is needed to build protobuf module from source because
by default, the system-installed version of protobuf is used
by libprotobuf-mutator, and this version can be too old.

Part of #4823

NO_CHANGELOG=<dependencies>
NO_DOC=<dependencies>
NO_TEST=<dependencies>
---
 cmake/ProtobufMutator.cmake | 91 +++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
 create mode 100644 cmake/ProtobufMutator.cmake

diff --git a/cmake/ProtobufMutator.cmake b/cmake/ProtobufMutator.cmake
new file mode 100644
index 0000000000..da0a4a2fe9
--- /dev/null
+++ b/cmake/ProtobufMutator.cmake
@@ -0,0 +1,91 @@
+set(LPM_TARGET external.protobuf_mutator)
+set(LPM_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/${LPM_TARGET})
+set(LPM_REPO https://github.com/google/libprotobuf-mutator.git)
+
+set(LPM_INCLUDE_DIRS ${LPM_INSTALL_DIR}/include)
+include_directories(${LPM_INCLUDE_DIRS})
+include_directories(${LPM_INCLUDE_DIRS}/libprotobuf-mutator)
+include_directories(${CMAKE_CURRENT_BINARY_DIR})
+
+# This exact order due to dependencies of libraries.
+set(LPM_LIBRARIES protobuf-mutator-libfuzzer protobuf-mutator)
+
+foreach(lib ${LPM_LIBRARIES})
+  set(LIB_PATH ${LPM_INSTALL_DIR}/lib/lib${lib}.a)
+  list(APPEND LPM_BUILD_BYPRODUCTS ${LIB_PATH})
+  add_library(${lib} STATIC IMPORTED)
+  set_property(TARGET ${lib} PROPERTY IMPORTED_LOCATION
+               ${LIB_PATH})
+  add_dependencies(${lib} ${LPM_TARGET})
+endforeach(lib)
+
+# Part of protobuf.cmake moved here due to build problems of
+# libprotobuf-mutator with separate protobuf installation
+
+# FindProtobuf is present in newer versions of CMake:
+# https://cmake.org/cmake/help/latest/module/FindProtobuf.html
+# We only need protobuf_generate_cpp from FindProtobuf, and the rest will be
+# downloaded with LPM.
+include (FindProtobuf)
+
+set(PROTOBUF_TARGET external.protobuf)
+set(PROTOBUF_INSTALL_DIR ${LPM_INSTALL_DIR}/src/${LPM_TARGET}-build/${PROTOBUF_TARGET})
+
+set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INSTALL_DIR}/include)
+include_directories(${PROTOBUF_INCLUDE_DIRS})
+
+set(PROTOBUF_LIBRARIES protobufd)
+
+foreach(lib ${PROTOBUF_LIBRARIES})
+  set(LIB_PROTOBUF_PATH ${PROTOBUF_INSTALL_DIR}/lib/lib${lib}.a)
+  list(APPEND PROTOBUF_BUILD_BYPRODUCTS ${LIB_PROTOBUF_PATH})
+
+  add_library(${lib} STATIC IMPORTED)
+  set_property(TARGET ${lib} PROPERTY IMPORTED_LOCATION
+               ${LIB_PROTOBUF_PATH})
+  add_dependencies(${lib} ${PROTOBUF_TARGET} ${LPM_TARGET})
+endforeach(lib)
+
+set(PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_INSTALL_DIR}/bin/protoc)
+list(APPEND PROTOBUF_BUILD_BYPRODUCTS ${PROTOBUF_PROTOC_EXECUTABLE})
+
+if(${CMAKE_VERSION} VERSION_LESS "3.10.0")
+  set(PROTOBUF_PROTOC_TARGET protoc)
+else()
+  set(PROTOBUF_PROTOC_TARGET protobuf::protoc)
+endif()
+
+if(NOT TARGET ${PROTOBUF_PROTOC_TARGET})
+  add_executable(${PROTOBUF_PROTOC_TARGET} IMPORTED)
+endif()
+set_property(TARGET ${PROTOBUF_PROTOC_TARGET} PROPERTY IMPORTED_LOCATION
+             ${PROTOBUF_PROTOC_EXECUTABLE})
+add_dependencies(${PROTOBUF_PROTOC_TARGET} ${PROTOBUF_TARGET} ${LPM_TARGET})
+
+include (ExternalProject)
+ExternalProject_Add(${LPM_TARGET}
+  PREFIX ${LPM_TARGET}
+  GIT_REPOSITORY ${LPM_REPO}
+  GIT_TAG a304ec4
+  UPDATE_COMMAND ""
+  CONFIGURE_COMMAND ${CMAKE_COMMAND} ${LPM_INSTALL_DIR}/src/${LPM_TARGET}
+        -G${CMAKE_GENERATOR}
+        -DCMAKE_INSTALL_PREFIX=${LPM_INSTALL_DIR}
+        -DCMAKE_BUILD_TYPE=Debug
+        -DCMAKE_POSITION_INDEPENDENT_CODE=ON
+        -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
+        -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
+        -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
+        -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
+        -DLIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF=ON
+        -DLIB_PROTO_MUTATOR_TESTING=OFF
+        -DLIB_PROTO_MUTATOR_WITH_ASAN=${ENABLE_ASAN}
+  BUILD_BYPRODUCTS ${LPM_BUILD_BYPRODUCTS} ${PROTOBUF_BUILD_BYPRODUCTS}
+)
+
+# CMake 3.6+: All input and output variables use the Protobuf_ prefix.
+# Variables with PROTOBUF_ prefix are still supported for compatibility.
+# See https://cmake.org/cmake/help/latest/module/FindProtobuf.html
+set(Protobuf_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIRS})
+set(Protobuf_LIBRARIES ${PROTOBUF_LIBRARIES})
+set(Protobuf_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
-- 
GitLab