Skip to content

Commit 0a48a6a

Browse files
authored
Merge pull request #122 from powerapi-ng/refactor/cpp-compat
refactor: Adapt codebase for C++ compilation
2 parents 4b7ebec + 1336dba commit 0a48a6a

20 files changed

+164
-156
lines changed

CMakeLists.txt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
cmake_minimum_required(VERSION 3.13)
2-
project(hwpc-sensor LANGUAGES C)
2+
project(hwpc-sensor LANGUAGES CXX)
33

44
option(WITH_MONGODB "Build with support for MongoDB storage module" ON)
55

6-
set(CMAKE_C_STANDARD 11)
76
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
8-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
97

108
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
119

12-
add_compile_options(-Werror -Wall -Wextra -Wpedantic -Wformat=2 -Wnull-dereference -Wno-gnu-statement-expression)
13-
add_link_options(-Wl,-z,relro,-z,now -pie)
14-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og -fsanitize=address,undefined -fno-omit-frame-pointer")
15-
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2 -fstack-protector-strong")
10+
add_compile_options(-Werror -Wall -Wextra -Wformat=2 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE)
11+
add_link_options(-pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -Wl,-z,defs -Wl,--as-needed)
12+
13+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
14+
# These warnings need to be suppressed temporarily, as the only fix is to rewrite the code in C++
15+
add_compile_options(-Wno-deprecated -Wno-c99-designator -Wno-vla-cxx-extension)
16+
endif()
17+
18+
if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
19+
add_compile_options(-Og -fsanitize=address,undefined -fno-omit-frame-pointer)
20+
add_link_options(-fsanitize=address,undefined)
21+
endif()
1622

1723
set(SENSOR_SOURCES
1824
src/config_cli.c
@@ -51,5 +57,13 @@ if(DEFINED ENV{GIT_TAG} AND DEFINED ENV{GIT_REV})
5157
endif()
5258

5359
add_executable(hwpc-sensor "${SENSOR_SOURCES}")
60+
61+
foreach(src ${SENSOR_SOURCES})
62+
set_source_files_properties(${src} PROPERTIES LANGUAGE CXX)
63+
endforeach()
64+
65+
target_compile_features(hwpc-sensor PUBLIC cxx_std_20)
66+
set_target_properties(hwpc-sensor PROPERTIES CXX_EXTENSIONS OFF LINKER_LANGUAGE CXX)
67+
5468
target_include_directories(hwpc-sensor SYSTEM PRIVATE "${LIBPFM_INCLUDE_DIRS}" "${CZMQ_INCLUDE_DIRS}" "${JSONC_INCLUDE_DIRS}" "${MONGOC_INCLUDE_DIRS}")
5569
target_link_libraries(hwpc-sensor "${LIBPFM_LIBRARIES}" "${CZMQ_LIBRARIES}" "${JSONC_LIBRARIES}" "${MONGOC_LIBRARIES}")

src/config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
struct config *
4747
config_create(void)
4848
{
49-
struct config *config = malloc(sizeof(struct config));
49+
struct config *config = (struct config *) malloc(sizeof(struct config));
5050

5151
if (!config)
5252
return NULL;
@@ -104,7 +104,7 @@ is_events_group_empty(zhashx_t *events_groups)
104104
{
105105
struct events_group *events_group = NULL;
106106

107-
for (events_group = zhashx_first(events_groups); events_group; events_group = zhashx_next(events_groups)) {
107+
for (events_group = (struct events_group *) zhashx_first(events_groups); events_group; events_group = (struct events_group *) zhashx_next(events_groups)) {
108108
if (zlistx_size(events_group->events) == 0) {
109109
zsys_error("config: Events group '%s' is empty", events_group->name);
110110
return -1;

src/config_cli.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ config_setup_from_cli(int argc, char **argv, struct config *config)
329329
if (setup_global_events_group(config, optarg)) {
330330
return -1;
331331
}
332-
current_events_group = zhashx_lookup(config->events.system, optarg);
332+
current_events_group = (struct events_group *) zhashx_lookup(config->events.system, optarg);
333333
break;
334334

335335
case 'c':
336336
if (setup_cgroups_events_group(config, optarg)) {
337337
return -1;
338338
}
339-
current_events_group = zhashx_lookup(config->events.containers, optarg);
339+
current_events_group = (struct events_group *) zhashx_lookup(config->events.containers, optarg);
340340
break;
341341

342342
case 'o':

src/config_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static int
469469
parse_json_configuration_file_from_fd(int fd, json_object **obj)
470470
{
471471
int ret = -1;
472-
char buffer[JSON_FILE_BUFFER_SIZE] = {0};
472+
char buffer[JSON_FILE_BUFFER_SIZE] = {};
473473
json_tokener *tok = NULL;
474474
enum json_tokener_error jerr;
475475
size_t line, column;

src/events.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ get_msr_pmu_event_encoding(const char *event_name, struct perf_event_attr *attr)
9999
static int
100100
setup_perf_event_attr(const char *event_name, struct perf_event_attr *attr)
101101
{
102-
pfm_perf_encode_arg_t arg = {0};
102+
pfm_perf_encode_arg_t arg = {};
103103

104104
attr->size = sizeof(struct perf_event_attr);
105105
attr->disabled = 1;
@@ -120,11 +120,11 @@ setup_perf_event_attr(const char *event_name, struct perf_event_attr *attr)
120120
struct event_config *
121121
event_config_create(const char *event_name)
122122
{
123-
struct perf_event_attr attr = {0};
123+
struct perf_event_attr attr = {};
124124
struct event_config *config = NULL;
125125

126126
if (!setup_perf_event_attr(event_name, &attr)) {
127-
config = malloc(sizeof(struct event_config));
127+
config = (struct event_config *) malloc(sizeof(struct event_config));
128128
if (config) {
129129
snprintf(config->name, NAME_MAX, "%s", event_name);
130130
config->attr = attr;
@@ -140,7 +140,7 @@ event_config_dup(struct event_config *config)
140140
struct event_config *copy = NULL;
141141

142142
if (config) {
143-
copy = malloc(sizeof(struct event_config));
143+
copy = (struct event_config *) malloc(sizeof(struct event_config));
144144
if (copy) {
145145
snprintf(copy->name, NAME_MAX, "%s", config->name);
146146
copy->attr = config->attr;
@@ -162,7 +162,7 @@ event_config_destroy(struct event_config **config)
162162
struct events_group *
163163
events_group_create(const char *name)
164164
{
165-
struct events_group *group = malloc(sizeof(struct events_group));
165+
struct events_group *group = (struct events_group *) malloc(sizeof(struct events_group));
166166

167167
if (group) {
168168
snprintf(group->name, NAME_MAX, "%s", name);
@@ -182,7 +182,7 @@ events_group_dup(struct events_group *group)
182182
struct events_group *copy = NULL;
183183

184184
if (group) {
185-
copy = malloc(sizeof(struct events_group));
185+
copy = (struct events_group *) malloc(sizeof(struct events_group));
186186
if (copy) {
187187
snprintf(copy->name, NAME_MAX, "%s", group->name);
188188
copy->type = group->type;

src/hwinfo.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
static struct hwinfo_pkg *
5353
hwinfo_pkg_create(void)
5454
{
55-
struct hwinfo_pkg *pkg = malloc(sizeof(struct hwinfo_pkg));
55+
struct hwinfo_pkg *pkg = (struct hwinfo_pkg *) malloc(sizeof(struct hwinfo_pkg));
5656

5757
if (!pkg)
5858
return NULL;
@@ -67,7 +67,7 @@ hwinfo_pkg_create(void)
6767
static struct hwinfo_pkg *
6868
hwinfo_pkg_dup(struct hwinfo_pkg *pkg)
6969
{
70-
struct hwinfo_pkg *pkgcpy = malloc(sizeof(struct hwinfo_pkg));
70+
struct hwinfo_pkg *pkgcpy = (struct hwinfo_pkg *) malloc(sizeof(struct hwinfo_pkg));
7171

7272
if (!pkgcpy)
7373
return NULL;
@@ -92,7 +92,7 @@ static int
9292
get_cpu_online_status(const char *cpu_dir)
9393
{
9494
int status = 1;
95-
char path[PATH_MAX] = {0};
95+
char path[PATH_MAX] = {};
9696
FILE *f = NULL;
9797
char buffer[2]; /* boolean expected */
9898

@@ -118,7 +118,7 @@ static char *
118118
get_package_id(const char *cpu_dir)
119119
{
120120
FILE *f = NULL;
121-
char path[PATH_MAX] = {0};
121+
char path[PATH_MAX] = {};
122122
char buffer[24]; /* log10(ULLONG_MAX) */
123123
char *id = NULL;
124124

@@ -138,7 +138,7 @@ get_package_id(const char *cpu_dir)
138138
static char *
139139
parse_cpu_id_from_name(const char *str)
140140
{
141-
regex_t re = {0};
141+
regex_t re = {};
142142
regmatch_t matches[CPU_ID_REGEX_EXPECTED_MATCHES];
143143
char *id = NULL;
144144

@@ -191,7 +191,7 @@ do_packages_detection(struct hwinfo *hwinfo)
191191
}
192192

193193
/* get cpu pkg or create it if never encountered */
194-
pkg = zhashx_lookup(hwinfo->pkgs, pkg_id);
194+
pkg = (struct hwinfo_pkg *) zhashx_lookup(hwinfo->pkgs, pkg_id);
195195
if (!pkg) {
196196
pkg = hwinfo_pkg_create();
197197
if (!pkg) {
@@ -201,7 +201,7 @@ do_packages_detection(struct hwinfo *hwinfo)
201201

202202
zhashx_insert(hwinfo->pkgs, pkg_id, pkg);
203203
hwinfo_pkg_destroy(&pkg);
204-
pkg = zhashx_lookup(hwinfo->pkgs, pkg_id); /* get the copy the pkg done by zhashx_insert */
204+
pkg = (struct hwinfo_pkg *) zhashx_lookup(hwinfo->pkgs, pkg_id); /* get the copy the pkg done by zhashx_insert */
205205
}
206206

207207
zlistx_add_end(pkg->cpus_id, cpu_id);
@@ -235,7 +235,7 @@ hwinfo_detect(struct hwinfo *hwinfo)
235235
struct hwinfo *
236236
hwinfo_create(void)
237237
{
238-
struct hwinfo *hw = malloc(sizeof(struct hwinfo));
238+
struct hwinfo *hw = (struct hwinfo *) malloc(sizeof(struct hwinfo));
239239

240240
if (!hw)
241241
return NULL;
@@ -250,7 +250,7 @@ hwinfo_create(void)
250250
struct hwinfo *
251251
hwinfo_dup(struct hwinfo *hwinfo)
252252
{
253-
struct hwinfo *hwinfocpy = malloc(sizeof(struct hwinfo));
253+
struct hwinfo *hwinfocpy = (struct hwinfo *) malloc(sizeof(struct hwinfo));
254254

255255
if (!hwinfocpy)
256256
return NULL;

src/payload.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
struct payload_cpu_data *
3939
payload_cpu_data_create(void)
4040
{
41-
struct payload_cpu_data *data = malloc(sizeof(struct payload_cpu_data));
41+
struct payload_cpu_data *data = (struct payload_cpu_data *) malloc(sizeof(struct payload_cpu_data));
4242

4343
if (!data)
4444
return NULL;
@@ -64,7 +64,7 @@ payload_cpu_data_destroy(struct payload_cpu_data **data_ptr)
6464
struct payload_pkg_data *
6565
payload_pkg_data_create(void)
6666
{
67-
struct payload_pkg_data *data = malloc(sizeof(struct payload_pkg_data));
67+
struct payload_pkg_data *data = (struct payload_pkg_data *) malloc(sizeof(struct payload_pkg_data));
6868

6969
if (!data)
7070
return NULL;
@@ -89,7 +89,7 @@ payload_pkg_data_destroy(struct payload_pkg_data **data_ptr)
8989
struct payload_group_data *
9090
payload_group_data_create(void)
9191
{
92-
struct payload_group_data *data = malloc(sizeof(struct payload_group_data));
92+
struct payload_group_data *data = (struct payload_group_data *) malloc(sizeof(struct payload_group_data));
9393

9494
if (!data)
9595
return NULL;
@@ -114,7 +114,7 @@ payload_group_data_destroy(struct payload_group_data **data_ptr)
114114
struct payload *
115115
payload_create(uint64_t timestamp, const char *target_name)
116116
{
117-
struct payload *payload = malloc(sizeof(struct payload));
117+
struct payload *payload = (struct payload *) malloc(sizeof(struct payload));
118118

119119
if (!payload)
120120
return NULL;

0 commit comments

Comments
 (0)