• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TESTING "Enable test building" ON)
22option(LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF
23       "Automatically download working protobuf" OFF)
24option(LIB_PROTO_MUTATOR_WITH_ASAN "Enable address sanitizer" OFF)
25option(PKG_CONFIG_PATH "Directory to install pkgconfig file" "share/pkgconfig")
26set(LIB_PROTO_MUTATOR_FUZZER_LIBRARIES "" CACHE STRING "Fuzzing engine libs")
27
28# External dependencies
29set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)
30
31# External dependencies
32include(ProcessorCount)
33include(CheckCCompilerFlag)
34include(CheckCXXCompilerFlag)
35
36set(THREADS_PREFER_PTHREAD_FLAG ON)
37find_package(Threads)
38
39find_package(LibLZMA)
40include_directories(${LIBLZMA_INCLUDE_DIRS})
41
42find_package(ZLIB)
43include_directories(${ZLIB_INCLUDE_DIRS})
44
45set(CMAKE_CXX_STANDARD 11)
46
47include_directories(${PROJECT_SOURCE_DIR})
48
49if (MSVC)
50  option(LIB_PROTO_MUTATOR_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
51  if (LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF OR LIB_PROTO_MUTATOR_MSVC_STATIC_RUNTIME)
52    # This is the default for protobuf with MSVC
53    # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
54    foreach(flag_var
55        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
56        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
57      if(${flag_var} MATCHES "/MD")
58        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
59      endif(${flag_var} MATCHES "/MD")
60    endforeach(flag_var)
61  endif (LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF OR LIB_PROTO_MUTATOR_MSVC_STATIC_RUNTIME)
62endif (MSVC)
63
64set(CMAKE_REQUIRED_FLAGS "-fsanitize=address")
65check_cxx_compiler_flag(-fsanitize=address LIB_PROTO_MUTATOR_HAS_SANITIZE_ADDRESS)
66check_cxx_compiler_flag("-fsanitize=address -fsanitize-address-use-after-scope"
67                        LIB_PROTO_MUTATOR_HAS_SANITIZE_SCOPE)
68unset(CMAKE_REQUIRED_FLAGS)
69
70set(CMAKE_REQUIRED_FLAGS "-fsanitize-coverage=0")
71check_cxx_compiler_flag(-fsanitize-coverage= LIB_PROTO_MUTATOR_HAS_NO_COVERAGE)
72unset(CMAKE_REQUIRED_FLAGS)
73
74set(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
75check_cxx_compiler_flag(-fsanitize=fuzzer-no-link LIB_PROTO_MUTATOR_HAS_SANITIZE_FUZZER)
76unset(CMAKE_REQUIRED_FLAGS)
77
78set(CMAKE_REQUIRED_FLAGS "-fno-sanitize=fuzzer")
79check_cxx_compiler_flag(-fno-sanitize=fuzzer LIB_PROTO_MUTATOR_HAS_NO_SANITIZE_FUZZER)
80unset(CMAKE_REQUIRED_FLAGS)
81
82check_cxx_compiler_flag(-Wstring-conversion LIB_PROTO_MUTATOR_HAS_WSTRING_CONVERSION)
83
84if (NOT MSVC)
85  set(EXTRA_FLAGS "-fno-exceptions -Werror -Wall")
86  if (LIB_PROTO_MUTATOR_HAS_WSTRING_CONVERSION)
87    set(EXTRA_FLAGS "${EXTRA_FLAGS} -Wstring-conversion")
88  endif()
89endif(NOT MSVC)
90
91if (LIB_PROTO_MUTATOR_WITH_ASAN)
92  if (LIB_PROTO_MUTATOR_HAS_SANITIZE_ADDRESS)
93    set(EXTRA_FLAGS "${EXTRA_FLAGS} -fsanitize=address")
94    if (LIB_PROTO_MUTATOR_HAS_SANITIZE_SCOPE)
95      set(EXTRA_FLAGS "${EXTRA_FLAGS} -fsanitize-address-use-after-scope")
96    endif()
97  endif()
98endif()
99
100# Assume CFLAGS has coverage options if LIB_PROTO_MUTATOR_FUZZER_LIBRARIES was set
101if ("${LIB_PROTO_MUTATOR_FUZZER_LIBRARIES}" STREQUAL "")
102  if (LIB_PROTO_MUTATOR_HAS_SANITIZE_FUZZER)
103    set(FUZZING_FLAGS "-fsanitize=fuzzer-no-link")
104    set(FUZZING_FLAGS_BINARY "-fsanitize=fuzzer")
105  endif()
106  if (LIB_PROTO_MUTATOR_HAS_SANITIZE_NO_FUZZER)
107    set(NO_FUZZING_FLAGS "-fno-sanitize=fuzzer")
108  endif()
109endif()
110if (LIB_PROTO_MUTATOR_HAS_NO_COVERAGE)
111  set(NO_FUZZING_FLAGS "${NO_FUZZING_FLAGS} -fsanitize-coverage=0")
112endif()
113
114set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_FLAGS}")
115set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}")
116
117set(PROTOBUF_CFLAGS "${CMAKE_C_FLAGS} ${NO_FUZZING_FLAGS} -w")
118set(PROTOBUF_CXXFLAGS "${CMAKE_CXX_FLAGS} ${NO_FUZZING_FLAGS} -w")
119if(CMAKE_USE_PTHREADS_INIT)
120  set(PROTOBUF_CFLAGS "${PROTOBUF_CFLAGS} -pthread")
121  set(PROTOBUF_CXXFLAGS "${PROTOBUF_CXXFLAGS} -pthread")
122endif()
123
124if (LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF)
125  include(protobuf)
126else()
127  find_package(Protobuf REQUIRED)
128  include_directories(${PROTOBUF_INCLUDE_DIRS})
129  include_directories(${CMAKE_CURRENT_BINARY_DIR})
130endif()
131
132set(LIB_DIR "lib${LIB_SUFFIX}")
133
134if (LIB_PROTO_MUTATOR_TESTING)
135  enable_testing()
136
137  include(googletest)
138
139  if (NOT LIB_PROTO_MUTATOR_CTEST_JOBS)
140    ProcessorCount(LIB_PROTO_MUTATOR_CTEST_JOBS)
141  endif()
142  add_custom_target(check
143                    COMMAND ${CMAKE_CTEST_COMMAND} -j${LIB_PROTO_MUTATOR_CTEST_JOBS} --output-on-failure)
144endif()
145
146add_subdirectory(src)
147
148if (NOT "${LIB_PROTO_MUTATOR_FUZZER_LIBRARIES}" STREQUAL "" OR
149    NOT "${FUZZING_FLAGS}" STREQUAL "")
150  add_subdirectory(examples EXCLUDE_FROM_ALL)
151endif()
152
153install(EXPORT libprotobuf-mutatorTargets FILE libprotobuf-mutatorTargets.cmake
154  NAMESPACE libprotobuf-mutator:: DESTINATION lib/cmake/libprotobuf-mutator)
155configure_file(libprotobuf-mutatorConfig.cmake.in libprotobuf-mutatorConfig.cmake @ONLY)
156install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libprotobuf-mutatorConfig.cmake"
157  DESTINATION lib/cmake/libprotobuf-mutator)
158configure_file("libprotobuf-mutator.pc.in" "libprotobuf-mutator.pc" @ONLY)
159install(FILES "${CMAKE_BINARY_DIR}/libprotobuf-mutator.pc"
160  DESTINATION ${PKG_CONFIG_PATH})
161install(DIRECTORY ./port ./src DESTINATION include/libprotobuf-mutator
162  FILES_MATCHING PATTERN "*.h")
163