i18n: fix exgettext handling of C++ sources
[platform/upstream/gcc.git] / gcc / po / exgettext
1 #! /bin/sh
2 # Wrapper around gettext for programs using the msgid convention.
3 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
4
5 # Written by Paul Eggert <eggert@twinsun.com>.
6 # Revised by Zack Weinberg <zackw@stanford.edu> for no-POTFILES operation.
7
8 # This file is part of GCC.
9
10 # GCC is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3, or (at your option)
13 # any later version.
14
15 # GCC is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19
20 # You should have received a copy of the GNU General Public License
21 # along with GCC; see the file COPYING3.  If not see
22 # <http://www.gnu.org/licenses/>.
23
24 BUGURL="https://gcc.gnu.org/bugs/"
25
26 # Always operate in the C locale.
27 LANG=C
28 LANGUAGE=C
29 LC_ALL=C
30 export LANG LANGUAGE LC_ALL
31
32 # Set AWK if environment has not already set it.
33 AWK=${AWK-awk}
34
35 # The arguments to this wrapper are: the program to execute, the
36 # name of the "package", and the path to the source directory.
37
38 if [ $# -ne 3 ]
39 then    echo "usage: $0 <xgettext> <package> <srcdir>"
40         exit 1
41 fi
42
43 xgettext=$1
44 package=$2
45 srcdir=$3
46
47 case `$xgettext --version | sed -e 1q | sed -e 's/^\([^0-9]*\)//'` in
48   0.14.[5-9]* | 0.14.[1-9][0-9]* | 0.1[5-9]* | 0.[2-9][0-9]* | [1-9].*) : ;;
49   *) echo "$xgettext is too old.  GNU xgettext 0.14.5 is required"
50      exit 1 ;;
51 esac
52
53 nl='
54 '
55
56 set -e
57
58 # Create temporary directory for scratch files.
59 T=exg$$.d
60 mkdir $T
61 trap "rm -r $T" 0
62
63 pwd=`${PWDCMD-pwd}`
64 kopt=$pwd/$T/keyword-options
65 kopt2=$pwd/$T/keyword2-options
66 emsg=$pwd/$T/emsgids.c
67 posr=$pwd/$T/po-sources
68 posrcxx=$pwd/$T/po-cxx-sources
69 pottmp1=$pwd/$T/tmp1.pot
70 pottmp2=$pwd/$T/tmp2.pot
71 pottmp3=$pwd/$T/tmp3.pot
72 pottmp4=$pwd/$T/tmp4.pot
73 pottmp=$pwd/$T/tmp.pot
74
75 # Locate files to scan.  We scan the following directories:
76 #  $srcdir
77 #  $srcdir/c-family
78 #  $srcdir/common
79 #  $srcdir/common/config
80 #  $srcdir/common/config/*
81 #  $srcdir/config
82 #  $srcdir/config/*
83 #  all subdirectories of $srcdir containing a config-lang.in file, and
84 #    all subdirectories of those directories.
85 # Within those directories, we examine all .c, .cc, .h, and .def files.
86 # However, any files listed in $srcdir/po/EXCLUDE are not examined.
87 #
88 # Then generate keyword options for xgettext, by scanning for declarations
89 # of functions whose parameter names end in "msgid".
90 #
91 # Finally, generate a source file containing all %e and %n strings from
92 # driver specs, so those can be translated too.
93 #
94 # All in one huge awk script.
95
96 echo "scanning for keywords, %e and %n strings..." >&2
97
98 ( cd $srcdir
99   lang_subdirs=`echo */config-lang.in */*/config-lang.in | sed -e 's|/config-lang\.in||g'`
100   { for dir in "" c-family/ common/ common/config/ common/config/*/ \
101       config/ config/*/ \
102       `find $lang_subdirs -type d -print | fgrep -v .svn | sort | sed -e 's|$|/|'`
103     do  for glob in '*.c' '*.cc' '*.h' '*.def'
104         do  eval echo $dir$glob
105         done
106     done;
107   } | tr ' ' "$nl" | grep -v '\*' |
108   $AWK -v excl=po/EXCLUDES -v posr=$posr -v posrcxx=$posrcxx -v kopt=$kopt -v kopt2=$kopt2 -v emsg=$emsg '
109 function keyword_option(line) {
110     paren_index = index(line, "(")
111     name = substr(line, 1, paren_index - 1)
112     sub(/[^0-9A-Z_a-z]*$/, "", name)
113     sub(/[       ]+PARAMS/, "", name)
114     sub(/[       ]+VPARAMS/, "", name)
115     sub(/.*[^0-9A-Z_a-z]/, "", name)
116
117     args = substr(line, paren_index)
118     sub(/msgid[,\)].*/, "", args)
119     for (n = 1; sub(/^[^,]*,/, "", args); n++) {
120         continue
121     }
122     format=""
123     if (args ~ /g$/)
124         format="gcc-internal-format"
125     else if (args ~ /noc$/)
126         format="no-c-format"
127     else if (args ~ /c$/)
128         format="c-format"
129
130     if (n == 1) { keyword = "--keyword=" name }
131     else {
132        keyword = "--keyword=" name ":" n
133        if (name ~ /_n$/)
134          keyword = keyword "," (n + 1)
135     }
136     if (format) {
137         keyword=keyword "\n--flag=" name ":" n ":" format
138         if (name ~ /_n$/)
139           keyword = keyword "\n--flag=" name ":" (n + 1) ":" format
140     }
141
142     if (! keyword_seen[name]) {
143         if (format == "gcc-internal-format")
144                 print keyword > kopt2
145         else
146                 print keyword > kopt
147         keyword_seen[name] = keyword
148     } else if (keyword_seen[name] != keyword) {
149         printf("%s used incompatibly as both %s and %s\n",
150                name, keyword_seen[name], keyword)
151         exit (1)
152     }
153 }
154
155 function spec_error_string (line) {
156     if (index(line, "%e") != 0 && index(line, "%n") != 0) return
157     while ((percent_index = index(line, "%e")) != 0 || 
158            (percent_index = index(line, "%n")) != 0) {
159         line = substr(line, percent_index + 2)
160
161         bracket_index = index(line, "}")
162         newline_index = index(line, "\\n")
163                 
164         quote_index = index(line, "\"")
165         if (bracket_index == 0 && newline_index == 0) return
166
167         if (bracket_index != 0) {
168           if (quote_index != 0 && bracket_index > quote_index) return
169           msgid = substr(line, 1, bracket_index - 1)
170           line = substr(line, bracket_index + 1)
171         }
172         else if (newline_index != 0) {
173           if (quote_index != 0 && quote_index > newline_index) return
174           msgid = substr(line, 1, newline_index - 1)
175           line = substr(line, newline_index + 1)
176         }
177
178         if (index(msgid, "%") != 0) continue
179
180         if ((newline_index = index(msgid, "\\n")) != 0)
181           msgid = substr(msgid, 1, newline_index - 1)
182         printf("#line %d \"%s\"\n", lineno, file) > emsg
183         printf("_(\"%s\")\n", msgid) > emsg
184     }
185 }
186
187 BEGIN {
188   while ((getline < excl) > 0) {
189     if ($0 ~ /^#/ || $0 ~ /^[   ]*$/)
190       continue
191     excludes[$1] = 1
192   }
193 }
194
195 { if (!($0 in excludes)) {
196     if ($0 ~ /.cc$/) {
197       print > posrcxx
198     } else {
199       print > posr
200     }
201     files[NR] = $0
202   }
203 }
204
205 END {
206     for (f in files) {
207         file = files[f]
208         lineno = 1
209         while (getline < file) {
210             if (/^(#[   ]*define[       ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
211                 keyword_option($0)
212             } else if (/^(#[   ]*define[       ]*)?[A-Za-z_].*(\(|\(.*,)$/) {
213                 name_line = $0
214                 while (getline < file) {
215                   lineno++
216                   if (/msgid[,\)]/){
217                     keyword_option(name_line $0)
218                     break
219                   } else if (/,$/) {
220                       name_line = name_line $0
221                       continue
222                   } else break
223                 }
224             } else if (/%e/ || /%n/) {
225                 spec_error_string($0)
226             }
227             lineno++
228         }
229     }
230     print emsg > posr
231     print "--keyword=__opt_help_text\n--flag=__opt_help_text:1:no-c-format" >> kopt
232 }'
233 ) || exit
234
235 echo "scanning option files..." >&2
236
237 ( cd $srcdir; find . -name '*.opt' -print |
238   $AWK '{
239     file = $1
240     lineno = 1
241     field = 0
242     while (getline < file) {
243         if (/^[ \t]*(;|$)/ || !/^[^ \t]/) {
244             if (field > 2)
245                 printf("__opt_help_text(\"%s\")\n", line)
246             field = 0
247         } else {
248             if ((field == 1) && /MissingArgError/) {
249                 line = $0
250                 sub(".*MissingArgError\\(", "", line)
251                 if (line ~ "^{") {
252                         sub("^{", "", line)
253                         sub("}\\).*", "", line)
254                 } else
255                         sub("\\).*", "", line)
256                 printf("#line %d \"%s\"\n", lineno, file)
257                 printf("error(\"%s\")\n", line)
258             }
259             if ((field == 1) && /UnknownError/) {
260                 line = $0
261                 sub(".*UnknownError\\(", "", line)
262                 if (line ~ "^{") {
263                         sub("^{", "", line)
264                         sub("}\\).*", "", line)
265                 } else
266                         sub("\\).*", "", line)
267                 printf("#line %d \"%s\"\n", lineno, file)
268                 printf("error(\"%s\")\n", line)
269             }
270             if ((field == 1) && /Warn\(/) {
271                 line = $0
272                 sub(".*Warn\\(", "", line)
273                 if (line ~ "^{") {
274                         sub("^{", "", line)
275                         sub("}\\).*", "", line)
276                 } else
277                         sub("\\).*", "", line)
278                 printf("#line %d \"%s\"\n", lineno, file)
279                 printf("warning(0, \"%s\")\n", line)
280             }
281             if (field == 2) {
282                 line = $0
283                 printf("#line %d \"%s\"\n", lineno, file)
284             } else if (field > 2) {
285                 line = line " " $0
286             }
287             field++;
288         }
289         lineno++;
290     }
291     if (field > 2)
292         printf("__opt_help_text(\"%s\")\n", line)
293   }') >> $emsg
294
295 # Run the xgettext commands, with temporary added as a file to scan.
296 echo "running xgettext..." >&2
297 $xgettext --default-domain=$package --directory=$srcdir \
298           --add-comments `cat $kopt` --files-from=$posr \
299           --copyright-holder="Free Software Foundation, Inc." \
300           --msgid-bugs-address="$BUGURL" \
301           --language=c -o $pottmp1
302 if test -s $posrcxx; then
303   $xgettext --default-domain=$package --directory=$srcdir \
304             --add-comments `cat $kopt` --files-from=$posrcxx \
305             --copyright-holder="Free Software Foundation, Inc." \
306             --msgid-bugs-address="$BUGURL" \
307             --language=c++ -o $pottmp2
308 else
309   echo > $pottmp2
310 fi
311 $xgettext --default-domain=$package --directory=$srcdir \
312           --add-comments --keyword= `cat $kopt2` --files-from=$posr \
313           --copyright-holder="Free Software Foundation, Inc." \
314           --msgid-bugs-address="$BUGURL" \
315           --language=GCC-source -o $pottmp3
316 $xgettext --default-domain=$package --directory=$srcdir \
317           --add-comments --keyword= `cat $kopt2` --files-from=$posrcxx \
318           --copyright-holder="Free Software Foundation, Inc." \
319           --msgid-bugs-address="$BUGURL" \
320           --language=GCC-source -o $pottmp4
321 $xgettext --default-domain=$package \
322           --add-comments $pottmp1 $pottmp2 $pottmp3 $pottmp4 \
323           --copyright-holder="Free Software Foundation, Inc." \
324           --msgid-bugs-address="$BUGURL" \
325           --language=PO -o $pottmp
326 # Remove local paths from .pot file.
327 sed "s:$srcdir/::g;s:$pwd/::g;" <$pottmp >po/$package.pot