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