NLS =		true
CPUFREQ_BENCH = true
DESTDIR =
VERSION=			$(shell ./utils/version-gen.sh)
LIB_MAJ=			maj
LIB_MIN=			0

PACKAGE =			cpupower LIB_MAJ
PACKAGE_BUGREPORT =		cpufreq@vger.kernel.org
LANGUAGES = 			LIB_de LIB_fr LIB_it LIB_cs LIB_pt

bindir =	/usr/bin
sbindir =	/usr/sbin
mandir =	/usr/man
includedir =	/usr/include
libdir =	/usr/lib
localedir =	/usr/share/locale
docdir =       /usr/share/doc/packages/cpupower
confdir =      /etc/

CP = cp -fpR
INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL}
INSTALL_DATA  = ${INSTALL} -m 644
INSTALL_SCRIPT = ${INSTALL_PROGRAM}

CROSS = 
CC = $(CROSS)gcc
LD = $(CROSS)gcc
AR = $(CROSS)ar
STRIP = $(CROSS)strip
RANLIB = $(CROSS)ranlib
HOSTCC = gcc

PWD = $(shell pwd)

GMO_FILES = ${shell for HLANG in ${LANGUAGES}; do echo po.gmo; done;}

cc-supports = ${shell if $(CC) ${1} -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; fi;}

OPTIMIZATION := $(call cc-supports,-Os,-O2)

WARNINGS := -Wall -Wchar-subscripts -Wpointer-arith -Wsign-compare
WARNINGS += $(call cc-supports,-Wno-pointer-sign)
WARNINGS += $(call cc-supports,-Wdeclaration-after-statement)
WARNINGS += -Wshadow

defined := $(1)_SRC

CFLAGS += -DVERSION=\"$(VERSION)\" -DPACKAGE=\"$(PACKAGE)\" -DPACKAGE_BUGREPORT=\"$(PACKAGE_BUGREPORT)\" -D_GNU_SOURCE CFLAGS

CPPFLG =  $(CC) helpers.h utils/cpupower-monitor.h $(CPPFLG)

UTIL_OBJS =  utils/helpers/amd.o utils/helpers/topology.o utils/helpers/msr.o utils/helpers/sysfs.o utils/helpers/misc.o utils/helpers/cpuid.o utils/helpers/pci.o utils/helpers/bitmask.o utils/idle_monitor/nhm_idle.o utils/idle_monitor/snb_idle.o utils/idle_monitor/amd_fam14h_idle.o utils/idle_monitor/cpuidle_sysfs.o utils/idle_monitor/mperf_monitor.o utils/idle_monitor/cpupower-monitor.o utils/cpupower.o utils/cpufreq-info.o utils/cpufreq-set.o utils/cpupower-set.o utils/cpupower-info.o utils/cpuidle-info.o

UTIL_HEADERS = utils/helpers/helpers.h utils/idle_monitor/cpupower-monitor.h utils/helpers/bitmask.h utils/idle_monitor/idle_monitors.h utils/idle_monitor/idle_monitors.def

UTIL_SRC := $(patsubst %.o,%.c, $(UTIL_OBJS))

$(call defined, LIB_)

LIB_HEADERS = 	lib/cpufreq.h lib/sysfs.h
LIB_SRC = 	lib/cpufreq.c lib/sysfs.c
LIB_OBJS = 	lib/cpufreq.o lib/sysfs.o

CFLAGS +=	-pipe

ifeq ($(strip $(NLS)),true)
	INSTALL_NLS += install-gmo
	CFLAGS += -DNLS
endif

ifeq ($(strip $(CPUFREQ_BENCH)),true)
	INSTALL_BENCH += install-bench
endif

CFLAGS += $(WARNINGS)
ECHO=@echo
QUIET=

CFLAGS += -O1 -g -DDEBUG 
STRIPCMD = /bin/true -Since_we_are_debugging


all: libcpupower cpupower

cmd.po : cmd.gmo
	$(QUIET) msgfmt -o $@

lib/%.o: $(LIB_SRC) $(LIB_HEADERS) libcpupower.hh.cb sysfs.hh.cb
	$(QUIET) $(CC) $(CFLAGS) -fPIC -o $@ -c lib.c $($(call defined, LIB_))

libcpupower.so.$(LIB_MAJ): $(LIB_OBJS) lib/sysfs.o
	$(QUIET) $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ -Wl,-soname,libcpupower.so.$(LIB_MIN) $(LIB_OBJS)

libcpupower: sysfs.hh.cb libcpupower.so.$(LIB_MAJ) libcpupower.hh.cb

po/libcpupower.po : $(shell find 'po/libcpupower.gmo')

$(UTIL_OBJS): $(UTIL_HEADERS) sysfs.hh.cb libcpupower.hh.cb

cpupower: $(UTIL_OBJS) libcpupower.so.$(LIB_MAJ) sysfs.hh.cb
	$(QUIET) $(STRIPCMD) $@

%.gmo: %.po
	$(QUIET) msgfmt -o $@

create-gmo: ${GMO_FILES}

clean:
	-rm -f $(UTIL_BINS)
	-rm -f $(IDLE_OBJS)

install-lib:
	$(INSTALL) -d $(DESTDIR)${libdir}

install-tools:
	$(INSTALL) -d $(DESTDIR)${libdir}

install-man:
	$(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1 CFLAGS

install-gmo:
	$(INSTALL) -d $(DESTDIR)${localedir}

install: all install-lib install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)