History

fixdep: refactor parse_dep_file()

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.

Masahiro Yamada committed 8 years ago

Commit 87b95a81 parent 5d1ef761 file · +40 −40Browse files

Showing the changes to scripts/basic (1 of 1 file). Show all

Unified · Split

scripts/basic/fixdep.c+40−40
@@ -239,15 +239,14 @@ static void parse_config_file(const char *p) · 238 unmodified lines
239239 }
240240
241241 /* test if s ends in sub */
242−static int strrcmp(const char *s, const char *sub)
242+static int str_ends_with(const char *s, int slen, const char *sub)
243243 {
244− int slen = strlen(s);
245244 int sublen = strlen(sub);
246245
247246 if (sublen > slen)
248− return 1;
247+ return 0;
249248
250− return memcmp(s + slen - sublen, sub, sublen);
249+ return !memcmp(s + slen - sublen, sub, sublen);
251250 }
252251
253252 static void *read_file(const char *filename)
@@ -282,6 +281,16 @@ static void *read_file(const char *filename) · 28 unmodified lines
282281 return buf;
283282 }
284283
284+/* Ignore certain dependencies */
285+static int is_ignored_file(const char *s, int len)
286+{
287+ return str_ends_with(s, len, "include/generated/autoconf.h") ||
288+ str_ends_with(s, len, "include/generated/autoksyms.h") ||
289+ str_ends_with(s, len, "arch/um/include/uml-config.h") ||
290+ str_ends_with(s, len, "include/linux/kconfig.h") ||
291+ str_ends_with(s, len, ".ver");
292+}
293+
285294 /*
286295 * Important: The below generated source_foo.o and deps_foo.o variable
287296 * assignments are parsed not only by make, but also by the rather simple
@@ -314,47 +323,38 @@ static void parse_dep_file(char *m, const char *target, int insert_extra_deps) · 26 unmodified lines
314323 if (is_target) {
315324 /* The /next/ file is the first dependency */
316325 is_first_dep = 1;
317− } else {
326+ } else if (!is_ignored_file(m, p - m)) {
318327 *p = '\0';
319328
320− /* Ignore certain dependencies */
321− if (strrcmp(m, "include/generated/autoconf.h") &&
322− strrcmp(m, "include/generated/autoksyms.h") &&
323− strrcmp(m, "arch/um/include/uml-config.h") &&
324− strrcmp(m, "include/linux/kconfig.h") &&
325− strrcmp(m, ".ver")) {
329+ /*
330+ * Do not list the source file as dependency, so that
331+ * kbuild is not confused if a .c file is rewritten
332+ * into .S or vice versa. Storing it in source_* is
333+ * needed for modpost to compute srcversions.
334+ */
335+ if (is_first_dep) {
326336 /*
327− * Do not list the source file as dependency,
328− * so that kbuild is not confused if a .c file
329− * is rewritten into .S or vice versa. Storing
330− * it in source_* is needed for modpost to
331− * compute srcversions.
337+ * If processing the concatenation of multiple
338+ * dependency files, only process the first
339+ * target name, which will be the original
340+ * source name, and ignore any other target
341+ * names, which will be intermediate temporary
342+ * files.
332343 */
333− if (is_first_dep) {
334− /*
335− * If processing the concatenation of
336− * multiple dependency files, only
337− * process the first target name, which
338− * will be the original source name,
339− * and ignore any other target names,
340− * which will be intermediate temporary
341− * files.
342− */
343− if (!saw_any_target) {
344− saw_any_target = 1;
345− printf("source_%s := %s\n\n",
346− target, m);
347− printf("deps_%s := \\\n",
348− target);
349− }
350− is_first_dep = 0;
351− } else
352− printf(" %s \\\n", m);
353−
354− buf = read_file(m);
355− parse_config_file(buf);
356− free(buf);
344+ if (!saw_any_target) {
345+ saw_any_target = 1;
346+ printf("source_%s := %s\n\n",
347+ target, m);
348+ printf("deps_%s := \\\n", target);
349+ }
350+ is_first_dep = 0;
351+ } else {
352+ printf(" %s \\\n", m);
357353 }
354+
355+ buf = read_file(m);
356+ parse_config_file(buf);
357+ free(buf);
358358 }
359359
360360 if (is_last)