Makefile 13 KB

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