2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* "Optimize" a list of dependencies as spit out by gcc -MD
|
|
|
|
* for the kernel build
|
|
|
|
* ===========================================================================
|
|
|
|
*
|
|
|
|
* Author Kai Germaschewski
|
|
|
|
* Copyright 2002 by Kai Germaschewski <kai.germaschewski@gmx.de>
|
|
|
|
*
|
|
|
|
* This software may be used and distributed according to the terms
|
|
|
|
* of the GNU General Public License, incorporated herein by reference.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Introduction:
|
|
|
|
*
|
|
|
|
* gcc produces a very nice and correct list of dependencies which
|
|
|
|
* tells make when to remake a file.
|
|
|
|
*
|
|
|
|
* To use this list as-is however has the drawback that virtually
|
2009-10-17 22:49:24 +00:00
|
|
|
* every file in the kernel includes autoconf.h.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2009-10-17 22:49:24 +00:00
|
|
|
* If the user re-runs make *config, autoconf.h will be
|
2005-04-16 22:20:36 +00:00
|
|
|
* regenerated. make notices that and will rebuild every file which
|
|
|
|
* includes autoconf.h, i.e. basically all files. This is extremely
|
|
|
|
* annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
|
|
|
|
*
|
|
|
|
* So we play the same trick that "mkdep" played before. We replace
|
2009-10-17 22:49:24 +00:00
|
|
|
* the dependency on autoconf.h by a dependency on every config
|
2017-08-08 13:20:50 +00:00
|
|
|
* option which is mentioned in any of the listed prerequisites.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2007-03-29 09:27:14 +00:00
|
|
|
* kconfig populates a tree in include/config/ with an empty file
|
|
|
|
* for each config symbol and when the configuration is updated
|
|
|
|
* the files representing changed config options are touched
|
|
|
|
* which then let make pick up the changes and the files that use
|
|
|
|
* the config symbols are rebuilt.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* So if the user changes his CONFIG_HIS_DRIVER option, only the objects
|
2021-04-15 17:36:07 +00:00
|
|
|
* which depend on "include/config/HIS_DRIVER" will be rebuilt,
|
2005-04-16 22:20:36 +00:00
|
|
|
* so most likely only his driver ;-)
|
|
|
|
*
|
|
|
|
* The idea above dates, by the way, back to Michael E Chastain, AFAIK.
|
|
|
|
*
|
|
|
|
* So to get dependencies right, there are two issues:
|
|
|
|
* o if any of the files the compiler read changed, we need to rebuild
|
|
|
|
* o if the command line given to the compile the file changed, we
|
|
|
|
* better rebuild as well.
|
|
|
|
*
|
|
|
|
* The former is handled by using the -MD output, the later by saving
|
|
|
|
* the command line used to compile the old object and comparing it
|
|
|
|
* to the one we would now use.
|
|
|
|
*
|
|
|
|
* Again, also this idea is pretty old and has been discussed on
|
|
|
|
* kbuild-devel a long time ago. I don't have a sensibly working
|
|
|
|
* internet connection right now, so I rather don't mention names
|
|
|
|
* without double checking.
|
|
|
|
*
|
|
|
|
* This code here has been based partially based on mkdep.c, which
|
|
|
|
* says the following about its history:
|
|
|
|
*
|
|
|
|
* Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
|
|
|
|
* This is a C version of syncdep.pl by Werner Almesberger.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* It is invoked as
|
|
|
|
*
|
|
|
|
* fixdep <depfile> <target> <cmdline>
|
|
|
|
*
|
|
|
|
* and will read the dependency file <depfile>
|
|
|
|
*
|
|
|
|
* The transformed dependency snipped is written to stdout.
|
|
|
|
*
|
|
|
|
* It first generates a line
|
|
|
|
*
|
2022-12-29 09:15:00 +00:00
|
|
|
* savedcmd_<target> = <cmdline>
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* and then basically copies the .<target>.d file to stdout, in the
|
2009-10-17 22:49:24 +00:00
|
|
|
* process filtering out the dependency on autoconf.h and adding
|
2021-04-15 17:36:07 +00:00
|
|
|
* dependencies on include/config/MY_OPTION for every
|
2017-08-08 13:20:50 +00:00
|
|
|
* CONFIG_MY_OPTION encountered in any of the prerequisites.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2016-08-24 18:03:05 +00:00
|
|
|
* We don't even try to really parse the header files, but
|
2005-04-16 22:20:36 +00:00
|
|
|
* merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
|
|
|
|
* be picked up as well. It's not a problem with respect to
|
|
|
|
* correctness, since that can only give too many dependencies, thus
|
|
|
|
* we cannot miss a rebuild. Since people tend to not mention totally
|
|
|
|
* unrelated CONFIG_ options all over the place, it's not an
|
|
|
|
* efficiency problem either.
|
|
|
|
*
|
|
|
|
* (Note: it'd be easy to port over the complete mkdep state machine,
|
|
|
|
* but I don't think the added complexity is worth it)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2023-01-07 09:18:16 +00:00
|
|
|
#include <stdbool.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2024-08-12 12:48:53 +00:00
|
|
|
#include <xalloc.h>
|
|
|
|
|
2009-09-18 19:49:23 +00:00
|
|
|
static void usage(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-11-30 01:05:26 +00:00
|
|
|
fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-11-09 15:29:27 +00:00
|
|
|
struct item {
|
|
|
|
struct item *next;
|
|
|
|
unsigned int len;
|
|
|
|
unsigned int hash;
|
modpost,fixdep: Replace zero-length array with flexible-array
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.
Also, notice that, dynamic memory allocations won't be affected by
this change:
"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]
sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.
This issue was found with the help of Coccinelle.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
2020-05-07 18:56:01 +00:00
|
|
|
char name[];
|
2010-11-09 15:29:27 +00:00
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-11-09 15:29:27 +00:00
|
|
|
#define HASHSZ 256
|
fixdep: avoid parsing the same file over again
The dep files (*.d files) emitted by C compilers usually contain the
deduplicated list of included files.
One exceptional case is when a header is included by the -include
command line option, and also by #include directive.
For example, the top Makefile adds the command line option,
"-include $(srctree)/include/linux/kconfig.h". You do not need to
include <linux/kconfig.h> in every source file.
In fact, include/linux/kconfig.h is listed twice in many .*.cmd files
due to include/linux/xarray.h having "#include <linux/kconfig.h>".
I did not fix that since it is a small redundancy.
However, this is more annoying for rustc. rustc emits the dependency
for each emission type.
For example, cmd_rustc_library emits dep-info, obj, and metadata.
So, the emitted *.d file contains the dependency for those 3 targets,
which makes fixdep parse the same file 3 times.
$ grep rust/alloc/raw_vec.rs rust/.alloc.o.cmd
rust/alloc/raw_vec.rs \
rust/alloc/raw_vec.rs \
rust/alloc/raw_vec.rs \
To skip the second parsing, this commit adds a hash table for parsed
files, just like we did for CONFIG options.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Tested-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-01-07 09:18:19 +00:00
|
|
|
static struct item *config_hashtab[HASHSZ], *file_hashtab[HASHSZ];
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-11-09 15:29:27 +00:00
|
|
|
static unsigned int strhash(const char *str, unsigned int sz)
|
|
|
|
{
|
|
|
|
/* fnv32 hash */
|
|
|
|
unsigned int i, hash = 2166136261U;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-11-09 15:29:27 +00:00
|
|
|
for (i = 0; i < sz; i++)
|
|
|
|
hash = (hash ^ str[i]) * 0x01000193;
|
|
|
|
return hash;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new value to the configuration string.
|
|
|
|
*/
|
2023-01-07 09:18:18 +00:00
|
|
|
static void add_to_hashtable(const char *name, int len, unsigned int hash,
|
|
|
|
struct item *hashtab[])
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2024-08-12 12:48:53 +00:00
|
|
|
struct item *aux;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-08-12 12:48:53 +00:00
|
|
|
aux = xmalloc(sizeof(*aux) + len);
|
2010-11-09 15:29:27 +00:00
|
|
|
memcpy(aux->name, name, len);
|
|
|
|
aux->len = len;
|
|
|
|
aux->hash = hash;
|
|
|
|
aux->next = hashtab[hash % HASHSZ];
|
|
|
|
hashtab[hash % HASHSZ] = aux;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 09:18:18 +00:00
|
|
|
/*
|
|
|
|
* Lookup a string in the hash table. If found, just return true.
|
|
|
|
* If not, add it to the hashtable and return false.
|
|
|
|
*/
|
|
|
|
static bool in_hashtable(const char *name, int len, struct item *hashtab[])
|
|
|
|
{
|
|
|
|
struct item *aux;
|
|
|
|
unsigned int hash = strhash(name, len);
|
|
|
|
|
|
|
|
for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
|
|
|
|
if (aux->hash == hash && aux->len == len &&
|
|
|
|
memcmp(aux->name, name, len) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_hashtable(name, len, hash, hashtab);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Record the use of a CONFIG_* word.
|
|
|
|
*/
|
2010-11-09 15:29:27 +00:00
|
|
|
static void use_config(const char *m, int slen)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-01-07 09:18:18 +00:00
|
|
|
if (in_hashtable(m, slen, config_hashtab))
|
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2021-04-15 17:36:07 +00:00
|
|
|
/* Print out a dependency path from a symbol name. */
|
fixdep: use fflush() and ferror() to ensure successful write to files
Currently, fixdep checks the return value from (v)printf(), but it does
not ensure the complete write to the .cmd file.
printf() just writes data to the internal buffer, which usually succeeds.
(Of course, it may fail for another reason, for example when the file
descriptor is closed, but that is another story.)
When the buffer (4k?) is full, an actual write occurs, and printf() may
really fail. One of typical cases is "No space left on device" when the
disk is full.
The data remaining in the buffer will be pushed out to the file when
the program exits, but we never know if it is successful.
One straight-forward fix would be to add the following code at the end
of the program.
ret = fflush(stdout);
if (ret < 0) {
/* error handling */
}
However, it is tedious to check the return code in all the call sites
of printf(), fflush(), fclose(), and whatever can cause actual writes
to the end device. Doing that lets the program bail out at the first
failure but is usually not worth the effort.
Instead, let's check the error status from ferror(). This is 'sticky',
so you need to check it just once. You still need to call fflush().
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: David Laight <david.laight@aculab.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
2022-03-06 07:25:35 +00:00
|
|
|
printf(" $(wildcard include/config/%.*s) \\\n", slen, m);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 13:05:46 +00:00
|
|
|
/* test if s ends in sub */
|
|
|
|
static int str_ends_with(const char *s, int slen, const char *sub)
|
|
|
|
{
|
|
|
|
int sublen = strlen(sub);
|
|
|
|
|
|
|
|
if (sublen > slen)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return !memcmp(s + slen - sublen, sub, sublen);
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:03:05 +00:00
|
|
|
static void parse_config_file(const char *p)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2016-08-24 18:03:05 +00:00
|
|
|
const char *q, *r;
|
2018-02-28 19:17:36 +00:00
|
|
|
const char *start = p;
|
2016-08-24 18:03:05 +00:00
|
|
|
|
|
|
|
while ((p = strstr(p, "CONFIG_"))) {
|
2018-02-28 19:17:36 +00:00
|
|
|
if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
|
|
|
|
p += 7;
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-24 05:18:45 +00:00
|
|
|
p += 7;
|
2016-08-24 18:03:05 +00:00
|
|
|
q = p;
|
2020-02-18 10:00:31 +00:00
|
|
|
while (isalnum(*q) || *q == '_')
|
2016-08-24 18:03:05 +00:00
|
|
|
q++;
|
2018-01-11 13:05:46 +00:00
|
|
|
if (str_ends_with(p, q - p, "_MODULE"))
|
2016-08-24 18:03:05 +00:00
|
|
|
r = q - 7;
|
|
|
|
else
|
|
|
|
r = q;
|
|
|
|
if (r > p)
|
|
|
|
use_config(p, r - p);
|
|
|
|
p = q;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:05:42 +00:00
|
|
|
static void *read_file(const char *filename)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int fd;
|
2018-01-11 13:05:42 +00:00
|
|
|
char *buf;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2018-01-11 13:05:42 +00:00
|
|
|
fprintf(stderr, "fixdep: error opening file: ");
|
2005-04-16 22:20:36 +00:00
|
|
|
perror(filename);
|
|
|
|
exit(2);
|
|
|
|
}
|
2015-12-07 21:26:08 +00:00
|
|
|
if (fstat(fd, &st) < 0) {
|
2018-01-11 13:05:42 +00:00
|
|
|
fprintf(stderr, "fixdep: error fstat'ing file: ");
|
2015-12-07 21:26:08 +00:00
|
|
|
perror(filename);
|
|
|
|
exit(2);
|
|
|
|
}
|
2024-08-12 12:48:53 +00:00
|
|
|
buf = xmalloc(st.st_size + 1);
|
2018-01-11 13:05:42 +00:00
|
|
|
if (read(fd, buf, st.st_size) != st.st_size) {
|
2016-08-24 18:03:05 +00:00
|
|
|
perror("fixdep: read");
|
2018-01-08 10:04:01 +00:00
|
|
|
exit(2);
|
2016-08-24 18:03:05 +00:00
|
|
|
}
|
2018-01-11 13:05:42 +00:00
|
|
|
buf[st.st_size] = '\0';
|
2016-08-24 18:03:05 +00:00
|
|
|
close(fd);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-01-11 13:05:42 +00:00
|
|
|
return buf;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 13:05:45 +00:00
|
|
|
/* Ignore certain dependencies */
|
|
|
|
static int is_ignored_file(const char *s, int len)
|
|
|
|
{
|
kbuild: implement CONFIG_TRIM_UNUSED_KSYMS without recursion
When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses
the directory tree to determine which EXPORT_SYMBOL to trim. If an
EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the
second traverse, where some source files are recompiled with their
EXPORT_SYMBOL() tuned into a no-op.
Linus stated negative opinions about this slowness in commits:
- 5cf0fd591f2e ("Kbuild: disable TRIM_UNUSED_KSYMS option")
- a555bdd0c58c ("Kbuild: enable TRIM_UNUSED_KSYMS again, with some guarding")
We can do this better now. The final data structures of EXPORT_SYMBOL
are generated by the modpost stage, so modpost can selectively emit
KSYMTAB entries that are really used by modules.
Commit f73edc8951b2 ("kbuild: unify two modpost invocations") is another
ground-work to do this in a one-pass algorithm. With the list of modules,
modpost sets sym->used if it is used by a module. modpost emits KSYMTAB
only for symbols with sym->used==true.
BTW, Nicolas explained why the trimming was implemented with recursion:
https://lore.kernel.org/all/2o2rpn97-79nq-p7s2-nq5-8p83391473r@syhkavp.arg/
Actually, we never achieved that level of optimization where the chain
reaction of trimming comes into play because:
- CONFIG_LTO_CLANG cannot remove any unused symbols
- CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is enabled only for vmlinux,
but not modules
If deeper trimming is required, we need to revisit this, but I guess
that is unlikely to happen.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
2023-06-11 15:50:57 +00:00
|
|
|
return str_ends_with(s, len, "include/generated/autoconf.h");
|
2018-01-11 13:05:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 09:18:20 +00:00
|
|
|
/* Do not parse these files */
|
|
|
|
static int is_no_parse_file(const char *s, int len)
|
|
|
|
{
|
|
|
|
/* rustc may list binary files in dep-info */
|
|
|
|
return str_ends_with(s, len, ".rlib") ||
|
|
|
|
str_ends_with(s, len, ".rmeta") ||
|
|
|
|
str_ends_with(s, len, ".so");
|
|
|
|
}
|
|
|
|
|
2011-03-11 21:34:47 +00:00
|
|
|
/*
|
|
|
|
* Important: The below generated source_foo.o and deps_foo.o variable
|
|
|
|
* assignments are parsed not only by make, but also by the rather simple
|
|
|
|
* parser in scripts/mod/sumversion.c.
|
|
|
|
*/
|
2023-01-07 09:18:16 +00:00
|
|
|
static void parse_dep_file(char *p, const char *target)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-01-07 09:18:16 +00:00
|
|
|
bool saw_any_target = false;
|
|
|
|
bool is_target = true;
|
|
|
|
bool is_source = false;
|
|
|
|
bool need_parse;
|
|
|
|
char *q, saved_c;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
/* handle some special characters first. */
|
|
|
|
switch (*p) {
|
|
|
|
case '#':
|
|
|
|
/*
|
|
|
|
* skip comments.
|
|
|
|
* rustc may emit comments to dep-info.
|
|
|
|
*/
|
|
|
|
p++;
|
|
|
|
while (*p != '\0' && *p != '\n') {
|
|
|
|
/*
|
|
|
|
* escaped newlines continue the comment across
|
|
|
|
* multiple lines.
|
|
|
|
*/
|
|
|
|
if (*p == '\\')
|
|
|
|
p++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
/* skip whitespaces */
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
case '\\':
|
|
|
|
/*
|
|
|
|
* backslash/newline combinations continue the
|
|
|
|
* statement. Skip it just like a whitespace.
|
|
|
|
*/
|
|
|
|
if (*(p + 1) == '\n') {
|
|
|
|
p += 2;
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-11 13:05:41 +00:00
|
|
|
break;
|
2023-01-07 09:18:16 +00:00
|
|
|
case '\n':
|
|
|
|
/*
|
|
|
|
* Makefiles use a line-based syntax, where the newline
|
|
|
|
* is the end of a statement. After seeing a newline,
|
|
|
|
* we expect the next token is a target.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
p++;
|
2023-01-07 09:18:16 +00:00
|
|
|
is_target = true;
|
|
|
|
continue;
|
|
|
|
case ':':
|
2018-01-11 13:05:45 +00:00
|
|
|
/*
|
2023-01-07 09:18:16 +00:00
|
|
|
* assume the first dependency after a colon as the
|
|
|
|
* source file.
|
2018-01-11 13:05:45 +00:00
|
|
|
*/
|
2023-01-07 09:18:16 +00:00
|
|
|
p++;
|
|
|
|
is_target = false;
|
|
|
|
is_source = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the end of the token */
|
|
|
|
q = p;
|
|
|
|
while (*q != ' ' && *q != '\t' && *q != '\n' && *q != '#' && *q != ':') {
|
|
|
|
if (*q == '\\') {
|
kbuild: fixdep: support concatenated dep files
The current use-case for fixdep is: a source file is run through a single
processing step, which creates a single dependency file as a side-effect,
which fixdep transforms into the file used by the kernel build process.
In order to transparently run the C pre-processor on device-tree files,
we wish to run both gcc -E and dtc on a source file in a single rule.
This generates two dependency files, which must be transformed together
into the file used by the kernel build process. This change modifies
fixdep so it can process the concatenation of multiple separate input
dependency files, and produce a correct unified output.
The code changes have the slight benefit of transforming the loop in
parse_dep_file() into more of a lexer/tokenizer, with the loop body being
more of a parser. Previously, some of this logic was mixed together
before the loop. I also added some comments, which I hope are useful.
Benchmarking shows that on a cross-compiled ARM tegra_defconfig build,
there is less than 0.5 seconds speed decrease with this change, on top
of a build time of ~2m24s. This is probably within the noise.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Rob Herring <rob.herring@calxeda.com>
2013-03-06 17:27:45 +00:00
|
|
|
/*
|
2023-01-07 09:18:16 +00:00
|
|
|
* backslash/newline combinations work like as
|
|
|
|
* a whitespace, so this is the end of token.
|
kbuild: fixdep: support concatenated dep files
The current use-case for fixdep is: a source file is run through a single
processing step, which creates a single dependency file as a side-effect,
which fixdep transforms into the file used by the kernel build process.
In order to transparently run the C pre-processor on device-tree files,
we wish to run both gcc -E and dtc on a source file in a single rule.
This generates two dependency files, which must be transformed together
into the file used by the kernel build process. This change modifies
fixdep so it can process the concatenation of multiple separate input
dependency files, and produce a correct unified output.
The code changes have the slight benefit of transforming the loop in
parse_dep_file() into more of a lexer/tokenizer, with the loop body being
more of a parser. Previously, some of this logic was mixed together
before the loop. I also added some comments, which I hope are useful.
Benchmarking shows that on a cross-compiled ARM tegra_defconfig build,
there is less than 0.5 seconds speed decrease with this change, on top
of a build time of ~2m24s. This is probably within the noise.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Rob Herring <rob.herring@calxeda.com>
2013-03-06 17:27:45 +00:00
|
|
|
*/
|
2023-01-07 09:18:16 +00:00
|
|
|
if (*(q + 1) == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* escaped special characters */
|
|
|
|
if (*(q + 1) == '#' || *(q + 1) == ':') {
|
|
|
|
memmove(p + 1, p, q - p);
|
|
|
|
p++;
|
2018-01-11 13:05:45 +00:00
|
|
|
}
|
2023-01-07 09:18:16 +00:00
|
|
|
|
|
|
|
q++;
|
kbuild: fixdep: support concatenated dep files
The current use-case for fixdep is: a source file is run through a single
processing step, which creates a single dependency file as a side-effect,
which fixdep transforms into the file used by the kernel build process.
In order to transparently run the C pre-processor on device-tree files,
we wish to run both gcc -E and dtc on a source file in a single rule.
This generates two dependency files, which must be transformed together
into the file used by the kernel build process. This change modifies
fixdep so it can process the concatenation of multiple separate input
dependency files, and produce a correct unified output.
The code changes have the slight benefit of transforming the loop in
parse_dep_file() into more of a lexer/tokenizer, with the loop body being
more of a parser. Previously, some of this logic was mixed together
before the loop. I also added some comments, which I hope are useful.
Benchmarking shows that on a cross-compiled ARM tegra_defconfig build,
there is less than 0.5 seconds speed decrease with this change, on top
of a build time of ~2m24s. This is probably within the noise.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Rob Herring <rob.herring@calxeda.com>
2013-03-06 17:27:45 +00:00
|
|
|
}
|
2018-01-11 13:05:45 +00:00
|
|
|
|
2023-01-07 09:18:16 +00:00
|
|
|
if (*q == '\0')
|
|
|
|
break;
|
|
|
|
q++;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2018-01-11 13:05:41 +00:00
|
|
|
|
2023-01-07 09:18:16 +00:00
|
|
|
/* Just discard the target */
|
|
|
|
if (is_target) {
|
|
|
|
p = q;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_c = *q;
|
|
|
|
*q = '\0';
|
|
|
|
need_parse = false;
|
2018-01-11 13:05:41 +00:00
|
|
|
|
kbuild: fixdep: support concatenated dep files
The current use-case for fixdep is: a source file is run through a single
processing step, which creates a single dependency file as a side-effect,
which fixdep transforms into the file used by the kernel build process.
In order to transparently run the C pre-processor on device-tree files,
we wish to run both gcc -E and dtc on a source file in a single rule.
This generates two dependency files, which must be transformed together
into the file used by the kernel build process. This change modifies
fixdep so it can process the concatenation of multiple separate input
dependency files, and produce a correct unified output.
The code changes have the slight benefit of transforming the loop in
parse_dep_file() into more of a lexer/tokenizer, with the loop body being
more of a parser. Previously, some of this logic was mixed together
before the loop. I also added some comments, which I hope are useful.
Benchmarking shows that on a cross-compiled ARM tegra_defconfig build,
there is less than 0.5 seconds speed decrease with this change, on top
of a build time of ~2m24s. This is probably within the noise.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Rob Herring <rob.herring@calxeda.com>
2013-03-06 17:27:45 +00:00
|
|
|
/*
|
2023-01-07 09:18:16 +00:00
|
|
|
* Do not list the source file as dependency, so that kbuild is
|
|
|
|
* not confused if a .c file is rewritten into .S or vice versa.
|
|
|
|
* Storing it in source_* is needed for modpost to compute
|
|
|
|
* srcversions.
|
kbuild: fixdep: support concatenated dep files
The current use-case for fixdep is: a source file is run through a single
processing step, which creates a single dependency file as a side-effect,
which fixdep transforms into the file used by the kernel build process.
In order to transparently run the C pre-processor on device-tree files,
we wish to run both gcc -E and dtc on a source file in a single rule.
This generates two dependency files, which must be transformed together
into the file used by the kernel build process. This change modifies
fixdep so it can process the concatenation of multiple separate input
dependency files, and produce a correct unified output.
The code changes have the slight benefit of transforming the loop in
parse_dep_file() into more of a lexer/tokenizer, with the loop body being
more of a parser. Previously, some of this logic was mixed together
before the loop. I also added some comments, which I hope are useful.
Benchmarking shows that on a cross-compiled ARM tegra_defconfig build,
there is less than 0.5 seconds speed decrease with this change, on top
of a build time of ~2m24s. This is probably within the noise.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Rob Herring <rob.herring@calxeda.com>
2013-03-06 17:27:45 +00:00
|
|
|
*/
|
2023-01-07 09:18:16 +00:00
|
|
|
if (is_source) {
|
|
|
|
/*
|
|
|
|
* The DT build rule concatenates multiple dep files.
|
|
|
|
* When processing them, only process the first source
|
|
|
|
* name, which will be the original one, and ignore any
|
|
|
|
* other source names, which will be intermediate
|
|
|
|
* temporary files.
|
fixdep: avoid parsing the same file over again
The dep files (*.d files) emitted by C compilers usually contain the
deduplicated list of included files.
One exceptional case is when a header is included by the -include
command line option, and also by #include directive.
For example, the top Makefile adds the command line option,
"-include $(srctree)/include/linux/kconfig.h". You do not need to
include <linux/kconfig.h> in every source file.
In fact, include/linux/kconfig.h is listed twice in many .*.cmd files
due to include/linux/xarray.h having "#include <linux/kconfig.h>".
I did not fix that since it is a small redundancy.
However, this is more annoying for rustc. rustc emits the dependency
for each emission type.
For example, cmd_rustc_library emits dep-info, obj, and metadata.
So, the emitted *.d file contains the dependency for those 3 targets,
which makes fixdep parse the same file 3 times.
$ grep rust/alloc/raw_vec.rs rust/.alloc.o.cmd
rust/alloc/raw_vec.rs \
rust/alloc/raw_vec.rs \
rust/alloc/raw_vec.rs \
To skip the second parsing, this commit adds a hash table for parsed
files, just like we did for CONFIG options.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Tested-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-01-07 09:18:19 +00:00
|
|
|
*
|
|
|
|
* rustc emits the same dependency list for each
|
|
|
|
* emission type. It is enough to list the source name
|
|
|
|
* just once.
|
2023-01-07 09:18:16 +00:00
|
|
|
*/
|
|
|
|
if (!saw_any_target) {
|
|
|
|
saw_any_target = true;
|
|
|
|
printf("source_%s := %s\n\n", target, p);
|
|
|
|
printf("deps_%s := \\\n", target);
|
|
|
|
need_parse = true;
|
|
|
|
}
|
fixdep: avoid parsing the same file over again
The dep files (*.d files) emitted by C compilers usually contain the
deduplicated list of included files.
One exceptional case is when a header is included by the -include
command line option, and also by #include directive.
For example, the top Makefile adds the command line option,
"-include $(srctree)/include/linux/kconfig.h". You do not need to
include <linux/kconfig.h> in every source file.
In fact, include/linux/kconfig.h is listed twice in many .*.cmd files
due to include/linux/xarray.h having "#include <linux/kconfig.h>".
I did not fix that since it is a small redundancy.
However, this is more annoying for rustc. rustc emits the dependency
for each emission type.
For example, cmd_rustc_library emits dep-info, obj, and metadata.
So, the emitted *.d file contains the dependency for those 3 targets,
which makes fixdep parse the same file 3 times.
$ grep rust/alloc/raw_vec.rs rust/.alloc.o.cmd
rust/alloc/raw_vec.rs \
rust/alloc/raw_vec.rs \
rust/alloc/raw_vec.rs \
To skip the second parsing, this commit adds a hash table for parsed
files, just like we did for CONFIG options.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Tested-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-01-07 09:18:19 +00:00
|
|
|
} else if (!is_ignored_file(p, q - p) &&
|
|
|
|
!in_hashtable(p, q - p, file_hashtab)) {
|
2023-01-07 09:18:16 +00:00
|
|
|
printf(" %s \\\n", p);
|
|
|
|
need_parse = true;
|
|
|
|
}
|
|
|
|
|
2023-01-07 09:18:20 +00:00
|
|
|
if (need_parse && !is_no_parse_file(p, q - p)) {
|
2023-01-07 09:18:16 +00:00
|
|
|
void *buf;
|
|
|
|
|
|
|
|
buf = read_file(p);
|
|
|
|
parse_config_file(buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
is_source = false;
|
|
|
|
*q = saved_c;
|
|
|
|
p = q;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
kbuild: fixdep: support concatenated dep files
The current use-case for fixdep is: a source file is run through a single
processing step, which creates a single dependency file as a side-effect,
which fixdep transforms into the file used by the kernel build process.
In order to transparently run the C pre-processor on device-tree files,
we wish to run both gcc -E and dtc on a source file in a single rule.
This generates two dependency files, which must be transformed together
into the file used by the kernel build process. This change modifies
fixdep so it can process the concatenation of multiple separate input
dependency files, and produce a correct unified output.
The code changes have the slight benefit of transforming the loop in
parse_dep_file() into more of a lexer/tokenizer, with the loop body being
more of a parser. Previously, some of this logic was mixed together
before the loop. I also added some comments, which I hope are useful.
Benchmarking shows that on a cross-compiled ARM tegra_defconfig build,
there is less than 0.5 seconds speed decrease with this change, on top
of a build time of ~2m24s. This is probably within the noise.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Rob Herring <rob.herring@calxeda.com>
2013-03-06 17:27:45 +00:00
|
|
|
|
|
|
|
if (!saw_any_target) {
|
|
|
|
fprintf(stderr, "fixdep: parse error; no targets found\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
fixdep: use fflush() and ferror() to ensure successful write to files
Currently, fixdep checks the return value from (v)printf(), but it does
not ensure the complete write to the .cmd file.
printf() just writes data to the internal buffer, which usually succeeds.
(Of course, it may fail for another reason, for example when the file
descriptor is closed, but that is another story.)
When the buffer (4k?) is full, an actual write occurs, and printf() may
really fail. One of typical cases is "No space left on device" when the
disk is full.
The data remaining in the buffer will be pushed out to the file when
the program exits, but we never know if it is successful.
One straight-forward fix would be to add the following code at the end
of the program.
ret = fflush(stdout);
if (ret < 0) {
/* error handling */
}
However, it is tedious to check the return code in all the call sites
of printf(), fflush(), fclose(), and whatever can cause actual writes
to the end device. Doing that lets the program bail out at the first
failure but is usually not worth the effort.
Instead, let's check the error status from ferror(). This is 'sticky',
so you need to check it just once. You still need to call fflush().
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: David Laight <david.laight@aculab.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
2022-03-06 07:25:35 +00:00
|
|
|
printf("\n%s: $(deps_%s)\n\n", target, target);
|
|
|
|
printf("$(deps_%s):\n", target);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-01-11 13:05:44 +00:00
|
|
|
const char *depfile, *target, *cmdline;
|
2018-01-11 13:05:42 +00:00
|
|
|
void *buf;
|
|
|
|
|
2018-11-30 01:05:26 +00:00
|
|
|
if (argc != 4)
|
2005-04-16 22:20:36 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
depfile = argv[1];
|
|
|
|
target = argv[2];
|
|
|
|
cmdline = argv[3];
|
|
|
|
|
2022-12-29 09:15:00 +00:00
|
|
|
printf("savedcmd_%s := %s\n\n", target, cmdline);
|
2018-01-11 13:05:42 +00:00
|
|
|
|
|
|
|
buf = read_file(depfile);
|
2018-11-30 01:05:26 +00:00
|
|
|
parse_dep_file(buf, target);
|
2018-01-11 13:05:42 +00:00
|
|
|
free(buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
fixdep: use fflush() and ferror() to ensure successful write to files
Currently, fixdep checks the return value from (v)printf(), but it does
not ensure the complete write to the .cmd file.
printf() just writes data to the internal buffer, which usually succeeds.
(Of course, it may fail for another reason, for example when the file
descriptor is closed, but that is another story.)
When the buffer (4k?) is full, an actual write occurs, and printf() may
really fail. One of typical cases is "No space left on device" when the
disk is full.
The data remaining in the buffer will be pushed out to the file when
the program exits, but we never know if it is successful.
One straight-forward fix would be to add the following code at the end
of the program.
ret = fflush(stdout);
if (ret < 0) {
/* error handling */
}
However, it is tedious to check the return code in all the call sites
of printf(), fflush(), fclose(), and whatever can cause actual writes
to the end device. Doing that lets the program bail out at the first
failure but is usually not worth the effort.
Instead, let's check the error status from ferror(). This is 'sticky',
so you need to check it just once. You still need to call fflush().
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: David Laight <david.laight@aculab.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
2022-03-06 07:25:35 +00:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the intended usage, the stdout is redirected to .*.cmd files.
|
|
|
|
* Call ferror() to catch errors such as "No space left on device".
|
|
|
|
*/
|
|
|
|
if (ferror(stdout)) {
|
|
|
|
fprintf(stderr, "fixdep: not all data was written to the output\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|