xref: /linux/tools/power/cpupower/Makefile (revision f688b599d711d169b22e99f2d055847d66c4e0d3)
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
76unitdir ?=	/usr/lib/systemd/system
77includedir ?=	/usr/include
78localedir ?=	/usr/share/locale
79docdir ?=       /usr/share/doc/packages/cpupower
80confdir ?=      /etc/
81bash_completion_dir ?= /usr/share/bash-completion/completions
82
83# Toolchain: what tools do we use, and what options do they need:
84
85CP = cp -fpR
86INSTALL = /usr/bin/install -c
87INSTALL_PROGRAM = ${INSTALL}
88INSTALL_DATA  = ${INSTALL} -m 644
89SETPERM_DATA  = chmod 644
90#bash completion scripts get sourced and so they should be rw only.
91INSTALL_SCRIPT = ${INSTALL} -m 644
92
93# If you are running a cross compiler, you may want to set this
94# to something more interesting, like "arm-linux-".  If you want
95# to compile vs uClibc, that can be done here as well.
96CROSS ?= #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
97ifneq ($(CROSS), )
98CC = $(CROSS)gcc
99LD = $(CROSS)gcc
100AR = $(CROSS)ar
101STRIP = $(CROSS)strip
102RANLIB = $(CROSS)ranlib
103else
104CC ?= $(CROSS)gcc
105LD ?= $(CROSS)gcc
106AR ?= $(CROSS)ar
107STRIP ?= $(CROSS)strip
108RANLIB ?= $(CROSS)ranlib
109endif
110HOSTCC = gcc
111MKDIR = mkdir
112
113# Now we set up the build system
114#
115
116GMO_FILES = ${shell for HLANG in ${LANGUAGES}; do echo $(OUTPUT)po/$$HLANG.gmo; done;}
117
118export CROSS CC AR STRIP RANLIB CFLAGS LDFLAGS LIB_OBJS
119
120# check if compiler option is supported
121cc-supports = ${shell if $(CC) ${1} -S -o /dev/null -x c /dev/null > /dev/null 2>&1; then echo "$(1)"; fi;}
122
123# use '-Os' optimization if available, else use -O2
124OPTIMIZATION := $(call cc-supports,-Os,-O2)
125
126WARNINGS := -Wall -Wchar-subscripts -Wpointer-arith -Wsign-compare
127WARNINGS += $(call cc-supports,-Wno-pointer-sign)
128WARNINGS += $(call cc-supports,-Wdeclaration-after-statement)
129WARNINGS += -Wshadow
130
131override CFLAGS += -DVERSION=\"$(VERSION)\" -DPACKAGE=\"$(PACKAGE)\" \
132		-DPACKAGE_BUGREPORT=\"$(PACKAGE_BUGREPORT)\" -D_GNU_SOURCE
133
134UTIL_OBJS =  utils/helpers/amd.o utils/helpers/msr.o \
135	utils/helpers/sysfs.o utils/helpers/misc.o utils/helpers/cpuid.o \
136	utils/helpers/pci.o utils/helpers/bitmask.o \
137	utils/idle_monitor/nhm_idle.o utils/idle_monitor/snb_idle.o \
138	utils/idle_monitor/hsw_ext_idle.o \
139	utils/idle_monitor/amd_fam14h_idle.o utils/idle_monitor/cpuidle_sysfs.o \
140	utils/idle_monitor/mperf_monitor.o utils/idle_monitor/cpupower-monitor.o \
141	utils/idle_monitor/rapl_monitor.o \
142	utils/cpupower.o utils/cpufreq-info.o utils/cpufreq-set.o \
143	utils/cpupower-set.o utils/cpupower-info.o utils/cpuidle-info.o \
144	utils/cpuidle-set.o utils/powercap-info.o
145
146UTIL_SRC := $(UTIL_OBJS:.o=.c)
147
148UTIL_OBJS := $(addprefix $(OUTPUT),$(UTIL_OBJS))
149
150UTIL_HEADERS = utils/helpers/helpers.h utils/idle_monitor/cpupower-monitor.h \
151	utils/helpers/bitmask.h \
152	utils/idle_monitor/idle_monitors.h utils/idle_monitor/idle_monitors.def
153
154LIB_HEADERS = 	lib/cpufreq.h lib/cpupower.h lib/cpuidle.h lib/acpi_cppc.h \
155	lib/powercap.h
156LIB_SRC = 	lib/cpufreq.c lib/cpupower.c lib/cpuidle.c lib/acpi_cppc.c \
157	lib/powercap.c
158LIB_OBJS = 	lib/cpufreq.o lib/cpupower.o lib/cpuidle.o lib/acpi_cppc.o \
159	lib/powercap.o
160LIB_OBJS :=	$(addprefix $(OUTPUT),$(LIB_OBJS))
161
162override CFLAGS +=	-pipe
163
164ifeq ($(strip $(NLS)),true)
165	INSTALL_NLS += install-gmo
166	COMPILE_NLS += create-gmo
167	override CFLAGS += -DNLS
168endif
169
170ifeq ($(strip $(CPUFREQ_BENCH)),true)
171	INSTALL_BENCH += install-bench
172	COMPILE_BENCH += compile-bench
173endif
174
175ifeq ($(strip $(STATIC)),true)
176        UTIL_OBJS += $(LIB_OBJS)
177        UTIL_HEADERS += $(LIB_HEADERS)
178        UTIL_SRC += $(LIB_SRC)
179endif
180
181override CFLAGS += $(WARNINGS)
182
183ifeq ($(strip $(V)),false)
184	QUIET=@
185	ECHO=@echo
186else
187	QUIET=
188	ECHO=@\#
189endif
190export QUIET ECHO
191
192# if DEBUG is enabled, then we do not strip or optimize
193ifeq ($(strip $(DEBUG)),true)
194	override CFLAGS += -O1 -g -DDEBUG
195	STRIPCMD = /bin/true -Since_we_are_debugging
196else
197	override CFLAGS += $(OPTIMIZATION) -fomit-frame-pointer
198	STRIPCMD = $(STRIP) -s --remove-section=.note --remove-section=.comment
199endif
200
201
202# the actual make rules
203
204all: libcpupower $(OUTPUT)cpupower $(COMPILE_NLS) $(COMPILE_BENCH)
205
206$(OUTPUT)lib/%.o: $(LIB_SRC) $(LIB_HEADERS)
207	$(ECHO) "  CC      " $@
208	$(QUIET) $(CC) $(CFLAGS) -fPIC -o $@ -c lib/$*.c
209
210$(OUTPUT)libcpupower.so.$(LIB_VER): $(LIB_OBJS)
211	$(ECHO) "  LD      " $@
212	$(QUIET) $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ \
213		-Wl,-soname,libcpupower.so.$(LIB_MAJ) $(LIB_OBJS)
214	@ln -sf $(@F) $(OUTPUT)libcpupower.so
215	@ln -sf $(@F) $(OUTPUT)libcpupower.so.$(LIB_MAJ)
216
217libcpupower: $(OUTPUT)libcpupower.so.$(LIB_VER)
218
219# Let all .o files depend on its .c file and all headers
220# Might be worth to put this into utils/Makefile at some point of time
221$(UTIL_OBJS): $(UTIL_HEADERS)
222
223$(OUTPUT)%.o: %.c
224	$(ECHO) "  CC      " $@
225	$(QUIET) $(CC) $(CFLAGS) -I./lib -I ./utils -o $@ -c $*.c
226
227$(OUTPUT)cpupower: $(UTIL_OBJS) $(OUTPUT)libcpupower.so.$(LIB_VER)
228	$(ECHO) "  CC      " $@
229ifeq ($(strip $(STATIC)),true)
230	$(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lrt -lpci -L$(OUTPUT) -o $@
231else
232	$(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lcpupower -lrt -lpci -L$(OUTPUT) -o $@
233endif
234	$(QUIET) $(STRIPCMD) $@
235
236ifeq (, $(shell which xgettext))
237$(warning "Install xgettext to extract translatable strings.")
238else
239$(OUTPUT)po/$(PACKAGE).pot: $(UTIL_SRC)
240	$(ECHO) "  GETTEXT " $@
241	$(QUIET) xgettext --default-domain=$(PACKAGE) --add-comments \
242		--keyword=_ --keyword=N_ $(UTIL_SRC) -p $(@D) -o $(@F)
243endif
244
245ifeq (, $(shell which msgfmt))
246$(warning "Install msgfmt to generate binary message catalogs.")
247else
248$(OUTPUT)po/%.gmo: po/%.po
249	$(ECHO) "  MSGFMT  " $@
250	$(QUIET) msgfmt -o $@ po/$*.po
251endif
252
253create-gmo: ${GMO_FILES}
254
255ifeq (, $(shell which msgmerge))
256$(warning "Install msgmerge to merge translations.")
257else
258update-po: $(OUTPUT)po/$(PACKAGE).pot
259	$(ECHO) "  MSGMRG  " $@
260	$(QUIET) @for HLANG in $(LANGUAGES); do \
261		echo -n "Updating $$HLANG "; \
262		if msgmerge po/$$HLANG.po $< -o \
263		   $(OUTPUT)po/$$HLANG.new.po; then \
264			mv -f $(OUTPUT)po/$$HLANG.new.po $(OUTPUT)po/$$HLANG.po; \
265		else \
266			echo "msgmerge for $$HLANG failed!"; \
267			rm -f $(OUTPUT)po/$$HLANG.new.po; \
268		fi; \
269	done;
270endif
271
272compile-bench: $(OUTPUT)libcpupower.so.$(LIB_VER)
273	@V=$(V) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT)
274
275# we compile into subdirectories. if the target directory is not the
276# source directory, they might not exists. So we depend the various
277# files onto their directories.
278DIRECTORY_DEPS = $(LIB_OBJS) $(UTIL_OBJS) $(GMO_FILES)
279$(DIRECTORY_DEPS): | $(sort $(dir $(DIRECTORY_DEPS)))
280
281# In the second step, we make a rule to actually create these directories
282$(sort $(dir $(DIRECTORY_DEPS))):
283	$(ECHO) "  MKDIR      " $@
284	$(QUIET) $(MKDIR) -p $@ 2>/dev/null
285
286clean:
287	-find $(OUTPUT) \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \
288	 | xargs rm -f
289	-rm -f $(OUTPUT)cpupower
290	-rm -f $(OUTPUT)libcpupower.so*
291	-rm -rf $(OUTPUT)po/*.gmo
292	-rm -rf $(OUTPUT)po/*.pot
293	$(MAKE) -C bench O=$(OUTPUT) clean
294
295
296install-lib: libcpupower
297	$(INSTALL) -d $(DESTDIR)${libdir}
298	$(CP) $(OUTPUT)libcpupower.so* $(DESTDIR)${libdir}/
299	$(INSTALL) -d $(DESTDIR)${includedir}
300	$(INSTALL_DATA) lib/cpufreq.h $(DESTDIR)${includedir}/cpufreq.h
301	$(INSTALL_DATA) lib/cpuidle.h $(DESTDIR)${includedir}/cpuidle.h
302	$(INSTALL_DATA) lib/powercap.h $(DESTDIR)${includedir}/powercap.h
303
304install-tools: $(OUTPUT)cpupower
305	$(INSTALL) -d $(DESTDIR)${bindir}
306	$(INSTALL_PROGRAM) $(OUTPUT)cpupower $(DESTDIR)${bindir}
307	$(INSTALL) -d $(DESTDIR)${bash_completion_dir}
308	$(INSTALL_SCRIPT) cpupower-completion.sh '$(DESTDIR)${bash_completion_dir}/cpupower'
309	$(INSTALL) -d $(DESTDIR)${confdir}
310	$(INSTALL_DATA) cpupower-service.conf '$(DESTDIR)${confdir}'
311	$(INSTALL) -d $(DESTDIR)${libexecdir}
312	$(INSTALL_PROGRAM) cpupower.sh '$(DESTDIR)${libexecdir}/cpupower'
313	$(INSTALL) -d $(DESTDIR)${unitdir}
314	sed 's|___CDIR___|${confdir}|; s|___LDIR___|${libexecdir}|' cpupower.service.in > '$(DESTDIR)${unitdir}/cpupower.service'
315	$(SETPERM_DATA) '$(DESTDIR)${unitdir}/cpupower.service'
316
317install-man:
318	$(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1
319	$(INSTALL_DATA) -D man/cpupower-frequency-set.1 $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1
320	$(INSTALL_DATA) -D man/cpupower-frequency-info.1 $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1
321	$(INSTALL_DATA) -D man/cpupower-idle-set.1 $(DESTDIR)${mandir}/man1/cpupower-idle-set.1
322	$(INSTALL_DATA) -D man/cpupower-idle-info.1 $(DESTDIR)${mandir}/man1/cpupower-idle-info.1
323	$(INSTALL_DATA) -D man/cpupower-set.1 $(DESTDIR)${mandir}/man1/cpupower-set.1
324	$(INSTALL_DATA) -D man/cpupower-info.1 $(DESTDIR)${mandir}/man1/cpupower-info.1
325	$(INSTALL_DATA) -D man/cpupower-monitor.1 $(DESTDIR)${mandir}/man1/cpupower-monitor.1
326	$(INSTALL_DATA) -D man/cpupower-powercap-info.1 $(DESTDIR)${mandir}/man1/cpupower-powercap-info.1
327
328install-gmo: create-gmo
329	$(INSTALL) -d $(DESTDIR)${localedir}
330	for HLANG in $(LANGUAGES); do \
331		echo '$(INSTALL_DATA) -D $(OUTPUT)po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo'; \
332		$(INSTALL_DATA) -D $(OUTPUT)po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
333	done;
334
335install-bench: compile-bench
336	@#DESTDIR must be set from outside to survive
337	@sbindir=$(sbindir) bindir=$(bindir) docdir=$(docdir) confdir=$(confdir) $(MAKE) -C bench O=$(OUTPUT) install
338
339ifeq ($(strip $(STATIC)),true)
340install: all install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
341else
342install: all install-lib install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
343endif
344
345uninstall:
346	- rm -f $(DESTDIR)${libdir}/libcpupower.*
347	- rm -f $(DESTDIR)${includedir}/cpufreq.h
348	- rm -f $(DESTDIR)${includedir}/cpuidle.h
349	- rm -f $(DESTDIR)${bindir}/utils/cpupower
350	- rm -f $(DESTDIR)${confdir}cpupower-service.conf
351	- rm -f $(DESTDIR)${libexecdir}/cpupower
352	- rm -f $(DESTDIR)${unitdir}/cpupower.service
353	- rm -f $(DESTDIR)${mandir}/man1/cpupower.1
354	- rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1
355	- rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1
356	- rm -f $(DESTDIR)${mandir}/man1/cpupower-set.1
357	- rm -f $(DESTDIR)${mandir}/man1/cpupower-info.1
358	- rm -f $(DESTDIR)${mandir}/man1/cpupower-monitor.1
359	- rm -f $(DESTDIR)${mandir}/man1/cpupower-powercap-info.1
360	- for HLANG in $(LANGUAGES); do \
361		rm -f $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
362	  done;
363
364help:
365	@echo  'Building targets:'
366	@echo  '  all		  - Default target. Could be omitted. Put build artifacts'
367	@echo  '                    to "O" cmdline option dir (default: current dir)'
368	@echo  '  install	  - Install previously built project files from the output'
369	@echo  '                    dir defined by "O" cmdline option (default: current dir)'
370	@echo  '                    to the install dir  defined by "DESTDIR" cmdline or'
371	@echo  '                    Makefile config block option (default: "")'
372	@echo  '  install-lib	  - Install previously built library binary from the output'
373	@echo  '                    dir defined by "O" cmdline option (default: current dir)'
374	@echo  '                    and library headers from "lib/" for userspace to the install'
375	@echo  '                    dir  defined by "DESTDIR" cmdline (default: "")'
376	@echo  '  install-tools	  - Install previously built "cpupower" util from the output'
377	@echo  '                    dir defined by "O" cmdline option (default: current dir) and'
378	@echo  '                    "cpupower-completion.sh" script from the src dir to the'
379	@echo  '                    install dir  defined by "DESTDIR" cmdline or Makefile'
380	@echo  '                    config block option (default: "")'
381	@echo  '  install-man	  - Install man pages from the "man" src subdir to the'
382	@echo  '                    install dir  defined by "DESTDIR" cmdline or Makefile'
383	@echo  '                    config block option (default: "")'
384	@echo  '  install-gmo	  - Install previously built language files from the output'
385	@echo  '                    dir defined by "O" cmdline option (default: current dir)'
386	@echo  '                    to the install dir defined by "DESTDIR" cmdline or Makefile'
387	@echo  '                    config block option (default: "")'
388	@echo  '  install-bench	  - Install previously built "cpufreq-bench" util files from the'
389	@echo  '                    output dir defined by "O" cmdline option (default: current dir)'
390	@echo  '                    to the install dir  defined by "DESTDIR" cmdline or Makefile'
391	@echo  '                    config block option (default: "")'
392	@echo  ''
393	@echo  'Cleaning targets:'
394	@echo  '  clean		  - Clean build artifacts from the dir defined by "O" cmdline'
395	@echo  '                    option (default: current dir)'
396	@echo  '  uninstall	  - Remove previously installed files from the dir defined by "DESTDIR"'
397	@echo  '                    cmdline or Makefile config block option (default: "")'
398
399.PHONY: all utils libcpupower update-po create-gmo install-lib install-tools install-man install-gmo install uninstall clean help
400