Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libprotobuf-mutator] Compatible with Conan 2.x #24163

Merged
merged 10 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions recipes/libprotobuf-mutator/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
cmake_minimum_required(VERSION 2.8.11)
project(cmake_wrapper)
cmake_minimum_required(VERSION 3.15)
project(cmake_wrapper CXX)

include(conanbuildinfo.cmake)
conan_basic_setup()
find_package(Protobuf REQUIRED)
find_package(absl CONFIG REQUIRED)

add_subdirectory(source_subfolder)
add_subdirectory(src)

if(MSVC)
# Should be added because of
# https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initonceexecuteonce
add_definitions(-D_WIN32_WINNT=0x0600)
endif()
target_link_libraries(protobuf-mutator PUBLIC protobuf::libprotobuf absl::strings)
6 changes: 3 additions & 3 deletions recipes/libprotobuf-mutator/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sources:
"cci.20210831":
url: "https://github.com/google/libprotobuf-mutator/archive/ffd86a32874e5c08a143019aad1aaf0907294c9f.zip"
sha256: "14f595863452808483776ce1964209db00cdac79e649b31eb4d4b06ca72912d8"
"1.3":
url: "https://github.com/google/libprotobuf-mutator/archive/refs/tags/v1.3.tar.gz"
sha256: "1ee3473a6b0274494fce599539605bb19305c0efadc62b58d645812132c31baa"
159 changes: 103 additions & 56 deletions recipes/libprotobuf-mutator/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, replace_in_file, rmdir
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime
from conan.tools.env import VirtualBuildEnv
from conan.tools.scm import Version
from conan.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.54.0"


class LibProtobufMutatorConan(ConanFile):
Expand All @@ -13,76 +19,117 @@ class LibProtobufMutatorConan(ConanFile):
description = "A library to randomly mutate protobuffers."
topics = ("test", "fuzzing", "protobuf")
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
exports_sources = ["CMakeLists.txt"]

_cmake = None
package_type = "library"
options = {
"shared": [True, False],
"fPIC": [True, False]
}
default_options = {
"shared": False,
"fPIC": True
}

@property
def _source_subfolder(self):
return "source_subfolder"
def _min_cppstd(self):
return 14

@property
def _build_subfolder(self):
return "build_subfolder"
def _compilers_minimum_version(self):
return {
"apple-clang": "10",
"clang": "7",
"gcc": "5",
"msvc": "191",
"Visual Studio": "15",
}

def export_sources(self):
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.get_safe("shared"):
self.options.rm_safe("fPIC")
if is_msvc(self):
self.options.rm_safe("shared")
self.package_type = "static-library"

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
self.requires("protobuf/3.17.1")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)
# Protobuf headers are required by public src/binary_format.h and
self.requires("protobuf/4.25.3", transitive_headers=True)
# Abseil headers are required by public src/field_instance.h
self.requires("abseil/20240116.2")

def validate(self):
if self.settings.compiler != "clang":
raise ConanInvalidConfiguration("Only clang allowed")
if self.settings.compiler.libcxx != "libstdc++11":
raise ConanInvalidConfiguration("Requires either compiler.libcxx=libstdc++11")
if self.settings.compiler.cppstd:
tools.check_min_cppstd(self, 11)
check_min_cppstd(self, self._min_cppstd)
minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support.")

def build_requirements(self):
self.tool_requires("cmake/[>=3.24 <4]")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def _patch_sources(self):
tools.replace_in_file(
os.path.join(self._source_subfolder, 'CMakeLists.txt'),
"""include_directories(${PROTOBUF_INCLUDE_DIRS})""",
"""include_directories(${protobuf_INCLUDE_DIRS})""")
tools.replace_in_file(
os.path.join(self._source_subfolder, 'CMakeLists.txt'),
"""set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)""",
"""# (disabled by conan) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)""")
tools.replace_in_file(
os.path.join(self._source_subfolder, 'CMakeLists.txt'),
"""add_subdirectory(examples EXCLUDE_FROM_ALL)""",
"""# (disabled by conan) add_subdirectory(examples EXCLUDE_FROM_ALL)""")

def _configure_cmake(self):
if self._cmake:
return self._cmake

self._cmake = CMake(self)
self._cmake.definitions["LIB_PROTO_MUTATOR_TESTING"] = "OFF"
self._cmake.definitions["LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF"] = "OFF"
self._cmake.definitions["LIB_PROTO_MUTATOR_WITH_ASAN"] = "OFF"
self._cmake.definitions["LIB_PROTO_MUTATOR_FUZZER_LIBRARIES"] = ""
# todo: check option(LIB_PROTO_MUTATOR_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
# Preserves Conan as dependency manager
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
"set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)",
"",
)
# Fix libprotobuf-mutator.pc installation origin path
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
"${CMAKE_BINARY_DIR}/libprotobuf-mutator.pc",
"${CMAKE_CURRENT_BINARY_DIR}/libprotobuf-mutator.pc",
)
# Do not include examples when running CMake configure to avoid more dependencies
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
"add_subdirectory(examples EXCLUDE_FROM_ALL)",
"",
)

def generate(self):
tc = VirtualBuildEnv(self)
tc.generate()
tc = CMakeToolchain(self)
tc.variables["LIB_PROTO_MUTATOR_TESTING"] = False
tc.variables["LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF"] = False
tc.variables["LIB_PROTO_MUTATOR_WITH_ASAN"] = False
tc.variables["PKG_CONFIG_PATH"] = "share"
if is_msvc(self):
tc.variables["LIB_PROTO_MUTATOR_MSVC_STATIC_RUNTIME"] = is_msvc_static_runtime(self)
tc.generate()
tc = CMakeDeps(self)
tc.generate()

def build(self):
self._patch_sources()
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
cmake.build()

def package(self):
self.copy(pattern="LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
copy(self, pattern="LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "OFF"))
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "share"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.names["cmake_find_package"] = "libprotobuf-mutator"
self.cpp_info.names["cmake_find_package_multi"] = "libprotobuf-mutator"

self.cpp_info.libs = ['protobuf-mutator-libfuzzer', 'protobuf-mutator']
self.cpp_info.includedirs.append(os.path.join("include", "libprotobuf-mutator"))
self.cpp_info.components["mutator"].libs = ["protobuf-mutator"]
self.cpp_info.components["mutator"].set_property("cmake_target_name", "libprotobuf-mutator::protobuf-mutator")
self.cpp_info.components["mutator"].includedirs.append("include/libprotobuf-mutator")
self.cpp_info.components["mutator"].requires = ["protobuf::libprotobuf", "abseil::absl_strings"]

self.cpp_info.components["fuzzer"].libs = ['protobuf-mutator-libfuzzer']
self.cpp_info.components["fuzzer"].set_property("cmake_target_name", "libprotobuf-mutator::protobuf-mutator-libfuzzer")
self.cpp_info.components["fuzzer"].includedirs.append("include/libprotobuf-mutator")
self.cpp_info.components["fuzzer"].requires = ["mutator", "protobuf::libprotobuf"]
21 changes: 5 additions & 16 deletions recipes/libprotobuf-mutator/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES CXX)


include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
find_package(libprotobuf-mutator REQUIRED CONFIG)


protobuf_generate_cpp(
MSG_PROTO_SRCS
MSG_PROTO_HDRS
msg.proto
)


add_executable(${PROJECT_NAME} ${MSG_PROTO_SRCS} test_package.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${PROJECT_NAME} libprotobuf-mutator::libprotobuf-mutator)
add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE libprotobuf-mutator::protobuf-mutator libprotobuf-mutator::protobuf-mutator-libfuzzer)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)
19 changes: 14 additions & 5 deletions recipes/libprotobuf-mutator/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
11 changes: 0 additions & 11 deletions recipes/libprotobuf-mutator/all/test_package/msg.proto

This file was deleted.

24 changes: 6 additions & 18 deletions recipes/libprotobuf-mutator/all/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
#include <cmath>
#include <iostream>
#include <cstdlib>

#include "msg.pb.h"
#include <libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h>
#include <libprotobuf-mutator/src/mutator.h>

DEFINE_PROTO_FUZZER(const libfuzzer_example::Msg& message) {
protobuf_mutator::protobuf::FileDescriptorProto file;

// Emulate a bug.
if (message.optional_uint64() == std::hash<std::string>{}(message.optional_string()) &&
message.optional_string() == "abcdefghijklmnopqrstuvwxyz" &&
!std::isnan(message.optional_float()) &&
std::fabs(message.optional_float()) > 1000 &&
message.any().UnpackTo(&file) && !file.name().empty())
{
std::cerr << message.DebugString() << "\n";
}
}

int main() {
return 0;
protobuf_mutator::Mutator mutator{};
const auto seed_value{42};
mutator.Seed(seed_value);
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion recipes/libprotobuf-mutator/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
versions:
"cci.20210831":
"1.3":
folder: all
Loading