linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer
@ 2018-01-09  8:30 Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 2/6] fixdep: merge do_config_file() and print_deps() into load_file() Masahiro Yamada
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-01-09  8:30 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Michal Marek, Lukas Bulwahn, Masahiro Yamada, linux-kernel

Commit dee81e988674 ("fixdep: faster CONFIG_ search") changed how to
load files in which CONFIG options are searched.  It used malloc()
and read() instead of mmap() because it needed to zero-terminate the
buffer to use strstr().  print_deps() was left untouched since there
was no reason to change it.

Now, I have two motivations to change it in the same way.

 - do_config_file() and print_deps() do quite similar things; they
   open a file, map it onto memory, and pass it to a parser function.
   If we use malloc() and read() for print_deps() too, we can factor
   out the common code.  (I will do this in the next commit.)

 - parse_dep_file() copies each token to a temporary buffer because
   it needs to zero-terminate it to be passed to printf().  It is not
   possible to modify the buffer directly because it is mmap'ed with
   O_RDONLY.  If we load the file content into a malloc'ed buffer, we
   can insert '\0' after each token, and save memcpy().  (I will do
   this in the commit after next.)

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/basic/fixdep.c | 55 +++++++++++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 23 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 86a61d6..7efbeb7 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -309,24 +309,27 @@ static void do_config_file(const char *filename)
  * assignments are parsed not only by make, but also by the rather simple
  * parser in scripts/mod/sumversion.c.
  */
-static void parse_dep_file(void *map, size_t len)
+static void parse_dep_file(char *m)
 {
-	char *m = map;
-	char *end = m + len;
 	char *p;
 	char s[PATH_MAX];
-	int is_target;
+	int is_last, is_target;
 	int saw_any_target = 0;
 	int is_first_dep = 0;
 
-	while (m < end) {
+	while (1) {
 		/* Skip any "white space" */
-		while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
+		while (*m == ' ' || *m == '\\' || *m == '\n')
 			m++;
+
+		if (!*m)
+			break;
+
 		/* Find next "white space" */
 		p = m;
-		while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
+		while (*p && *p != ' ' && *p != '\\' && *p != '\n')
 			p++;
+		is_last = (*p == '\0');
 		/* Is the token we found a target name? */
 		is_target = (*(p-1) == ':');
 		/* Don't write any target names into the dependency file */
@@ -374,6 +377,10 @@ static void parse_dep_file(void *map, size_t len)
 				do_config_file(s);
 			}
 		}
+
+		if (is_last)
+			break;
+
 		/*
 		 * Start searching for next token immediately after the first
 		 * "whitespace" character that follows this token.
@@ -392,40 +399,42 @@ static void parse_dep_file(void *map, size_t len)
 	printf("$(deps_%s):\n", target);
 }
 
-static void print_deps(void)
+static void print_deps(const char *filename)
 {
 	struct stat st;
 	int fd;
-	void *map;
+	char *buf;
 
-	fd = open(depfile, O_RDONLY);
+	fd = open(filename, O_RDONLY);
 	if (fd < 0) {
 		fprintf(stderr, "fixdep: error opening depfile: ");
-		perror(depfile);
+		perror(filename);
 		exit(2);
 	}
 	if (fstat(fd, &st) < 0) {
 		fprintf(stderr, "fixdep: error fstat'ing depfile: ");
-		perror(depfile);
+		perror(filename);
 		exit(2);
 	}
 	if (st.st_size == 0) {
-		fprintf(stderr,"fixdep: %s is empty\n",depfile);
 		close(fd);
 		return;
 	}
-	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-	if ((long) map == -1) {
-		perror("fixdep: mmap");
-		close(fd);
-		return;
+	buf = malloc(st.st_size + 1);
+	if (!buf) {
+		perror("fixdep: malloc");
+		exit(2);
 	}
+	if (read(fd, buf, st.st_size) != st.st_size) {
+		perror("fixdep: read");
+		exit(2);
+	}
+	buf[st.st_size] = '\0';
+	close(fd);
 
-	parse_dep_file(map, st.st_size);
-
-	munmap(map, st.st_size);
+	parse_dep_file(buf);
 
-	close(fd);
+	free(buf);
 }
 
 int main(int argc, char *argv[])
@@ -441,7 +450,7 @@ int main(int argc, char *argv[])
 	cmdline = argv[3];
 
 	print_cmdline();
-	print_deps();
+	print_deps(depfile);
 
 	return 0;
 }
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/6] fixdep: merge do_config_file() and print_deps() into load_file()
  2018-01-09  8:30 [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Masahiro Yamada
@ 2018-01-09  8:30 ` Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 3/6] fixdep: remove unneeded memcpy() in parse_dep_file() Masahiro Yamada
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-01-09  8:30 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Michal Marek, Lukas Bulwahn, Masahiro Yamada, linux-kernel

Now, do_config_files() and print_deps() are almost the same.  Only
the difference is the parser function called (parse_config_file vs
parse_dep_file).

We can reduce the code duplication by putting the common code into
load_file() - this function allocates a buffer and loads a file to
it.  It returns the pointer to the allocated buffer.  (As before,
it bails out by exit(2) for any error.)  The caller must free the
buffer when done.

Having empty source files is possible; fixdep should simply skip them.
I deleted the "st.st_size == 0" check, so load_file() allocates 1-byte
buffer for an empty file.  strstr() will just return NULL, and this is
what we expect.

On the other hand, empty dep_file should be treated as an error.  In
this case, parse_dep_file() will error out with "no targets found"
and it is a correct error message.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/basic/fixdep.c | 75 ++++++++++++++------------------------------------
 1 file changed, 20 insertions(+), 55 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 7efbeb7..f954f226 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -266,42 +266,36 @@ static int strrcmp(const char *s, const char *sub)
 	return memcmp(s + slen - sublen, sub, sublen);
 }
 
-static void do_config_file(const char *filename)
+static void *load_file(const char *filename)
 {
 	struct stat st;
 	int fd;
-	char *map;
+	char *buf;
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0) {
-		fprintf(stderr, "fixdep: error opening config file: ");
+		fprintf(stderr, "fixdep: error opening file: ");
 		perror(filename);
 		exit(2);
 	}
 	if (fstat(fd, &st) < 0) {
-		fprintf(stderr, "fixdep: error fstat'ing config file: ");
+		fprintf(stderr, "fixdep: error fstat'ing file: ");
 		perror(filename);
 		exit(2);
 	}
-	if (st.st_size == 0) {
-		close(fd);
-		return;
-	}
-	map = malloc(st.st_size + 1);
-	if (!map) {
+	buf = malloc(st.st_size + 1);
+	if (!buf) {
 		perror("fixdep: malloc");
 		exit(2);
 	}
-	if (read(fd, map, st.st_size) != st.st_size) {
+	if (read(fd, buf, st.st_size) != st.st_size) {
 		perror("fixdep: read");
 		exit(2);
 	}
-	map[st.st_size] = '\0';
+	buf[st.st_size] = '\0';
 	close(fd);
 
-	parse_config_file(map);
-
-	free(map);
+	return buf;
 }
 
 /*
@@ -316,6 +310,7 @@ static void parse_dep_file(char *m)
 	int is_last, is_target;
 	int saw_any_target = 0;
 	int is_first_dep = 0;
+	void *buf;
 
 	while (1) {
 		/* Skip any "white space" */
@@ -374,7 +369,10 @@ static void parse_dep_file(char *m)
 					is_first_dep = 0;
 				} else
 					printf("  %s \\\n", s);
-				do_config_file(s);
+
+				buf = load_file(s);
+				parse_config_file(buf);
+				free(buf);
 			}
 		}
 
@@ -399,46 +397,10 @@ static void parse_dep_file(char *m)
 	printf("$(deps_%s):\n", target);
 }
 
-static void print_deps(const char *filename)
-{
-	struct stat st;
-	int fd;
-	char *buf;
-
-	fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		fprintf(stderr, "fixdep: error opening depfile: ");
-		perror(filename);
-		exit(2);
-	}
-	if (fstat(fd, &st) < 0) {
-		fprintf(stderr, "fixdep: error fstat'ing depfile: ");
-		perror(filename);
-		exit(2);
-	}
-	if (st.st_size == 0) {
-		close(fd);
-		return;
-	}
-	buf = malloc(st.st_size + 1);
-	if (!buf) {
-		perror("fixdep: malloc");
-		exit(2);
-	}
-	if (read(fd, buf, st.st_size) != st.st_size) {
-		perror("fixdep: read");
-		exit(2);
-	}
-	buf[st.st_size] = '\0';
-	close(fd);
-
-	parse_dep_file(buf);
-
-	free(buf);
-}
-
 int main(int argc, char *argv[])
 {
+	void *buf;
+
 	if (argc == 5 && !strcmp(argv[1], "-e")) {
 		insert_extra_deps = 1;
 		argv++;
@@ -450,7 +412,10 @@ int main(int argc, char *argv[])
 	cmdline = argv[3];
 
 	print_cmdline();
-	print_deps(depfile);
+
+	buf = load_file(depfile);
+	parse_dep_file(buf);
+	free(buf);
 
 	return 0;
 }
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/6] fixdep: remove unneeded memcpy() in parse_dep_file()
  2018-01-09  8:30 [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 2/6] fixdep: merge do_config_file() and print_deps() into load_file() Masahiro Yamada
@ 2018-01-09  8:30 ` Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 4/6] fixdep: move global variables to local variables of main() Masahiro Yamada
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-01-09  8:30 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Michal Marek, Lukas Bulwahn, Masahiro Yamada, linux-kernel

Each token in the depfile is copied to the temporary buffer 's' to
terminate the token with zero.  We do not need to do this any more
because the parsed buffer is now writable.  Insert '\0' directly in
the buffer without calling memcpy().

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/basic/fixdep.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index f954f226..bc2364e 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -306,7 +306,6 @@ static void *load_file(const char *filename)
 static void parse_dep_file(char *m)
 {
 	char *p;
-	char s[PATH_MAX];
 	int is_last, is_target;
 	int saw_any_target = 0;
 	int is_first_dep = 0;
@@ -332,16 +331,14 @@ static void parse_dep_file(char *m)
 			/* The /next/ file is the first dependency */
 			is_first_dep = 1;
 		} else {
-			/* Save this token/filename */
-			memcpy(s, m, p-m);
-			s[p - m] = 0;
+			*p = '\0';
 
 			/* Ignore certain dependencies */
-			if (strrcmp(s, "include/generated/autoconf.h") &&
-			    strrcmp(s, "include/generated/autoksyms.h") &&
-			    strrcmp(s, "arch/um/include/uml-config.h") &&
-			    strrcmp(s, "include/linux/kconfig.h") &&
-			    strrcmp(s, ".ver")) {
+			if (strrcmp(m, "include/generated/autoconf.h") &&
+			    strrcmp(m, "include/generated/autoksyms.h") &&
+			    strrcmp(m, "arch/um/include/uml-config.h") &&
+			    strrcmp(m, "include/linux/kconfig.h") &&
+			    strrcmp(m, ".ver")) {
 				/*
 				 * Do not list the source file as dependency,
 				 * so that kbuild is not confused if a .c file
@@ -362,15 +359,15 @@ static void parse_dep_file(char *m)
 					if (!saw_any_target) {
 						saw_any_target = 1;
 						printf("source_%s := %s\n\n",
-							target, s);
+							target, m);
 						printf("deps_%s := \\\n",
 							target);
 					}
 					is_first_dep = 0;
 				} else
-					printf("  %s \\\n", s);
+					printf("  %s \\\n", m);
 
-				buf = load_file(s);
+				buf = load_file(m);
 				parse_config_file(buf);
 				free(buf);
 			}
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/6] fixdep: move global variables to local variables of main()
  2018-01-09  8:30 [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 2/6] fixdep: merge do_config_file() and print_deps() into load_file() Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 3/6] fixdep: remove unneeded memcpy() in parse_dep_file() Masahiro Yamada
@ 2018-01-09  8:30 ` Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 5/6] fixdep: refactor parse_dep_file() Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 6/6] fixdep: use existing helper to check modular CONFIG options Masahiro Yamada
  4 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-01-09  8:30 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Michal Marek, Lukas Bulwahn, Masahiro Yamada, linux-kernel

I do not mind global variables where they are useful enough.  In this
case, I do not see a good reason to use global variables since they
are just referenced in shallow places.  It is easy to pass them via
function arguments.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/basic/fixdep.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index bc2364e..bb6d2da 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -114,11 +114,6 @@
 #include <ctype.h>
 #include <arpa/inet.h>
 
-int insert_extra_deps;
-char *target;
-char *depfile;
-char *cmdline;
-
 static void usage(void)
 {
 	fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n");
@@ -129,7 +124,7 @@ static void usage(void)
 /*
  * Print out the commandline prefixed with cmd_<target filename> :=
  */
-static void print_cmdline(void)
+static void print_cmdline(const char *target, const char *cmdline)
 {
 	printf("cmd_%s := %s\n\n", target, cmdline);
 }
@@ -155,16 +150,16 @@ static void print_config(const char *m, int slen)
 
 static void do_extra_deps(void)
 {
-	if (insert_extra_deps) {
-		char buf[80];
-		while(fgets(buf, sizeof(buf), stdin)) {
-			int len = strlen(buf);
-			if (len < 2 || buf[len-1] != '\n') {
-				fprintf(stderr, "fixdep: bad data on stdin\n");
-				exit(1);
-			}
-			print_config(buf, len-1);
+	char buf[80];
+
+	while (fgets(buf, sizeof(buf), stdin)) {
+		int len = strlen(buf);
+
+		if (len < 2 || buf[len - 1] != '\n') {
+			fprintf(stderr, "fixdep: bad data on stdin\n");
+			exit(1);
 		}
+		print_config(buf, len - 1);
 	}
 }
 
@@ -303,7 +298,7 @@ static void *load_file(const char *filename)
  * assignments are parsed not only by make, but also by the rather simple
  * parser in scripts/mod/sumversion.c.
  */
-static void parse_dep_file(char *m)
+static void parse_dep_file(char *m, const char *target, int insert_extra_deps)
 {
 	char *p;
 	int is_last, is_target;
@@ -388,7 +383,8 @@ static void parse_dep_file(char *m)
 		exit(1);
 	}
 
-	do_extra_deps();
+	if (insert_extra_deps)
+		do_extra_deps();
 
 	printf("\n%s: $(deps_%s)\n\n", target, target);
 	printf("$(deps_%s):\n", target);
@@ -396,6 +392,8 @@ static void parse_dep_file(char *m)
 
 int main(int argc, char *argv[])
 {
+	const char *depfile, *target, *cmdline;
+	int insert_extra_deps = 0;
 	void *buf;
 
 	if (argc == 5 && !strcmp(argv[1], "-e")) {
@@ -408,10 +406,10 @@ int main(int argc, char *argv[])
 	target = argv[2];
 	cmdline = argv[3];
 
-	print_cmdline();
+	print_cmdline(target, cmdline);
 
 	buf = load_file(depfile);
-	parse_dep_file(buf);
+	parse_dep_file(buf, target, insert_extra_deps);
 	free(buf);
 
 	return 0;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 5/6] fixdep: refactor parse_dep_file()
  2018-01-09  8:30 [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Masahiro Yamada
                   ` (2 preceding siblings ...)
  2018-01-09  8:30 ` [PATCH 4/6] fixdep: move global variables to local variables of main() Masahiro Yamada
@ 2018-01-09  8:30 ` Masahiro Yamada
  2018-01-09  8:30 ` [PATCH 6/6] fixdep: use existing helper to check modular CONFIG options Masahiro Yamada
  4 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-01-09  8:30 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Michal Marek, Lukas Bulwahn, Masahiro Yamada, linux-kernel

parse_dep_file() has too much indentation, and puts the code far to
the right.  This commit refactors the code and reduces the one level
of indentation.

strrcmp() computes 'slen' by itself, but the caller already knows the
length of the token, so 'slen' can be passed via function argument.
With this, we can swap the order of strrcmp() and "*p = \0;"

Also, strrcmp() is an ambiguous function name.  Flip the logic and
rename it to str_ends_with().

I added a new helper is_ignored_file() - this returns 1 if the token
represents a file that should be ignored.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/basic/fixdep.c | 80 +++++++++++++++++++++++++-------------------------
 1 file changed, 40 insertions(+), 40 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index bb6d2da..3881bf1 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -250,15 +250,14 @@ static void parse_config_file(const char *p)
 }
 
 /* test if s ends in sub */
-static int strrcmp(const char *s, const char *sub)
+static int str_ends_with(const char *s, int slen, const char *sub)
 {
-	int slen = strlen(s);
 	int sublen = strlen(sub);
 
 	if (sublen > slen)
-		return 1;
+		return 0;
 
-	return memcmp(s + slen - sublen, sub, sublen);
+	return !memcmp(s + slen - sublen, sub, sublen);
 }
 
 static void *load_file(const char *filename)
@@ -293,6 +292,16 @@ static void *load_file(const char *filename)
 	return buf;
 }
 
+/* Ignore certain dependencies */
+static int is_ignored_file(const char *s, int len)
+{
+	return str_ends_with(s, len, "include/generated/autoconf.h") ||
+	       str_ends_with(s, len, "include/generated/autoksyms.h") ||
+	       str_ends_with(s, len, "arch/um/include/uml-config.h") ||
+	       str_ends_with(s, len, "include/linux/kconfig.h") ||
+	       str_ends_with(s, len, ".ver");
+}
+
 /*
  * 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
@@ -325,47 +334,38 @@ static void parse_dep_file(char *m, const char *target, int insert_extra_deps)
 		if (is_target) {
 			/* The /next/ file is the first dependency */
 			is_first_dep = 1;
-		} else {
+		} else if (!is_ignored_file(m, p - m)) {
 			*p = '\0';
 
-			/* Ignore certain dependencies */
-			if (strrcmp(m, "include/generated/autoconf.h") &&
-			    strrcmp(m, "include/generated/autoksyms.h") &&
-			    strrcmp(m, "arch/um/include/uml-config.h") &&
-			    strrcmp(m, "include/linux/kconfig.h") &&
-			    strrcmp(m, ".ver")) {
+			/*
+			 * 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.
+			 */
+			if (is_first_dep) {
 				/*
-				 * 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.
+				 * If processing the concatenation of multiple
+				 * dependency files, only process the first
+				 * target name, which will be the original
+				 * source name, and ignore any other target
+				 * names, which will be intermediate temporary
+				 * files.
 				 */
-				if (is_first_dep) {
-					/*
-					 * If processing the concatenation of
-					 * multiple dependency files, only
-					 * process the first target name, which
-					 * will be the original source name,
-					 * and ignore any other target names,
-					 * which will be intermediate temporary
-					 * files.
-					 */
-					if (!saw_any_target) {
-						saw_any_target = 1;
-						printf("source_%s := %s\n\n",
-							target, m);
-						printf("deps_%s := \\\n",
-							target);
-					}
-					is_first_dep = 0;
-				} else
-					printf("  %s \\\n", m);
-
-				buf = load_file(m);
-				parse_config_file(buf);
-				free(buf);
+				if (!saw_any_target) {
+					saw_any_target = 1;
+					printf("source_%s := %s\n\n",
+					       target, m);
+					printf("deps_%s := \\\n", target);
+				}
+				is_first_dep = 0;
+			} else {
+				printf("  %s \\\n", m);
 			}
+
+			buf = load_file(m);
+			parse_config_file(buf);
+			free(buf);
 		}
 
 		if (is_last)
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 6/6] fixdep: use existing helper to check modular CONFIG options
  2018-01-09  8:30 [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Masahiro Yamada
                   ` (3 preceding siblings ...)
  2018-01-09  8:30 ` [PATCH 5/6] fixdep: refactor parse_dep_file() Masahiro Yamada
@ 2018-01-09  8:30 ` Masahiro Yamada
  4 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-01-09  8:30 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Michal Marek, Lukas Bulwahn, Masahiro Yamada, linux-kernel

str_ends_with() (it was strrcmp() before) was introduced to check if
the given token ends with a particular string.  Currently, it is used
to check file paths without $(srctree).

Actually, we have one more place where this helper is useful.  Use it
to check if CONFIG option ends with _MODULE.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 scripts/basic/fixdep.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 3881bf1..9642302 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -230,6 +230,17 @@ static void use_config(const char *m, int slen)
 	print_config(m, slen);
 }
 
+/* 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);
+}
+
 static void parse_config_file(const char *p)
 {
 	const char *q, *r;
@@ -239,7 +250,7 @@ static void parse_config_file(const char *p)
 		q = p;
 		while (*q && (isalnum(*q) || *q == '_'))
 			q++;
-		if (memcmp(q - 7, "_MODULE", 7) == 0)
+		if (str_ends_with(p, q - p, "_MODULE"))
 			r = q - 7;
 		else
 			r = q;
@@ -249,17 +260,6 @@ static void parse_config_file(const char *p)
 	}
 }
 
-/* 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);
-}
-
 static void *load_file(const char *filename)
 {
 	struct stat st;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-01-09  8:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-09  8:30 [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Masahiro Yamada
2018-01-09  8:30 ` [PATCH 2/6] fixdep: merge do_config_file() and print_deps() into load_file() Masahiro Yamada
2018-01-09  8:30 ` [PATCH 3/6] fixdep: remove unneeded memcpy() in parse_dep_file() Masahiro Yamada
2018-01-09  8:30 ` [PATCH 4/6] fixdep: move global variables to local variables of main() Masahiro Yamada
2018-01-09  8:30 ` [PATCH 5/6] fixdep: refactor parse_dep_file() Masahiro Yamada
2018-01-09  8:30 ` [PATCH 6/6] fixdep: use existing helper to check modular CONFIG options Masahiro Yamada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).