RN rnmake  3.0.0
Std.mk
Go to the documentation of this file.
1 ################################################################################
2 #
3 # Std.mk
4 #
5 ifdef RNMAKE_DOXY
6 /*!
7 \file
8 
9 \brief A collection of make defined functions, canned sequences, variables, and
10 targets that any RN Make System makefile may use.
11 
12 This makefile is mostly independent of RNMAKE variables and, hence, can usually
13 be included anywhere in the including makefile.
14 
15 Exceptions are:
16 - RNMAKE_ROOT for colors
17 - printDirBanner requires RNMAKE_PKG_ROOT
18 - printPkgBanner requires RNMAKE_PKG_ROOT
19 
20 \pkgsynopsis
21 RN Make System
22 
23 \pkgfile{Std.mk}
24 
25 \pkgauthor{Robin Knight,robin.knight@roadnarrows.com}
26 
27 \pkgcopyright{2018,RoadNarrows LLC,http://www.roadnarrows.com}
28 
29 \license{MIT}
30 
31 \EulaBegin
32 \EulaEnd
33 
34 \cond RNMAKE_DOXY
35  */
36 endif
37 #
38 ################################################################################
39 
40 _STD_MK = 1
41 
42 # include colars
43 ifeq ($(_COLORS_MK),)
44  $(eval -include $(RNMAKE_ROOT)/Colors.mk)
45 endif
46 
47 #------------------------------------------------------------------------------
48 # Inter-makefile helper functions and canned sequences
49 
50 # $(call findGoals,goalpattern...)
51 # Find goal patterns in command-line goal list. Returns whitespace separated
52 # list of matched goals or empty string.
53 define findGoals =
54 $(filter $(1),$(GOAL_LIST))
55 endef
56 
57 # $(call includeIfGoals,goalpattern...,makefile)
58 # Conditionally include makefile if one of the goal patterns matches the
59 # command-line goal list.
60 define includeIfGoals =
61 $(if $(call findGoals,$(1)),$(eval include $(2)))
62 endef
63 
64 # $(call neq,op1,op2)
65 # $(call eq,op1,op2)
66 # Comparison operators. GNU Make has no comparison functions. Why??? Fake it.
67 # Returns non-empty string if true.
68 neq = $(filter-out $(1),$(2))
69 eq = $(if $(call neq,$(1),$(2)),,1)
70 
71 # $(call isFile,file)
72 # $(call isDir,file)
73 # Tests if file exists and specifies a regular file (directory).
74 # Returns non-empty string on true, empty string on false.
75 isFile = $(shell if [ -f $(1) ]; then echo 1; fi)
76 isDir = $(shell if [ -d $(1) ]; then echo 1; fi)
77 
78 # $(call findReqFile,file,errmsg)
79 # Find the required file. On failure calls error with appended optional
80 # errmsg. Returns absolute filename.
81 define findReqFile =
82 $(if $(realpath $(1)),$(strip $(realpath $(1))),\
83 $(error $(1): No such file$(if $(2),: $(strip $(2)),)))
84 endef
85 
86 # $(call makePath,dir...)
87 # Make search path dir[:dir...].
88 define makePath =
89 $(shell
90  p=""; \
91  for d in $(1); \
92  do \
93  if [ -z "$${p}" ];
94  then \
95  p="$${d}"; \
96  else \
97  p="$${p}:$${d}"; \
98  fi; \
99  done; \
100  echo "$${p}")
101 endef
102 
103 # $(call printPkgBanner,pkgname,arch,goallist)
104 # Print the package banner. Typically:
105 # pkgname = RNMAKE_PKG_FULL_NAME
106 # arch = RNMAKE_ARCH
107 # goallist = MAKECMDGOALS
108 define printPkgBanner =
109  @if [ "$(MAKELEVEL)" = "0" ]; then \
110  printf "$(color_pkg_banner)$(boldline)\n";\
111  printf "Package: $(1)\n";\
112  printf "Package Root: $(RNMAKE_PKG_ROOT)\n";\
113  printf "Architecture: $(2)\n";\
114  printf "Directory: $(CURDIR)\n";\
115  printf "Goal(s): $(3)\n";\
116  printf "Start: `date`\n";\
117  printf "$(boldline)$(color_end)\n";\
118  fi
119 endef
120 
121 # $(call printDirBanner,dir,goal)
122 # Print directory banner. The dir parameter is relative path from
123 # $(RNMAKE_PKG_ROOT). If the dir parameter is empty, the basename of
124 # $(RNMAKE_PKG_ROOT) is used.
125 define printDirBanner =
126  @if [ "$(1)" != "" ]; then \
127  subdirname="$(patsubst $(dir $(RNMAKE_PKG_ROOT))%,%,$(CURDIR)/$(1))";\
128  else \
129  subdirname=$(notdir $(RNMAKE_PKG_ROOT));\
130  fi; \
131  printf "\n";\
132  printf "$(color_dir_banner)$(normline)\n";\
133  printf "Directory: $$subdirname\n";\
134  printf "Goal: $(2)\n";\
135  printf "$(normline)$(color_end)\n";\
136  fi
137 endef
138 
139 # $(call printGoalBanner,goal)
140 # Print goal banner.
141 define printGoalBanner =
142  @if [ "$(1)" != "" ]; then \
143  printf "\n $(color_tgt_file)$(1)$(color_end)\n";\
144  fi
145 endef
146 
147 # $(call printGoalDesc,goal,desc)
148 #
149 # Print goal with optional description.
150 define printGoalDesc =
151  $(call printGoalBanner,$(1));
152  $(if $(2),@printf "$(2)\n",)
153 endef
154 
155 # $(call printEchoTgtGoalDesc,desc)
156 # Silly print goal with description. The goal is determined by stripping off
157 # the 'echo-' prefix from the current target $(@).
158 define printEchoTgtGoalDesc =
159  $(call printGoalDesc,$(patsubst echo-%,%,$(@)),$(1))
160 endef
161 
162 # $(call printFooter,curgoal,lastgoal)
163 # Conditionally print footer. If the goal is the last command-line goal and
164 # the make level is 0 (top), then the footer is printed.
165 define printFooter =
166  @if [ "$(MAKELEVEL)" = "0" -a "$(1)" = "$(2)" ]; then \
167  printf "\n";\
168  printf "$(color_pkg_banner) ###\n";\
169  printf "Finished: `date`\n";\
170  printf " ###$(color_end)\n";\
171  fi
172 endef
173 
174 # $(printTgtGoal)
175 # Print goal banner using the current target $(@).
176 printTgtGoal = $(call printGoalBanner,$(@))
177 
178 
179 eqline := \
180 ================================================================================
181 dotline := \
182 ................................................................................
183 tildeline := \
184 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185 underline := \
186 ________________________________________________________________________________
187 
188 boldline := $(eqline)
189 normline := $(tildeline)