2 # Wrapper around gettext for programs using the msgid convention.
3 # Copyright 1998, 2001, 2002, 2003, 2009, 2010 Free Software Foundation, Inc.
5 # Written by Paul Eggert <eggert@twinsun.com>.
6 # Revised by Zack Weinberg <zackw@stanford.edu> for no-POTFILES operation.
8 # This file is part of GCC.
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)
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.
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/>.
24 # Always operate in the C locale.
28 export LANG LANGUAGE LC_ALL
30 # Set AWK if environment has not already set it.
33 # The arguments to this wrapper are: the program to execute, the
34 # name of the "package", and the path to the source directory.
37 then echo "usage: $0 <xgettext> <package> <srcdir>"
45 case `$xgettext --version | sed -e 1q | sed -e 's/^\([^0-9]*\)//'` in
46 0.14.[5-9]* | 0.14.[1-9][0-9]* | 0.1[5-9]* | 0.[2-9][0-9]* | [1-9].*) : ;;
47 *) echo "$xgettext is too old. GNU xgettext 0.14.5 is required"
56 # Create temporary directory for scratch files.
62 kopt=$pwd/$T/keyword-options
63 kopt2=$pwd/$T/keyword2-options
64 emsg=$pwd/$T/emsgids.c
65 posr=$pwd/$T/po-sources
66 pottmp1=$pwd/$T/tmp1.pot
67 pottmp2=$pwd/$T/tmp2.pot
68 pottmp=$pwd/$T/tmp.pot
70 # Locate files to scan, and generate the list. All .c, .h, and .def files
71 # in $srcdir are examined, likewise $srcdir/config and $srcdir/config/*
72 # (directories). Also, all subdirectories of $srcdir that contain a
73 # config-lang.in. Exclusions come from $srcdir/po/EXCLUDE.
75 # Then generate keyword options for xgettext, by scanning for declarations
76 # of functions whose parameter names end in "msgid".
78 # Finally, generate a source file containing all %e and %n strings from
79 # driver specs, so those can be translated too.
81 # All in one huge awk script.
83 echo "scanning for keywords, %e and %n strings..." >&2
86 lang_subdirs=`echo */config-lang.in */*/config-lang.in | sed -e 's|config-lang\.in||g'`
87 { for dir in "" c-family/ config/ config/*/ $lang_subdirs
88 do for glob in '*.c' '*.h' '*.def'
89 do eval echo $dir$glob
92 } | tr ' ' "$nl" | grep -v '\*' |
93 $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v kopt2=$kopt2 -v emsg=$emsg '
94 function keyword_option(line) {
95 paren_index = index(line, "(")
96 name = substr(line, 1, paren_index - 1)
97 sub(/[^0-9A-Z_a-z]*$/, "", name)
98 sub(/[ ]+PARAMS/, "", name)
99 sub(/[ ]+VPARAMS/, "", name)
100 sub(/.*[^0-9A-Z_a-z]/, "", name)
102 args = substr(line, paren_index)
103 sub(/msgid[,\)].*/, "", args)
104 for (n = 1; sub(/^[^,]*,/, "", args); n++) {
109 format="gcc-internal-format"
110 else if (args ~ /noc$/)
112 else if (args ~ /c$/)
115 if (n == 1) { keyword = "--keyword=" name }
117 keyword = "--keyword=" name ":" n
119 keyword = keyword "," (n + 1)
122 keyword=keyword "\n--flag=" name ":" n ":" format
124 keyword = keyword "\n--flag=" name ":" (n + 1) ":" format
127 if (! keyword_seen[name]) {
128 if (format == "gcc-internal-format")
129 print keyword > kopt2
132 keyword_seen[name] = keyword
133 } else if (keyword_seen[name] != keyword) {
134 printf("%s used incompatibly as both %s and %s\n",
135 name, keyword_seen[name], keyword)
140 function spec_error_string (line) {
141 if (index(line, "%e") != 0 && index(line, "%n") != 0) return
142 while ((percent_index = index(line, "%e")) != 0 ||
143 (percent_index = index(line, "%n")) != 0) {
144 line = substr(line, percent_index + 2)
146 bracket_index = index(line, "}")
147 newline_index = index(line, "\\n")
149 quote_index = index(line, "\"")
150 if (bracket_index == 0 && newline_index == 0) return
152 if (bracket_index != 0) {
153 if (quote_index != 0 && bracket_index > quote_index) return
154 msgid = substr(line, 1, bracket_index - 1)
155 line = substr(line, bracket_index + 1)
157 else if (newline_index != 0) {
158 if (quote_index != 0 && quote_index > newline_index) return
159 msgid = substr(line, 1, newline_index - 1)
160 line = substr(line, newline_index + 1)
163 if (index(msgid, "%") != 0) continue
165 if ((newline_index = index(msgid, "\\n")) != 0)
166 msgid = substr(msgid, 1, newline_index - 1)
167 printf("#line %d \"%s\"\n", lineno, file) > emsg
168 printf("_(\"%s\")\n", msgid) > emsg
173 while ((getline < excl) > 0) {
174 if ($0 ~ /^#/ || $0 ~ /^[ ]*$/)
180 { if (!($0 in excludes)) {
190 while (getline < file) {
191 if (/^(#[ ]*define[ ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
193 } else if (/^(#[ ]*define[ ]*)?[A-Za-z_].*(\(|\(.*,)$/) {
195 while (getline < file) {
198 keyword_option(name_line $0)
201 name_line = name_line $0
205 } else if (/%e/ || /%n/) {
206 spec_error_string($0)
215 echo "scanning option files..." >&2
217 ( cd $srcdir; find . -name '*.opt' -print |
222 while (getline < file) {
223 if (/^[ \t]*(;|$)/ || !/^[^ \t]/) {
226 if ((field == 1) && /MissingArgError/) {
228 sub(".*MissingArgError\\(", "", line)
231 sub("}\\).*", "", line)
233 sub("\\).*", "", line)
234 printf("#line %d \"%s\"\n", lineno, file)
235 printf("_(\"%s\")\n", line)
237 if ((field == 1) && /Warn/) {
239 sub(".*Warn\\(", "", line)
242 sub("}\\).*", "", line)
244 sub("\\).*", "", line)
245 printf("#line %d \"%s\"\n", lineno, file)
246 printf("_(\"%s\")\n", line)
250 printf("#line %d \"%s\"\n", lineno, file)
251 printf("_(\"%s\")\n", line)
259 # Run the xgettext command, with temporary added as a file to scan.
260 echo "running xgettext..." >&2
261 $xgettext --default-domain=$package --directory=$srcdir \
262 --add-comments `cat $kopt` --files-from=$posr \
263 --copyright-holder="Free Software Foundation, Inc." \
264 --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
265 --language=c -o $pottmp1
266 $xgettext --default-domain=$package --directory=$srcdir \
267 --add-comments --keyword= `cat $kopt2` --files-from=$posr \
268 --copyright-holder="Free Software Foundation, Inc." \
269 --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
270 --language=GCC-source -o $pottmp2
271 $xgettext --default-domain=$package \
272 --add-comments $pottmp1 $pottmp2 \
273 --copyright-holder="Free Software Foundation, Inc." \
274 --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
275 --language=PO -o $pottmp
276 # Remove local paths from .pot file.
277 sed "s:$srcdir/::g;s:$pwd/::g;" <$pottmp >po/$package.pot