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