fukasawa e60969
#!/bin/awk -f
fukasawa e60969
# scripts/options.awk - library build configuration control
fukasawa e60969
#
fukasawa e60969
# last changed in libpng version 1.6.11 - June 5, 2014
fukasawa e60969
#
fukasawa e60969
# Copyright (c) 1998-2014 Glenn Randers-Pehrson
fukasawa e60969
#
fukasawa e60969
# This code is released under the libpng license.
fukasawa e60969
# For conditions of distribution and use, see the disclaimer
fukasawa e60969
# and license in png.h
fukasawa e60969
fukasawa e60969
# The output of this script is written to the file given by
fukasawa e60969
# the variable 'out'.  The script is run twice, once with
fukasawa e60969
# an intermediate output file, 'options.tmp' then again on
fukasawa e60969
# that file to produce the final output:
fukasawa e60969
#
fukasawa e60969
#  awk -f scripts/options.awk out=options.tmp scripts/options.dfa 1>&2
fukasawa e60969
#  awk -f scripts/options.awk out=options.dfn options.tmp 1>&2
fukasawa e60969
#
fukasawa e60969
# Some options may be specified on the command line:
fukasawa e60969
#
fukasawa e60969
#  deb=1            Causes debugging to be output
fukasawa e60969
#  logunsupported=1 Causes all options to be recorded in the output
fukasawa e60969
#  everything=off   Causes all options to be disabled by default
fukasawa e60969
#  everything=on    Causes all options to be enabled by default
fukasawa e60969
#
fukasawa e60969
# If awk fails on your platform, try nawk instead.
fukasawa e60969
#
fukasawa e60969
# These options may also be specified in the original input file (and
fukasawa e60969
# are copied to the preprocessed file).
fukasawa e60969
fukasawa e60969
BEGIN{
fukasawa e60969
   out=""                       # intermediate, preprocessed, file
fukasawa e60969
   pre=-1                       # preprocess (first line)
fukasawa e60969
   version="libpng version unknown" # version information
fukasawa e60969
   version_file=""              # where to find the version
fukasawa e60969
   err=0                        # in-line exit sets this
fukasawa e60969
   # The following definitions prevent the C preprocessor noticing the lines
fukasawa e60969
   # that will be in the final output file.  Some C preprocessors tokenise
fukasawa e60969
   # the lines, for example by inserting spaces around operators, and all
fukasawa e60969
   # C preprocessors notice lines that start with '#', most remove comments.
fukasawa e60969
   # The technique adopted here is to make the final output lines into
fukasawa e60969
   # C strings (enclosed in double quotes), preceeded by PNG_DFN.  As a
fukasawa e60969
   # consequence the output cannot contain a 'raw' double quote - instead put
fukasawa e60969
   # @' in, this will be replaced by a single " afterward.  See the parser
fukasawa e60969
   # script dfn.awk for more capabilities (not required here).  Note that if
fukasawa e60969
   # you need a " in a 'setting' in pnglibconf.dfa it must also be @'!
fukasawa e60969
   dq="@'"                      # For a single double quote
fukasawa e60969
   start=" PNG_DFN \""          # Start stuff to output (can't contain a "!)
fukasawa e60969
   end="\" "                    # End stuff to output
fukasawa e60969
   subs="@\" "                  # Substitute start (substitute a C macro)
fukasawa e60969
   sube=" \"@"                  # Substitute end
fukasawa e60969
   comment=start "/*"           # Comment start
fukasawa e60969
   cend="*/" end                # Comment end
fukasawa e60969
   def=start "#define PNG_"     # Arbitrary define
fukasawa e60969
   sup="_SUPPORTED" end         # end supported option
fukasawa e60969
   und=comment "#undef PNG_"    # Unsupported option
fukasawa e60969
   une="_SUPPORTED" cend        # end unsupported option
fukasawa e60969
   error=start "ERROR:"         # error message, terminate with 'end'
fukasawa e60969
fukasawa e60969
   # Variables
fukasawa e60969
   deb=0                        # debug - set on command line
fukasawa e60969
   everything=""                # do not override defaults
fukasawa e60969
   logunsupported=0             # write unsupported options too
fukasawa e60969
fukasawa e60969
   # Precreate arrays
fukasawa e60969
   # for each option:
fukasawa e60969
   option[""] = ""    # list of all options: default enabled/disabled
fukasawa e60969
   done[""] = 1       # marks option as having been output
fukasawa e60969
   requires[""] = ""  # requires by option
fukasawa e60969
   iffs[""] = ""      # if by option
fukasawa e60969
   enabledby[""] = "" # options that enable it by option
fukasawa e60969
   sets[""] = ""      # settings set by each option
fukasawa e60969
   setval[""] = ""    # value to set (indexed: 'option sets[option]')
fukasawa e60969
   # for each setting:
fukasawa e60969
   setting[""] = ""   # requires by setting
fukasawa e60969
   defaults[""] = ""  # used for a defaulted value
fukasawa e60969
   doneset[""] = 1    # marks setting as having been output
fukasawa e60969
   r[""] = ""         # Temporary array
fukasawa e60969
fukasawa e60969
   # For decorating the output file
fukasawa e60969
   protect = ""
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# The output file must be specified before any input:
fukasawa e60969
out == "" {
fukasawa e60969
   print "out=output.file must be given on the command line"
fukasawa e60969
   err = 1
fukasawa e60969
   exit 1
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# The very first line indicates whether we are reading pre-processed
fukasawa e60969
# input or not, this must come *first* because 'PREPROCESSED' needs
fukasawa e60969
# to be the very first line in the temporary file.
fukasawa e60969
pre == -1{
fukasawa e60969
   if ($0 == "PREPROCESSED") {
fukasawa e60969
      pre = 0
fukasawa e60969
      next
fukasawa e60969
   } else {
fukasawa e60969
      pre = 1
fukasawa e60969
      print "PREPROCESSED" >out
fukasawa e60969
      # And fall through to continue processing
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# While pre-processing if version is set to "search" look for a version string
fukasawa e60969
# in the following file.
fukasawa e60969
pre && version == "search" && version_file == ""{
fukasawa e60969
   version_file = FILENAME
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
pre && version == "search" && version_file != FILENAME{
fukasawa e60969
   print "version string not found in", version_file
fukasawa e60969
   err = 1
fukasawa e60969
   exit 1
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
pre && version == "search" && $0 ~ /^ \* libpng version/{
fukasawa e60969
   version = substr($0, 4)
fukasawa e60969
   print "version =", version >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
pre && FILENAME == version_file{
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# variable=value
fukasawa e60969
#   Sets the given variable to the given value (the syntax is fairly
fukasawa e60969
#   free form, except for deb (you are expected to understand how to
fukasawa e60969
#   set the debug variable...)
fukasawa e60969
#
fukasawa e60969
#   This happens before the check on 'pre' below skips most of the
fukasawa e60969
#   rest of the actions, so the variable settings happen during
fukasawa e60969
#   preprocessing but are recorded in the END action too.  This
fukasawa e60969
#   allows them to be set on the command line too.
fukasawa e60969
$0 ~ /^[ 	]*version[ 	]*=/{
fukasawa e60969
   sub(/^[  ]*version[  ]*=[  ]*/, "")
fukasawa e60969
   version = $0
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
$0 ~ /^[ 	]*everything[ 	=]*off[ 	]*$/{
fukasawa e60969
   everything = "off"
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
$0 ~ /^[ 	]*everything[ 	=]*on[ 	]*$/{
fukasawa e60969
   everything = "on"
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
$0 ~ /^[ 	]*logunsupported[ 	=]*0[ 	]*$/{
fukasawa e60969
   logunsupported = 0
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
$0 ~ /^[ 	]*logunsupported[ 	=]*1[ 	]*$/{
fukasawa e60969
   logunsupported = 1
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
$1 == "deb" && $2 == "=" && NF == 3{
fukasawa e60969
   deb = $3
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# Preprocessing - this just copies the input file with lines
fukasawa e60969
# that need preprocessing (just chunk at present) expanded
fukasawa e60969
# The bare "pre" instead of "pre != 0" crashes under Sunos awk
fukasawa e60969
pre && $1 != "chunk"{
fukasawa e60969
   print >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# The first characters of the line determine how it is processed,
fukasawa e60969
# leading spaces are ignored.  In general tokens that are not
fukasawa e60969
# keywords are the names of options.  An option 'name' is
fukasawa e60969
# controlled by the definition of the corresponding macros:
fukasawa e60969
#
fukasawa e60969
#   PNG_name_SUPPORTED    The option is turned on
fukasawa e60969
#   PNG_NO_name
fukasawa e60969
#   PNG_NO_name_SUPPORTED If the first macro is not defined
fukasawa e60969
#                         either of these will turn the option off
fukasawa e60969
#
fukasawa e60969
# If none of these macros are defined the option is turned on, unless
fukasawa e60969
# the keyword 'off' is given in a line relating to the option.  The
fukasawa e60969
# keyword 'on' can also be given, but it will be ignored (since it is
fukasawa e60969
# the default.)
fukasawa e60969
#
fukasawa e60969
# In the syntax below a 'name' is indicated by "NAME", other macro
fukasawa e60969
# values are indicated by "MACRO", as with "NAME" the leading "PNG_"
fukasawa e60969
# is omitted, but in this case the "NO_" prefix and the "_SUPPORTED"
fukasawa e60969
# suffix are never used.
fukasawa e60969
#
fukasawa e60969
# Each line is introduced by a keyword - the first non-space characters
fukasawa e60969
# on the line.  A line starting with a '#' is a comment - it is totally
fukasawa e60969
# ignored.  Keywords are as follows, a NAME, is simply a macro name
fukasawa e60969
# without the leading PNG_, PNG_NO_ or the trailing _SUPPORTED.
fukasawa e60969
fukasawa e60969
$1 ~ /^#/ || $0 ~ /^[ 	]*$/{
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# com <comment></comment>
fukasawa e60969
#   The whole line is placed in the output file as a comment with
fukasawa e60969
#   the preceding 'com' removed
fukasawa e60969
$1 == "com"{
fukasawa e60969
   if (NF > 1) {
fukasawa e60969
      # sub(/^[ 	]*com[ 	]*/, "")
fukasawa e60969
      $1 = ""
fukasawa e60969
      print comment $0, cend >out
fukasawa e60969
   } else
fukasawa e60969
      print start end >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# version
fukasawa e60969
#   Inserts a version comment
fukasawa e60969
$1 == "version" && NF == 1{
fukasawa e60969
   if (version == "") {
fukasawa e60969
      print "ERROR: no version string set"
fukasawa e60969
      err = 1 # prevent END{} running
fukasawa e60969
      exit 1
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   print comment, version, cend >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# file output input protect
fukasawa e60969
#   Informational: the official name of the input file (without
fukasawa e60969
#   make generated local directories), the official name of the
fukasawa e60969
#   output file and, if required, a name to use in a protection
fukasawa e60969
#   macro for the contents.
fukasawa e60969
$1 == "file" && NF >= 2{
fukasawa e60969
   print comment, $2, cend >out
fukasawa e60969
   print comment, "Machine generated file: DO NOT EDIT", cend >out
fukasawa e60969
   if (NF >= 3)
fukasawa e60969
      print comment, "Derived from:", $3, cend >out
fukasawa e60969
   protect = $4
fukasawa e60969
   if (protect != "") {
fukasawa e60969
      print start "#ifndef", protect end >out
fukasawa e60969
      print start "#define", protect end >out
fukasawa e60969
   }
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# option NAME ( (requires|enables|if) NAME* | on | off | disabled |
fukasawa e60969
#                sets SETTING VALUE+ )*
fukasawa e60969
#     
fukasawa e60969
#   Declares an option 'NAME' and describes its default setting (disabled)
fukasawa e60969
#   and its relationship to other options.  The option is disabled
fukasawa e60969
#   unless *all* the options listed after 'requires' are set and at
fukasawa e60969
#   least one of the options listed after 'if' is set.  If the
fukasawa e60969
#   option is set then it turns on all the options listed after 'enables'.
fukasawa e60969
#
fukasawa e60969
#   Note that "enables" takes priority over the required/if/disabled/off
fukasawa e60969
#   setting of the target option.
fukasawa e60969
#
fukasawa e60969
#   The definition file may list an option as 'disabled': off by default,
fukasawa e60969
#   otherwise the option is enabled: on by default.  A later (and it must
fukasawa e60969
#   be later) entry may turn an option on or off explicitly.
fukasawa e60969
fukasawa e60969
$1 == "option" && NF >= 2{
fukasawa e60969
   opt = $2
fukasawa e60969
   sub(/,$/,"",opt)
fukasawa e60969
   onoff = option[opt]  # records current (and the default is "", enabled)
fukasawa e60969
   key = ""
fukasawa e60969
   istart = 3
fukasawa e60969
   do {
fukasawa e60969
      if (istart == 1) {     # continuation line
fukasawa e60969
         val = getline
fukasawa e60969
fukasawa e60969
         if (val != 1) { # error reading it
fukasawa e60969
            if (val == 0)
fukasawa e60969
               print "option", opt ": ERROR: missing continuation line"
fukasawa e60969
            else
fukasawa e60969
               print "option", opt ": ERROR: error reading continuation line"
fukasawa e60969
fukasawa e60969
            # This is a hard error
fukasawa e60969
            err = 1 # prevent END{} running
fukasawa e60969
            exit 1
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      for (i=istart; i<=NF; ++i) {
fukasawa e60969
         val=$(i)
fukasawa e60969
         sub(/,$/,"",val)
fukasawa e60969
         if (val == "on" || val == "off" || val == "disabled" || val =="enabled") {
fukasawa e60969
            key = ""
fukasawa e60969
            if (onoff != val) {
fukasawa e60969
               # on or off can zap disabled or enabled:
fukasawa e60969
               if (onoff == "" || (onoff == "disabled" || onoff == "enabled") &&
fukasawa e60969
                   (val == "on" || val == "off")) {
fukasawa e60969
                  # It's easy to mis-spell the option when turning it
fukasawa e60969
                  # on or off, so warn about it here:
fukasawa e60969
                  if (onoff == "" && (val == "on" || val == "off")) {
fukasawa e60969
                     print "option", opt ": ERROR: turning unrecognized option", val
fukasawa e60969
                     # For the moment error out - it is safer
fukasawa e60969
                     err = 1 # prevent END{} running
fukasawa e60969
                     exit 1
fukasawa e60969
                  }
fukasawa e60969
                  onoff = val
fukasawa e60969
               } else {
fukasawa e60969
                  # Print a message, otherwise the error
fukasawa e60969
                  # below is incomprehensible
fukasawa e60969
                  print "option", opt ": currently", onoff ": attempt to turn", val
fukasawa e60969
                  break
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
         } else if (val == "requires" || val == "if" || val == "enables" || val =="sets") {
fukasawa e60969
            key = val
fukasawa e60969
         } else if (key == "requires") {
fukasawa e60969
            requires[opt] = requires[opt] " " val
fukasawa e60969
         } else if (key == "if") {
fukasawa e60969
            iffs[opt] = iffs[opt] " " val
fukasawa e60969
         } else if (key == "enables") {
fukasawa e60969
            enabledby[val] = enabledby[val] " " opt
fukasawa e60969
         } else if (key == "sets") {
fukasawa e60969
            sets[opt] = sets[opt] " " val
fukasawa e60969
            key = "setval"
fukasawa e60969
            set = val
fukasawa e60969
         } else if (key == "setval") {
fukasawa e60969
            setval[opt " " set] = setval[opt " " set] " " val
fukasawa e60969
         } else
fukasawa e60969
            break # bad line format
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      istart = 1
fukasawa e60969
   } while (i > NF && $0 ~ /,$/)
fukasawa e60969
fukasawa e60969
   if (i > NF) {
fukasawa e60969
      # Set the option, defaulting to 'enabled'
fukasawa e60969
      if (onoff == "") onoff = "enabled"
fukasawa e60969
      option[opt] = onoff
fukasawa e60969
      next
fukasawa e60969
   }
fukasawa e60969
   # Else fall through to the error handler
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# chunk NAME [requires OPT] [enables LIST] [on|off|disabled]
fukasawa e60969
#   Expands to the 'option' settings appropriate to the reading and
fukasawa e60969
#   writing of an ancilliary PNG chunk 'NAME':
fukasawa e60969
#
fukasawa e60969
#   option READ_NAME requires READ_ANCILLARY_CHUNKS [READ_OPT]
fukasawa e60969
#   option READ_NAME enables NAME LIST
fukasawa e60969
#   [option READ_NAME off]
fukasawa e60969
#   option WRITE_NAME requires WRITE_ANCILLARY_CHUNKS [WRITE_OPT]
fukasawa e60969
#   option WRITE_NAME enables NAME LIST
fukasawa e60969
#   [option WRITE_NAME off]
fukasawa e60969
fukasawa e60969
pre != 0 && $1 == "chunk" && NF >= 2{
fukasawa e60969
   # 'chunk' is handled on the first pass by writing appropriate
fukasawa e60969
   # 'option' lines into the intermediate file.
fukasawa e60969
   opt = $2
fukasawa e60969
   sub(/,$/,"",opt)
fukasawa e60969
   onoff = ""
fukasawa e60969
   reqread = ""
fukasawa e60969
   reqwrite = ""
fukasawa e60969
   enables = ""
fukasawa e60969
   req = 0
fukasawa e60969
   istart = 3
fukasawa e60969
   do {
fukasawa e60969
      if (istart == 1) {     # continuation line
fukasawa e60969
         val = getline
fukasawa e60969
fukasawa e60969
         if (val != 1) { # error reading it
fukasawa e60969
            if (val == 0)
fukasawa e60969
               print "chunk", opt ": ERROR: missing continuation line"
fukasawa e60969
            else
fukasawa e60969
               print "chunk", opt ": ERROR: error reading continuation line"
fukasawa e60969
fukasawa e60969
            # This is a hard error
fukasawa e60969
            err = 1 # prevent END{} running
fukasawa e60969
            exit 1
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      # read the keywords/additional OPTS
fukasawa e60969
      for (i=istart; i<=NF; ++i) {
fukasawa e60969
         val = $(i)
fukasawa e60969
         sub(/,$/,"",val)
fukasawa e60969
         if (val == "on" || val == "off" || val == "disabled") {
fukasawa e60969
            if (onoff != val) {
fukasawa e60969
               if (onoff == "")
fukasawa e60969
                  onoff = val
fukasawa e60969
               else
fukasawa e60969
                  break # on/off conflict
fukasawa e60969
            }
fukasawa e60969
            req = 0
fukasawa e60969
         } else if (val == "requires")
fukasawa e60969
            req = 1
fukasawa e60969
         else if (val == "enables")
fukasawa e60969
            req = 2
fukasawa e60969
         else if (req == 1){
fukasawa e60969
            reqread = reqread " READ_" val
fukasawa e60969
            reqwrite = reqwrite " WRITE_" val
fukasawa e60969
         } else if (req == 2)
fukasawa e60969
            enables = enables " " val
fukasawa e60969
         else
fukasawa e60969
            break # bad line: handled below
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      istart = 1
fukasawa e60969
   } while (i > NF && $0 ~ /,$/)
fukasawa e60969
fukasawa e60969
   if (i > NF) {
fukasawa e60969
      # Output new 'option' lines to the intermediate file (out)
fukasawa e60969
      print "option READ_" opt, "requires READ_ANCILLARY_CHUNKS" reqread, "enables", opt enables , onoff >out
fukasawa e60969
      print "option WRITE_" opt, "requires WRITE_ANCILLARY_CHUNKS" reqwrite, "enables", opt enables, onoff >out
fukasawa e60969
      next
fukasawa e60969
   }
fukasawa e60969
   # Else hit the error handler below - bad line format!
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# setting MACRO ( requires MACRO* )* [ default VALUE ]
fukasawa e60969
#   Behaves in a similar way to 'option' without looking for NO_ or
fukasawa e60969
#   _SUPPORTED; the macro is enabled if it is defined so long as all
fukasawa e60969
#   the 'requires' macros are also defined.  The definitions may be
fukasawa e60969
#   empty, an error will be issued if the 'requires' macros are
fukasawa e60969
#   *not* defined.  If given the 'default' value is used if the
fukasawa e60969
#   macro is not defined.  The default value will be re-tokenised.
fukasawa e60969
#   (BTW: this is somewhat restrictive, it mainly exists for the
fukasawa e60969
#   support of non-standard configurations and numeric parameters,
fukasawa e60969
#   see the uses in scripts/options.dat
fukasawa e60969
fukasawa e60969
$1 == "setting" && (NF == 2 || NF >= 3 && ($3 == "requires" || $3 == "default")){
fukasawa e60969
   reqs = ""
fukasawa e60969
   deflt = ""
fukasawa e60969
   isdef = 0
fukasawa e60969
   key = ""
fukasawa e60969
   for (i=3; i<=NF; ++i)
fukasawa e60969
      if ($(i) == "requires" || $(i) == "default") {
fukasawa e60969
         key = $(i)
fukasawa e60969
         if (key == "default") isdef = 1
fukasawa e60969
      } else if (key == "requires")
fukasawa e60969
         reqs = reqs " " $(i)
fukasawa e60969
      else if (key == "default")
fukasawa e60969
         deflt = deflt " " $(i)
fukasawa e60969
      else
fukasawa e60969
         break # Format error, handled below
fukasawa e60969
fukasawa e60969
   setting[$2] = reqs
fukasawa e60969
   # NOTE: this overwrites a previous value silently
fukasawa e60969
   if (isdef && deflt == "")
fukasawa e60969
      deflt = " " # as a flag to force output
fukasawa e60969
   defaults[$2] = deflt
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# The order of the dependency lines (option, chunk, setting) is irrelevant
fukasawa e60969
# - the 'enables', 'requires' and 'if' settings will be used to determine
fukasawa e60969
# the correct order in the output and the final values in pnglibconf.h are
fukasawa e60969
# not order dependent.  'requires' and 'if' entries take precedence over
fukasawa e60969
# 'enables' from other options; if an option requires another option it
fukasawa e60969
# won't be set regardless of any options that enable it unless the other
fukasawa e60969
# option is also enabled.
fukasawa e60969
#
fukasawa e60969
# Similarly 'enables' trumps a NO_ definition in CFLAGS or pngusr.h
fukasawa e60969
#
fukasawa e60969
# For simplicity cycles in the definitions are regarded as errors,
fukasawa e60969
# even if they are not ambiguous.
fukasawa e60969
# A given NAME can be specified in as many 'option' lines as required, the
fukasawa e60969
# definitions are additive.
fukasawa e60969
fukasawa e60969
# For backwards compatibility equivalent macros may be listed thus:
fukasawa e60969
#
fukasawa e60969
# = [NO_]NAME MACRO
fukasawa e60969
#   Makes -DMACRO equivalent to -DPNG_NO_NAME or -DPNG_NAME_SUPPORTED
fukasawa e60969
#   as appropriate.
fukasawa e60969
#
fukasawa e60969
# The definition is injected into the C compiler input when encountered
fukasawa e60969
# in the second pass (so all these definitions appear *after* the @
fukasawa e60969
# lines!)
fukasawa e60969
#
fukasawa e60969
# 'NAME' is as above, but 'MACRO' is the full text of the equivalent
fukasawa e60969
# old, deprecated, macro.
fukasawa e60969
fukasawa e60969
$1 == "=" && NF == 3{
fukasawa e60969
   print "#ifdef PNG_" $3 >out
fukasawa e60969
   if ($2 ~ /^NO_/)
fukasawa e60969
      print "#   define PNG_" $2 >out
fukasawa e60969
   else
fukasawa e60969
      print "#   define PNG_" $2 "_SUPPORTED" >out
fukasawa e60969
   print "#endif" >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# Lines may be injected into the C compiler input by preceding them
fukasawa e60969
# with an "@" character.  The line is copied with just the leading
fukasawa e60969
# @ removed.
fukasawa e60969
fukasawa e60969
$1 ~ /^@/{
fukasawa e60969
   # sub(/^[ 	]*@/, "")
fukasawa e60969
   $1 = substr($1, 2)
fukasawa e60969
   print >out
fukasawa e60969
   next
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# Check for unrecognized lines, because of the preprocessing chunk
fukasawa e60969
# format errors will be detected on the first pass independent of
fukasawa e60969
# any other format errors.
fukasawa e60969
{
fukasawa e60969
   print "options.awk: bad line (" NR "):", $0
fukasawa e60969
   err = 1 # prevent END{} running
fukasawa e60969
   exit 1
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# For checking purposes names that start with "ok_" or "fail_" are
fukasawa e60969
# not output to pnglibconf.h and must be either enabled or disabled
fukasawa e60969
# respectively for the build to succeed.  This allows interdependencies
fukasawa e60969
# between options of the form "at least one of" or "at most one of"
fukasawa e60969
# to be checked.  For example:
fukasawa e60969
#
fukasawa e60969
# option FLOATING_POINT enables ok_math
fukasawa e60969
# option FIXED_POINT enables ok_math
fukasawa e60969
#   This ensures that at least one of FLOATING_POINT and FIXED_POINT
fukasawa e60969
#   must be set for the build to succeed.
fukasawa e60969
#
fukasawa e60969
# option fail_math requires FLOATING_POINT FIXED_POINT
fukasawa e60969
#   This means the build will fail if *both* FLOATING_POINT and
fukasawa e60969
#   FIXED_POINT are set (this is an example; in fact both are allowed.)
fukasawa e60969
#
fukasawa e60969
# If all these options were given the build would require exactly one
fukasawa e60969
# of the names to be enabled.
fukasawa e60969
fukasawa e60969
END{
fukasawa e60969
   # END{} gets run on an exit (a traditional awk feature)
fukasawa e60969
   if (err) exit 1
fukasawa e60969
fukasawa e60969
   if (pre) {
fukasawa e60969
      # Record the final value of the variables
fukasawa e60969
      print "deb =", deb >out
fukasawa e60969
      if (everything != "") {
fukasawa e60969
         print "everything =", everything >out
fukasawa e60969
      }
fukasawa e60969
      print "logunsupported =", logunsupported >out
fukasawa e60969
      exit 0
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   # Do the options first (allowing options to set settings).  The dependency
fukasawa e60969
   # tree is thus:
fukasawa e60969
   #
fukasawa e60969
   #   name     >     name
fukasawa e60969
   #   name requires  name
fukasawa e60969
   #   name if        name
fukasawa e60969
   #   name enabledby name
fukasawa e60969
   #
fukasawa e60969
   # First build a list 'tree' by option of all the things on which
fukasawa e60969
   # it depends.
fukasawa e60969
   print "" >out
fukasawa e60969
   print "/* OPTIONS */" >out
fukasawa e60969
   print comment, "options", cend >out
fukasawa e60969
   for (opt in enabledby) tree[opt] = 1  # may not be explicit options
fukasawa e60969
   for (opt in option) if (opt != "") {
fukasawa e60969
      o = option[opt]
fukasawa e60969
      # option should always be one of the following values
fukasawa e60969
      if (o != "on" && o != "off" && o != "disabled" && o != "enabled") {
fukasawa e60969
         print "internal option error (" o ")"
fukasawa e60969
         exit 1
fukasawa e60969
      }
fukasawa e60969
      tree[opt] = ""   # so unlisted options marked
fukasawa e60969
   }
fukasawa e60969
   for (opt in tree) if (opt != "") {
fukasawa e60969
      if (tree[opt] == 1) {
fukasawa e60969
         tree[opt] = ""
fukasawa e60969
         if (option[opt] != "") {
fukasawa e60969
            print "internal error (1)"
fukasawa e60969
            exit 1
fukasawa e60969
         }
fukasawa e60969
         # Macros only listed in 'enables' remain off unless
fukasawa e60969
         # one of the enabling macros is on.
fukasawa e60969
         option[opt] = "disabled"
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      split("", list) # clear 'list'
fukasawa e60969
      # Now add every requires, iffs or enabledby entry to 'list'
fukasawa e60969
      # so that we can add a unique list of requirements to tree[i]
fukasawa e60969
      split(requires[opt] iffs[opt] enabledby[opt], r)
fukasawa e60969
      for (i in r) list[r[i]] = 1
fukasawa e60969
      for (i in list) tree[opt] = tree[opt] " " i
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   # print the tree for extreme debugging
fukasawa e60969
   if (deb > 2) for (i in tree) if (i != "") print i, "depends-on" tree[i]
fukasawa e60969
fukasawa e60969
   # Ok, now check all options marked explicitly 'on' or 'off':
fukasawa e60969
   #
fukasawa e60969
   # If an option[opt] is 'on' then turn on all requires[opt]
fukasawa e60969
   # If an option[opt] is 'off' then turn off all enabledby[opt]
fukasawa e60969
   #
fukasawa e60969
   # Error out if we have to turn 'on' to an 'off' option or vice versa.
fukasawa e60969
   npending = 0
fukasawa e60969
   for (opt in option) if (opt != "") {
fukasawa e60969
      if (option[opt] == "on" || option[opt] == "off") {
fukasawa e60969
         pending[++npending] = opt
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   err = 0 # set on error
fukasawa e60969
   while (npending > 0) {
fukasawa e60969
      opt = pending[npending--]
fukasawa e60969
      if (option[opt] == "on") {
fukasawa e60969
         nreqs = split(requires[opt], r)
fukasawa e60969
         for (j=1; j<=nreqs; ++j) {
fukasawa e60969
            if (option[r[j]] == "off") {
fukasawa e60969
               print "option", opt, "turned on, but requirement", r[j], "is turned off"
fukasawa e60969
               err = 1
fukasawa e60969
            } else if (option[r[j]] != "on") {
fukasawa e60969
               option[r[j]] = "on"
fukasawa e60969
               pending[++npending] = r[j]
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      } else {
fukasawa e60969
         if (option[opt] != "off") {
fukasawa e60969
            print "internal error (2)"
fukasawa e60969
            exit 1
fukasawa e60969
         }
fukasawa e60969
         nreqs = split(enabledby[opt], r)
fukasawa e60969
         for (j=1; j<=nreqs; ++j) {
fukasawa e60969
            if (option[r[j]] == "on") {
fukasawa e60969
               print "option", opt, "turned off, but enabled by", r[j], "which is turned on"
fukasawa e60969
               err = 1
fukasawa e60969
            } else if (option[r[j]] != "off") {
fukasawa e60969
               option[r[j]] = "off"
fukasawa e60969
               pending[++npending] = r[j]
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   if (err) exit 1
fukasawa e60969
fukasawa e60969
   # Sort options:
fukasawa e60969
   print "PNG_DFN_START_SORT 2" >out
fukasawa e60969
fukasawa e60969
   # option[i] is now the complete list of all the tokens we may
fukasawa e60969
   # need to output, go through it as above, depth first.
fukasawa e60969
   finished = 0
fukasawa e60969
   while (!finished) {
fukasawa e60969
      finished = 1
fukasawa e60969
      movement = 0 # done nothing
fukasawa e60969
      for (i in option) if (!done[i]) {
fukasawa e60969
         nreqs = split(tree[i], r)
fukasawa e60969
         if (nreqs > 0) {
fukasawa e60969
            for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
fukasawa e60969
               break
fukasawa e60969
            }
fukasawa e60969
            if (j<=nreqs) {
fukasawa e60969
               finished = 0
fukasawa e60969
               continue  # next option
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         # All the requirements have been processed, output
fukasawa e60969
         # this option.  An option is _SUPPORTED if:
fukasawa e60969
         #
fukasawa e60969
         # all 'requires' are _SUPPORTED AND
fukasawa e60969
         # at least one of the 'if' options are _SUPPORTED AND
fukasawa e60969
         # EITHER:
fukasawa e60969
         #   The name is _SUPPORTED (on the command line)
fukasawa e60969
         # OR:
fukasawa e60969
         #   an 'enabledby' is _SUPPORTED
fukasawa e60969
         # OR:
fukasawa e60969
         #   NO_name is not defined AND
fukasawa e60969
         #   the option is not disabled; an option is disabled if:
fukasawa e60969
         #    option == off
fukasawa e60969
         #    option == disabled && everything != on
fukasawa e60969
         #    option == "" && everything == off
fukasawa e60969
         if (deb) print "option", i
fukasawa e60969
         print "" >out
fukasawa e60969
         print "/* option:", i, option[i] >out
fukasawa e60969
         print " *   requires:  " requires[i] >out
fukasawa e60969
         print " *   if:        " iffs[i] >out
fukasawa e60969
         print " *   enabled-by:" enabledby[i] >out
fukasawa e60969
         print " *   sets:      " sets[i], "*/" >out
fukasawa e60969
         print "#undef PNG_on" >out
fukasawa e60969
         print "#define PNG_on 1" >out
fukasawa e60969
fukasawa e60969
         # requires
fukasawa e60969
         nreqs = split(requires[i], r)
fukasawa e60969
         for (j=1; j<=nreqs; ++j) {
fukasawa e60969
            print "#ifndef PNG_" r[j] "_SUPPORTED" >out
fukasawa e60969
            print "#   undef PNG_on /*!" r[j] "*/" >out
fukasawa e60969
            # This error appears in the final output if something
fukasawa e60969
            # was switched 'on' but the processing above to force
fukasawa e60969
            # the requires did not work
fukasawa e60969
            if (option[i] == "on") {
fukasawa e60969
               print error, i, "requires", r[j] end >out
fukasawa e60969
            }
fukasawa e60969
            print "#endif" >out
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         # if
fukasawa e60969
         have_ifs = 0
fukasawa e60969
         nreqs = split(iffs[i], r)
fukasawa e60969
         print "#undef PNG_no_if" >out
fukasawa e60969
         if (nreqs > 0) {
fukasawa e60969
            have_ifs = 1
fukasawa e60969
            print "/* if" iffs[i], "*/" >out
fukasawa e60969
            print "#define PNG_no_if 1" >out
fukasawa e60969
            for (j=1; j<=nreqs; ++j) {
fukasawa e60969
               print "#ifdef PNG_" r[j] "_SUPPORTED" >out
fukasawa e60969
               print "#   undef PNG_no_if /*" r[j] "*/" >out
fukasawa e60969
               print "#endif" >out
fukasawa e60969
            }
fukasawa e60969
            print "#ifdef PNG_no_if /*missing if*/" >out
fukasawa e60969
            print "#   undef PNG_on" >out
fukasawa e60969
            # There is no checking above for this, because we
fukasawa e60969
            # don't know which 'if' to choose, so whine about
fukasawa e60969
            # it here:
fukasawa e60969
            if (option[i] == "on") {
fukasawa e60969
               print error, i, "needs one of:", iffs[i] end >out
fukasawa e60969
            }
fukasawa e60969
            print "#endif" >out
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         print "#ifdef PNG_on /*requires, if*/" >out
fukasawa e60969
         # enables
fukasawa e60969
         print "#   undef PNG_not_enabled" >out
fukasawa e60969
         print "#   define PNG_not_enabled 1" >out
fukasawa e60969
         print "   /* enabled by" enabledby[i], "*/" >out
fukasawa e60969
         nreqs = split(enabledby[i], r)
fukasawa e60969
         for (j=1; j<=nreqs; ++j) {
fukasawa e60969
            print "#ifdef PNG_" r[j] "_SUPPORTED" >out
fukasawa e60969
            print "#   undef PNG_not_enabled /*" r[j] "*/" >out
fukasawa e60969
            # Oops, probably not intended (should be factored
fukasawa e60969
            # out by the checks above).
fukasawa e60969
            if (option[i] == "off") {
fukasawa e60969
               print error, i, "enabled by:", r[j] end >out
fukasawa e60969
            }
fukasawa e60969
            print "#endif" >out
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         print "#   ifndef PNG_" i "_SUPPORTED /*!command line*/" >out
fukasawa e60969
         print "#    ifdef PNG_not_enabled /*!enabled*/" >out
fukasawa e60969
         # 'have_ifs' here means that everything = "off" still allows an 'if' on
fukasawa e60969
         # an otherwise enabled option to turn it on; otherwise the 'if'
fukasawa e60969
         # handling is effectively disabled by 'everything = off'
fukasawa e60969
         if (option[i] == "off" || option[i] == "disabled" && everything != "on" || option[i] == "enabled" && everything == "off" && !have_ifs) {
fukasawa e60969
            print "#      undef PNG_on /*default off*/" >out
fukasawa e60969
         } else {
fukasawa e60969
            print "#      ifdef PNG_NO_" i >out
fukasawa e60969
            print "#       undef PNG_on /*turned off*/" >out
fukasawa e60969
            print "#      endif" >out
fukasawa e60969
            print "#      ifdef PNG_NO_" i "_SUPPORTED" >out
fukasawa e60969
            print "#       undef PNG_on /*turned off*/" >out
fukasawa e60969
            print "#      endif" >out
fukasawa e60969
         }
fukasawa e60969
         print "#    endif /*!enabled*/" >out
fukasawa e60969
         print "#    ifdef PNG_on" >out
fukasawa e60969
         # The _SUPPORTED macro must be defined so that dependent
fukasawa e60969
         # options output later work.
fukasawa e60969
         print "#      define PNG_" i "_SUPPORTED" >out
fukasawa e60969
         print "#    endif" >out
fukasawa e60969
         print "#   endif /*!command line*/" >out
fukasawa e60969
         # If PNG_on is still set the option should be defined in
fukasawa e60969
         # pnglibconf.h
fukasawa e60969
         print "#   ifdef PNG_on" >out
fukasawa e60969
         if (i ~ /^fail_/) {
fukasawa e60969
            print error, i, "is on: enabled by:" iffs[i] enabledby[i] ", requires" requires[i] end >out
fukasawa e60969
         } else if (i !~ /^ok_/) {
fukasawa e60969
            print def i sup >out
fukasawa e60969
            # Supported option, set required settings
fukasawa e60969
            nreqs = split(sets[i], r)
fukasawa e60969
            for (j=1; j<=nreqs; ++j) {
fukasawa e60969
               print "#    ifdef PNG_set_" r[j] >out
fukasawa e60969
               # Some other option has already set a value:
fukasawa e60969
               print error, i, "sets", r[j] ": duplicate setting" end >out
fukasawa e60969
               print error, "   previous value: " end "PNG_set_" r[j] >out
fukasawa e60969
               print "#    else" >out
fukasawa e60969
               # Else set the default: note that this won't accept arbitrary
fukasawa e60969
               # values, the setval string must be acceptable to all the C
fukasawa e60969
               # compilers we use.  That means it must be VERY simple; a number,
fukasawa e60969
               # a name or a string.
fukasawa e60969
               print "#     define PNG_set_" r[j], setval[i " " r[j]] >out
fukasawa e60969
               print "#    endif" >out
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
         print "#   endif /* definition */" >out
fukasawa e60969
         print "#endif /*requires, if*/" >out
fukasawa e60969
         if (logunsupported || i ~ /^ok_/) {
fukasawa e60969
            print "#ifndef  PNG_on" >out
fukasawa e60969
            if (logunsupported) {
fukasawa e60969
               print und i une >out
fukasawa e60969
            }
fukasawa e60969
            if (i ~ /^ok_/) {
fukasawa e60969
               print error, i, "not enabled: requires:" requires[i] ", enabled by:" iffs[i] enabledby[i] end >out
fukasawa e60969
            }
fukasawa e60969
            print "#endif" >out
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         done[i] = 1
fukasawa e60969
         ++movement
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!finished && !movement) {
fukasawa e60969
         print "option: loop or missing option in dependency tree, cannot process:"
fukasawa e60969
         for (i in option) if (!done[i]) {
fukasawa e60969
            print "  option", i, "depends on" tree[i], "needs:"
fukasawa e60969
            nreqs = split(tree[i], r)
fukasawa e60969
            if (nreqs > 0) for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
fukasawa e60969
               print "   " r[j]
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
         exit 1
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   print "PNG_DFN_END_SORT" >out
fukasawa e60969
   print comment, "end of options", cend >out
fukasawa e60969
fukasawa e60969
   # Do the 'setting' values second, the algorithm the standard
fukasawa e60969
   # tree walk (O(1)) done in an O(2) while/for loop; interations
fukasawa e60969
   # settings x depth, outputing the deepest required macros
fukasawa e60969
   # first.
fukasawa e60969
   print "" >out
fukasawa e60969
   print "/* SETTINGS */" >out
fukasawa e60969
   print comment, "settings", cend >out
fukasawa e60969
   # Sort (in dfn.awk) on field 2, the setting name
fukasawa e60969
   print "PNG_DFN_START_SORT 2" >out
fukasawa e60969
   finished = 0
fukasawa e60969
   while (!finished) {
fukasawa e60969
      finished = 1
fukasawa e60969
      movement = 0 # done nothing
fukasawa e60969
      for (i in setting) if (!doneset[i]) {
fukasawa e60969
         nreqs = split(setting[i], r)
fukasawa e60969
         if (nreqs > 0) {
fukasawa e60969
            # By default assume the requires values are options, but if there
fukasawa e60969
            # is no option with that name check for a setting
fukasawa e60969
            for (j=1; j<=nreqs; ++j) if (option[r[j]] == "" && !doneset[r[j]]) {
fukasawa e60969
               break
fukasawa e60969
            }
fukasawa e60969
            if (j<=nreqs) {
fukasawa e60969
               finished = 0
fukasawa e60969
               continue # try a different setting
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         # All the requirements have been processed, output
fukasawa e60969
         # this setting.
fukasawa e60969
         if (deb) print "setting", i
fukasawa e60969
         deflt = defaults[i]
fukasawa e60969
         # Remove any spurious trailing spaces
fukasawa e60969
         sub(/ *$/,"",deflt)
fukasawa e60969
         # A leading @ means leave it unquoted so the preprocessor
fukasawa e60969
         # can substitute the build time value
fukasawa e60969
         if (deflt ~ /^ @/)
fukasawa e60969
            deflt = " " subs substr(deflt, 3) sube
fukasawa e60969
         print "" >out
fukasawa e60969
         print "/* setting: ", i >out
fukasawa e60969
         print " *   requires:" setting[i] >out
fukasawa e60969
         print " *   default: ", defaults[i] deflt, "*/" >out
fukasawa e60969
         for (j=1; j<=nreqs; ++j) {
fukasawa e60969
            if (option[r[j]] != "")
fukasawa e60969
               print "#ifndef PNG_" r[j] "_SUPPORTED" >out
fukasawa e60969
            else
fukasawa e60969
               print "#ifndef PNG_" r[j] >out
fukasawa e60969
            print error, i, "requires", r[j] end >out
fukasawa e60969
            print "# endif" >out
fukasawa e60969
         }
fukasawa e60969
         # The precedence is:
fukasawa e60969
         #
fukasawa e60969
         #  1) External definition; trumps:
fukasawa e60969
         #  2) Option 'sets' value; trumps:
fukasawa e60969
         #  3) Setting 'default'
fukasawa e60969
         #
fukasawa e60969
         print "#ifdef PNG_" i >out
fukasawa e60969
         # PNG_ is defined, so substitute the value:
fukasawa e60969
         print def i, subs "PNG_" i sube end >out
fukasawa e60969
         print "#else /* use default */" >out
fukasawa e60969
         print "# ifdef PNG_set_" i >out
fukasawa e60969
         # Value from an option 'sets' argument
fukasawa e60969
         print def i, subs "PNG_set_" i sube end >out
fukasawa e60969
         # This is so that subsequent tests on the setting work:
fukasawa e60969
         print "#  define PNG_" i, "1" >out
fukasawa e60969
         if (defaults[i] != "") {
fukasawa e60969
            print "# else /*default*/" >out
fukasawa e60969
            print def i deflt end >out
fukasawa e60969
            print "#  define PNG_" i, "1" >out
fukasawa e60969
         }
fukasawa e60969
         print "# endif /* defaults */" >out
fukasawa e60969
         print "#endif /* setting", i, "*/" >out
fukasawa e60969
fukasawa e60969
         doneset[i] = 1
fukasawa e60969
         ++movement
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!finished && !movement) {
fukasawa e60969
         print "setting: loop or missing setting in 'requires', cannot process:"
fukasawa e60969
         for (i in setting) if (!doneset[i]) {
fukasawa e60969
            print "  setting", i, "requires" setting[i]
fukasawa e60969
         }
fukasawa e60969
         exit 1
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   print "PNG_DFN_END_SORT" >out
fukasawa e60969
   print comment, "end of settings", cend >out
fukasawa e60969
fukasawa e60969
   # Regular end - everything looks ok
fukasawa e60969
   if (protect != "") {
fukasawa e60969
      print start "#endif", "/*", protect, "*/" end >out
fukasawa e60969
   }
fukasawa e60969
}