-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCMakeLists.txt
220 lines (185 loc) · 8.52 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
219
220
cmake_minimum_required(VERSION 3.6...3.31)
project(energymon VERSION 0.7.0
LANGUAGES C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
add_compile_options(-Wall)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(GNUInstallDirs)
set(ENERGYMON_UTIL ${PROJECT_SOURCE_DIR}/common/energymon-util.c)
set(ENERGYMON_TIME_UTIL ${PROJECT_SOURCE_DIR}/common/energymon-time-util.c;${PROJECT_SOURCE_DIR}/common/ptime/ptime.c)
if(UNIX AND NOT APPLE)
find_library(LIBM m)
endif()
# Configurable build options
set(ENERGYMON_BUILD_LIB "ALL" CACHE STRING "Specify a single library to build, ALL, or NONE")
set(ENERGYMON_BUILD_DEFAULT "dummy" CACHE STRING "Default EnergyMon implementation, or NONE to disable building energymon-default")
if (DEFAULT AND NOT ENERGYMON_BUILD_DEFAULT)
# for backward compatibility with old variable "DEFAULT"
set(ENERGYMON_BUILD_DEFAULT DEFAULT)
endif()
set(ENERGYMON_BUILD_SHMEM_PROVIDERS TRUE CACHE BOOL "Enable/disable building shared memory providers")
set(ENERGYMON_BUILD_UTILITIES TRUE CACHE BOOL "Enable/disable building utility applications")
set(ENERGYMON_BUILD_TESTS TRUE CACHE BOOL "Enable/disable building tests")
set(ENERGYMON_BUILD_EXAMPLES TRUE CACHE BOOL "Enable/disable building example code")
set(ENERGYMON_INSTALL_CMAKE_PACKAGES FALSE CACHE BOOL "[Experimental] Enable/disable installing cmake package configuration files")
# Utilities
add_subdirectory(utils)
add_subdirectory(test)
# Functions
function(add_energymon_library TARGET SHORT_NAME)
# Parse arguments
set(options BUILD_SHMEM_PROVIDER)
set(oneValueArgs ENERGYMON_GET_HEADER
ENERGYMON_GET_FUNCTION
ENERGYMON_GET_C_OUTPUT)
set(multiValueArgs SOURCES
PUBLIC_HEADER
PUBLIC_BUILD_INCLUDE_DIRS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "")
message(FATAL_ERROR "add_energymon_library: unrecognized args: ${ARG_UNPARSED_ARGUMENTS}")
endif()
# Create library
add_library(${TARGET} ${ARG_SOURCES})
target_include_directories(${TARGET} PRIVATE ${PROJECT_SOURCE_DIR}/common
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/inc$<SEMICOLON>${ARG_PUBLIC_BUILD_INCLUDE_DIRS}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>)
set_target_properties(${TARGET} PROPERTIES PUBLIC_HEADER "${PROJECT_SOURCE_DIR}/inc/energymon.h;${ARG_PUBLIC_HEADER}")
if(BUILD_SHARED_LIBS)
set_target_properties(${TARGET} PROPERTIES VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
endif()
install(TARGETS ${TARGET}
EXPORT EnergyMonTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
# Tests and Utils
set(ENERGYMON_GET_HEADER ${ARG_ENERGYMON_GET_HEADER})
set(ENERGYMON_GET_FUNCTION ${ARG_ENERGYMON_GET_FUNCTION})
configure_file(${PROJECT_SOURCE_DIR}/common/energymon-get.c.in ${ARG_ENERGYMON_GET_C_OUTPUT})
add_energymon_tests(${SHORT_NAME} ${TARGET} ${ARG_ENERGYMON_GET_C_OUTPUT})
add_energymon_utils(${SHORT_NAME} ${TARGET} ${ARG_ENERGYMON_GET_C_OUTPUT})
# shmem provider
if(ARG_BUILD_SHMEM_PROVIDER)
add_energymon_shmem_provider(${SHORT_NAME} ${TARGET} ${ARG_ENERGYMON_GET_C_OUTPUT})
endif()
endfunction()
# Forwards through to add_energymon_library, but the only argument should be SOURCES
function(add_energymon_default_library)
add_energymon_library(energymon-default default ${ARGV}
PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/inc/energymon-default.h
ENERGYMON_GET_HEADER energymon-default.h
ENERGYMON_GET_FUNCTION "energymon_get_default"
ENERGYMON_GET_C_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/energymon-default/energymon-get.c)
target_compile_definitions(energymon-default PRIVATE "ENERGYMON_DEFAULT")
endfunction()
function(add_energymon_pkg_config TARGET DESCRIPTION REQUIRES_PRIVATE LIBS_PRIVATE)
set(PKG_CONFIG_PREFIX "${CMAKE_INSTALL_PREFIX}")
set(PKG_CONFIG_EXEC_PREFIX "\${prefix}")
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(PKG_CONFIG_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
else()
set(PKG_CONFIG_LIBDIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
endif()
if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
set(PKG_CONFIG_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}")
else()
set(PKG_CONFIG_INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}")
endif()
set(PKG_CONFIG_NAME "${TARGET}")
set(PKG_CONFIG_DESCRIPTION "${DESCRIPTION}")
string(REPLACE ";" " " PKG_CONFIG_REQUIRES_PRIVATE "${REQUIRES_PRIVATE}")
set(PKG_CONFIG_CFLAGS "-I\${includedir}")
set(PKG_CONFIG_LIBS "-L\${libdir} -l${TARGET}")
string(REPLACE ";" " " PKG_CONFIG_LIBS_PRIVATE "${LIBS_PRIVATE}")
configure_file(
${PROJECT_SOURCE_DIR}/pkgconfig.in
${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.pc
@ONLY
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endfunction()
function(energymon_export_dependency PACKAGE)
if(BUILD_SHARED_LIBS)
return()
endif()
set_property(GLOBAL APPEND PROPERTY ENERGYMON_CMAKE_PACKAGE_DEPENDENCIES "find_dependency(${PACKAGE})")
endfunction()
function(energymon_export_pkg_dependency PREFIX MODULE_SPEC)
# Parse arguments
set(options IMPORTED_TARGET)
set(oneValueArgs)
set(multiValueArgs)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "")
message(FATAL_ERROR "energymon_export_pkg_dependency: unrecognized args: ${ARG_UNPARSED_ARGUMENTS}")
endif()
if(BUILD_SHARED_LIBS)
return()
endif()
set_property(GLOBAL APPEND PROPERTY ENERGYMON_CMAKE_PACKAGE_DEPENDENCIES "find_pkg_config_dependency(${PREFIX} ${ARG_IMPORTED_TARGET} ${MODULE_SPEC})")
endfunction()
# Libraries
# Must be added before libraries that build shmem providers
add_subdirectory(shmem)
# Please keep these in alphabetical order
add_subdirectory(cray-pm)
add_subdirectory(dummy)
add_subdirectory(ibmpowernv)
add_subdirectory(ipg)
add_subdirectory(jetson)
add_subdirectory(msr)
add_subdirectory(odroid)
add_subdirectory(osp)
add_subdirectory(osp3)
add_subdirectory(rapl)
add_subdirectory(raplcap-msr)
add_subdirectory(wattsup)
add_subdirectory(zcu102)
if(NOT TARGET energymon-default AND NOT ${ENERGYMON_BUILD_DEFAULT} MATCHES "NONE")
message(FATAL_ERROR
"No build target for ENERGYMON_BUILD_DEFAULT=${ENERGYMON_BUILD_DEFAULT}\n"
"Specified implementation may be unknown, unsupported for the target system, or skipped due to missing build dependencies\n"
"Retry with a supported value or set ENERGYMON_BUILD_DEFAULT=NONE")
endif()
# CMake package helpers
if (ENERGYMON_INSTALL_CMAKE_PACKAGES)
include(CMakePackageConfigHelpers)
set(ENERGYMON_CMAKE_CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/energymon)
get_property(PACKAGE_CONFIG_REQUIRED_DEPENDENCIES GLOBAL PROPERTY ENERGYMON_CMAKE_PACKAGE_DEPENDENCIES)
list(REMOVE_DUPLICATES PACKAGE_CONFIG_REQUIRED_DEPENDENCIES)
string(REPLACE ";" "\n" CONFIG_FIND_DEPENDENCIES "${PACKAGE_CONFIG_REQUIRED_DEPENDENCIES}")
set(CONFIG_TARGETS_FILE EnergyMonTargets.cmake)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/Config.cmake.in
${CMAKE_BINARY_DIR}/EnergyMonConfig.cmake
INSTALL_DESTINATION ${ENERGYMON_CMAKE_CONFIG_DIR}
)
write_basic_package_version_file(
${CMAKE_BINARY_DIR}/EnergyMonConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES ${CMAKE_BINARY_DIR}/EnergyMonConfig.cmake
${CMAKE_BINARY_DIR}/EnergyMonConfigVersion.cmake
DESTINATION ${ENERGYMON_CMAKE_CONFIG_DIR})
if(NOT BUILD_SHARED_LIBS)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/cmake/
DESTINATION ${ENERGYMON_CMAKE_CONFIG_DIR}/Modules
FILES_MATCHING PATTERN "Find*.cmake")
endif()
install(EXPORT EnergyMonTargets
DESTINATION ${ENERGYMON_CMAKE_CONFIG_DIR}
NAMESPACE EnergyMon::)
endif()
# Uninstall
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
@ONLY
)
add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)