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