blob: dc09e122d36b63092d02540517fb2603475dfc2f [file] [log] [blame]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -05001#!/bin/awk -f
2# scripts/options.awk - library build configuration control
3#
John Bowler92a1d462011-11-07 22:19:30 -06004# last changed in libpng version 1.5.7 - November 11, 2011
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -05005#
Glenn Randers-Pehrson5c5db5a2011-01-21 23:23:34 -06006# Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -05007#
8# This code is released under the libpng license.
9# For conditions of distribution and use, see the disclaimer
10# and license in png.h
11
12# The output of this script is written to the file given by
13# the variable 'out'. The script is run twice, once with
14# an intermediate output file, 'options.tmp' then again on
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050015# that file to produce the final output:
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -050016#
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050017# awk -f scripts/options.awk out=options.tmp scripts/options.dfa 1>&2
18# awk -f scripts/options.awk out=options.dfn options.tmp 1>&2
19#
20# Some options may be specified on the command line:
21#
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050022# deb=1 Causes debugging to be output
23# logunsupported=1 Causes all options to be recorded in the output
24# everything=off Causes all options to be disabled by default
25# everything=on Causes all options to be enabled by default
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050026#
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -050027# If awk fails on your platform, try nawk instead.
28#
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050029# These options may also be specified in the original input file (and
30# are copied to the preprocessed file).
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -050031
32BEGIN{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050033 out="/dev/null" # intermediate, preprocessed, file
34 pre=-1 # preprocess (first line)
John Bowler92a1d462011-11-07 22:19:30 -060035 version="" # version information
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050036 err=0 # in-line exit sets this
37 start="PNG_DEFN_MAGIC-" # Arbitrary start
38 end="-PNG_DEFN_END" # Arbitrary end
John Bowler1d8b7552011-11-03 18:19:53 -050039 ct="PNG_JOIN" # Join two tokens
40 cx= "/" ct "*" # Open C comment for output file
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050041 comment=start cx # Comment start
42 cend="*/" end # Comment end
John Bowler1d8b7552011-11-03 18:19:53 -050043 def=start "#define PNG_" ct # Arbitrary define
44 sup=ct "_SUPPORTED" end # end supported option
45 und=comment "#undef PNG_" ct # Unsupported option
46 une=ct "_SUPPORTED" cend # end unsupported option
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050047 error=start "ERROR:" # error message
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050048
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050049 # Variables
50 deb=0 # debug - set on command line
51 everything="" # do not override defaults
52 logunsupported=0 # write unsupported options too
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -050053
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050054 # Precreate arrays
55 option[""] = "" # list of all options: default enabled/disabled
56 done[""] = 1 # marks option as having been output
57 requires[""] = "" # requires by option
58 iffs[""] = "" # if by option
59 enabledby[""] = "" # options that enable it by option
60 setting[""] = "" # requires by setting
61 defaults[""] = "" # used for a defaulted value
62 doneset[""] = 1 # marks setting as having been output
63 r[""] = "" # Temporary array
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -050064
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050065 # For decorating the output file
66 protect = ""
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -050067}
68
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050069# The output file must be specified before any input:
70out == "/dev/null" {
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050071 print "out=output.file must be given on the command line"
72 err = 1
73 exit 1
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050074}
75
76# The very first line indicates whether we are reading pre-processed
77# input or not, this must come *first* because 'PREPROCESSED' needs
78# to be the very first line in the temporary file.
79pre == -1{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050080 if ($0 == "PREPROCESSED") {
81 pre = 0
82 next
83 } else {
84 pre = 1
85 print "PREPROCESSED" >out
86 # And fall through to continue processing
87 }
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050088}
89
John Bowler92a1d462011-11-07 22:19:30 -060090# While pre-processing if the filename is ANNOUNCE then just look for a line
91# which gives the version information for this build.
92pre && FILENAME ~ /ANNOUNCE$/ && version == "" && $1 == "Libpng"{
93 version=$0
94 print "version =", version >out
95}
96
97pre && FILENAME ~ /ANNOUNCE$/{
98 next
99}
100
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500101# variable=value
102# Sets the given variable to the given value (the syntax is fairly
103# free form, except for deb (you are expected to understand how to
104# set the debug variable...)
105#
106# This happens before the check on 'pre' below skips most of the
107# rest of the actions, so the variable settings happen during
108# preprocessing but are recorded in the END action too. This
109# allows them to be set on the command line too.
John Bowler92a1d462011-11-07 22:19:30 -0600110$0 ~ /^[ ]*version[ ]*=/{
111 sub(/^[ ]*version[ ]*=[ ]*/, "")
112 version = $0
113 next
114}
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500115$0 ~ /^[ ]*everything[ =]*off[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500116 everything = "off"
117 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500118}
119$0 ~ /^[ ]*everything[ =]*on[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500120 everything = "on"
121 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500122}
123$0 ~ /^[ ]*logunsupported[ =]*0[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500124 logunsupported = 0
125 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500126}
127$0 ~ /^[ ]*logunsupported[ =]*1[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500128 logunsupported = 1
129 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500130}
131$1 == "deb" && $2 == "=" && NF == 3{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500132 deb = $3
133 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500134}
135
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500136# Preprocessing - this just copies the input file with lines
137# that need preprocessing (just chunk at present) expanded
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500138# The bare "pre" instead of "pre != 0" crashes under Sunos awk
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500139pre && $1 != "chunk"{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500140 print >out
141 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500142}
143
144# The first characters of the line determine how it is processed,
145# leading spaces are ignored. In general tokens that are not
146# keywords are the names of options. An option 'name' is
147# controlled by the definition of the corresponding macros:
148#
149# PNG_name_SUPPORTED The option is turned on
150# PNG_NO_name
151# PNG_NO_name_SUPPORTED If the first macro is not defined
152# either of these will turn the option off
153#
154# If none of these macros are defined the option is turned on, unless
155# the keyword 'off' is given in a line relating to the option. The
156# keyword 'on' can also be given, but it will be ignored (since it is
157# the default.)
158#
159# In the syntax below a 'name' is indicated by "NAME", other macro
160# values are indicated by "MACRO", as with "NAME" the leading "PNG_"
161# is omitted, but in this case the "NO_" prefix and the "_SUPPORTED"
162# suffix are never used.
163#
164# Each line is introduced by a keyword - the first non-space characters
165# on the line. A line starting with a '#' is a comment - it is totally
166# ignored. Keywords are as follows, a NAME, is simply a macro name
167# without the leading PNG_, PNG_NO_ or the trailing _SUPPORTED.
168
169$1 ~ /^#/ || $0 ~ /^[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500170 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500171}
172
173# com <comment>
174# The whole line is placed in the output file as a comment with
175# the preceding 'com' removed
176$1 == "com"{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500177 if (NF > 1) {
178 # sub(/^[ ]*com[ ]*/, "")
179 $1 = ""
180 print comment, $0, cend >out
181 } else
182 print start end >out
183 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500184}
185
John Bowler92a1d462011-11-07 22:19:30 -0600186# version
187# Inserts a version comment
188$1 == "version" && NF == 1{
189 if (version == "") {
190 print "ERROR: no version string set"
191 err = 1 # prevent END{} running
192 exit 1
193 }
194
195 print comment, version, cend >out
196 next
197}
198
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500199# file output input protect
200# Informational: the official name of the input file (without
201# make generated local directories), the official name of the
202# output file and, if required, a name to use in a protection
203# macro for the contents.
204$1 == "file" && NF >= 2{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500205 print comment, $2, cend >out
206 print comment, "Machine generated file: DO NOT EDIT", cend >out
207 if (NF >= 3)
208 print comment, "Derived from:", $3, cend >out
209 protect = $4
210 if (protect != "") {
211 print start "#ifndef", protect end >out
212 print start "#define", protect end >out
213 }
214 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500215}
216
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500217# option NAME ( (requires|enables|if) NAME* | on | off | disabled )*
218# Declares an option 'NAME' and describes its default setting (disabled)
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500219# and its relationship to other options. The option is disabled
220# unless *all* the options listed after 'requires' are set and at
221# least one of the options listed after 'if' is set. If the
222# option is set then it turns on all the options listed after 'enables'.
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500223#
224# Note that "enables" takes priority over the required/if/disabled/off
225# setting of the target option.
226#
227# The definition file may list an option as 'disabled': off by default,
228# otherwise the option is enabled: on by default. A later (and it must
229# be later) entry may turn an option on or off explicitly.
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500230
231$1 == "option" && NF >= 2{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500232 onoff = option[$2] # records current (and the default is "", enabled)
233 key = ""
234 for (i=3; i<=NF; ++i) {
235 if ($(i) == "on" || $(i) == "off" || $(i) == "disabled") {
236 key = ""
237 if (onoff != $(i)) {
238 # on or off can zap disabled or enabled:
239 if (onoff == "" || (onoff == "disabled" || onoff == "enabled") && ($(i) == "on" || $(i) == "off")) {
240 # It's easy to mis-spell the option when turning it
241 # on or off, so warn about it here:
242 if (onoff == "" && ($(i) == "on" || $(i) == "off")) {
243 print $2 ": ERROR: turning unrecognized option", $(i)
244 # For the moment error out - it is safer
245 err = 1 # prevent END{} running
246 exit 1
247 }
248 onoff = $(i)
249 } else {
250 # Print a message, otherwise the error
251 # below is incomprehensible
252 print $2 ": currently", onoff ": attempt to turn", $(i)
253 break
254 }
255 }
256 } else if ($(i) == "requires" || $(i) == "if" || $(i) == "enables") {
257 key = $(i)
258 } else if (key == "requires") {
259 requires[$2] = requires[$2] " " $(i)
260 } else if (key == "if") {
261 iffs[$2] = iffs[$2] " " $(i)
262 } else if (key == "enables") {
263 enabledby[$(i)] = enabledby[$(i)] " " $2
264 } else
265 break # bad line format
266 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500267
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500268 if (i > NF) {
269 # Set the option, defaulting to 'enabled'
270 if (onoff == "") onoff = "enabled"
271 option[$2] = onoff
272 next
273 }
274 # Else fall through to the error handler
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500275}
276
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500277# chunk NAME [requires OPT] [on|off|disabled]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500278# Expands to the 'option' settings appropriate to the reading and
279# writing of an ancilliary PNG chunk 'NAME':
280#
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500281# option READ_NAME requires READ_ANCILLARY_CHUNKS [READ_OPT]
282# option READ_NAME enables NAME
283# [option READ_NAME off]
284# option WRITE_NAME requires WRITE_ANCILLARY_CHUNKS [WRITE_OPT]
285# option WRITE_NAME enables NAME
286# [option WRITE_NAME off]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500287
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500288pre != 0 && $1 == "chunk" && NF >= 2{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500289 # 'chunk' is handled on the first pass by writing appropriate
290 # 'option' lines into the intermediate file.
291 onoff = ""
292 reqread = ""
293 reqwrite = ""
294 i = 3 # indicates format error
295 if (NF > 2) {
296 # read the keywords/additional OPTS
297 req = 0
298 for (i=3; i<=NF; ++i) {
299 if ($(i) == "on" || $(i) == "off" || $(i) == "disabled") {
300 if (onoff != $(i)) {
301 if (onoff == "")
302 onoff = $(i)
303 else
304 break # on/off conflict
305 }
306 } else if ($(i) == "requires")
307 req = 1
308 else if (req != 1)
309 break # bad line: handled below
310 else {
311 reqread = reqread " READ_" $(i)
312 reqwrite = reqwrite " WRITE_" $(i)
313 }
314 }
315 }
316
317 if (i > NF) {
318 # Output new 'option' lines to the intermediate file (out)
319 print "option READ_" $2, "requires READ_ANCILLARY_CHUNKS" reqread, "enables", $2, onoff >out
320 print "option WRITE_" $2, "requires WRITE_ANCILLARY_CHUNKS" reqwrite, "enables", $2, onoff >out
321 next
322 }
323 # Else hit the error handler below - bad line format!
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500324}
325
326# setting MACRO ( requires MACRO* )* [ default VALUE ]
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500327# Behaves in a similar way to 'option' without looking for NO_ or
328# _SUPPORTED; the macro is enabled if it is defined so long as all
329# the 'requires' macros are also defined. The definitions may be
330# empty, an error will be issued if the 'requires' macros are
331# *not* defined. If given the 'default' value is used if the
332# macro is not defined. The default value will be re-tokenised.
333# (BTW: this is somewhat restrictive, it mainly exists for the
334# support of non-standard configurations and numeric parameters,
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -0500335# see the uses in scripts/options.dat
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500336
337$1 == "setting" && (NF == 2 || NF >= 3 && ($3 == "requires" || $3 == "default")){
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500338 reqs = ""
339 deflt = ""
340 isdef = 0
341 key = ""
342 for (i=3; i<=NF; ++i)
343 if ($(i) == "requires" || $(i) == "default") {
344 key = $(i)
345 if (key == "default") isdef = 1
346 } else if (key == "requires")
347 reqs = reqs " " $(i)
348 else if (key == "default")
349 deflt = deflt " " $(i)
350 else
351 break # Format error, handled below
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500352
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500353 setting[$2] = reqs
354 # NOTE: this overwrites a previous value silently
355 if (isdef && deflt == "")
356 deflt = " " # as a flag to force output
357 defaults[$2] = deflt
358 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500359}
360
361# The order of the dependency lines (option, chunk, setting) is irrelevant
362# - the 'enables', 'requires' and 'if' settings will be used to determine
Glenn Randers-Pehrson98b4f002010-04-16 22:30:26 -0500363# the correct order in the output and the final values in pnglibconf.h are
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500364# not order dependent. 'requires' and 'if' entries take precedence over
365# 'enables' from other options; if an option requires another option it
366# won't be set regardless of any options that enable it unless the other
367# option is also enabled.
368#
369# Similarly 'enables' trumps a NO_ definition in CFLAGS or pngusr.h
370#
371# For simplicity cycles in the definitions are regarded as errors,
372# even if they are not ambiguous.
373# A given NAME can be specified in as many 'option' lines as required, the
374# definitions are additive.
375
376# For backwards compatibility equivalent macros may be listed thus:
377#
378# = [NO_]NAME MACRO
379# Makes -DMACRO equivalent to -DPNG_NO_NAME or -DPNG_NAME_SUPPORTED
380# as appropriate.
381#
382# The definition is injected into the C compiler input when encountered
383# in the second pass (so all these definitions appear *after* the @
384# lines!)
385#
386# 'NAME' is as above, but 'MACRO' is the full text of the equivalent
387# old, deprecated, macro.
388
389$1 == "=" && NF == 3{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500390 print "#ifdef PNG_" $3 >out
391 if ($2 ~ /^NO_/)
392 print "# define PNG_" $2 >out
393 else
394 print "# define PNG_" $2 "_SUPPORTED" >out
395 print "#endif" >out
396 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500397}
398
399# Lines may be injected into the C compiler input by preceding them
400# with an "@" character. The line is copied with just the leading
401# @ removed.
402
403$1 ~ /^@/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500404 # sub(/^[ ]*@/, "")
405 $1 = substr($1, 2)
406 print >out
407 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500408}
409
410# Check for unreognized lines, because of the preprocessing chunk
411# format errors will be detected on the first pass independent of
412# any other format errors.
413{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500414 print "options.awk: bad line (" NR "):", $0
415 err = 1 # prevent END{} running
416 exit 1
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500417}
418
419# For checking purposes names that start with "ok_" or "fail_" are
Glenn Randers-Pehrson98b4f002010-04-16 22:30:26 -0500420# not output to pnglibconf.h and must be either enabled or disabled
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500421# respectively for the build to succeed. This allows interdependencies
422# between options of the form "at least one of" or "at most one of"
423# to be checked. For example:
424#
425# option FLOATING_POINT enables ok_math
426# option FIXED_POINT enables ok_math
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500427# This ensures that at least one of FLOATING_POINT and FIXED_POINT
428# must be set for the build to succeed.
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500429#
430# option fail_math requires FLOATING_POINT FIXED_POINT
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500431# This means the build will fail if *both* FLOATING_POINT and
432# FIXED_POINT are set (this is an example; in fact both are allowed.)
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500433#
434# If all these options were given the build would require exactly one
435# of the names to be enabled.
436
437END{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500438 # END{} gets run on an exit (a traditional awk feature)
439 if (err) exit 1
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500440
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500441 if (pre) {
442 # Record the final value of the variables
443 print "deb =", deb >out
444 if (everything != "") {
445 print "everything =", everything >out
446 }
447 print "logunsupported =", logunsupported >out
448 exit 0
449 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500450
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500451 # Do the 'setting' values first, the algorithm the standard
452 # tree walk (O(1)) done in an O(2) while/for loop; interations
453 # settings x depth, outputing the deepest required macros
454 # first.
455 print "" >out
456 print "/* SETTINGS */" >out
457 print comment, "settings", cend >out
458 finished = 0
459 while (!finished) {
460 finished = 1
461 movement = 0 # done nothing
462 for (i in setting) if (!doneset[i]) {
463 nreqs = split(setting[i], r)
464 if (nreqs > 0) {
465 for (j=1; j<=nreqs; ++j) if (!doneset[r[j]]) {
466 break
467 }
468 if (j<=nreqs) {
469 finished = 0
470 continue # try a different setting
471 }
472 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500473
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500474 # All the requirements have been processed, output
475 # this setting.
476 if (deb) print "setting", i
477 print "" >out
478 print "/* setting: ", i >out
479 print " * requires:" setting[i] >out
480 print " * default: ", defaults[i], "*/" >out
481 if (defaults[i] == "") { # no default, only check if defined
482 print "#ifdef PNG_" i >out
483 }
484 for (j=1; j<=nreqs; ++j) {
485 print "# ifndef PNG_" r[j] >out
486 print error, i, "requires", r[j] end >out
487 print "# endif" >out
488 }
489 if (defaults[i] != "") { # default handling
490 print "#ifdef PNG_" i >out
491 }
492 print def i, "PNG_" i end >out
493 if (defaults[i] != "") {
494 print "#else /*default*/" >out
495 # And add the default definition for the benefit
496 # of later settings an options test:
497 print "# define PNG_" i defaults[i] >out
498 print def i defaults[i] end >out
499 }
500 print "#endif" >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500501
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500502 doneset[i] = 1
503 ++movement
504 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500505
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500506 if (!finished && !movement) {
507 print "setting: loop or missing setting in 'requires', cannot process:"
508 for (i in setting) if (!doneset[i]) {
509 print " setting", i, "requires" setting[i]
510 }
511 exit 1
512 }
513 }
514 print comment, "end of settings", cend >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500515
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500516 # Now do the options - somewhat more complex. The dependency
517 # tree is thus:
518 #
519 # name > name
520 # name requires name
521 # name if name
522 # name enabledby name
523 #
524 # First build a list 'tree' by option of all the things on which
525 # it depends.
526 print "" >out
527 print "/* OPTIONS */" >out
528 print comment, "options", cend >out
529 for (opt in enabledby) tree[opt] = 1 # may not be explicit options
530 for (opt in option) if (opt != "") {
531 o = option[opt]
532 # option should always be one of the following values
533 if (o != "on" && o != "off" && o != "disabled" && o != "enabled") {
534 print "internal option error (" o ")"
535 exit 1
536 }
537 tree[opt] = "" # so unlisted options marked
538 }
539 for (opt in tree) if (opt != "") {
540 if (tree[opt] == 1) {
541 tree[opt] = ""
542 if (option[opt] != "") {
543 print "internal error (1)"
544 exit 1
545 }
546 # Macros only listed in 'enables' remain off unless
547 # one of the enabling macros is on.
548 option[opt] = "disabled"
549 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500550
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500551 split("", list) # clear 'list'
552 # Now add every requires, iffs or enabledby entry to 'list'
553 # so that we can add a unique list of requirements to tree[i]
554 split(requires[opt] iffs[opt] enabledby[opt], r)
555 for (i in r) list[r[i]] = 1
556 for (i in list) tree[opt] = tree[opt] " " i
557 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500558
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500559 # print the tree for extreme debugging
560 if (deb > 2) for (i in tree) if (i != "") print i, "depends-on" tree[i]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500561
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500562 # Ok, now check all options marked explicitly 'on' or 'off':
563 #
564 # If an option[opt] is 'on' then turn on all requires[opt]
565 # If an option[opt] is 'off' then turn off all enabledby[opt]
566 #
567 # Error out if we have to turn 'on' an 'off' option or vice versa.
568 npending = 0
569 for (opt in option) if (opt != "") {
570 if (option[opt] == "on" || option[opt] == "off") {
571 pending[++npending] = opt
572 }
573 }
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500574
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500575 err = 0 # set on error
576 while (npending > 0) {
577 opt = pending[npending--]
578 if (option[opt] == "on") {
579 nreqs = split(requires[opt], r)
580 for (j=1; j<=nreqs; ++j) {
581 if (option[r[j]] == "off") {
582 print "option", opt, "turned on, but requirement", r[j], "is turned off"
583 err = 1
584 } else if (option[r[j]] != "on") {
585 option[r[j]] = "on"
586 pending[++npending] = r[j]
587 }
588 }
589 } else {
590 if (option[opt] != "off") {
591 print "internal error (2)"
592 exit 1
593 }
594 nreqs = split(enabledby[opt], r)
595 for (j=1; j<=nreqs; ++j) {
596 if (option[r[j]] == "on") {
597 print "option", opt, "turned off, but enabled by", r[j], "which is turned on"
598 err = 1
599 } else if (option[r[j]] != "off") {
600 option[r[j]] = "off"
601 pending[++npending] = r[j]
602 }
603 }
604 }
605 }
606 if (err) exit 1
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500607
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500608 # option[i] is now the complete list of all the tokens we may
609 # need to output, go through it as above, depth first.
610 finished = 0
611 while (!finished) {
612 finished = 1
613 movement = 0 # done nothing
614 for (i in option) if (!done[i]) {
615 nreqs = split(tree[i], r)
616 if (nreqs > 0) {
617 for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
618 break
619 }
620 if (j<=nreqs) {
621 finished = 0
622 continue # next option
623 }
624 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500625
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500626 # All the requirements have been processed, output
627 # this option. An option is _SUPPORTED if:
628 #
629 # all 'requires' are _SUPPORTED AND
630 # at least one of the 'if' options are _SUPPORTED AND
631 # EITHER:
632 # The name is _SUPPORTED (on the command line)
633 # OR:
634 # an 'enabledby' is _SUPPORTED
635 # OR:
636 # NO_name is not defined AND
637 # the option is not disabled; an option is disabled if:
638 # option == off
639 # option == disabled && everything != on
640 # option == "" && everything == off
641 if (deb) print "option", i
642 print "" >out
643 print "/* option:", i, option[i] >out
644 print " * requires: " requires[i] >out
645 print " * if: " iffs[i] >out
646 print " * enabled-by:" enabledby[i], "*/" >out
647 print "#undef PNG_on" >out
648 print "#define PNG_on 1" >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500649
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500650 # requires
651 nreqs = split(requires[i], r)
652 for (j=1; j<=nreqs; ++j) {
653 print "#ifndef PNG_" r[j] "_SUPPORTED" >out
654 print "# undef PNG_on /*!" r[j] "*/" >out
655 # this error appears in the final output if something
656 # was switched 'on' but the processing above to force
657 # the requires did not work
658 if (option[i] == "on") {
659 print error, i, "requires", r[j] end >out
660 }
661 print "#endif" >out
662 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500663
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500664 # if
665 nreqs = split(iffs[i], r)
666 print "#undef PNG_no_if" >out
667 if (nreqs > 0) {
668 print "/* if" iffs[i], "*/" >out
669 print "#define PNG_no_if 1" >out
670 for (j=1; j<=nreqs; ++j) {
671 print "#ifdef PNG_" r[j] "_SUPPORTED" >out
672 print "# undef PNG_no_if /*" r[j] "*/" >out
673 print "#endif" >out
674 }
675 print "#ifdef PNG_no_if /*missing if*/" >out
676 print "# undef PNG_on" >out
677 # There is no checking above for this, because we
678 # don't know which 'if' to choose, so whine about
679 # it here:
680 if (option[i] == "on") {
681 print error, i, "needs one of:", iffs[i] end >out
682 }
683 print "#endif" >out
684 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500685
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500686 print "#ifdef PNG_on /*requires, if*/" >out
687 # enables
688 print "# undef PNG_not_enabled" >out
689 print "# define PNG_not_enabled 1" >out
690 print " /* enabled by" enabledby[i], "*/" >out
691 nreqs = split(enabledby[i], r)
692 for (j=1; j<=nreqs; ++j) {
693 print "#ifdef PNG_" r[j] "_SUPPORTED" >out
694 print "# undef PNG_not_enabled /*" r[j] "*/" >out
695 # Oops, probably not intended (should be factored
696 # out by the checks above).
697 if (option[i] == "off") {
698 print error, i, "enabled by:", r[j] end >out
699 }
700 print "#endif" >out
701 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500702
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500703 print "# ifndef PNG_" i "_SUPPORTED /*!command line*/" >out
704 print "# ifdef PNG_not_enabled /*!enabled*/" >out
705 if (option[i] == "off" || option[i] == "disabled" && everything != "on" || option[i] == "enabled" && everything == "off") {
706 print "# undef PNG_on /*default off*/" >out
707 } else {
708 print "# ifdef PNG_NO_" i >out
709 print "# undef PNG_on /*turned off*/" >out
710 print "# endif" >out
711 print "# ifdef PNG_NO_" i "_SUPPORTED" >out
712 print "# undef PNG_on /*turned off*/" >out
713 print "# endif" >out
714 }
715 print "# endif /*!enabled*/" >out
716 print "# ifdef PNG_on" >out
717 # The _SUPPORTED macro must be defined so that dependent
718 # options output later work.
719 print "# define PNG_" i "_SUPPORTED" >out
720 print "# endif" >out
721 print "# endif /*!command line*/" >out
722 # If PNG_on is still set the option should be defined in
723 # pnglibconf.h
724 print "# ifdef PNG_on" >out
725 if (i ~ /^fail_/) {
726 print error, i, "is on: enabled by:" iffs[i] enabledby[i] ", requires" requires[i] end >out
727 } else if (i !~ /^ok_/) {
728 print def i sup >out
729 }
730 print "# endif /* definition */" >out
731 print "#endif /*requires, if*/" >out
732 if (logunsupported || i ~ /^ok_/) {
733 print "#ifndef PNG_on" >out
734 if (logunsupported) {
735 print und i une >out
736 }
737 if (i ~ /^ok_/) {
738 print error, i, "not enabled: requires:" requires[i] ", enabled by:" iffs[i] enabledby[i] end >out
739 }
740 print "#endif" >out
741 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500742
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500743 done[i] = 1
744 ++movement
745 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500746
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500747 if (!finished && !movement) {
748 print "option: loop or missing option in dependency tree, cannot process:"
749 for (i in option) if (!done[i]) {
750 print " option", i, "depends on" tree[i], "needs:"
751 nreqs = split(tree[i], r)
752 if (nreqs > 0) for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
753 print " " r[j]
754 }
755 }
756 exit 1
757 }
758 }
759 print comment, "end of options", cend >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500760
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500761 # Regular end - everything looks ok
762 if (protect != "") {
763 print start "#endif", cx, protect, "*/" end >out
764 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500765}