-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCMakeLists.txt
192 lines (156 loc) · 6.61 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
cmake_minimum_required (VERSION 3.16.3)
project (appanvil C CXX)
#### Set some variables #####
set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libappanvil)
set(AUTOGEN_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/generated)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/dist)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/dist)
set(LIBRARY_NAME ${PROJECT_NAME}-dev)
# Create the directory, so that it exists for FLEX/BISON to use
file(MAKE_DIRECTORY ${AUTOGEN_SOURCE_DIR})
### Sources that need to be built ###
set(SOURCES
${PROJECT_SOURCE_DIR}/tree/TreeNode.cc
${PROJECT_SOURCE_DIR}/tree/ParseTree.cc
${PROJECT_SOURCE_DIR}/tree/ProfileRule.cc
${PROJECT_SOURCE_DIR}/tree/PrefixNode.cc
${PROJECT_SOURCE_DIR}/tree/AliasNode.cc
${PROJECT_SOURCE_DIR}/tree/RuleNode.cc
${PROJECT_SOURCE_DIR}/tree/FileRule.cc
${PROJECT_SOURCE_DIR}/tree/LinkRule.cc
${PROJECT_SOURCE_DIR}/tree/AbstractionRule.cc
${PROJECT_SOURCE_DIR}/tree/RuleList.cc
${PROJECT_SOURCE_DIR}/tree/FileMode.cc
${PROJECT_SOURCE_DIR}/tree/AllRule.cc
${PROJECT_SOURCE_DIR}/parser/lib.c
${PROJECT_SOURCE_DIR}/parser/parser.cc
${PROJECT_SOURCE_DIR}/apparmor_parser.cc
)
set(PARSE_INPUT ${PROJECT_SOURCE_DIR}/parser/parser_yacc.y)
set(LEXER_INPUT ${PROJECT_SOURCE_DIR}/parser/parser_lex.l)
# Public headers that will be used by the client
set(OUTPUT_HEADERS
${PROJECT_SOURCE_DIR}/apparmor_parser.hh
)
set(OUTPUT_TREE_HEADERS
${PROJECT_SOURCE_DIR}/tree/TreeNode.hh
${PROJECT_SOURCE_DIR}/tree/ProfileRule.hh
${PROJECT_SOURCE_DIR}/tree/PrefixNode.hh
${PROJECT_SOURCE_DIR}/tree/AliasNode.hh
${PROJECT_SOURCE_DIR}/tree/RuleNode.hh
${PROJECT_SOURCE_DIR}/tree/FileRule.hh
${PROJECT_SOURCE_DIR}/tree/LinkRule.hh
${PROJECT_SOURCE_DIR}/tree/AbstractionRule.hh
${PROJECT_SOURCE_DIR}/tree/FileMode.hh
${PROJECT_SOURCE_DIR}/tree/RuleList.hh
)
#### Bison stuff ####
find_package(BISON REQUIRED)
BISON_TARGET(PARSER
${PARSE_INPUT}
${AUTOGEN_SOURCE_DIR}/parser_yacc.cc
DEFINES_FILE ${AUTOGEN_SOURCE_DIR}/parser_yacc.hh)
#### Flex stuff ####
find_package(FLEX 2.5.5 REQUIRED)
FLEX_TARGET(LEXER
${LEXER_INPUT}
${AUTOGEN_SOURCE_DIR}/parser_lex.cc
DEFINES_FILE ${AUTOGEN_SOURCE_DIR}/parser_lex.hh)
ADD_FLEX_BISON_DEPENDENCY(LEXER PARSER)
#### GLib (we use spawn_sync) ####
find_package(PkgConfig)
pkg_check_modules(GLIBMM glibmm-2.4 REQUIRED)
#### Linter and Static Analysis ####
find_program(CLANG_TIDY NAMES clang-tidy)
find_program(CPPCHECK NAMES cppcheck)
# If cmake was compiled with DANALYZE=TRUE
string(TOLOWER "${ANALYZE}" ANALYZE)
if("${ANALYZE}" STREQUAL "true")
message(STATUS "Adding static-analysis tools to build:")
set(USE_CLANG_TIDY TRUE)
set(USE_CPPCHECK TRUE)
unset(ANALYZE CACHE)
endif()
# If we want to use clang-tidy
if(${USE_CLANG_TIDY})
if(${CLANG_TIDY} STREQUAL "CLANG_TIDY-NOTFOUND")
message(WARNING "Could not find clang-tidy installation. Please install clang-tidy if you want to use it for static analysis.")
else()
message(STATUS "Adding clang-tidy to build")
set(CMAKE_CXX_CLANG_TIDY clang-tidy; --config-file ${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy)
endif()
unset(USE_CLANG_TIDY CACHE)
endif()
add_custom_target(echo
COMMAND echo test["$<$<CONFIG:Release>:yes>"]
COMMAND_EXPAND_LISTS
)
# If we want to use cppcheck
if(${USE_CPPCHECK})
if(${CPPCHECK} STREQUAL "CPPCHECK-NOTFOUND")
message(WARNING "Could not find cppcheck installation. Please install cppcheck if you want to use it for static analysis.")
else()
message(STATUS "Adding cppcheck to build")
set(CMAKE_CXX_CPPCHECK cppcheck
--enable=warning,performance,portability,information
--suppress=missingInclude --suppress=unmatchedSuppression --suppress=internalAstError
--quiet
--inline-suppr
${SOURCES}
)
endif()
unset(USE_CPPCHECK CACHE)
endif()
#### Set Compiler Options ####
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra")
set(CMAKE_CXX_STANDARD 20)
## set_source_files_properties(${CXX_SOURCES} PROPERTIES LANGUAGE CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#### Compile the autogenerated code ####
add_library(autogenerated_sources OBJECT ${FLEX_LEXER_OUTPUTS} ${BISON_PARSER_OUTPUT_SOURCE})
target_include_directories(autogenerated_sources PRIVATE ${PROJECT_SOURCE_DIR}/parser)
target_include_directories(autogenerated_sources PRIVATE ${PROJECT_SOURCE_DIR})
target_include_directories(autogenerated_sources SYSTEM PRIVATE ${AUTOGEN_SOURCE_DIR})
# Disable compiler warnings, clang-tidy, and cppcheck for this target
set_target_properties(autogenerated_sources
PROPERTIES
CXX_CLANG_TIDY ""
CXX_CPPCHECK ""
)
target_compile_options(autogenerated_sources PRIVATE "-w")
#### Create the main library ####
add_library(${LIBRARY_NAME} ${SOURCES} $<TARGET_OBJECTS:autogenerated_sources>)
target_compile_options(${LIBRARY_NAME} PUBLIC "-fPIE")
target_link_options(${LIBRARY_NAME} PUBLIC "-fPIE")
target_include_directories(${LIBRARY_NAME} PUBLIC ${PROJECT_SOURCE_DIR})
target_include_directories(${LIBRARY_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/parser)
target_include_directories(${LIBRARY_NAME} SYSTEM PRIVATE ${AUTOGEN_SOURCE_DIR})
target_include_directories(${LIBRARY_NAME} SYSTEM PUBLIC ${GLIBMM_INCLUDE_DIRS})
target_link_libraries(${LIBRARY_NAME} PUBLIC ${GLIBMM_LIBRARIES})
# Pkg-config module (I couldn't figure out the configuration for find_package)
set(INSTALL_NAME "libappanvil")
set(PKG_CONFIG_FILE_IN "libappanvil.pc.in")
set(PKG_CONFIG_FILE_OUT "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/libappanvil.pc")
configure_file("${PKG_CONFIG_FILE_IN}" "${PKG_CONFIG_FILE_OUT}" @ONLY)
# Create target to install library
if(NOT DEFINED DISABLE_LIBAPPANVIL_INSTALL OR DISABLE_LIBAPPANVIL_INSTALL STREQUAL "false")
install(TARGETS ${LIBRARY_NAME} DESTINATION lib/)
install(FILES ${OUTPUT_HEADERS} DESTINATION include/${INSTALL_NAME})
install(FILES ${OUTPUT_TREE_HEADERS} DESTINATION include/${INSTALL_NAME}/tree/)
install(FILES ${PKG_CONFIG_FILE_OUT} DESTINATION lib/pkgconfig)
endif()
# Create uninstall target
# Copied from: https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake
if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake)
endif()
#### Create aa-replace executable ####
add_subdirectory(aa-replace)
#### Create the test executable ####
enable_testing()
add_subdirectory(test)