blob: f5d7729f7b12293c05ef7f93566d5690f4d14ba3 [file] [log] [blame]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -05001#!/bin/awk -f
2# scripts/options.awk - library build configuration control
3#
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06004# last changed in libpng version 1.5.7 - December 15, 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 Bowlerd4973832011-11-08 19:34:54 -060035 version="libpng version unknown" # version information
36 version_file="" # where to find the version
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050037 err=0 # in-line exit sets this
38 start="PNG_DEFN_MAGIC-" # Arbitrary start
39 end="-PNG_DEFN_END" # Arbitrary end
John Bowler1d8b7552011-11-03 18:19:53 -050040 ct="PNG_JOIN" # Join two tokens
41 cx= "/" ct "*" # Open C comment for output file
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050042 comment=start cx # Comment start
43 cend="*/" end # Comment end
John Bowler1d8b7552011-11-03 18:19:53 -050044 def=start "#define PNG_" ct # Arbitrary define
45 sup=ct "_SUPPORTED" end # end supported option
46 und=comment "#undef PNG_" ct # Unsupported option
47 une=ct "_SUPPORTED" cend # end unsupported option
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050048 error=start "ERROR:" # error message
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050049
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050050 # Variables
51 deb=0 # debug - set on command line
52 everything="" # do not override defaults
53 logunsupported=0 # write unsupported options too
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -050054
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050055 # Precreate arrays
56 option[""] = "" # list of all options: default enabled/disabled
57 done[""] = 1 # marks option as having been output
58 requires[""] = "" # requires by option
59 iffs[""] = "" # if by option
60 enabledby[""] = "" # options that enable it by option
61 setting[""] = "" # requires by setting
62 defaults[""] = "" # used for a defaulted value
63 doneset[""] = 1 # marks setting as having been output
64 r[""] = "" # Temporary array
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -050065
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050066 # For decorating the output file
67 protect = ""
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -050068}
69
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050070# The output file must be specified before any input:
71out == "/dev/null" {
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050072 print "out=output.file must be given on the command line"
73 err = 1
74 exit 1
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050075}
76
77# The very first line indicates whether we are reading pre-processed
78# input or not, this must come *first* because 'PREPROCESSED' needs
79# to be the very first line in the temporary file.
80pre == -1{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -050081 if ($0 == "PREPROCESSED") {
82 pre = 0
83 next
84 } else {
85 pre = 1
86 print "PREPROCESSED" >out
87 # And fall through to continue processing
88 }
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -050089}
90
John Bowlerd4973832011-11-08 19:34:54 -060091# While pre-processing if version is set to "search" look for a version string
92# in the following file.
93pre && version == "search" && version_file == ""{
94 version_file = FILENAME
John Bowler92a1d462011-11-07 22:19:30 -060095}
96
John Bowlerd4973832011-11-08 19:34:54 -060097pre && version == "search" && version_file != FILENAME{
98 print "version string not found in", version_file
99 err = 1
100 exit 1
101}
102
103pre && version == "search" && $0 ~ /^ \* libpng version/{
104 version = substr($0, 4)
105 print "version =", version >out
106 next
107}
108
109pre && FILENAME == version_file{
John Bowler92a1d462011-11-07 22:19:30 -0600110 next
111}
112
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500113# variable=value
114# Sets the given variable to the given value (the syntax is fairly
115# free form, except for deb (you are expected to understand how to
116# set the debug variable...)
117#
118# This happens before the check on 'pre' below skips most of the
119# rest of the actions, so the variable settings happen during
120# preprocessing but are recorded in the END action too. This
121# allows them to be set on the command line too.
John Bowler92a1d462011-11-07 22:19:30 -0600122$0 ~ /^[ ]*version[ ]*=/{
123 sub(/^[ ]*version[ ]*=[ ]*/, "")
124 version = $0
125 next
126}
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500127$0 ~ /^[ ]*everything[ =]*off[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500128 everything = "off"
129 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500130}
131$0 ~ /^[ ]*everything[ =]*on[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500132 everything = "on"
133 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500134}
135$0 ~ /^[ ]*logunsupported[ =]*0[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500136 logunsupported = 0
137 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500138}
139$0 ~ /^[ ]*logunsupported[ =]*1[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500140 logunsupported = 1
141 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500142}
143$1 == "deb" && $2 == "=" && NF == 3{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500144 deb = $3
145 next
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500146}
147
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500148# Preprocessing - this just copies the input file with lines
149# that need preprocessing (just chunk at present) expanded
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500150# The bare "pre" instead of "pre != 0" crashes under Sunos awk
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500151pre && $1 != "chunk"{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500152 print >out
153 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500154}
155
156# The first characters of the line determine how it is processed,
157# leading spaces are ignored. In general tokens that are not
158# keywords are the names of options. An option 'name' is
159# controlled by the definition of the corresponding macros:
160#
161# PNG_name_SUPPORTED The option is turned on
162# PNG_NO_name
163# PNG_NO_name_SUPPORTED If the first macro is not defined
164# either of these will turn the option off
165#
166# If none of these macros are defined the option is turned on, unless
167# the keyword 'off' is given in a line relating to the option. The
168# keyword 'on' can also be given, but it will be ignored (since it is
169# the default.)
170#
171# In the syntax below a 'name' is indicated by "NAME", other macro
172# values are indicated by "MACRO", as with "NAME" the leading "PNG_"
173# is omitted, but in this case the "NO_" prefix and the "_SUPPORTED"
174# suffix are never used.
175#
176# Each line is introduced by a keyword - the first non-space characters
177# on the line. A line starting with a '#' is a comment - it is totally
178# ignored. Keywords are as follows, a NAME, is simply a macro name
179# without the leading PNG_, PNG_NO_ or the trailing _SUPPORTED.
180
181$1 ~ /^#/ || $0 ~ /^[ ]*$/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500182 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500183}
184
185# com <comment>
186# The whole line is placed in the output file as a comment with
187# the preceding 'com' removed
188$1 == "com"{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500189 if (NF > 1) {
190 # sub(/^[ ]*com[ ]*/, "")
191 $1 = ""
192 print comment, $0, cend >out
193 } else
194 print start end >out
195 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500196}
197
John Bowler92a1d462011-11-07 22:19:30 -0600198# version
199# Inserts a version comment
200$1 == "version" && NF == 1{
201 if (version == "") {
202 print "ERROR: no version string set"
203 err = 1 # prevent END{} running
204 exit 1
205 }
206
207 print comment, version, cend >out
208 next
209}
210
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500211# file output input protect
212# Informational: the official name of the input file (without
213# make generated local directories), the official name of the
214# output file and, if required, a name to use in a protection
215# macro for the contents.
216$1 == "file" && NF >= 2{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500217 print comment, $2, cend >out
218 print comment, "Machine generated file: DO NOT EDIT", cend >out
219 if (NF >= 3)
220 print comment, "Derived from:", $3, cend >out
221 protect = $4
222 if (protect != "") {
223 print start "#ifndef", protect end >out
224 print start "#define", protect end >out
225 }
226 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500227}
228
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500229# option NAME ( (requires|enables|if) NAME* | on | off | disabled )*
230# Declares an option 'NAME' and describes its default setting (disabled)
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500231# and its relationship to other options. The option is disabled
232# unless *all* the options listed after 'requires' are set and at
233# least one of the options listed after 'if' is set. If the
234# option is set then it turns on all the options listed after 'enables'.
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500235#
236# Note that "enables" takes priority over the required/if/disabled/off
237# setting of the target option.
238#
239# The definition file may list an option as 'disabled': off by default,
240# otherwise the option is enabled: on by default. A later (and it must
241# be later) entry may turn an option on or off explicitly.
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500242
243$1 == "option" && NF >= 2{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500244 onoff = option[$2] # records current (and the default is "", enabled)
245 key = ""
246 for (i=3; i<=NF; ++i) {
247 if ($(i) == "on" || $(i) == "off" || $(i) == "disabled") {
248 key = ""
249 if (onoff != $(i)) {
250 # on or off can zap disabled or enabled:
251 if (onoff == "" || (onoff == "disabled" || onoff == "enabled") && ($(i) == "on" || $(i) == "off")) {
252 # It's easy to mis-spell the option when turning it
253 # on or off, so warn about it here:
254 if (onoff == "" && ($(i) == "on" || $(i) == "off")) {
255 print $2 ": ERROR: turning unrecognized option", $(i)
256 # For the moment error out - it is safer
257 err = 1 # prevent END{} running
258 exit 1
259 }
260 onoff = $(i)
261 } else {
262 # Print a message, otherwise the error
263 # below is incomprehensible
264 print $2 ": currently", onoff ": attempt to turn", $(i)
265 break
266 }
267 }
268 } else if ($(i) == "requires" || $(i) == "if" || $(i) == "enables") {
269 key = $(i)
270 } else if (key == "requires") {
271 requires[$2] = requires[$2] " " $(i)
272 } else if (key == "if") {
273 iffs[$2] = iffs[$2] " " $(i)
274 } else if (key == "enables") {
275 enabledby[$(i)] = enabledby[$(i)] " " $2
276 } else
277 break # bad line format
278 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500279
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500280 if (i > NF) {
281 # Set the option, defaulting to 'enabled'
282 if (onoff == "") onoff = "enabled"
283 option[$2] = onoff
284 next
285 }
286 # Else fall through to the error handler
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500287}
288
John Bowlerb11b31a2012-03-21 07:55:46 -0500289# chunk NAME [requires OPT] [enables LIST] [on|off|disabled]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500290# Expands to the 'option' settings appropriate to the reading and
291# writing of an ancilliary PNG chunk 'NAME':
292#
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500293# option READ_NAME requires READ_ANCILLARY_CHUNKS [READ_OPT]
John Bowlerb11b31a2012-03-21 07:55:46 -0500294# option READ_NAME enables NAME LIST
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500295# [option READ_NAME off]
296# option WRITE_NAME requires WRITE_ANCILLARY_CHUNKS [WRITE_OPT]
John Bowlerb11b31a2012-03-21 07:55:46 -0500297# option WRITE_NAME enables NAME LIST
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500298# [option WRITE_NAME off]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500299
Glenn Randers-Pehrson5feb87c2010-06-21 12:28:05 -0500300pre != 0 && $1 == "chunk" && NF >= 2{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500301 # 'chunk' is handled on the first pass by writing appropriate
302 # 'option' lines into the intermediate file.
303 onoff = ""
304 reqread = ""
305 reqwrite = ""
John Bowlerb11b31a2012-03-21 07:55:46 -0500306 enables = ""
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500307 i = 3 # indicates format error
308 if (NF > 2) {
309 # read the keywords/additional OPTS
310 req = 0
311 for (i=3; i<=NF; ++i) {
312 if ($(i) == "on" || $(i) == "off" || $(i) == "disabled") {
313 if (onoff != $(i)) {
314 if (onoff == "")
315 onoff = $(i)
316 else
317 break # on/off conflict
318 }
John Bowlerb11b31a2012-03-21 07:55:46 -0500319 req = 0
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500320 } else if ($(i) == "requires")
321 req = 1
John Bowlerb11b31a2012-03-21 07:55:46 -0500322 else if ($(i) == "enables")
323 req = 2
324 else if (req == 1){
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500325 reqread = reqread " READ_" $(i)
326 reqwrite = reqwrite " WRITE_" $(i)
John Bowlerb11b31a2012-03-21 07:55:46 -0500327 } else if (req == 2)
328 enables = enables " " $(i)
329 else
330 break # bad line: handled below
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500331 }
332 }
333
334 if (i > NF) {
335 # Output new 'option' lines to the intermediate file (out)
John Bowlerb11b31a2012-03-21 07:55:46 -0500336 print "option READ_" $2, "requires READ_ANCILLARY_CHUNKS" reqread, "enables", $2 enables , onoff >out
337 print "option WRITE_" $2, "requires WRITE_ANCILLARY_CHUNKS" reqwrite, "enables", $2 enables, onoff >out
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500338 next
339 }
340 # Else hit the error handler below - bad line format!
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500341}
342
343# setting MACRO ( requires MACRO* )* [ default VALUE ]
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500344# Behaves in a similar way to 'option' without looking for NO_ or
345# _SUPPORTED; the macro is enabled if it is defined so long as all
346# the 'requires' macros are also defined. The definitions may be
347# empty, an error will be issued if the 'requires' macros are
348# *not* defined. If given the 'default' value is used if the
349# macro is not defined. The default value will be re-tokenised.
350# (BTW: this is somewhat restrictive, it mainly exists for the
351# support of non-standard configurations and numeric parameters,
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -0500352# see the uses in scripts/options.dat
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500353
354$1 == "setting" && (NF == 2 || NF >= 3 && ($3 == "requires" || $3 == "default")){
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500355 reqs = ""
356 deflt = ""
357 isdef = 0
358 key = ""
359 for (i=3; i<=NF; ++i)
360 if ($(i) == "requires" || $(i) == "default") {
361 key = $(i)
362 if (key == "default") isdef = 1
363 } else if (key == "requires")
364 reqs = reqs " " $(i)
365 else if (key == "default")
366 deflt = deflt " " $(i)
367 else
368 break # Format error, handled below
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500369
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500370 setting[$2] = reqs
371 # NOTE: this overwrites a previous value silently
372 if (isdef && deflt == "")
373 deflt = " " # as a flag to force output
374 defaults[$2] = deflt
375 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500376}
377
378# The order of the dependency lines (option, chunk, setting) is irrelevant
379# - the 'enables', 'requires' and 'if' settings will be used to determine
Glenn Randers-Pehrson98b4f002010-04-16 22:30:26 -0500380# the correct order in the output and the final values in pnglibconf.h are
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500381# not order dependent. 'requires' and 'if' entries take precedence over
382# 'enables' from other options; if an option requires another option it
383# won't be set regardless of any options that enable it unless the other
384# option is also enabled.
385#
386# Similarly 'enables' trumps a NO_ definition in CFLAGS or pngusr.h
387#
388# For simplicity cycles in the definitions are regarded as errors,
389# even if they are not ambiguous.
390# A given NAME can be specified in as many 'option' lines as required, the
391# definitions are additive.
392
393# For backwards compatibility equivalent macros may be listed thus:
394#
395# = [NO_]NAME MACRO
396# Makes -DMACRO equivalent to -DPNG_NO_NAME or -DPNG_NAME_SUPPORTED
397# as appropriate.
398#
399# The definition is injected into the C compiler input when encountered
400# in the second pass (so all these definitions appear *after* the @
401# lines!)
402#
403# 'NAME' is as above, but 'MACRO' is the full text of the equivalent
404# old, deprecated, macro.
405
406$1 == "=" && NF == 3{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500407 print "#ifdef PNG_" $3 >out
408 if ($2 ~ /^NO_/)
409 print "# define PNG_" $2 >out
410 else
411 print "# define PNG_" $2 "_SUPPORTED" >out
412 print "#endif" >out
413 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500414}
415
416# Lines may be injected into the C compiler input by preceding them
417# with an "@" character. The line is copied with just the leading
418# @ removed.
419
420$1 ~ /^@/{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500421 # sub(/^[ ]*@/, "")
422 $1 = substr($1, 2)
423 print >out
424 next
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500425}
426
427# Check for unreognized lines, because of the preprocessing chunk
428# format errors will be detected on the first pass independent of
429# any other format errors.
430{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500431 print "options.awk: bad line (" NR "):", $0
432 err = 1 # prevent END{} running
433 exit 1
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500434}
435
436# For checking purposes names that start with "ok_" or "fail_" are
Glenn Randers-Pehrson98b4f002010-04-16 22:30:26 -0500437# not output to pnglibconf.h and must be either enabled or disabled
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500438# respectively for the build to succeed. This allows interdependencies
439# between options of the form "at least one of" or "at most one of"
440# to be checked. For example:
441#
442# option FLOATING_POINT enables ok_math
443# option FIXED_POINT enables ok_math
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500444# This ensures that at least one of FLOATING_POINT and FIXED_POINT
445# must be set for the build to succeed.
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500446#
447# option fail_math requires FLOATING_POINT FIXED_POINT
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500448# This means the build will fail if *both* FLOATING_POINT and
449# FIXED_POINT are set (this is an example; in fact both are allowed.)
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500450#
451# If all these options were given the build would require exactly one
452# of the names to be enabled.
453
454END{
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500455 # END{} gets run on an exit (a traditional awk feature)
456 if (err) exit 1
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500457
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500458 if (pre) {
459 # Record the final value of the variables
460 print "deb =", deb >out
461 if (everything != "") {
462 print "everything =", everything >out
463 }
464 print "logunsupported =", logunsupported >out
465 exit 0
466 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500467
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500468 # Do the 'setting' values first, the algorithm the standard
469 # tree walk (O(1)) done in an O(2) while/for loop; interations
470 # settings x depth, outputing the deepest required macros
471 # first.
472 print "" >out
473 print "/* SETTINGS */" >out
474 print comment, "settings", cend >out
475 finished = 0
476 while (!finished) {
477 finished = 1
478 movement = 0 # done nothing
479 for (i in setting) if (!doneset[i]) {
480 nreqs = split(setting[i], r)
481 if (nreqs > 0) {
482 for (j=1; j<=nreqs; ++j) if (!doneset[r[j]]) {
483 break
484 }
485 if (j<=nreqs) {
486 finished = 0
487 continue # try a different setting
488 }
489 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500490
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500491 # All the requirements have been processed, output
492 # this setting.
493 if (deb) print "setting", i
494 print "" >out
495 print "/* setting: ", i >out
496 print " * requires:" setting[i] >out
497 print " * default: ", defaults[i], "*/" >out
498 if (defaults[i] == "") { # no default, only check if defined
499 print "#ifdef PNG_" i >out
500 }
501 for (j=1; j<=nreqs; ++j) {
502 print "# ifndef PNG_" r[j] >out
503 print error, i, "requires", r[j] end >out
504 print "# endif" >out
505 }
506 if (defaults[i] != "") { # default handling
507 print "#ifdef PNG_" i >out
508 }
509 print def i, "PNG_" i end >out
510 if (defaults[i] != "") {
511 print "#else /*default*/" >out
512 # And add the default definition for the benefit
513 # of later settings an options test:
514 print "# define PNG_" i defaults[i] >out
515 print def i defaults[i] end >out
516 }
517 print "#endif" >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500518
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500519 doneset[i] = 1
520 ++movement
521 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500522
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500523 if (!finished && !movement) {
524 print "setting: loop or missing setting in 'requires', cannot process:"
525 for (i in setting) if (!doneset[i]) {
526 print " setting", i, "requires" setting[i]
527 }
528 exit 1
529 }
530 }
531 print comment, "end of settings", cend >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500532
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500533 # Now do the options - somewhat more complex. The dependency
534 # tree is thus:
535 #
536 # name > name
537 # name requires name
538 # name if name
539 # name enabledby name
540 #
541 # First build a list 'tree' by option of all the things on which
542 # it depends.
543 print "" >out
544 print "/* OPTIONS */" >out
545 print comment, "options", cend >out
546 for (opt in enabledby) tree[opt] = 1 # may not be explicit options
547 for (opt in option) if (opt != "") {
548 o = option[opt]
549 # option should always be one of the following values
550 if (o != "on" && o != "off" && o != "disabled" && o != "enabled") {
551 print "internal option error (" o ")"
552 exit 1
553 }
554 tree[opt] = "" # so unlisted options marked
555 }
556 for (opt in tree) if (opt != "") {
557 if (tree[opt] == 1) {
558 tree[opt] = ""
559 if (option[opt] != "") {
560 print "internal error (1)"
561 exit 1
562 }
563 # Macros only listed in 'enables' remain off unless
564 # one of the enabling macros is on.
565 option[opt] = "disabled"
566 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500567
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500568 split("", list) # clear 'list'
569 # Now add every requires, iffs or enabledby entry to 'list'
570 # so that we can add a unique list of requirements to tree[i]
571 split(requires[opt] iffs[opt] enabledby[opt], r)
572 for (i in r) list[r[i]] = 1
573 for (i in list) tree[opt] = tree[opt] " " i
574 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500575
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500576 # print the tree for extreme debugging
577 if (deb > 2) for (i in tree) if (i != "") print i, "depends-on" tree[i]
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500578
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500579 # Ok, now check all options marked explicitly 'on' or 'off':
580 #
581 # If an option[opt] is 'on' then turn on all requires[opt]
582 # If an option[opt] is 'off' then turn off all enabledby[opt]
583 #
584 # Error out if we have to turn 'on' an 'off' option or vice versa.
585 npending = 0
586 for (opt in option) if (opt != "") {
587 if (option[opt] == "on" || option[opt] == "off") {
588 pending[++npending] = opt
589 }
590 }
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500591
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500592 err = 0 # set on error
593 while (npending > 0) {
594 opt = pending[npending--]
595 if (option[opt] == "on") {
596 nreqs = split(requires[opt], r)
597 for (j=1; j<=nreqs; ++j) {
598 if (option[r[j]] == "off") {
599 print "option", opt, "turned on, but requirement", r[j], "is turned off"
600 err = 1
601 } else if (option[r[j]] != "on") {
602 option[r[j]] = "on"
603 pending[++npending] = r[j]
604 }
605 }
606 } else {
607 if (option[opt] != "off") {
608 print "internal error (2)"
609 exit 1
610 }
611 nreqs = split(enabledby[opt], r)
612 for (j=1; j<=nreqs; ++j) {
613 if (option[r[j]] == "on") {
614 print "option", opt, "turned off, but enabled by", r[j], "which is turned on"
615 err = 1
616 } else if (option[r[j]] != "off") {
617 option[r[j]] = "off"
618 pending[++npending] = r[j]
619 }
620 }
621 }
622 }
623 if (err) exit 1
Glenn Randers-Pehrsoncd745492010-04-28 07:52:16 -0500624
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500625 # option[i] is now the complete list of all the tokens we may
626 # need to output, go through it as above, depth first.
627 finished = 0
628 while (!finished) {
629 finished = 1
630 movement = 0 # done nothing
631 for (i in option) if (!done[i]) {
632 nreqs = split(tree[i], r)
633 if (nreqs > 0) {
634 for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
635 break
636 }
637 if (j<=nreqs) {
638 finished = 0
639 continue # next option
640 }
641 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500642
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500643 # All the requirements have been processed, output
644 # this option. An option is _SUPPORTED if:
645 #
646 # all 'requires' are _SUPPORTED AND
647 # at least one of the 'if' options are _SUPPORTED AND
648 # EITHER:
649 # The name is _SUPPORTED (on the command line)
650 # OR:
651 # an 'enabledby' is _SUPPORTED
652 # OR:
653 # NO_name is not defined AND
654 # the option is not disabled; an option is disabled if:
655 # option == off
656 # option == disabled && everything != on
657 # option == "" && everything == off
658 if (deb) print "option", i
659 print "" >out
660 print "/* option:", i, option[i] >out
661 print " * requires: " requires[i] >out
662 print " * if: " iffs[i] >out
663 print " * enabled-by:" enabledby[i], "*/" >out
664 print "#undef PNG_on" >out
665 print "#define PNG_on 1" >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500666
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500667 # requires
668 nreqs = split(requires[i], r)
669 for (j=1; j<=nreqs; ++j) {
670 print "#ifndef PNG_" r[j] "_SUPPORTED" >out
671 print "# undef PNG_on /*!" r[j] "*/" >out
672 # this error appears in the final output if something
673 # was switched 'on' but the processing above to force
674 # the requires did not work
675 if (option[i] == "on") {
676 print error, i, "requires", r[j] end >out
677 }
678 print "#endif" >out
679 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500680
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500681 # if
682 nreqs = split(iffs[i], r)
683 print "#undef PNG_no_if" >out
684 if (nreqs > 0) {
685 print "/* if" iffs[i], "*/" >out
686 print "#define PNG_no_if 1" >out
687 for (j=1; j<=nreqs; ++j) {
688 print "#ifdef PNG_" r[j] "_SUPPORTED" >out
689 print "# undef PNG_no_if /*" r[j] "*/" >out
690 print "#endif" >out
691 }
692 print "#ifdef PNG_no_if /*missing if*/" >out
693 print "# undef PNG_on" >out
694 # There is no checking above for this, because we
695 # don't know which 'if' to choose, so whine about
696 # it here:
697 if (option[i] == "on") {
698 print error, i, "needs one of:", iffs[i] 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 "#ifdef PNG_on /*requires, if*/" >out
704 # enables
705 print "# undef PNG_not_enabled" >out
706 print "# define PNG_not_enabled 1" >out
707 print " /* enabled by" enabledby[i], "*/" >out
708 nreqs = split(enabledby[i], r)
709 for (j=1; j<=nreqs; ++j) {
710 print "#ifdef PNG_" r[j] "_SUPPORTED" >out
711 print "# undef PNG_not_enabled /*" r[j] "*/" >out
712 # Oops, probably not intended (should be factored
713 # out by the checks above).
714 if (option[i] == "off") {
715 print error, i, "enabled by:", r[j] end >out
716 }
717 print "#endif" >out
718 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500719
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500720 print "# ifndef PNG_" i "_SUPPORTED /*!command line*/" >out
721 print "# ifdef PNG_not_enabled /*!enabled*/" >out
722 if (option[i] == "off" || option[i] == "disabled" && everything != "on" || option[i] == "enabled" && everything == "off") {
723 print "# undef PNG_on /*default off*/" >out
724 } else {
725 print "# ifdef PNG_NO_" i >out
726 print "# undef PNG_on /*turned off*/" >out
727 print "# endif" >out
728 print "# ifdef PNG_NO_" i "_SUPPORTED" >out
729 print "# undef PNG_on /*turned off*/" >out
730 print "# endif" >out
731 }
732 print "# endif /*!enabled*/" >out
733 print "# ifdef PNG_on" >out
734 # The _SUPPORTED macro must be defined so that dependent
735 # options output later work.
736 print "# define PNG_" i "_SUPPORTED" >out
737 print "# endif" >out
738 print "# endif /*!command line*/" >out
739 # If PNG_on is still set the option should be defined in
740 # pnglibconf.h
741 print "# ifdef PNG_on" >out
742 if (i ~ /^fail_/) {
743 print error, i, "is on: enabled by:" iffs[i] enabledby[i] ", requires" requires[i] end >out
744 } else if (i !~ /^ok_/) {
745 print def i sup >out
746 }
747 print "# endif /* definition */" >out
748 print "#endif /*requires, if*/" >out
749 if (logunsupported || i ~ /^ok_/) {
750 print "#ifndef PNG_on" >out
751 if (logunsupported) {
752 print und i une >out
753 }
754 if (i ~ /^ok_/) {
755 print error, i, "not enabled: requires:" requires[i] ", enabled by:" iffs[i] enabledby[i] end >out
756 }
757 print "#endif" >out
758 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500759
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500760 done[i] = 1
761 ++movement
762 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500763
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500764 if (!finished && !movement) {
765 print "option: loop or missing option in dependency tree, cannot process:"
766 for (i in option) if (!done[i]) {
767 print " option", i, "depends on" tree[i], "needs:"
768 nreqs = split(tree[i], r)
769 if (nreqs > 0) for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
770 print " " r[j]
771 }
772 }
773 exit 1
774 }
775 }
776 print comment, "end of options", cend >out
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500777
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -0500778 # Regular end - everything looks ok
779 if (protect != "") {
780 print start "#endif", cx, protect, "*/" end >out
781 }
Glenn Randers-Pehrson862cb202010-04-16 22:12:51 -0500782}