OS-agnostic process stat
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
4.3 KiB

  1. ROOT_DIR := $(shell dirname "$(realpath $(lastword $(MAKEFILE_LIST)))")
  2. BUILD ?= $(ROOT_DIR)/build
  3. CC ?= cc
  4. CFLAGS ?= -Wall -g -fPIC -std=c99
  5. LIB :=
  6. INC := -I. -I$(BUILD) -I$(BUILD)/include/pstat
  7. C_SRCS:= $(wildcard *.c) $(wildcard os/*.c)
  8. TOOL_C_SRCS := $(wildcard tools/*.c)
  9. HEADERS := $(wildcard *.h) $(wildcard os/*.h)
  10. OBJ := $(patsubst %.c,$(BUILD)/%.o,$(C_SRCS))
  11. TOOL_OBJ := $(patsubst %.c,$(BUILD)/%.o,$(TOOL_C_SRCS))
  12. DEFS := -D_THREAD_SAFE -D__STDC_FORMAT_MACROS
  13. VERSION_MAJOR := 1
  14. VERSION_MINOR := 0
  15. VERSION_PATCH := 2
  16. LIBPSTAT := libpstat.so
  17. LIBPSTAT_SO := libpstat.so.$(VERSION_MAJOR)
  18. LIBPSTAT_LIB := libpstat.so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
  19. PSTAT := tools/pstat
  20. PREFIX ?= /usr/local
  21. DESTDIR ?= /
  22. INCLUDE_PREFIX ?= $(PREFIX)
  23. BINDIR ?= $(DESTDIR)/$(PREFIX)/bin
  24. LIBDIR ?= $(DESTDIR)/$(PREFIX)/lib
  25. INCLUDE_DIR ?= $(DESTDIR)/$(INCLUDE_PREFIX)/include/pstat
  26. PKGCONFIGDIR ?= $(DESTDIR)/$(PREFIX)/lib/pkgconfig
  27. OS ?= LINUX
  28. BUILD_LIBPSTAT_LIB := $(patsubst %,$(BUILD)/$(LIBDIR)/%,$(LIBPSTAT_LIB))
  29. INSTALL_LIBPSTAT_LIB := $(patsubst $(BUILD)/$(LIBDIR)/%,$(LIBDIR)/%,$(BUILD_LIBPSTAT_LIB))
  30. BUILD_LIBPSTAT_SO := $(patsubst %,$(BUILD)/$(LIBDIR)/%,$(LIBPSTAT_SO))
  31. INSTALL_LIBPSTAT_SO := $(patsubst $(BUILD)/$(LIBDIR)/%,$(LIBDIR)/%,$(BUILD_LIBPSTAT_SO))
  32. BUILD_LIBPSTAT := $(patsubst %,$(BUILD)/$(LIBDIR)/%,$(LIBPSTAT))
  33. INSTALL_LIBPSTAT := $(patsubst $(BUILD)/$(LIBDIR)/%,$(LIBDIR)/%,$(BUILD_LIBPSTAT))
  34. # NOTE: don't install os/ headers, since they're different for each OS.
  35. BUILD_HEADERS := $(patsubst %,$(BUILD)/$(INCLUDE_DIR)/%,$(HEADERS))
  36. INSTALL_HEADERS := $(patsubst $(BUILD)/$(INCLUDE_DIR)/%,$(INCLUDE_DIR)/%,$(BUILD_HEADERS))
  37. BUILD_PC_FILE := $(BUILD)/libpstat.pc
  38. INSTALL_PC_FILE := $(PKGCONFIGDIR)/libpstat.pc
  39. BUILD_PSTAT := $(patsubst tools/%,$(BUILD)/$(BINDIR)/%,$(PSTAT))
  40. INSTALL_PSTAT := $(patsubst $(BUILD)/$(BINDIR)/%,$(BINDIR)/%,$(BUILD_PSTAT))
  41. all: tools libs
  42. tools: $(BUILD_PSTAT)
  43. libs: $(BUILD_HEADERS) $(BUILD_PC_FILE) $(BUILD_LIBPSTAT) $(BUILD_LIBPSTAT_SO) $(BUILD_LIBPSTAT_LIB)
  44. $(BUILD_PC_FILE): libpstat.pc.in
  45. @mkdir -p "$(shell dirname "$@")"
  46. @cat $< | \
  47. sed -e 's~@PREFIX@~$(PREFIX)~g;' | \
  48. sed -e 's~@INCLUDEDIR@~$(INCLUDE_DIR)~g;' | \
  49. sed -e 's~@VERSION@~$(VERSION)~g; ' | \
  50. sed -e 's~@LIBS@~$(LIB)~g; ' | \
  51. sed -e 's~@LIBDIR@~$(LIBDIR)~g; ' | \
  52. sed -e 's~@VERSION_MAJOR@~$(VERSION_MAJOR)~g; ' | \
  53. sed -e 's~@VERSION_MINOR@~$(VERSION_MINOR)~g; ' | \
  54. sed -e 's~@VERSION_PATCH@~$(VERSION_PATCH)~g; ' > $@
  55. $(BUILD_LIBPSTAT_LIB): $(OBJ)
  56. @mkdir -p "$(shell dirname "$@")"
  57. $(CC) $(CFLAGS) -shared -Wl,-soname,$(LIBPSTAT_SO) -o "$@" $(OBJ) $(LIBINC) $(LIB)
  58. $(BUILD_LIBPSTAT_SO): $(BUILD_LIBPSTAT_LIB)
  59. @mkdir -p "$(shell dirname "$@")"
  60. @ln -sf "$(shell basename "$<")" "$@"
  61. $(BUILD_LIBPSTAT): $(BUILD_LIBPSTAT_SO)
  62. @mkdir -p "$(shell dirname "$@")"
  63. @ln -sf "$(shell basename "$<")" "$@"
  64. $(BUILD_PSTAT): $(TOOL_OBJ) $(BUILD_LIBPSTAT)
  65. @mkdir -p "$(shell dirname "$@")"
  66. $(CC) $(CFLAGS) -o "$@" $(TOOL_OBJ) $(LIBINC) -L$(BUILD)/$(LIBDIR) -lpstat
  67. $(BUILD)/$(INCLUDE_DIR)/%.h: %.h
  68. @mkdir -p "$(shell dirname "$@")"
  69. @cp -a "$<" "$@"
  70. $(INSTALL_LIBPSTAT): $(BUILD_LIBPSTAT)
  71. @mkdir -p "$(shell dirname "$@")"
  72. cp -a "$<" "$@"
  73. $(INSTALL_LIBPSTAT_SO): $(BUILD_LIBPSTAT_SO)
  74. @mkdir -p "$(shell dirname "$@")"
  75. cp -a "$<" "$@"
  76. $(INSTALL_LIBPSTAT_LIB): $(BUILD_LIBPSTAT_LIB)
  77. @mkdir -p "$(shell dirname "$@")"
  78. cp -a "$<" "$@"
  79. $(INCLUDE_DIR)/%.h: $(BUILD)/$(INCLUDE_DIR)/%.h
  80. @mkdir -p "$(shell dirname "$@")"
  81. cp -a "$<" "$@"
  82. $(INSTALL_PC_FILE): $(BUILD_PC_FILE)
  83. @mkdir -p "$(shell dirname "$@")"
  84. cp -a "$<" "$@"
  85. $(INSTALL_PSTAT): $(BUILD_PSTAT)
  86. @mkdir -p "$(shell dirname "$@")"
  87. cp -a "$<" "$@"
  88. libs-install: $(INSTALL_LIBPSTAT) $(INSTALL_LIBPSTAT_SO) $(INSTALL_LIBPSTAT_LIB)
  89. tools-install: $(INSTALL_PSTAT)
  90. headers-install: $(INSTALL_HEADERS)
  91. install: libs-install tools-install headers-install
  92. $(BUILD)/%.o : %.c
  93. @mkdir -p "$(shell dirname "$@")"
  94. $(CC) $(CFLAGS) -o "$@" $(INC) -c "$<" $(DEFS) -D_$(OS)
  95. .PHONY: clean
  96. clean:
  97. rm -f $(BUILD_HEADERS) $(BUILD_LIBPSTAT_LIB) $(BUILD_LIBPSTAT_SO) $(BUILD_LIBPSTAT) $(BUILD_PC_FILE) $(BUILD_HEADERS) $(BUILD_PSTAT) $(OBJ)
  98. .PHONY: uninstall
  99. uninstall:
  100. rm -f $(INSTALL_HEADERS) $(INSTALL_LIBPSTAT_LIB) $(INSTALL_LIBPSTAT_SO) $(INSTALL_LIBPSTAT) $(INSTALL_PC_FILE) $(INSTALL_PSTAT)
  101. print-%: ; @echo $*=$($*)