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