diff mbox series

[v2,1/3] rt-tests: Detect libcpupower presence

Message ID 20241113114509.1058593-2-tglozar@redhat.com
State New
Headers show
Series rt-tests: cyclictest: Support idle state disabling via libcpupower | expand

Commit Message

Tomas Glozar Nov. 13, 2024, 11:45 a.m. UTC
From: Tomas Glozar <tglozar@redhat.com>

Add test to detect the presence of libcpupower on the system, allowing
to conditionally build features that depend on it.

The test uses a similar mechanism to the Linux kernel's tools' feature
detection: a small C program using the library is compiled, and based on
the success of the compilation, the presence of the library is
determined.

no_libcpupower=1 may be passed to make to disable libcpupower dependency
even on systems where the library is present.

Signed-off-by: Tomas Glozar <tglozar@redhat.com>
---
 Makefile                   | 15 +++++++++++++++
 feature/Makefile           | 12 ++++++++++++
 feature/test-feature.mak   |  5 +++++
 feature/test-libcpupower.c |  8 ++++++++
 4 files changed, 40 insertions(+)
 create mode 100644 feature/Makefile
 create mode 100644 feature/test-feature.mak
 create mode 100644 feature/test-libcpupower.c
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index e2f8579..60d2f69 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,6 @@ 
 # SPDX-License-Identifier: GPL-2.0-or-later
+include feature/test-feature.mak
+
 VERSION = 2.7
 CC = $(CROSS_COMPILE)gcc
 AR = $(CROSS_COMPILE)ar
@@ -37,6 +39,19 @@  LDFLAGS ?=
 
 PYLIB ?= $(shell python3 -m get_pylib)
 
+# Check for optional libcpupower dependency
+ifneq ($(no_libcpupower), 1)
+ifeq ($(call test-feature,libcpupower), 0)
+CPPFLAGS += -DHAVE_LIBCPUPOWER_SUPPORT
+LDFLAGS += -lcpupower
+else
+$(warning libcpupower is missing, building without --deepest-idle-state support.)
+$(warning Please install libcpupower-dev/kernel-tools-libs-devel)
+endif
+else
+$(warning libcpupower disabled, building without --deepest-idle-state support.)
+endif
+
 # Check for errors, such as python3 not available
 ifeq (${PYLIB},)
 	undefine PYLIB
diff --git a/feature/Makefile b/feature/Makefile
new file mode 100644
index 0000000..de08121
--- /dev/null
+++ b/feature/Makefile
@@ -0,0 +1,12 @@ 
+# SPDX-License-Identifier: GPL-2.0-or-later
+all: feature-libcpupower
+
+feature-libcpupower: $(OBJDIR)/test-libcpupower.o
+
+$(OBJDIR)/test-libcpupower.o: feature/test-libcpupower.c
+	@$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< -lcpupower -o $@
+
+.PHONY: clean
+
+clean:
+	rm -f $(OBJDIR)/test-*.o
diff --git a/feature/test-feature.mak b/feature/test-feature.mak
new file mode 100644
index 0000000..0b3e51c
--- /dev/null
+++ b/feature/test-feature.mak
@@ -0,0 +1,5 @@ 
+# SPDX-License-Identifier: GPL-2.0-or-later
+define test-feature
+$(shell $(MAKE) OBJDIR=$(OBJDIR) CFLAGS=$(CFLAGS) CPPFLAGS=$(CPPFLAGS) LDFLAGS=$(LDFLAGS) \
+-f feature/Makefile feature-$1 clean >/dev/null 2>/dev/null; echo $$?)
+endef
diff --git a/feature/test-libcpupower.c b/feature/test-libcpupower.c
new file mode 100644
index 0000000..cd16269
--- /dev/null
+++ b/feature/test-libcpupower.c
@@ -0,0 +1,8 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include <cpuidle.h>
+
+int main(void)
+{
+	int rv = cpuidle_state_count(0);
+	return rv;
+}