Skip to content

Commit

Permalink
Add clean target
Browse files Browse the repository at this point in the history
  • Loading branch information
gilles-peskine-arm committed Oct 29, 2023
1 parent 7e23443 commit 9e2e8b7
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion scripts/make_makefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def __init__(self, options, source_path: str) -> None:
# A path to the source tree, from the root of the build tree.
self.source_from_build = pathlib.Path(source_path) #type: pathlib.Path

# Set of files to remove in "make clean".
self.clean_files = set() #type: Set[str]
# {extension: {directories}} to remove in "make clean"
self.clean_extensions = {} #type: Dict[str, Set[str]]
# {target: help_text}
self.help = {} #type: Dict[str, str]
# Directories containing targets
Expand Down Expand Up @@ -160,6 +164,7 @@ def target(self, # pylint: disable=too-many-arguments
dependencies: Iterable[str],
commands: Iterable[str],
help_text: Optional[str] = None,
clean=True,
phony=False,
) -> None:
"""Generate a makefile rule.
Expand All @@ -169,6 +174,8 @@ def target(self, # pylint: disable=too-many-arguments
* commands: a list of commands to run (the recipe).
* help_text: documentation to show for this target in "make help".
If this is omitted, the target is not listed in "make help".
* clean: if true, add this target to the list of files to remove
in "make clean". Ignored for phony targets.
* phony: if true, declare this target as phony.
"""
if not phony:
Expand All @@ -180,6 +187,8 @@ def target(self, # pylint: disable=too-many-arguments
self.help[name] = help_text
if phony:
self.line('.PHONY: ' + name)
if not phony and clean:
self.clean_files.add(name)

#### Analyze source files

Expand Down Expand Up @@ -239,7 +248,10 @@ def targets_for_c(self,
'$(CFLAGS)',
self.include_options_for(src.source_dir()),
'-o $@',
switch, src.make_path())])
switch, src.make_path())],
clean=False)
self.clean_extensions.setdefault(extension, set())
self.clean_extensions[extension].add(src.target_dir())

#### Generate makefile sections ####

Expand Down Expand Up @@ -275,6 +287,23 @@ def library_section(self) -> None:
[],
phony=True)

def patterns_to_clean(self) -> Iterable[str]:
"""Yield the files and wildcard patterns to remove in "make clean"."""
yield from sorted(self.clean_files)
for extension in sorted(self.clean_extensions.keys()):
# It would be nice, but hard, to ensure that if the extension
# is empty at runtime ("$(VAR)" with VAR having an empty value)
# then we avoid running "rm dir/*".
yield sjoin(*(os.path.join(dir, '*' + extension)
for dir in sorted(self.clean_extensions[extension])))

def clean_section(self) -> None:
"""Generate a clean target."""
self.target('clean', [],
['$(RM) ' + arg for arg in self.patterns_to_clean()],
help_text='Remove all generated files.',
phony=True)

def output_all(self) -> None:
"""Generate the makefile content."""
self.comment('Generated by ' + ' '.join(sys.argv))
Expand All @@ -284,6 +313,8 @@ def output_all(self) -> None:
self.blank_line()
self.library_section()
self.blank_line()
self.clean_section()
self.blank_line()
# The help target must come last because it displays accumulated help
# text set by previous calls to self.target. Set its own help manually
# because self.target would set it too late for it to be printed.
Expand Down

0 comments on commit 9e2e8b7

Please sign in to comment.