Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile improvements #4024

Merged
merged 3 commits into from
Mar 1, 2021
Merged

Makefile improvements #4024

merged 3 commits into from
Mar 1, 2021

Conversation

kmk3
Copy link
Collaborator

@kmk3 kmk3 commented Mar 1, 2021

$ git log --reverse --pretty='* %s' master..
* makefiles: fix whitespace
* makefiles: fix misc blank line consistency
* makefiles: make all, clean and distclean PHONY

Some minor cleanup and improvements before adding new stuff.


@Fred-Barclay From the amount of whitespace-fixing commits, I think you'll like
the commands from commit 0043776 ("makefiles: fix whitespace"):

$ git ls-files -z -- '*Makefile*' |
  xargs -0 -I '{}' sh -c \
  "test -s '{}' && printf '%s\n' \"\`git stripspace <'{}'\`\" >'{}'"

I don't know if you've used something like that before, but to me it has been
very handy on many occasions. From git-stripspace(1):

Read text, such as commit messages, notes, tags and branch descriptions, from
the standard input and clean it in the manner used by Git.

With no arguments, this will:

  • remove trailing whitespace from all lines

  • collapse multiple consecutive empty lines into one empty line

  • remove empty lines from the beginning and end of the input

  • add a missing \n to the last line if necessary.

In the case where the input consists entirely of whitespace characters, no
output will be produced.

NOTE: This is intended for cleaning metadata, prefer the --whitespace=fix
mode of git-apply(1) for correcting whitespace of patches or files in the
repository.

Though if there any plans to do a mass cleaning, it would be nice to have
something like that on a pre-commit hook/ci check beforehand, to catch errors
before they go to master and thus avoid the need for periodic clean-ups.

kmk3 added 3 commits March 1, 2021 16:04
With a fun little script:

    $ git ls-files -z -- '*Makefile*' |
      xargs -0 -I '{}' sh -c \
      "test -s '{}' && printf '%s\n' \"\`git stripspace <'{}'\`\" >'{}'"
Avoid a stat() call for each affected target and also potentially speed
up parallel builds.

From the GNU make manual[1]:

> Phony targets are also useful in conjunction with recursive
> invocations of make (see Recursive Use of make).  In this situation
> the makefile will often contain a variable which lists a number of
> sub-directories to be built.

[...]

> The implicit rule search (see Implicit Rules) is skipped for .PHONY
> targets.  This is why declaring a target as .PHONY is good for
> performance, even if you are not worried about the actual file
> existing.

Commands used to search, replace and cleanup:

    $ find -type f -name '*Makefile.in' -exec sed -i.bak \
      -e 's/^all:/.PHONY: all\nall:/' \
      -e 's/^clean:/.PHONY: clean\nclean:/' \
      -e 's/^distclean:/.PHONY: distclean\ndistclean:/' '{}' +
    $ find -type f -name '*Makefile.in.bak' -exec rm '{}' +

[1]: https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
@kmk3 kmk3 changed the title Improve makefiles Makefile improvements Mar 1, 2021
@reinerh reinerh merged commit 22fd89b into netblue30:master Mar 1, 2021
@kmk3 kmk3 deleted the improve-makefiles branch March 1, 2021 20:59
kmk3 added a commit to kmk3/firejail that referenced this pull request Mar 24, 2021
When using the "wildcard" internal functions.

This usage has been present since the first "real" commit in the
repository: commit 1379851 ("Baseline firejail 0.9.28").

>     H_FILE_LIST       = $(sort $(wildcard *.[h]))
>     C_FILE_LIST       = $(sort $(wildcard *.c))

There is only a single character (i.e.: "h") inside the character class,
so its usage should make no functional difference.  It may stem from a
construct that could have originally looked something like this:

    C_FILE_LIST       = $(sort $(wildcard *.[ch]))

Which would match both the implementation files and the headers.

From Section 4.4, [Using Wildcard Characters in File Names][1] of the
GNU make manual:

> A single file name can specify many files using wildcard characters.
> The wildcard characters in make are ‘*’, ‘?’ and ‘[…]’, the same as in
> the Bourne shell.  For example, *.c specifies a list of all the files
> (in the working directory) whose names end in ‘.c’.

See also Section 2.13, [Pattern Matching Notation][2] of POSIX.1-2017.

Commands used to search, replace and clean up:

    $ find . -name .git -prune -o -type f \
      \( -name Makefile -o -name Makefile.in \
      -o -name '*.mk' -o -name '*.mk.in' \) -print0 |
      xargs -0 grep -Fl '$(wildcard *.[h])' | tr '\n' '\000' |
      xargs -0 sed -i.bak -e \
      's/\$(wildcard \*.\[h\])/$(wildcard *.h)/'

    $ find . -name .git -prune -o -type f \
      -name '*.bak' -exec rm '{}' +

Note: To make sure that this doesn't actually change anything
functionally, I built firejail-git (AUR) on Artix from master and from
this commit and diffing the resulting files produced no output (other
than showing changes related to the build timestamps).

Misc: Reference to the previous makefile-related changes: commit
2465f92 ("makefiles: make all, clean and distclean PHONY") /
netblue30#4024

[1]: https://www.gnu.org/software/make/manual/html_node/Wildcards.html
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
@kmk3 kmk3 mentioned this pull request Sep 24, 2021
kmk3 added a commit to kmk3/firejail that referenced this pull request Feb 3, 2023
To improve clarity and to prevent unnecessary filesystem lookups.

Overall, this appears to reduce the amount of implicit rule searches by
~4% for the default build and by ~12% for the "man" target (as an
example):

    $ git checkout master >/dev/null 2>&1
    $ git show --pretty='%h %ai %s' -s
    b55cb6a 2023-01-31 18:56:42 -0500 testing
    $ ./configure >/dev/null
    $ make clean >/dev/null && make --debug=i -j 4     | grep -F 'Trying implicit' | wc -l
    7101
    $ make clean >/dev/null && make --debug=i -j 4 man | grep -F 'Trying implicit' | wc -l
    1239
    # (with this commit applied)
    $ make clean >/dev/null && make --debug=i -j 4     | grep -F 'Trying implicit' | wc -l
    6793
    $ make clean >/dev/null && make --debug=i -j 4 man | grep -F 'Trying implicit' | wc -l
    1085

Environment: GNU make 4.4-1 on Artix Linux.

Note: The amount lines printed is the same on non-parallel builds (that
is, without `-j 4`).

See commit 2465f92 ("makefiles: make all, clean and distclean PHONY",
2021-02-12) / PR netblue30#4024 for details.

Note: By "most phony targets" I mean all non-path targets except for the
testing targets, which were being changed recently (for example, the
"test-github" target) and so might still be under development.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants