1# SPDX-License-Identifier: GPL-2.0
2CC		= $(CROSS_COMPILE)gcc
3BUILD_OUTPUT	:= $(CURDIR)
4PREFIX		?= /usr
5DESTDIR		?=
6DAY		:= $(shell date +%Y.%m.%d)
7SNAPSHOT		= turbostat-$(DAY)
8
9ifeq ("$(origin O)", "command line")
10	BUILD_OUTPUT := $(O)
11endif
12
13turbostat : turbostat.c
14override CFLAGS +=	-O2 -Wall -Wextra -I../../../include
15override CFLAGS +=	-DMSRHEADER='"../../../../arch/x86/include/asm/msr-index.h"'
16override CFLAGS +=	-DINTEL_FAMILY_HEADER='"../../../../arch/x86/include/asm/intel-family.h"'
17override CFLAGS +=	-DBUILD_BUG_HEADER='"../../../../include/linux/build_bug.h"'
18override CFLAGS +=	-D_FILE_OFFSET_BITS=64
19override CFLAGS +=	-D_FORTIFY_SOURCE=2
20
21%: %.c
22	@mkdir -p $(BUILD_OUTPUT)
23	$(CC) $(CFLAGS) $< -o $(BUILD_OUTPUT)/$@ $(LDFLAGS) -lcap -lrt
24
25.PHONY : clean
26clean :
27	@rm -f $(BUILD_OUTPUT)/turbostat
28	@rm -f $(SNAPSHOT).tar.gz
29
30install : turbostat
31	install -d $(DESTDIR)$(PREFIX)/bin
32	install $(BUILD_OUTPUT)/turbostat $(DESTDIR)$(PREFIX)/bin/turbostat
33	install -d $(DESTDIR)$(PREFIX)/share/man/man8
34	install -m 644 turbostat.8 $(DESTDIR)$(PREFIX)/share/man/man8
35
36snapshot: turbostat
37	@rm -rf $(SNAPSHOT)
38	@mkdir $(SNAPSHOT)
39	@cp turbostat Makefile turbostat.c turbostat.8 ../../../../arch/x86/include/asm/intel-family.h $(SNAPSHOT)
40
41	@sed -e 's/^#include <linux\/bits.h>/#include "bits.h"/' ../../../../arch/x86/include/asm/msr-index.h > $(SNAPSHOT)/msr-index.h
42	@echo '#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))' >> $(SNAPSHOT)/msr-index.h
43	@echo "#define BIT(x) (1 << (x))" > $(SNAPSHOT)/bits.h
44	@echo "#define BIT_ULL(nr) (1ULL << (nr))" >> $(SNAPSHOT)/bits.h
45	@echo "#define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h))))" >> $(SNAPSHOT)/bits.h
46	@echo "#define GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 - (h))))" >> $(SNAPSHOT)/bits.h
47
48	@echo '#define BUILD_BUG_ON(cond) do { enum { compile_time_check ## __COUNTER__ = 1/(!(cond)) }; } while (0)' > $(SNAPSHOT)/build_bug.h
49	@echo '#define __must_be_array(arr) 0' >> $(SNAPSHOT)/build_bug.h
50
51	@echo PWD=. > $(SNAPSHOT)/Makefile
52	@echo "CFLAGS +=	-DMSRHEADER='\"msr-index.h\"'" >> $(SNAPSHOT)/Makefile
53	@echo "CFLAGS +=	-DINTEL_FAMILY_HEADER='\"intel-family.h\"'" >> $(SNAPSHOT)/Makefile
54	@echo "CFLAGS +=	-DBUILD_BUG_HEADER='\"build_bug.h\"'" >> $(SNAPSHOT)/Makefile
55	@sed -e's/.*MSRHEADER.*//' -e's/.*INTEL_FAMILY_HEADER.*//' -e's/.*BUILD_BUG_HEADER.*//' Makefile >> $(SNAPSHOT)/Makefile
56
57	@rm -f $(SNAPSHOT).tar.gz
58	tar cvzf $(SNAPSHOT).tar.gz $(SNAPSHOT)
59