1 # Copyright 2016 Google Inc. All rights reserved. 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 # http://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 cmake_minimum_required(VERSION 3.5) 16 project(LibProtobufMutator CXX) 17 18 enable_language(C) 19 enable_language(CXX) 20 21 option(LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF 22 "Automatically download working protobuf" OFF) 23 option(LIB_PROTO_MUTATOR_WITH_ASAN "Enable address sanitizer" OFF) 24 set(LIB_PROTO_MUTATOR_FUZZER_LIBRARIES "" CACHE STRING "Fuzzing engine libs") 25 26 # External dependencies 27 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external) 28 29 # External dependencies 30 include(ProcessorCount) 31 include(CheckCCompilerFlag) 32 include(CheckCXXCompilerFlag) 33 34 set(THREADS_PREFER_PTHREAD_FLAG ON) 35 find_package(Threads) 36 37 find_package(LibLZMA) 38 include_directories(${LIBLZMA_INCLUDE_DIRS}) 39 40 find_package(ZLIB) 41 include_directories(${ZLIB_INCLUDE_DIRS}) 42 43 set(CMAKE_CXX_STANDARD 11) 44 45 include_directories(${PROJECT_SOURCE_DIR}) 46 47 set(CMAKE_REQUIRED_FLAGS "-fsanitize=address") 48 check_cxx_compiler_flag(-fsanitize=address LIB_PROTO_MUTATOR_HAS_SANITIZE_ADDRESS) 49 check_cxx_compiler_flag("-fsanitize=address -fsanitize-address-use-after-scope" 50 LIB_PROTO_MUTATOR_HAS_SANITIZE_SCOPE) 51 unset(CMAKE_REQUIRED_FLAGS) 52 53 set(CMAKE_REQUIRED_FLAGS "-fsanitize-coverage=0") 54 check_cxx_compiler_flag(-fsanitize-coverage= LIB_PROTO_MUTATOR_HAS_NO_COVERAGE) 55 unset(CMAKE_REQUIRED_FLAGS) 56 57 set(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link") 58 check_cxx_compiler_flag(-fsanitize=fuzzer-no-link LIB_PROTO_MUTATOR_HAS_SANITIZE_FUZZER) 59 unset(CMAKE_REQUIRED_FLAGS) 60 61 set(CMAKE_REQUIRED_FLAGS "-fno-sanitize=fuzzer") 62 check_cxx_compiler_flag(-fno-sanitize=fuzzer LIB_PROTO_MUTATOR_HAS_NO_SANITIZE_FUZZER) 63 unset(CMAKE_REQUIRED_FLAGS) 64 65 check_cxx_compiler_flag(-Wstring-conversion LIB_PROTO_MUTATOR_HAS_WSTRING_CONVERSION) 66 67 set(EXTRA_FLAGS "-fno-exceptions -Werror -Wall") 68 if (LIB_PROTO_MUTATOR_HAS_WSTRING_CONVERSION) 69 set(EXTRA_FLAGS "${EXTRA_FLAGS} -Wstring-conversion") 70 endif() 71 72 if (LIB_PROTO_MUTATOR_WITH_ASAN) 73 if (LIB_PROTO_MUTATOR_HAS_SANITIZE_ADDRESS) 74 set(EXTRA_FLAGS "${EXTRA_FLAGS} -fsanitize=address") 75 if (LIB_PROTO_MUTATOR_HAS_SANITIZE_SCOPE) 76 set(EXTRA_FLAGS "${EXTRA_FLAGS} -fsanitize-address-use-after-scope") 77 endif() 78 endif() 79 endif() 80 81 # Assume CFLAGS has coverage options if LIB_PROTO_MUTATOR_FUZZER_LIBRARIES was set 82 if ("${LIB_PROTO_MUTATOR_FUZZER_LIBRARIES}" STREQUAL "") 83 if (LIB_PROTO_MUTATOR_HAS_SANITIZE_FUZZER) 84 set(FUZZING_FLAGS "-fsanitize=fuzzer-no-link") 85 set(FUZZING_FLAGS_BINARY "-fsanitize=fuzzer") 86 endif() 87 if (LIB_PROTO_MUTATOR_HAS_SANITIZE_NO_FUZZER) 88 set(NO_FUZZING_FLAGS "-fno-sanitize=fuzzer") 89 endif() 90 endif() 91 if (LIB_PROTO_MUTATOR_HAS_NO_COVERAGE) 92 set(NO_FUZZING_FLAGS "${NO_FUZZING_FLAGS} -fsanitize-coverage=0") 93 endif() 94 95 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_FLAGS}") 96 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}") 97 98 set(PROTOBUF_CFLAGS "${CMAKE_C_FLAGS} ${NO_FUZZING_FLAGS} -w") 99 set(PROTOBUF_CXXFLAGS "${CMAKE_CXX_FLAGS} ${NO_FUZZING_FLAGS} -w") 100 if(CMAKE_USE_PTHREADS_INIT) 101 set(PROTOBUF_CFLAGS "${PROTOBUF_CFLAGS} -pthread") 102 set(PROTOBUF_CXXFLAGS "${PROTOBUF_CXXFLAGS} -pthread") 103 endif() 104 105 if (LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF) 106 include(protobuf) 107 else() 108 find_package(Protobuf REQUIRED) 109 include_directories(${PROTOBUF_INCLUDE_DIRS}) 110 include_directories(${CMAKE_CURRENT_BINARY_DIR}) 111 endif() 112 113 enable_testing() 114 115 include(googletest) 116 117 if (NOT LIB_PROTO_MUTATOR_CTEST_JOBS) 118 ProcessorCount(LIB_PROTO_MUTATOR_CTEST_JOBS) 119 endif() 120 add_custom_target(check 121 COMMAND ${CMAKE_CTEST_COMMAND} -j${LIB_PROTO_MUTATOR_CTEST_JOBS} --output-on-failure) 122 123 add_subdirectory(src) 124 125 if (NOT "${LIB_PROTO_MUTATOR_FUZZER_LIBRARIES}" STREQUAL "" OR 126 NOT "${FUZZING_FLAGS}" STREQUAL "") 127 add_subdirectory(examples EXCLUDE_FROM_ALL) 128 endif() 129 130