Repository navigation
Expand file tree
/
Copy pathflags-stamp.mk
More file actions
168 lines (159 loc) · 8.95 KB
/
Copy pathflags-stamp.mk
File metadata and controls
168 lines (159 loc) · 8.95 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
###############################################################################
# BSD LICENSE
#
# Copyright(c) 2026 Intel Corporation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Intel Corporation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
# A file recording the flags the last build here used, for the objects and
# binaries to depend on. Included by lib/, pqos/ and the unit-test directories.
#
# Naming the Makefile as a prerequisite covers the flags written *in* the
# Makefile. It cannot cover the ones that arrive on the command line - SHARED,
# DEBUG and EXTRA_CFLAGS in lib/, unit-test/mock and unit-test/output, DEBUG and
# EXTRA_CFLAGS in pqos/, DEBUG in unit-test/lib, and any variable given there,
# LDLIBS for one - because nothing about the file changes when the command line
# does. So
# `make DEBUG=y` in a tree that has already been built found everything up to
# date and produced no debug build at all: ASSERT stayed compiled out, and a case
# that only reaches its subject under ASSERT passed without testing it, while
# telling the reader in a comment to build this way. And `make SHARED=y` after
# `make SHARED=n` in lib/ kept the -fPIE objects and failed to link a shared
# object from them.
#
# The flags themselves are the dependency, so this records them and is rewritten
# only when they differ from what it holds. A stamp rewritten every run would
# rebuild everything every run.
#
# Written with $(file ...) rather than a shell redirection because these flag
# sets contain both kinds of quote - -D'PQOS_LOCAL=' beside
# -DLOCKFILE=\"/tmp/...\" - and there is no way to pass that through a shell
# unharmed. $(file ...) needs no shell.
#
# The rule below cannot use it: a recipe's $(file ...) is evaluated when make
# expands the recipe, which is before the mkdir in it has run, so the write finds
# no directory. It hands the text to the shell through the environment instead -
# a target-specific export, so the recipe reads one variable and the quotes in it
# are never a shell's business either.
#
# A Makefile including this sets FLAGS_STAMP_DIR to the directory the stamp
# belongs in, and includes it *after* the last line that adds to the flags.
ifndef FLAGS_STAMP_DIR
$(error FLAGS_STAMP_DIR must be set before including flags-stamp.mk)
endif
FLAGS_STAMP = $(FLAGS_STAMP_DIR)/.build-flags
# every variable that decides what is produced here, in one line.
#
# AR as well as CC: with SHARED=n libpqos, the mock and the capture library are
# archives, and which ar builds them is as much a part of what came out as which
# compiler did. CPPFLAGS because lib/, unit-test/mock and unit-test/output have no
# compilation recipe of their own and fall through to make's built-in one, which
# is "$(CC) $(CPPFLAGS) $(CFLAGS) -c" - so a macro defined there decides what is
# produced and nothing else here would notice it.
#
# One field per variable, named and bracketed, because a plain concatenation does
# not say where one ends and the next begins: CFLAGS="-DX=1 -pthread" with
# LDFLAGS="-lm" and CFLAGS="-DX=1" with LDFLAGS="-pthread -lm" are different
# builds and the same string. Moving a token from one variable to another now
# changes the text, which is what a stamp is for.
#
# The text is also compared as it stands and not through $(strip). strip collapses
# runs of whitespace wherever they are, including inside a quoted macro value, so
# -DX="a b" and -DX="a b" recorded equal and the objects compiled with the first
# were kept for the second. The brackets make that safe at the ends too: whatever
# a variable leaves at either of its own ends is inside them.
#
# LDLIBS as well as LDFLAGS: pqos/ links with "$(LDFLAGS) <objects> $(LDLIBS)", so
# a library added on the command line changes what is linked.
FLAGS_STAMP_TEXT = CC=[$(CC)] AR=[$(AR)] CPPFLAGS=[$(CPPFLAGS)] \
CFLAGS=[$(CFLAGS)] LDFLAGS=[$(LDFLAGS)] LDLIBS=[$(LDLIBS)] WRAP=[$(WRAP)]
# An invocation that asks only for goals that take what is built, or remove it,
# leaves the stamp as the build left it. "make install" after "make DEBUG=y"
# installs that build, as it did before there was a stamp here: recording the
# install's own flags instead rebuilt every object - as root, under sudo - and
# installed a different build from the one that was compiled. That goes for
# flags sudo drops from the environment as well, such as an exported CC.
FLAGS_STAMP_READERS = install uninstall clean clobber help doxy style \
checkpatch clang-format codespell cppcheck
ifneq ($(filter-out $(FLAGS_STAMP_READERS),$(or $(MAKECMDGOALS),all)),)
ifneq ($(FLAGS_STAMP_TEXT),$(shell cat $(FLAGS_STAMP) 2>/dev/null))
$(shell mkdir -p $(FLAGS_STAMP_DIR))
# removed first: a stamp root wrote - "sudo make install" in a tree with none yet
# - cannot be opened for writing by the user the directory belongs to, and a
# build with other options then stopped right here, "make clean DEBUG=y" too.
# The directory is the user's, so the file can always be replaced.
$(shell rm -f $(FLAGS_STAMP))
$(file >$(FLAGS_STAMP),$(FLAGS_STAMP_TEXT))
endif
endif
# and a rule as well as the write above, for the invocation that removes it after
# it has been written: "make clean all" writes the stamp while the makefile is
# read, clean deletes it with the directory it is in, and all then wants a
# prerequisite that no longer exists - "No rule to make target
# 'obj/.build-flags'". The write above handles a change of flags; this handles the
# file not being there.
#
# And not the default goal of a Makefile that includes this before its own first
# rule: make takes the first target it reads, and a stamp in a subdirectory -
# pqos/obj/.build-flags - is one it will take (a bare dot-file is not), so "make"
# in pqos/ built the stamp and nothing else. The goal is put back as it was found.
FLAGS_STAMP_GOAL := $(.DEFAULT_GOAL)
$(FLAGS_STAMP): export FLAGS_STAMP_TEXT := $(FLAGS_STAMP_TEXT)
$(FLAGS_STAMP):
@mkdir -p $(@D)
@printf '%s' "$$FLAGS_STAMP_TEXT" > $@
.DEFAULT_GOAL := $(FLAGS_STAMP_GOAL)
# "make clean all" asks for both in one invocation, and make does not serialize
# goals: under -j the clean deletes what the build is writing - obj/ while a link
# is reading it, or this stamp after it has been written - and which wins is
# timing, so an invocation has passed and failed on one tree without anything
# changing in it. Naming clean as a prerequisite of the other goal is not enough
# on its own: that orders it before that goal's recipe and not before its other
# prerequisites, which under -j start while the clean is still running. So the
# invocation is run serially as well, through a .NOTPARALLEL that only this
# condition reads. A plain build is parallel as before.
#
# Here rather than in each Makefile, because every one of them that records flags
# has both goals and the same race.
#
# Order-only - the "|" - and not an ordinary prerequisite: the library recipes in
# unit-test/mock and unit-test/output hand $^ to the compiler, so "clean" named
# the ordinary way arrives on the link line and ld goes looking for a file called
# clean. Order-only prerequisites are not in $^.
ifneq ($(filter clean,$(MAKECMDGOALS)),)
ifneq ($(filter-out clean,$(MAKECMDGOALS)),)
.NOTPARALLEL:
# and the edge only where clean was asked for first, which is the order the
# invocation wants: "make <library> clean" asks to build and then tidy up, and an
# edge there would delete the objects between them and hand the recipe a $^ whose
# files are gone. With .NOTPARALLEL above, make takes the goals in the order they
# were given, which is what the other order needs.
ifeq ($(firstword $(MAKECMDGOALS)),clean)
$(filter-out clean,$(MAKECMDGOALS)): | clean
endif
endif
endif