-
Notifications
You must be signed in to change notification settings - Fork 476
/
CMakeLists.txt
218 lines (181 loc) · 8.38 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022, Intel Corporation
cmake_minimum_required(VERSION 3.5)
project(PCM)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
option(PCM_NO_ASAN "Disable address sanitizer" OFF)
option(PCM_FUZZ "Enable fuzzing" OFF)
option(PCM_BUILD_EXECUTABLES "Build PCM utilities" ON)
option(PCM_NO_STATIC_LIBASAN "Disable static address sanitizer library" ON)
if(MSVC)
option(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY "Disable using static runtime under MSVC" OFF)
endif()
foreach(opt NO_STATIC_MSVC_RUNTIME_LIBRARY;FUZZ;NO_ASAN;NO_STATIC_LIBASAN)
if(${opt})
message(DEPRECATION "Option \"${opt}\" is deprecated and will be removed soon. Please use \"PCM_${opt}\"")
set(PCM_${opt} ${opt})
endif()
endforeach()
include(GNUInstallDirs)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
# Check for pcm*.x artifacts generated by an old build scenario using Makefiles
file(GLOB_RECURSE PCM_X_ARTIFACTS ${CMAKE_CURRENT_SOURCE_DIR}/*.x)
foreach(file ${PCM_X_ARTIFACTS})
file(REMOVE ${file})
message(STATUS "Removing old artifact from current source directory : ${file}")
endforeach()
if(PCM_X_ARTIFACTS)
message(WARNING
" Old pcm utilities (.x) were indicated in build folder.\n"
" Old binaries are expected to be installed in system.\n"
" Make sure to install the new binaries(run 'cmake --install .') after building.)")
endif()
message(STATUS "System: ${CMAKE_SYSTEM}")
if(UNIX AND NOT APPLE)
if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
set(FREE_BSD TRUE)
else()
set(LINUX TRUE)
endif()
endif()
if(UNIX) # APPLE, LINUX, FREE_BSD
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Release)" FORCE)
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "initial CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "initial CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
# required PCM common flags
set (PCM_COMMON_FLAGS "-Wno-unknown-pragmas -DCMAKE_INSTALL_PREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
if(LINUX)
set (PCM_COMMON_FLAGS "${PCM_COMMON_FLAGS} -Wextra -DPCM_USE_PERF")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set (PCM_COMMON_FLAGS "${PCM_COMMON_FLAGS} -Wl,-z,now")
endif()
if(NOT DEFINED LINUX_SYSTEMD)
set(LINUX_SYSTEMD FALSE)
endif()
if(NOT DEFINED LINUX_SYSTEMD_UNITDIR)
set(LINUX_SYSTEMD_UNITDIR "${CMAKE_INSTALL_LIBDIR}/systemd/system")
endif()
if(LINUX_SYSTEMD)
message(STATUS "A systemd unit file will be generated")
message(STATUS "LINUX_SYSTEMD_UNITDIR:${LINUX_SYSTEMD_UNITDIR}")
else()
message(STATUS "Set LINUX_SYSTEMD=TRUE for a systemd unit file.")
endif()
endif(LINUX)
# adding the required PCM common flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PCM_COMMON_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PCM_COMMON_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
set(PCM_OPTIONAL_FLAGS "-Wall")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set (PCM_DYNAMIC "-rdynamic")
elseif()
set (PCM_DYNAMIC "")
endif()
if(APPLE)
set(PCM_NO_ASAN ON)
message(STATUS "AddressSanitizer is currently disabled on MacOS")
endif()
if(PCM_NO_ASAN)
message(STATUS "AddressSanitizer is disabled")
set(PCM_ASAN "")
else()
message(STATUS "AddressSanitizer is enabled")
message(STATUS "To disable AddressSanitizer, use -DPCM_NO_ASAN=1 option")
set(PCM_ASAN "-fsanitize=address")
endif()
set(PCM_HARDENING_FLAGS "-fPIE -fstack-protector -D_FORTIFY_SOURCE=2 -ftrapv ${PCM_ASAN} -fwrapv -fno-delete-null-pointer-checks -fno-strict-overflow -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5)
message(WARNING "Old gcc compiler (version < 5), -fsanitize=undefined option is not supported.")
elseif()
set(PCM_HARDENING_FLAGS "${PCM_HARDENING_FLAGS} -fsanitize=undefined")
endif()
set(PCM_LINKER_HARDENING_FLAGS "${PCM_ASAN}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PCM_LINKER_HARDENING_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PCM_LINKER_HARDENING_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${PCM_OPTIONAL_FLAGS} -O3 ${PCM_DYNAMIC} ${PCM_HARDENING_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${PCM_OPTIONAL_FLAGS} -O0 -g ${PCM_DYNAMIC} ${PCM_HARDENING_FLAGS}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${PCM_OPTIONAL_FLAGS} -O3 -g ${PCM_DYNAMIC} ${PCM_HARDENING_FLAGS}")
if(FREE_BSD)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -lexecinfo")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -lexecinfo")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -lexecinfo")
endif(FREE_BSD)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
message(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
message(STATUS "CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}")
message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif(UNIX)
if(PCM_FUZZ)
set(FUZZER_OPTIONS "-fsanitize=fuzzer,address -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_OPTIONS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_OPTIONS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_OPTIONS}")
endif(PCM_FUZZ)
#######################
# Install
#######################
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
add_subdirectory(src)
add_subdirectory(examples)
add_subdirectory(tests)
message(STATUS "Install directory: ${CMAKE_INSTALL_PREFIX}")
#######################
# CPack (only UNIX)
#######################
if(UNIX)
if(EXISTS "/etc/debian_version") # for Debian family
set(CPACK_GENERATOR "DEB")
message(STATUS "CPACK_GENERATOR is DEB")
elseif(EXISTS "/etc/redhat-release") # for RHEL, Fedora, CentOs
set(CPACK_GENERATOR "RPM")
message(STATUS "CPACK_GENERATOR is RPM")
else()
if(EXISTS "/etc/os-release")
file(READ "/etc/os-release" OS_PARAMS)
string(REGEX MATCH "suse" OUT ${OS_PARAMS}) # for Suse-like systems
if(OUT STREQUAL "suse")
set(CPACK_GENERATOR "RPM")
message(STATUS "CPACK_GENERATOR is RPM")
else()
set(CPACK_GENERATOR "TXZ")
set(CPACK_SET_DESTDIR ON)
message(STATUS "CPACK_GENERATOR is TXZ")
endif()
else()
set(CPACK_GENERATOR "TXZ")
set(CPACK_SET_DESTDIR ON)
message(STATUS "CPACK_GENERATOR is TXZ")
endif()
endif()
set(CPACK_PACKAGE_CONTACT "intel <[email protected]>")
set(CPACK_PACKAGE_NAME "pcm")
set(CPACK_PACKAGE_VERSION "0000")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Intel(r) Performance Counter Monitor (Intel(r) PCM)")
set(CPACK_PACKAGE_DESCRIPTION "\
Intel(r) Performance Counter Monitor (Intel(r) PCM) is an application programming\n\
interface (API) and a set of tools based on the API to monitor\n\
performance and energy metrics of Intel(r) Core(tm), Xeon(r), Atom(tm)\n\
and Xeon Phi(tm) processors. PCM works on Linux, Windows, Mac OS X,\n\
FreeBSD and DragonFlyBSD operating systems.")
set(CPACK_RPM_PACKAGE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION})
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_RPM_PACKAGE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CMAKE_INSTALL_PREFIX})
set(CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_RPM_PACKAGE_RELOCATABLE TRUE)
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_RPM_COMPONENT_INSTALL ON)
include (CPack)
endif(UNIX)