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