From f39632d9579d3c97f1e50a728efed3c5409747d2 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 27 May 2021 13:59:01 -0400 Subject: [PATCH] gdb: add make-init-c script I would like to modify how the init.c file is generated (its content). But as it is, a shell script with multiple sed invocations in a Makefile target, it's not very maintainable. Replace that with a shell script that does the same, but in a more readable way. The Makefile rule uses the "-" prefix in front of the for loop, I presume to ignore any error coming from the fact that xml-builtin.c and cp-name-parser.c are not found in the srcdir (they are generated source files). I prefer not to blindly ignore errors, so filter these files out of INIT_FILES instead (we already filter out other files). There are no expected meaningful changes to the generated init.c file. Just the _initialize_all_file declaration that is moved down and "void" in parenthesis that is removed. The new regular expression is a bit tighter than the existing one, it requires the init function to be followed by exactly ` ()`. Update bpf-tdep.c accordingly. gdb/ChangeLog: * Makefile.in (INIT_FILES_FILTER_OUT): New. (INIT_FILES): Use INIT_FILES_FILTER_OUT. (stamp-init): Use make-init-c. * bpf-tdep.c (_initialize_bpf_tdep): Remove "void". * silent-rules.mk (ECHO_INIT_C): Change. * make-init-c: New file. Change-Id: I6d6b12cbccf24ab79d1219bff05df01624c684f9 --- gdb/ChangeLog | 9 +++++++ gdb/Makefile.in | 39 +++++++++++-------------------- gdb/bpf-tdep.c | 2 +- gdb/make-init-c | 57 +++++++++++++++++++++++++++++++++++++++++++++ gdb/silent-rules.mk | 2 +- 5 files changed, 82 insertions(+), 27 deletions(-) create mode 100755 gdb/make-init-c diff --git a/gdb/ChangeLog b/gdb/ChangeLog index b52d06012f2..1db1264c302 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,12 @@ +2021-05-27 Simon Marchi + + * Makefile.in (INIT_FILES_FILTER_OUT): New. + (INIT_FILES): Use INIT_FILES_FILTER_OUT. + (stamp-init): Use make-init-c. + * bpf-tdep.c (_initialize_bpf_tdep): Remove "void". + * silent-rules.mk (ECHO_INIT_C): Change. + * make-init-c: New file. + 2021-05-27 Simon Marchi * command.h (add_alias_cmd): Accept target as diff --git a/gdb/Makefile.in b/gdb/Makefile.in index a9fade803b0..bb6c5dfa784 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1837,42 +1837,31 @@ test-cp-name-parser$(EXEEXT): test-cp-name-parser.o $(LIBIBERTY) # maybe we could just require every .o file to have an initialization routine # of a given name (top.o -> _initialize_top, etc.). # -# Formatting conventions: The name of the _initialize_* routines must start -# in column zero, and must not be inside #if. -# # Note that the set of files with init functions might change, or the names # of the functions might change, so this files needs to depend on all the # source files that will be linked into gdb. However, due to the way # this Makefile has generally been written, we do this indirectly, by # computing the list of source files from the list of object files. +INIT_FILES_FILTER_OUT = \ + cp-name-parser.o \ + init.o \ + version.o \ + xml-builtin.o \ + %_S.o \ + %_U.o + INIT_FILES = \ $(patsubst %.o,%.c, \ $(patsubst %-exp.o,%-exp.y, \ - $(filter-out init.o version.o %_S.o %_U.o,\ - $(COMMON_OBS)))) + $(filter-out $(INIT_FILES_FILTER_OUT), $(COMMON_OBS)))) init.c: stamp-init; @true -stamp-init: $(INIT_FILES) config.status - @$(ECHO_INIT_C) echo "Making init.c" - @rm -f init.c-tmp init.l-tmp - @touch init.c-tmp - @-for f in $(INIT_FILES); do \ - sed -n -e 's/^_initialize_\([a-z_0-9A-Z]*\).*/\1/p' \ - $(srcdir)/$$f 2>/dev/null; \ - done > init.l-tmp - @echo '/* Do not modify this file. */' >>init.c-tmp - @echo '/* It is created automatically by the Makefile. */'>>init.c-tmp - @echo '#include "defs.h" /* For initialize_file_ftype. */' >>init.c-tmp - @echo 'extern void initialize_all_files(void);' >>init.c-tmp - @sed -e 's/\(.*\)/extern initialize_file_ftype _initialize_\1;/' >init.c-tmp - @echo 'void' >>init.c-tmp - @echo 'initialize_all_files (void)' >>init.c-tmp - @echo '{' >>init.c-tmp - @sed -e 's/\(.*\)/ _initialize_\1 ();/' >init.c-tmp - @echo '}' >>init.c-tmp - @$(SHELL) $(srcdir)/../move-if-change init.c-tmp init.c - @echo stamp > stamp-init +stamp-init: $(INIT_FILES) config.status $(srcdir)/make-init-c + $(ECHO_INIT_C) + $(SILENCE) $(srcdir)/make-init-c $(addprefix $(srcdir)/,$(INIT_FILES)) > init.c-tmp + $(SILENCE) $(SHELL) $(srcdir)/../move-if-change init.c-tmp init.c + $(SILENCE) echo stamp > stamp-init .PRECIOUS: init.c diff --git a/gdb/bpf-tdep.c b/gdb/bpf-tdep.c index 352a22198e9..a29cd90ba38 100644 --- a/gdb/bpf-tdep.c +++ b/gdb/bpf-tdep.c @@ -370,7 +370,7 @@ bpf_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) void _initialize_bpf_tdep (); void -_initialize_bpf_tdep (void) +_initialize_bpf_tdep () { register_gdbarch_init (bfd_arch_bpf, bpf_gdbarch_init); diff --git a/gdb/make-init-c b/gdb/make-init-c new file mode 100755 index 00000000000..1588760112e --- /dev/null +++ b/gdb/make-init-c @@ -0,0 +1,57 @@ +#!/usr/bin/env sh + +# Copyright (C) 2013-2021 Free Software Foundation, Inc. +# +# This file is part of GDB. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Generate the init.c source file. +# +# Usage: +# +# ./make-init-c source-files > init.c-tmp +# +# Where SOURCE-FILES is the list of source files to extract init functions from. +# +# Formatting conventions: The name of the initialization routines must begin +# with `_initialize_`, must start in column zero, and be followed with exactly +# ` ()`. For example: +# +# void +# _initialize_foo () +# { +# ... +# } +# + +# Abort on command error. +set -e + +echo "/* Do not modify this file. */" +echo "/* It is created automatically by the Makefile. */" +echo "#include \"defs.h\" /* For initialize_file_ftype. */" +echo "" +sed -n -e 's/^\(_initialize_[a-zA-Z0-9_]*\) ()$/\1/p' "$@" | while read -r name; do + echo "extern initialize_file_ftype $name;" +done +echo "" +echo "void initialize_all_files ();" +echo "void" +echo "initialize_all_files ()" +echo "{" +sed -n -e 's/^\(_initialize_[a-zA-Z0-9_]*\) ()$/\1/p' "$@" | while read -r name; do + echo " $name ();" +done +echo "}" diff --git a/gdb/silent-rules.mk b/gdb/silent-rules.mk index 7ed73a767c6..f7b959f8390 100644 --- a/gdb/silent-rules.mk +++ b/gdb/silent-rules.mk @@ -9,7 +9,7 @@ ECHO_GEN_XML_BUILTIN = \ @echo " GEN xml-builtin.c"; ECHO_GEN_XML_BUILTIN_GENERATED = \ @echo " GEN xml-builtin-generated.c"; -ECHO_INIT_C = echo " GEN init.c" || +ECHO_INIT_C = @echo " GEN init.c" ECHO_SIGN = @echo " SIGN gdb"; ECHO_YACC = @echo " YACC $@"; ECHO_LEX = @echo " LEX $@"; -- 2.30.2