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