xref: /linux/tools/power/cpupower/Makefile (revision 3ae8cef210dd52ae95fd5a87f9bea0932bd4e470)
1# SPDX-License-Identifier: GPL-2.0-only
2# Makefile for cpupower
3#
4# Copyright (C) 2005,2006 Dominik Brodowski <linux@dominikbrodowski.net>
5# Copyright (C) 2025      Francesco Poli <invernomuto@paranoici.org>
6#
7# Based largely on the Makefile for udev by:
8#
9# Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
10#
11OUTPUT=./
12ifeq ("$(origin O)", "command line")
13	OUTPUT := $(O)/
14endif
15
16ifneq ($(OUTPUT),)
17# check that the output directory actually exists
18OUTDIR := $(shell cd $(OUTPUT) && pwd)
19$(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist))
20endif
21
22
23# --- CONFIGURATION BEGIN ---
24
25# Set the following to `true' to make a unstripped, unoptimized
26# binary. Leave this set to `false' for production use.
27DEBUG ?=	true
28
29# make the build silent. Set this to something else to make it noisy again.
30V ?=		false
31
32# Internationalization support (output in different languages).
33# Requires gettext.
34NLS ?=		true
35
36# Set the following to 'true' to build/install the
37# cpufreq-bench benchmarking tool
38CPUFREQ_BENCH ?= true
39
40# Do not build libraries, but build the code in statically
41# Libraries are still built, otherwise the Makefile code would
42# be rather ugly.
43export STATIC ?= false
44
45# Prefix to the directories we're installing to
46DESTDIR ?=
47
48# --- CONFIGURATION END ---
49
50
51
52# Package-related definitions. Distributions can modify the version
53# and _should_ modify the PACKAGE_BUGREPORT definition
54
55VERSION:=			$(shell ./utils/version-gen.sh)
56LIB_FIX=			1
57LIB_MIN=			0
58LIB_MAJ=			1
59LIB_VER=			$(LIB_MAJ).$(LIB_MIN).$(LIB_FIX)
60
61
62PACKAGE =			cpupower
63PACKAGE_BUGREPORT =		linux-pm@vger.kernel.org
64LANGUAGES = 			de fr it cs pt ka zh_CN
65
66
67# Directory definitions. These are default and most probably
68# do not need to be changed. Please note that DESTDIR is
69# added in front of any of them
70
71bindir ?=	/usr/bin
72sbindir ?=	/usr/sbin
73mandir ?=	/usr/man
74libdir ?=	/usr/lib
75libexecdir ?=	/usr/libexec
76includedir ?=	/usr/include
77localedir ?=	/usr/share/locale
78docdir ?=       /usr/share/doc/packages/cpupower
79confdir ?=      /etc/
80bash_completion_dir ?= /usr/share/bash-completion/completions
81
82# Toolchain: what tools do we use, and what options do they need:
83
84CP = cp -fpR
85INSTALL = /usr/bin/install -c
86INSTALL_PROGRAM = ${INSTALL}
87INSTALL_DATA  = ${INSTALL} -m 644
88SETPERM_DATA  = chmod 644
89#bash completion scripts get sourced and so they should be rw only.
90INSTALL_SCRIPT = ${INSTALL} -m 644
91
92# If you are running a cross compiler, you may want to set this
93# to something more interesting, like "arm-linux-".  If you want
94# to compile vs uClibc, that can be done here as well.
95CROSS ?= #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
96ifneq ($(CROSS), )
97CC = $(CROSS)gcc
98LD = $(CROSS)gcc
99AR = $(CROSS)ar
100STRIP = $(CROSS)strip
101RANLIB = $(CROSS)ranlib
102else
103CC ?= $(CROSS)gcc
104LD ?= $(CROSS)gcc
105AR ?= $(CROSS)ar
106STRIP ?= $(CROSS)strip
107RANLIB ?= $(CROSS)ranlib
108endif
109HOSTCC = gcc
110MKDIR = mkdir
111
112# Now we set up the build system
113#
114
115GMO_FILES = ${shell for HLANG in ${LANGUAGES}; do echo $(OUTPUT)po/$$HLANG.gmo; done;}
116
117export CROSS CC AR STRIP RANLIB CFLAGS LDFLAGS LIB_OBJS
118
119# check if compiler option is supported
120cc-supports = ${shell if $(CC) ${1} -S -o /dev/null -x c /dev/null > /dev/null 2>&1; then echo "$(1)"; fi;}
121
122# use '-Os' optimization if available, else use -O2
123OPTIMIZATION := $(call cc-supports,-Os,-O2)
124
125WARNINGS := -Wall -Wchar-subscripts -Wpointer-arith -Wsign-compare
126WARNINGS += $(call cc-supports,-Wno-pointer-sign)
127WARNINGS += $(call cc-supports,-Wdeclaration-after-statement)
128WARNINGS += -Wshadow
129
130override CFLAGS += -DVERSION=\"$(VERSION)\" -DPACKAGE=\"$(PACKAGE)\" \
131		-DPACKAGE_BUGREPORT=\"$(PACKAGE_BUGREPORT)\" -D_GNU_SOURCE
132
133UTIL_OBJS =  utils/helpers/amd.o utils/helpers/msr.o \
134	utils/helpers/sysfs.o utils/helpers/misc.o utils/helpers/cpuid.o \
135	utils/helpers/pci.o utils/helpers/bitmask.o \
136	utils/idle_monitor/nhm_idle.o utils/idle_monitor/snb_idle.o \
137	utils/idle_monitor/hsw_ext_idle.o \
138	utils/idle_monitor/amd_fam14h_idle.o utils/idle_monitor/cpuidle_sysfs.o \
139	utils/idle_monitor/mperf_monitor.o utils/idle_monitor/cpupower-monitor.o \
140	utils/idle_monitor/rapl_monitor.o \
141	utils/cpupower.o utils/cpufreq-info.o utils/cpufreq-set.o \
142	utils/cpupower-set.o utils/cpupower-info.o utils/cpuidle-info.o \
143	utils/cpuidle-set.o utils/powercap-info.o
144
145UTIL_SRC := $(UTIL_OBJS:.o=.c)
146
147UTIL_OBJS := $(addprefix $(OUTPUT),$(UTIL_OBJS))
148
149UTIL_HEADERS = utils/helpers/helpers.h utils/idle_monitor/cpupower-monitor.h \
150	utils/helpers/bitmask.h \
151	utils/idle_monitor/idle_monitors.h utils/idle_monitor/idle_monitors.def
152
153LIB_HEADERS = 	lib/cpufreq.h lib/cpupower.h lib/cpuidle.h lib/acpi_cppc.h \
154	lib/powercap.h
155LIB_SRC = 	lib/cpufreq.c lib/cpupower.c lib/cpuidle.c lib/acpi_cppc.c \
156	lib/powercap.c
157LIB_OBJS = 	lib/cpufreq.o lib/cpupower.o lib/cpuidle.o lib/acpi_cppc.o \
158	lib/powercap.o
159LIB_OBJS :=	$(addprefix $(OUTPUT),$(LIB_OBJS))
160
161override CFLAGS +=	-pipe
162
163ifeq ($(strip $(NLS)),true)
164	INSTALL_NLS += install-gmo
165	COMPILE_NLS += create-gmo
166	override CFLAGS += -DNLS
167endif
168
169ifeq ($(strip $(CPUFREQ_BENCH)),true)
170	INSTALL_BENCH += install-bench
171	COMPILE_BENCH += compile-bench
172endif
173
174ifeq ($(strip $(STATIC)),true)
175        UTIL_OBJS += $(LIB_OBJS)
176        UTIL_HEADERS += $(LIB_HEADERS)
177        UTIL_SRC += $(LIB_SRC)
178endif
179
180override CFLAGS += $(WARNINGS)
181
182ifeq ($(strip $(V)),false)
183	QUIET=@
184	ECHO=@echo
185else
186	QUIET=
187	ECHO=@\#
188endif
189export QUIET ECHO
190
191# if DEBUG is enabled, then we do not strip or optimize
192ifeq ($(strip $(DEBUG)),true)
193	override CFLAGS += -O1 -g -DDEBUG
194	STRIPCMD = /bin/true -Since_we_are_debugging
195else
196	override CFLAGS += $(OPTIMIZATION) -fomit-frame-pointer
197	STRIPCMD = $(STRIP) -s --remove-section=.note --remove-section=.comment
198endif
199
200
201# the actual make rules
202
203all: libcpupower $(OUTPUT)cpupower $(COMPILE_NLS) $(COMPILE_BENCH)
204
205$(OUTPUT)lib/%.o: $(LIB_SRC) $(LIB_HEADERS)
206	$(ECHO) "  CC      " $@
207	$(QUIET) $(CC) $(CFLAGS) -fPIC -o $@ -c lib/$*.c
208
209$(OUTPUT)libcpupower.so.$(LIB_VER): $(LIB_OBJS)
210	$(ECHO) "  LD      " $@
211	$(QUIET) $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ \
212		-Wl,-soname,libcpupower.so.$(LIB_MAJ) $(LIB_OBJS)
213	@ln -sf $(@F) $(OUTPUT)libcpupower.so
214	@ln -sf $(@F) $(OUTPUT)libcpupower.so.$(LIB_MAJ)
215
216libcpupower: $(OUTPUT)libcpupower.so.$(LIB_VER)
217
218# Let all .o files depend on its .c file and all headers
219# Might be worth to put this into utils/Makefile at some point of time
220$(UTIL_OBJS): $(UTIL_HEADERS)
221
222$(OUTPUT)%.o: %.c
223	$(ECHO) "  CC      " $@
224	$(QUIET) $(CC) $(CFLAGS) -I./lib -I ./utils -o $@ -c $*.c
225
226$(OUTPUT)cpupower: $(UTIL_OBJS) $(OUTPUT)libcpupower.so.$(LIB_VER)
227	$(ECHO) "  CC      " $@
228ifeq ($(strip $(STATIC)),true)
229	$(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lrt -lpci -L$(OUTPUT) -o $@
230else
231	$(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lcpupower -lrt -lpci -L$(OUTPUT) -o $@
232endif
233	$(QUIET) $(STRIPCMD) $@
234
235ifeq (, $(shell which xgettext))
236$(warning "Install xgettext to extract translatable strings.")
237else
238$(OUTPUT)po/$(PACKAGE).pot: $(UTIL_SRC)
239	$(ECHO) "  GETTEXT " $@
240	$(QUIET) xgettext --default-domain=$(PACKAGE) --add-comments \
241		--keyword=_ --keyword=N_ $(UTIL_SRC) -p $(@D) -o $(@F)
242endif
243
244ifeq (, $(shell which msgfmt))
245$(warning "Install msgfmt to generate binary message catalogs.")
246else
247$(OUTPUT)po/%.gmo: po/%.po
248	$(ECHO) "  MSGFMT  " $@
249	$(QUIET) msgfmt -o $@ po/$*.po
250endif
251
252create-gmo: ${GMO_FILES}
253
254ifeq (, $(shell which msgmerge))
255$(warning "Install msgmerge to merge translations.")
256else
257update-po: $(OUTPUT)po/$(PACKAGE).pot
258	$(ECHO) "  MSGMRG  " $@
259	$(QUIET) @for HLANG in $(LANGUAGES); do \
260		echo -n "Updating $$HLANG "; \
261		if msgmerge po/$$HLANG.po $< -o \
262		   $(OUTPUT)po/$$HLANG.new.po; then \
263			mv -f $(OUTPUT)po/$$HLANG.new.po $(OUTPUT)po/$$HLANG.po; \
264		else \
265			echo "msgmerge for $$HLANG failed!"; \
266			rm -f $(OUTPUT)po/$$HLANG.new.po; \
267		fi; \
268	done;
269endif
270
271compile-bench: $(OUTPUT)libcpupower.so.$(LIB_VER)
272	@V=$(V) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT)
273
274# we compile into subdirectories. if the target directory is not the
275# source directory, they might not exists. So we depend the various
276# files onto their directories.
277DIRECTORY_DEPS = $(LIB_OBJS) $(UTIL_OBJS) $(GMO_FILES)
278$(DIRECTORY_DEPS): | $(sort $(dir $(DIRECTORY_DEPS)))
279
280# In the second step, we make a rule to actually create these directories
281$(sort $(dir $(DIRECTORY_DEPS))):
282	$(ECHO) "  MKDIR      " $@
283	$(QUIET) $(MKDIR) -p $@ 2>/dev/null
284
285clean:
286	-find $(OUTPUT) \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \
287	 | xargs rm -f
288	-rm -f $(OUTPUT)cpupower
289	-rm -f $(OUTPUT)libcpupower.so*
290	-rm -rf $(OUTPUT)po/*.gmo
291	-rm -rf $(OUTPUT)po/*.pot
292	$(MAKE) -C bench O=$(OUTPUT) clean
293
294
295install-lib: libcpupower
296	$(INSTALL) -d $(DESTDIR)${libdir}
297	$(CP) $(OUTPUT)libcpupower.so* $(DESTDIR)${libdir}/
298	$(INSTALL) -d $(DESTDIR)${includedir}
299	$(INSTALL_DATA) lib/cpufreq.h $(DESTDIR)${includedir}/cpufreq.h
300	$(INSTALL_DATA) lib/cpuidle.h $(DESTDIR)${includedir}/cpuidle.h
301	$(INSTALL_DATA) lib/powercap.h $(DESTDIR)${includedir}/powercap.h
302
303install-tools: $(OUTPUT)cpupower
304	$(INSTALL) -d $(DESTDIR)${bindir}
305	$(INSTALL_PROGRAM) $(OUTPUT)cpupower $(DESTDIR)${bindir}
306	$(INSTALL) -d $(DESTDIR)${bash_completion_dir}
307	$(INSTALL_SCRIPT) cpupower-completion.sh '$(DESTDIR)${bash_completion_dir}/cpupower'
308	$(INSTALL) -d $(DESTDIR)${confdir}
309	$(INSTALL_DATA) cpupower-service.conf '$(DESTDIR)${confdir}'
310	$(INSTALL) -d $(DESTDIR)${libexecdir}
311	$(INSTALL_PROGRAM) cpupower.sh '$(DESTDIR)${libexecdir}/cpupower'
312	$(INSTALL) -d $(DESTDIR)${libdir}/systemd/system
313	sed 's|___CDIR___|${confdir}|; s|___LDIR___|${libexecdir}|' cpupower.service.in > '$(DESTDIR)${libdir}/systemd/system/cpupower.service'
314	$(SETPERM_DATA) '$(DESTDIR)${libdir}/systemd/system/cpupower.service'
315
316install-man:
317	$(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1
318	$(INSTALL_DATA) -D man/cpupower-frequency-set.1 $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1
319	$(INSTALL_DATA) -D man/cpupower-frequency-info.1 $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1
320	$(INSTALL_DATA) -D man/cpupower-idle-set.1 $(DESTDIR)${mandir}/man1/cpupower-idle-set.1
321	$(INSTALL_DATA) -D man/cpupower-idle-info.1 $(DESTDIR)${mandir}/man1/cpupower-idle-info.1
322	$(INSTALL_DATA) -D man/cpupower-set.1 $(DESTDIR)${mandir}/man1/cpupower-set.1
323	$(INSTALL_DATA) -D man/cpupower-info.1 $(DESTDIR)${mandir}/man1/cpupower-info.1
324	$(INSTALL_DATA) -D man/cpupower-monitor.1 $(DESTDIR)${mandir}/man1/cpupower-monitor.1
325	$(INSTALL_DATA) -D man/cpupower-powercap-info.1 $(DESTDIR)${mandir}/man1/cpupower-powercap-info.1
326
327install-gmo: create-gmo
328	$(INSTALL) -d $(DESTDIR)${localedir}
329	for HLANG in $(LANGUAGES); do \
330		echo '$(INSTALL_DATA) -D $(OUTPUT)po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo'; \
331		$(INSTALL_DATA) -D $(OUTPUT)po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
332	done;
333
334install-bench: compile-bench
335	@#DESTDIR must be set from outside to survive
336	@sbindir=$(sbindir) bindir=$(bindir) docdir=$(docdir) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT) install
337
338ifeq ($(strip $(STATIC)),true)
339install: all install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
340else
341install: all install-lib install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
342endif
343
344uninstall:
345	- rm -f $(DESTDIR)${libdir}/libcpupower.*
346	- rm -f $(DESTDIR)${includedir}/cpufreq.h
347	- rm -f $(DESTDIR)${includedir}/cpuidle.h
348	- rm -f $(DESTDIR)${bindir}/utils/cpupower
349	- rm -f $(DESTDIR)${confdir}cpupower-service.conf
350	- rm -f $(DESTDIR)${libexecdir}/cpupower
351	- rm -f $(DESTDIR)${libdir}/systemd/system/cpupower.service
352	- rm -f $(DESTDIR)${mandir}/man1/cpupower.1
353	- rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1
354	- rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1
355	- rm -f $(DESTDIR)${mandir}/man1/cpupower-set.1
356	- rm -f $(DESTDIR)${mandir}/man1/cpupower-info.1
357	- rm -f $(DESTDIR)${mandir}/man1/cpupower-monitor.1
358	- rm -f $(DESTDIR)${mandir}/man1/cpupower-powercap-info.1
359	- for HLANG in $(LANGUAGES); do \
360		rm -f $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
361	  done;
362
363help:
364	@echo  'Building targets:'
365	@echo  '  all		  - Default target. Could be omitted. Put build artifacts'
366	@echo  '                    to "O" cmdline option dir (default: current dir)'
367	@echo  '  install	  - Install previously built project files from the output'
368	@echo  '                    dir defined by "O" cmdline option (default: current dir)'
369	@echo  '                    to the install dir  defined by "DESTDIR" cmdline or'
370	@echo  '                    Makefile config block option (default: "")'
371	@echo  '  install-lib	  - Install previously built library binary from the output'
372	@echo  '                    dir defined by "O" cmdline option (default: current dir)'
373	@echo  '                    and library headers from "lib/" for userspace to the install'
374	@echo  '                    dir  defined by "DESTDIR" cmdline (default: "")'
375	@echo  '  install-tools	  - Install previously built "cpupower" util from the output'
376	@echo  '                    dir defined by "O" cmdline option (default: current dir) and'
377	@echo  '                    "cpupower-completion.sh" script from the src dir to the'
378	@echo  '                    install dir  defined by "DESTDIR" cmdline or Makefile'
379	@echo  '                    config block option (default: "")'
380	@echo  '  install-man	  - Install man pages from the "man" src subdir to the'
381	@echo  '                    install dir  defined by "DESTDIR" cmdline or Makefile'
382	@echo  '                    config block option (default: "")'
383	@echo  '  install-gmo	  - Install previously built language files from the output'
384	@echo  '                    dir defined by "O" cmdline option (default: current dir)'
385	@echo  '                    to the install dir defined by "DESTDIR" cmdline or Makefile'
386	@echo  '                    config block option (default: "")'
387	@echo  '  install-bench	  - Install previously built "cpufreq-bench" util files from the'
388	@echo  '                    output dir defined by "O" cmdline option (default: current dir)'
389	@echo  '                    to the install dir  defined by "DESTDIR" cmdline or Makefile'
390	@echo  '                    config block option (default: "")'
391	@echo  ''
392	@echo  'Cleaning targets:'
393	@echo  '  clean		  - Clean build artifacts from the dir defined by "O" cmdline'
394	@echo  '                    option (default: current dir)'
395	@echo  '  uninstall	  - Remove previously installed files from the dir defined by "DESTDIR"'
396	@echo  '                    cmdline or Makefile config block option (default: "")'
397
398.PHONY: all utils libcpupower update-po create-gmo install-lib install-tools install-man install-gmo install uninstall clean help
399