summaryrefslogtreecommitdiff
path: root/mk/build.mk
blob: 40b2b9ef2887939de36e8ef62e99d903cc38eea2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
O := build
src := $(abspath .)

obj = $(O)/$(rule_top)/$(rule_target)-$(build_id)
build_id = $(call per_target,build_id)

build_vars = $(foreach var,$(1),$(call add_build_var,$(var))$(newline))
add_build_var = \
  $(call target_var,build_id_text) += $$(let val,$$($(1)),$$(if $$(val),$(1)="$$(strip $$(val))"))

build_makefiles := $(wildcard mk/*.mk)
$(build_makefiles):

build_makefiles += Makefile
build_stack :=

define enter_build
  build_stack += $$(rule_target);$$(rule_top)

  rule_top := $(1)
  rule_target := $(if $(2),$(2),$(target))
  $$(call target_var,build_id_text) =
endef

define exit_build
  last_build := $$(lastword $$(build_stack))
  build_stack := $$(filter-out $$(last_build),$$(build_stack))
  last_build := $$(subst ;,$(space),$$(last_build))

  rule_top := $$(lastword $$(last_build))
  rule_target := $$(firstword $$(last_build))
endef

define setup_obj
  export build_id_text := $$(strip $$(call per_target,build_id_text))
  $$(call target_var,build_id) := $$(shell echo -n "$$$$build_id_text" | sha1sum | head -c8)
  unexport build_id_text

  $$(obj): export CONTENTS := $$(build_id_text)
  $$(obj):
	@mkdir -p $$@ && echo -n "$$$$CONTENTS" >$$@/build-vars
endef

define find_command_lazy
  $(2)_cmdline := $$($(2))
  override $(call defer,$(2),$$(call find_command,$(1),$(2)))
endef

define find_command
  override $(2) := $$($(2)_cmdline)
  ifeq (,$$($(2)))
    override $(2) := $(1)
  endif

  which_out := $$(shell which $$($(2)) 2>/dev/null)

  ifneq (0,$$(.SHELLSTATUS))
    which_out :=
  endif

  ifeq (,$$(which_out))
    $$(error $(1) ($$($2)) not found)
  endif
endef

shell_defer = $(call defer,$(1),$(1) := $$(call shell_checked,$(2)))
shell_checked = $(shell $(1))$(if $(filter-out 0,$(.SHELLSTATUS)),$(error Command failed: $(1)))

define find_with_pkgconfig
  pkgs := $(strip $(1))

  ifneq (,$$(pkgs))
    ifeq (undefined,$$(origin pkgconfig_cflags/$$(pkgs)))
      $$(eval $$(run_pkgconfig))
    endif

    $(2) += $$(pkgconfig_cflags/$$(pkgs))
    $(3) += $$(pkgconfig_libs/$$(pkgs))
  endif
endef

define run_pkgconfig
  pkgconfig_cflags/$$(pkgs) := $$(shell $$(PKG_CONFIG) --cflags $$(pkgs))
  ifeq (0,$$(.SHELLSTATUS))
    pkgconfig_libs/$$(pkgs) := $$(shell $$(PKG_CONFIG) --libs $$(pkgs))
  endif

  ifneq (0,$$(.SHELLSTATUS))
    $$(error pkg-config failed for package list: $$(pkgs))
  endif
endef