-
Notifications
You must be signed in to change notification settings - Fork 18
/
CMakeLists.txt
149 lines (131 loc) · 5.04 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#
# american fuzzy lop++ - grammar mutator
# --------------------------------------
#
# Written by Shengtuo Hu
#
# Copyright 2020 AFLplusplus Project. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# A grammar-based custom mutator written for GSoC '20.
#
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(Grammar-Mutator)
# Undocumented options
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(FATAL_ERROR "In-source builds are not allowed.")
endif ()
# CMake modules
# set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CMakeOptions.txt)
set(MEMORCHECK_COMMAND_OPTIONS "--leak-check=full --show-leak-kinds=all")
include(CTest)
# Check build type
if (ENABLE_DEBUG)
message(STATUS "Enable debug output")
add_definitions(-DDEBUG_BUILD)
endif ()
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose the build type" FORCE)
# Include "None" as option to disable any additional (optimization) flags,
# relying on just CMAKE_C_FLAGS and CMAKE_CXX_FLAGS (which are empty by
# default). These strings are presented in cmake-gui.
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"None" "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif ()
# Set -Wall -Wextra -Werror
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
# Do not disable assertions based on CMAKE_BUILD_TYPE.
foreach (_build_type "Release" "MinSizeRel" "RelWithDebInfo")
foreach (_lang C CXX)
string(TOUPPER "CMAKE_${_lang}_FLAGS_${_build_type}" _var)
string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" "" ${_var} "${${_var}}")
endforeach ()
endforeach ()
# C++ language standard
set(CMAKE_CXX_STANDARD 14)
# Toolchain
message(STATUS "C compiler: ${CMAKE_C_COMPILER}")
message(STATUS "C++ compiler: ${CMAKE_CXX_COMPILER}")
# Java
find_package(Java COMPONENTS Runtime REQUIRED)
# ANTLR 4 Jar
if (NOT ANTLR_JAR_LOCATION)
message(FATAL_ERROR "Missing antlr4.jar path. You can specify it's path using: -DANTLR_JAR_LOCATION=<path>")
endif ()
if (NOT IS_ABSOLUTE ${ANTLR_JAR_LOCATION})
message(FATAL_ERROR "Please provide the full path to antlr4.jar")
endif ()
if (NOT EXISTS "${ANTLR_JAR_LOCATION}")
message(FATAL_ERROR "Unable to find antlr4.jar in ${ANTLR_JAR_LOCATION}")
endif ()
get_filename_component(ANTLR_NAME ${ANTLR_JAR_LOCATION} NAME_WE)
message(STATUS "Found ${ANTLR_NAME}: ${ANTLR_JAR_LOCATION}")
# Python
find_program(PYTHON
NAMES "python3"
DOC "Python interpreter")
if (NOT PYTHON)
message(FATAL_ERROR "Unable to find Python!")
else ()
message(STATUS "Python: ${PYTHON}")
endif ()
# Generate "f1_c_fuzz.c" and "f1_c_fuzz.h"
# Check the input grammar file
if (NOT GRAMMAR_FILE)
message(FATAL_ERROR "Missing the grammar file path. You can specify it's path using: -DGRAMMAR_FILE=<path>")
endif ()
if (NOT IS_ABSOLUTE ${GRAMMAR_FILE})
message(FATAL_ERROR "Please provide the full path to the grammar file")
endif ()
if (NOT EXISTS ${GRAMMAR_FILE})
message(FATAL_ERROR "The grammar file does not exist: ${GRAMMAR_FILE}")
endif ()
if (NOT GRAMMAR_FILENAME)
get_filename_component(GRAMMAR_FILENAME ${GRAMMAR_FILE} NAME_WE)
string(TOLOWER ${GRAMMAR_FILENAME} GRAMMAR_FILENAME)
STRING(REGEX REPLACE "[_.-].*" "" GRAMMAR_FILENAME ${GRAMMAR_FILENAME})
message(STATUS "Selected grammar name: ${GRAMMAR_FILENAME} (from ${GRAMMAR_FILE})")
else ()
message(STATUS "Selected grammar name: ${GRAMMAR_FILENAME}")
endif ()
# Generate files at configure time
execute_process(
COMMAND mkdir -p f1/src
COMMAND mkdir -p f1/include
COMMAND ${PYTHON} ${CMAKE_SOURCE_DIR}/grammars/f1_c_gen.py ${GRAMMAR_FILE} ${CMAKE_BINARY_DIR}/f1
COMMAND ${PYTHON} ${CMAKE_SOURCE_DIR}/grammars/f1_g4_translate.py ${GRAMMAR_FILE} ${CMAKE_BINARY_DIR}/f1
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
if (result)
message(FATAL_ERROR "CMake step for f1_c_fuzz and g4 translation failed: ${result}")
endif ()
set(GRAMMAR_G4_FILE "${CMAKE_BINARY_DIR}/f1/Grammar.g4")
if (EXISTS ${GRAMMAR_G4_FILE})
message(STATUS "Grammar g4: ${GRAMMAR_G4_FILE}")
else ()
message(FATAL_ERROR "The g4 grammar file does not exist: ${GRAMMAR_FILE}")
endif ()
add_subdirectory(src)
add_subdirectory(lib)
add_subdirectory(third_party)
if (ENABLE_TESTING)
enable_testing()
add_subdirectory(tests)
# Valgrind
add_custom_target(test_memcheck
COMMAND ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --test-action memcheck
# Throw an error while finding any non-empty memory check reports
# - `find` always returns zero, even if it does not find anything
# - `grep .` will return non-zero, if no memory leaks are found
COMMAND find "${CMAKE_BINARY_DIR}/Testing/Temporary" -name "MemoryChecker.*.log" -not -empty -exec cat {} + | grep . || exit 0 && exit 2)
endif ()