Skip to content

Commit

Permalink
Don't output boolean macros in GNU makefiles unless they are really n…
Browse files Browse the repository at this point in the history
…eeded.

This is a cosmetic change which omits the macro definitions if they are not
used in the rest of the makefile.
  • Loading branch information
vadz committed Dec 19, 2013
1 parent ec19b6e commit 5f6bf79
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/bkl/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ def write(self, text):
text = text.encode(self.charset)
self.text += text

def replace(self, placeholder, value):
"""
Replaces the value of the given placeholder with its real value. This
is useful for parts of the output which are not known at the time they
are written because they depend on other parts coming after them.
Notice that only the first occurrency of the placeholder is replaced.
"""
self.text = self.text.replace(placeholder, value, 1)

def commit(self):
if self.eol == EOL_WINDOWS:
self.text = self.text.replace("\n", "\r\n")
Expand Down
2 changes: 1 addition & 1 deletion src/bkl/makefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _gen_makefile(self, build_graphs, module):
model=module)

mk_fmt = self.Formatter()
expr_fmt = self.ExprFormatter(paths_info)
expr_fmt = self.ExprFormatter(self, paths_info)

f = io.OutputFile(output, io.EOL_UNIX, creator=self, create_for=module)
self.on_header(f, module)
Expand Down
23 changes: 20 additions & 3 deletions src/bkl/plugins/gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,16 @@ def _is_multiarch_target(target):
endif
"""

# This is just a unique string, its exact syntax doesn't matter currently.
GMAKE_IFEXPR_MACROS_PLACEHOLDER = "{{{BKL_GMAKE_IFEXPR_MACROS}}}"

# GNU Make has some boolean functions, but not all that we need, so define them
GMAKE_IFEXPR_MACROS = """
_true := true
_false :=
_not = $(if $(1),$(_false),$(_true_))
_equal = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
"""


Expand Down Expand Up @@ -295,7 +299,12 @@ def multifile_target(self, outfiles, deps, commands):


class GnuExprFormatter(MakefileExprFormatter):
def __init__(self, toolset, paths_info):
MakefileExprFormatter.__init__(self, paths_info)
self.toolset = toolset

def bool_value(self, e):
self.toolset.uses_non_std_bool_macros = True
return "$(_true)" if e.value else "$(_false)"

def bool(self, e):
Expand All @@ -307,10 +316,13 @@ def bool(self, e):
if e.operator == BoolExpr.OR:
return "$(or %s,%s)" % (l, r)
if e.operator == BoolExpr.EQUAL:
self.toolset.uses_non_std_bool_macros = True
return "$(call _equal,%s,%s)" % (l, r)
if e.operator == BoolExpr.NOT_EQUAL:
self.toolset.uses_non_std_bool_macros = True
return "$(call _not,$(call _equal,%s,%s))" % (l, r)
if e.operator == BoolExpr.NOT:
self.toolset.uses_non_std_bool_macros = True
return "$(call _not,%s)" % l
assert False, "invalid operator"

Expand Down Expand Up @@ -403,14 +415,19 @@ def on_header(self, file, module):
CC := %s
CXX := %s
""" % (self.default_cc, self.default_cxx))
# TODO: only do this if non-const (depending on
# Settings/PlaceholderExpr) IfExpr is present in the file
file.write(GMAKE_IFEXPR_MACROS)
# This placeholder will be replaced either with the definition of the
# macros, if they turn out to be really needed, or nothing otherwise.
file.write(GMAKE_IFEXPR_MACROS_PLACEHOLDER)
self.uses_non_std_bool_macros = False

def on_phony_targets(self, file, targets):
file.write(".PHONY: %s\n" % " ".join(targets))

def on_footer(self, file, module):
file.replace(GMAKE_IFEXPR_MACROS_PLACEHOLDER,
GMAKE_IFEXPR_MACROS if self.uses_non_std_bool_macros
else "")

file.write("\n"
"# Dependencies tracking:\n"
"-include *.d\n")
Expand Down

0 comments on commit 5f6bf79

Please sign in to comment.