options.awk 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. #!/bin/awk -f
  2. # scripts/options.awk - library build configuration control
  3. #
  4. # last changed in libpng version 1.5.7 - December 15, 2011
  5. #
  6. # Copyright (c) 1998-2011 Glenn Randers-Pehrson
  7. #
  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. # The output of this script is written to the file given by
  12. # the variable 'out'. The script is run twice, once with
  13. # an intermediate output file, 'options.tmp' then again on
  14. # that file to produce the final output:
  15. #
  16. # awk -f scripts/options.awk out=options.tmp scripts/options.dfa 1>&2
  17. # awk -f scripts/options.awk out=options.dfn options.tmp 1>&2
  18. #
  19. # Some options may be specified on the command line:
  20. #
  21. # deb=1 Causes debugging to be output
  22. # logunsupported=1 Causes all options to be recorded in the output
  23. # everything=off Causes all options to be disabled by default
  24. # everything=on Causes all options to be enabled by default
  25. #
  26. # If awk fails on your platform, try nawk instead.
  27. #
  28. # These options may also be specified in the original input file (and
  29. # are copied to the preprocessed file).
  30. BEGIN{
  31. out="" # intermediate, preprocessed, file
  32. pre=-1 # preprocess (first line)
  33. version="libpng version unknown" # version information
  34. version_file="" # where to find the version
  35. err=0 # in-line exit sets this
  36. # The following definitions prevent the C preprocessor noticing the lines
  37. # that will be in the final output file. Some C preprocessors tokenise
  38. # the lines, for example by inserting spaces around operators, and all
  39. # C preprocessors notice lines that start with '#', most remove comments.
  40. # The technique adopted here is to make the final output lines into
  41. # C strings (enclosed in double quotes), preceeded by PNG_DFN. As a
  42. # consequence the output cannot contain a 'raw' double quote - instead put
  43. # @' in, this will be replaced by a single " afterward. See the parser
  44. # script dfn.awk for more capabilities (not required here). Note that if
  45. # you need a " in a 'setting' in pnglibconf.dfa it must also be @'!
  46. dq="@'" # For a single double quote
  47. start=" PNG_DFN \"" # Start stuff to output (can't contain a "!)
  48. end="\" " # End stuff to output
  49. subs="@\" " # Substitute start (substitute a C macro)
  50. sube=" \"@" # Substitute end
  51. comment=start "/*" # Comment start
  52. cend="*/" end # Comment end
  53. def=start "#define PNG_" # Arbitrary define
  54. sup="_SUPPORTED" end # end supported option
  55. und=comment "#undef PNG_" # Unsupported option
  56. une="_SUPPORTED" cend # end unsupported option
  57. error=start "ERROR:" # error message, terminate with 'end'
  58. # Variables
  59. deb=0 # debug - set on command line
  60. everything="" # do not override defaults
  61. logunsupported=0 # write unsupported options too
  62. # Precreate arrays
  63. # for each option:
  64. option[""] = "" # list of all options: default enabled/disabled
  65. done[""] = 1 # marks option as having been output
  66. requires[""] = "" # requires by option
  67. iffs[""] = "" # if by option
  68. enabledby[""] = "" # options that enable it by option
  69. sets[""] = "" # settings set by each option
  70. setval[""] = "" # value to set (indexed: 'option sets[option]')
  71. # for each setting:
  72. setting[""] = "" # requires by setting
  73. defaults[""] = "" # used for a defaulted value
  74. doneset[""] = 1 # marks setting as having been output
  75. r[""] = "" # Temporary array
  76. # For decorating the output file
  77. protect = ""
  78. }
  79. # The output file must be specified before any input:
  80. out == "" {
  81. print "out=output.file must be given on the command line"
  82. err = 1
  83. exit 1
  84. }
  85. # The very first line indicates whether we are reading pre-processed
  86. # input or not, this must come *first* because 'PREPROCESSED' needs
  87. # to be the very first line in the temporary file.
  88. pre == -1{
  89. if ($0 == "PREPROCESSED") {
  90. pre = 0
  91. next
  92. } else {
  93. pre = 1
  94. print "PREPROCESSED" >out
  95. # And fall through to continue processing
  96. }
  97. }
  98. # While pre-processing if version is set to "search" look for a version string
  99. # in the following file.
  100. pre && version == "search" && version_file == ""{
  101. version_file = FILENAME
  102. }
  103. pre && version == "search" && version_file != FILENAME{
  104. print "version string not found in", version_file
  105. err = 1
  106. exit 1
  107. }
  108. pre && version == "search" && $0 ~ /^ \* libpng version/{
  109. version = substr($0, 4)
  110. print "version =", version >out
  111. next
  112. }
  113. pre && FILENAME == version_file{
  114. next
  115. }
  116. # variable=value
  117. # Sets the given variable to the given value (the syntax is fairly
  118. # free form, except for deb (you are expected to understand how to
  119. # set the debug variable...)
  120. #
  121. # This happens before the check on 'pre' below skips most of the
  122. # rest of the actions, so the variable settings happen during
  123. # preprocessing but are recorded in the END action too. This
  124. # allows them to be set on the command line too.
  125. $0 ~ /^[ ]*version[ ]*=/{
  126. sub(/^[ ]*version[ ]*=[ ]*/, "")
  127. version = $0
  128. next
  129. }
  130. $0 ~ /^[ ]*everything[ =]*off[ ]*$/{
  131. everything = "off"
  132. next
  133. }
  134. $0 ~ /^[ ]*everything[ =]*on[ ]*$/{
  135. everything = "on"
  136. next
  137. }
  138. $0 ~ /^[ ]*logunsupported[ =]*0[ ]*$/{
  139. logunsupported = 0
  140. next
  141. }
  142. $0 ~ /^[ ]*logunsupported[ =]*1[ ]*$/{
  143. logunsupported = 1
  144. next
  145. }
  146. $1 == "deb" && $2 == "=" && NF == 3{
  147. deb = $3
  148. next
  149. }
  150. # Preprocessing - this just copies the input file with lines
  151. # that need preprocessing (just chunk at present) expanded
  152. # The bare "pre" instead of "pre != 0" crashes under Sunos awk
  153. pre && $1 != "chunk"{
  154. print >out
  155. next
  156. }
  157. # The first characters of the line determine how it is processed,
  158. # leading spaces are ignored. In general tokens that are not
  159. # keywords are the names of options. An option 'name' is
  160. # controlled by the definition of the corresponding macros:
  161. #
  162. # PNG_name_SUPPORTED The option is turned on
  163. # PNG_NO_name
  164. # PNG_NO_name_SUPPORTED If the first macro is not defined
  165. # either of these will turn the option off
  166. #
  167. # If none of these macros are defined the option is turned on, unless
  168. # the keyword 'off' is given in a line relating to the option. The
  169. # keyword 'on' can also be given, but it will be ignored (since it is
  170. # the default.)
  171. #
  172. # In the syntax below a 'name' is indicated by "NAME", other macro
  173. # values are indicated by "MACRO", as with "NAME" the leading "PNG_"
  174. # is omitted, but in this case the "NO_" prefix and the "_SUPPORTED"
  175. # suffix are never used.
  176. #
  177. # Each line is introduced by a keyword - the first non-space characters
  178. # on the line. A line starting with a '#' is a comment - it is totally
  179. # ignored. Keywords are as follows, a NAME, is simply a macro name
  180. # without the leading PNG_, PNG_NO_ or the trailing _SUPPORTED.
  181. $1 ~ /^#/ || $0 ~ /^[ ]*$/{
  182. next
  183. }
  184. # com <comment>
  185. # The whole line is placed in the output file as a comment with
  186. # the preceding 'com' removed
  187. $1 == "com"{
  188. if (NF > 1) {
  189. # sub(/^[ ]*com[ ]*/, "")
  190. $1 = ""
  191. print comment $0, cend >out
  192. } else
  193. print start end >out
  194. next
  195. }
  196. # version
  197. # Inserts a version comment
  198. $1 == "version" && NF == 1{
  199. if (version == "") {
  200. print "ERROR: no version string set"
  201. err = 1 # prevent END{} running
  202. exit 1
  203. }
  204. print comment, version, cend >out
  205. next
  206. }
  207. # file output input protect
  208. # Informational: the official name of the input file (without
  209. # make generated local directories), the official name of the
  210. # output file and, if required, a name to use in a protection
  211. # macro for the contents.
  212. $1 == "file" && NF >= 2{
  213. print comment, $2, cend >out
  214. print comment, "Machine generated file: DO NOT EDIT", cend >out
  215. if (NF >= 3)
  216. print comment, "Derived from:", $3, cend >out
  217. protect = $4
  218. if (protect != "") {
  219. print start "#ifndef", protect end >out
  220. print start "#define", protect end >out
  221. }
  222. next
  223. }
  224. # option NAME ( (requires|enables|if) NAME* | on | off | disabled |
  225. # sets SETTING VALUE+ )*
  226. #
  227. # Declares an option 'NAME' and describes its default setting (disabled)
  228. # and its relationship to other options. The option is disabled
  229. # unless *all* the options listed after 'requires' are set and at
  230. # least one of the options listed after 'if' is set. If the
  231. # option is set then it turns on all the options listed after 'enables'.
  232. #
  233. # Note that "enables" takes priority over the required/if/disabled/off
  234. # setting of the target option.
  235. #
  236. # The definition file may list an option as 'disabled': off by default,
  237. # otherwise the option is enabled: on by default. A later (and it must
  238. # be later) entry may turn an option on or off explicitly.
  239. $1 == "option" && NF >= 2{
  240. opt = $2
  241. sub(/,$/,"",opt)
  242. onoff = option[opt] # records current (and the default is "", enabled)
  243. key = ""
  244. istart = 3
  245. do {
  246. if (istart == 1) { # continuation line
  247. val = getline
  248. if (val != 1) { # error reading it
  249. if (val == 0)
  250. print "option", opt ": ERROR: missing contination line"
  251. else
  252. print "option", opt ": ERROR: error reading continuation line"
  253. # This is a hard error
  254. err = 1 # prevent END{} running
  255. exit 1
  256. }
  257. }
  258. for (i=istart; i<=NF; ++i) {
  259. val=$(i)
  260. sub(/,$/,"",val)
  261. if (val == "on" || val == "off" || val == "disabled" || val =="enabled") {
  262. key = ""
  263. if (onoff != val) {
  264. # on or off can zap disabled or enabled:
  265. if (onoff == "" || (onoff == "disabled" || onoff == "enabled") &&
  266. (val == "on" || val == "off")) {
  267. # It's easy to mis-spell the option when turning it
  268. # on or off, so warn about it here:
  269. if (onoff == "" && (val == "on" || val == "off")) {
  270. print "option", opt ": ERROR: turning unrecognized option", val
  271. # For the moment error out - it is safer
  272. err = 1 # prevent END{} running
  273. exit 1
  274. }
  275. onoff = val
  276. } else {
  277. # Print a message, otherwise the error
  278. # below is incomprehensible
  279. print "option", opt ": currently", onoff ": attempt to turn", val
  280. break
  281. }
  282. }
  283. } else if (val == "requires" || val == "if" || val == "enables" || val =="sets") {
  284. key = val
  285. } else if (key == "requires") {
  286. requires[opt] = requires[opt] " " val
  287. } else if (key == "if") {
  288. iffs[opt] = iffs[opt] " " val
  289. } else if (key == "enables") {
  290. enabledby[val] = enabledby[val] " " opt
  291. } else if (key == "sets") {
  292. sets[opt] = sets[opt] " " val
  293. key = "setval"
  294. set = val
  295. } else if (key == "setval") {
  296. setval[opt " " set] = setval[opt " " set] " " val
  297. } else
  298. break # bad line format
  299. }
  300. istart = 1
  301. } while (i > NF && $0 ~ /,$/)
  302. if (i > NF) {
  303. # Set the option, defaulting to 'enabled'
  304. if (onoff == "") onoff = "enabled"
  305. option[opt] = onoff
  306. next
  307. }
  308. # Else fall through to the error handler
  309. }
  310. # chunk NAME [requires OPT] [enables LIST] [on|off|disabled]
  311. # Expands to the 'option' settings appropriate to the reading and
  312. # writing of an ancilliary PNG chunk 'NAME':
  313. #
  314. # option READ_NAME requires READ_ANCILLARY_CHUNKS [READ_OPT]
  315. # option READ_NAME enables NAME LIST
  316. # [option READ_NAME off]
  317. # option WRITE_NAME requires WRITE_ANCILLARY_CHUNKS [WRITE_OPT]
  318. # option WRITE_NAME enables NAME LIST
  319. # [option WRITE_NAME off]
  320. pre != 0 && $1 == "chunk" && NF >= 2{
  321. # 'chunk' is handled on the first pass by writing appropriate
  322. # 'option' lines into the intermediate file.
  323. opt = $2
  324. sub(/,$/,"",opt)
  325. onoff = ""
  326. reqread = ""
  327. reqwrite = ""
  328. enables = ""
  329. req = 0
  330. istart = 3
  331. do {
  332. if (istart == 1) { # continuation line
  333. val = getline
  334. if (val != 1) { # error reading it
  335. if (val == 0)
  336. print "chunk", opt ": ERROR: missing contination line"
  337. else
  338. print "chunk", opt ": ERROR: error reading continuation line"
  339. # This is a hard error
  340. err = 1 # prevent END{} running
  341. exit 1
  342. }
  343. }
  344. # read the keywords/additional OPTS
  345. for (i=istart; i<=NF; ++i) {
  346. val = $(i)
  347. sub(/,$/,"",val)
  348. if (val == "on" || val == "off" || val == "disabled") {
  349. if (onoff != val) {
  350. if (onoff == "")
  351. onoff = val
  352. else
  353. break # on/off conflict
  354. }
  355. req = 0
  356. } else if (val == "requires")
  357. req = 1
  358. else if (val == "enables")
  359. req = 2
  360. else if (req == 1){
  361. reqread = reqread " READ_" val
  362. reqwrite = reqwrite " WRITE_" val
  363. } else if (req == 2)
  364. enables = enables " " val
  365. else
  366. break # bad line: handled below
  367. }
  368. istart = 1
  369. } while (i > NF && $0 ~ /,$/)
  370. if (i > NF) {
  371. # Output new 'option' lines to the intermediate file (out)
  372. print "option READ_" opt, "requires READ_ANCILLARY_CHUNKS" reqread, "enables", opt enables , onoff >out
  373. print "option WRITE_" opt, "requires WRITE_ANCILLARY_CHUNKS" reqwrite, "enables", opt enables, onoff >out
  374. next
  375. }
  376. # Else hit the error handler below - bad line format!
  377. }
  378. # setting MACRO ( requires MACRO* )* [ default VALUE ]
  379. # Behaves in a similar way to 'option' without looking for NO_ or
  380. # _SUPPORTED; the macro is enabled if it is defined so long as all
  381. # the 'requires' macros are also defined. The definitions may be
  382. # empty, an error will be issued if the 'requires' macros are
  383. # *not* defined. If given the 'default' value is used if the
  384. # macro is not defined. The default value will be re-tokenised.
  385. # (BTW: this is somewhat restrictive, it mainly exists for the
  386. # support of non-standard configurations and numeric parameters,
  387. # see the uses in scripts/options.dat
  388. $1 == "setting" && (NF == 2 || NF >= 3 && ($3 == "requires" || $3 == "default")){
  389. reqs = ""
  390. deflt = ""
  391. isdef = 0
  392. key = ""
  393. for (i=3; i<=NF; ++i)
  394. if ($(i) == "requires" || $(i) == "default") {
  395. key = $(i)
  396. if (key == "default") isdef = 1
  397. } else if (key == "requires")
  398. reqs = reqs " " $(i)
  399. else if (key == "default")
  400. deflt = deflt " " $(i)
  401. else
  402. break # Format error, handled below
  403. setting[$2] = reqs
  404. # NOTE: this overwrites a previous value silently
  405. if (isdef && deflt == "")
  406. deflt = " " # as a flag to force output
  407. defaults[$2] = deflt
  408. next
  409. }
  410. # The order of the dependency lines (option, chunk, setting) is irrelevant
  411. # - the 'enables', 'requires' and 'if' settings will be used to determine
  412. # the correct order in the output and the final values in pnglibconf.h are
  413. # not order dependent. 'requires' and 'if' entries take precedence over
  414. # 'enables' from other options; if an option requires another option it
  415. # won't be set regardless of any options that enable it unless the other
  416. # option is also enabled.
  417. #
  418. # Similarly 'enables' trumps a NO_ definition in CFLAGS or pngusr.h
  419. #
  420. # For simplicity cycles in the definitions are regarded as errors,
  421. # even if they are not ambiguous.
  422. # A given NAME can be specified in as many 'option' lines as required, the
  423. # definitions are additive.
  424. # For backwards compatibility equivalent macros may be listed thus:
  425. #
  426. # = [NO_]NAME MACRO
  427. # Makes -DMACRO equivalent to -DPNG_NO_NAME or -DPNG_NAME_SUPPORTED
  428. # as appropriate.
  429. #
  430. # The definition is injected into the C compiler input when encountered
  431. # in the second pass (so all these definitions appear *after* the @
  432. # lines!)
  433. #
  434. # 'NAME' is as above, but 'MACRO' is the full text of the equivalent
  435. # old, deprecated, macro.
  436. $1 == "=" && NF == 3{
  437. print "#ifdef PNG_" $3 >out
  438. if ($2 ~ /^NO_/)
  439. print "# define PNG_" $2 >out
  440. else
  441. print "# define PNG_" $2 "_SUPPORTED" >out
  442. print "#endif" >out
  443. next
  444. }
  445. # Lines may be injected into the C compiler input by preceding them
  446. # with an "@" character. The line is copied with just the leading
  447. # @ removed.
  448. $1 ~ /^@/{
  449. # sub(/^[ ]*@/, "")
  450. $1 = substr($1, 2)
  451. print >out
  452. next
  453. }
  454. # Check for unreognized lines, because of the preprocessing chunk
  455. # format errors will be detected on the first pass independent of
  456. # any other format errors.
  457. {
  458. print "options.awk: bad line (" NR "):", $0
  459. err = 1 # prevent END{} running
  460. exit 1
  461. }
  462. # For checking purposes names that start with "ok_" or "fail_" are
  463. # not output to pnglibconf.h and must be either enabled or disabled
  464. # respectively for the build to succeed. This allows interdependencies
  465. # between options of the form "at least one of" or "at most one of"
  466. # to be checked. For example:
  467. #
  468. # option FLOATING_POINT enables ok_math
  469. # option FIXED_POINT enables ok_math
  470. # This ensures that at least one of FLOATING_POINT and FIXED_POINT
  471. # must be set for the build to succeed.
  472. #
  473. # option fail_math requires FLOATING_POINT FIXED_POINT
  474. # This means the build will fail if *both* FLOATING_POINT and
  475. # FIXED_POINT are set (this is an example; in fact both are allowed.)
  476. #
  477. # If all these options were given the build would require exactly one
  478. # of the names to be enabled.
  479. END{
  480. # END{} gets run on an exit (a traditional awk feature)
  481. if (err) exit 1
  482. if (pre) {
  483. # Record the final value of the variables
  484. print "deb =", deb >out
  485. if (everything != "") {
  486. print "everything =", everything >out
  487. }
  488. print "logunsupported =", logunsupported >out
  489. exit 0
  490. }
  491. # Do the options first (allowing options to set settings). The dependency
  492. # tree is thus:
  493. #
  494. # name > name
  495. # name requires name
  496. # name if name
  497. # name enabledby name
  498. #
  499. # First build a list 'tree' by option of all the things on which
  500. # it depends.
  501. print "" >out
  502. print "/* OPTIONS */" >out
  503. print comment, "options", cend >out
  504. for (opt in enabledby) tree[opt] = 1 # may not be explicit options
  505. for (opt in option) if (opt != "") {
  506. o = option[opt]
  507. # option should always be one of the following values
  508. if (o != "on" && o != "off" && o != "disabled" && o != "enabled") {
  509. print "internal option error (" o ")"
  510. exit 1
  511. }
  512. tree[opt] = "" # so unlisted options marked
  513. }
  514. for (opt in tree) if (opt != "") {
  515. if (tree[opt] == 1) {
  516. tree[opt] = ""
  517. if (option[opt] != "") {
  518. print "internal error (1)"
  519. exit 1
  520. }
  521. # Macros only listed in 'enables' remain off unless
  522. # one of the enabling macros is on.
  523. option[opt] = "disabled"
  524. }
  525. split("", list) # clear 'list'
  526. # Now add every requires, iffs or enabledby entry to 'list'
  527. # so that we can add a unique list of requirements to tree[i]
  528. split(requires[opt] iffs[opt] enabledby[opt], r)
  529. for (i in r) list[r[i]] = 1
  530. for (i in list) tree[opt] = tree[opt] " " i
  531. }
  532. # print the tree for extreme debugging
  533. if (deb > 2) for (i in tree) if (i != "") print i, "depends-on" tree[i]
  534. # Ok, now check all options marked explicitly 'on' or 'off':
  535. #
  536. # If an option[opt] is 'on' then turn on all requires[opt]
  537. # If an option[opt] is 'off' then turn off all enabledby[opt]
  538. #
  539. # Error out if we have to turn 'on' an 'off' option or vice versa.
  540. npending = 0
  541. for (opt in option) if (opt != "") {
  542. if (option[opt] == "on" || option[opt] == "off") {
  543. pending[++npending] = opt
  544. }
  545. }
  546. err = 0 # set on error
  547. while (npending > 0) {
  548. opt = pending[npending--]
  549. if (option[opt] == "on") {
  550. nreqs = split(requires[opt], r)
  551. for (j=1; j<=nreqs; ++j) {
  552. if (option[r[j]] == "off") {
  553. print "option", opt, "turned on, but requirement", r[j], "is turned off"
  554. err = 1
  555. } else if (option[r[j]] != "on") {
  556. option[r[j]] = "on"
  557. pending[++npending] = r[j]
  558. }
  559. }
  560. } else {
  561. if (option[opt] != "off") {
  562. print "internal error (2)"
  563. exit 1
  564. }
  565. nreqs = split(enabledby[opt], r)
  566. for (j=1; j<=nreqs; ++j) {
  567. if (option[r[j]] == "on") {
  568. print "option", opt, "turned off, but enabled by", r[j], "which is turned on"
  569. err = 1
  570. } else if (option[r[j]] != "off") {
  571. option[r[j]] = "off"
  572. pending[++npending] = r[j]
  573. }
  574. }
  575. }
  576. }
  577. if (err) exit 1
  578. # Sort options:
  579. print "PNG_DFN_START_SORT 2" >out
  580. # option[i] is now the complete list of all the tokens we may
  581. # need to output, go through it as above, depth first.
  582. finished = 0
  583. while (!finished) {
  584. finished = 1
  585. movement = 0 # done nothing
  586. for (i in option) if (!done[i]) {
  587. nreqs = split(tree[i], r)
  588. if (nreqs > 0) {
  589. for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
  590. break
  591. }
  592. if (j<=nreqs) {
  593. finished = 0
  594. continue # next option
  595. }
  596. }
  597. # All the requirements have been processed, output
  598. # this option. An option is _SUPPORTED if:
  599. #
  600. # all 'requires' are _SUPPORTED AND
  601. # at least one of the 'if' options are _SUPPORTED AND
  602. # EITHER:
  603. # The name is _SUPPORTED (on the command line)
  604. # OR:
  605. # an 'enabledby' is _SUPPORTED
  606. # OR:
  607. # NO_name is not defined AND
  608. # the option is not disabled; an option is disabled if:
  609. # option == off
  610. # option == disabled && everything != on
  611. # option == "" && everything == off
  612. if (deb) print "option", i
  613. print "" >out
  614. print "/* option:", i, option[i] >out
  615. print " * requires: " requires[i] >out
  616. print " * if: " iffs[i] >out
  617. print " * enabled-by:" enabledby[i] >out
  618. print " * sets: " sets[i], "*/" >out
  619. print "#undef PNG_on" >out
  620. print "#define PNG_on 1" >out
  621. # requires
  622. nreqs = split(requires[i], r)
  623. for (j=1; j<=nreqs; ++j) {
  624. print "#ifndef PNG_" r[j] "_SUPPORTED" >out
  625. print "# undef PNG_on /*!" r[j] "*/" >out
  626. # this error appears in the final output if something
  627. # was switched 'on' but the processing above to force
  628. # the requires did not work
  629. if (option[i] == "on") {
  630. print error, i, "requires", r[j] end >out
  631. }
  632. print "#endif" >out
  633. }
  634. # if
  635. have_ifs = 0
  636. nreqs = split(iffs[i], r)
  637. print "#undef PNG_no_if" >out
  638. if (nreqs > 0) {
  639. have_ifs = 1
  640. print "/* if" iffs[i], "*/" >out
  641. print "#define PNG_no_if 1" >out
  642. for (j=1; j<=nreqs; ++j) {
  643. print "#ifdef PNG_" r[j] "_SUPPORTED" >out
  644. print "# undef PNG_no_if /*" r[j] "*/" >out
  645. print "#endif" >out
  646. }
  647. print "#ifdef PNG_no_if /*missing if*/" >out
  648. print "# undef PNG_on" >out
  649. # There is no checking above for this, because we
  650. # don't know which 'if' to choose, so whine about
  651. # it here:
  652. if (option[i] == "on") {
  653. print error, i, "needs one of:", iffs[i] end >out
  654. }
  655. print "#endif" >out
  656. }
  657. print "#ifdef PNG_on /*requires, if*/" >out
  658. # enables
  659. print "# undef PNG_not_enabled" >out
  660. print "# define PNG_not_enabled 1" >out
  661. print " /* enabled by" enabledby[i], "*/" >out
  662. nreqs = split(enabledby[i], r)
  663. for (j=1; j<=nreqs; ++j) {
  664. print "#ifdef PNG_" r[j] "_SUPPORTED" >out
  665. print "# undef PNG_not_enabled /*" r[j] "*/" >out
  666. # Oops, probably not intended (should be factored
  667. # out by the checks above).
  668. if (option[i] == "off") {
  669. print error, i, "enabled by:", r[j] end >out
  670. }
  671. print "#endif" >out
  672. }
  673. print "# ifndef PNG_" i "_SUPPORTED /*!command line*/" >out
  674. print "# ifdef PNG_not_enabled /*!enabled*/" >out
  675. # 'have_ifs' here means that everything = "off" still allows an 'if' on
  676. # an otherwise enabled option to turn it on; otherwise the 'if'
  677. # handling is effectively disabled by 'everything = off'
  678. if (option[i] == "off" || option[i] == "disabled" && everything != "on" || option[i] == "enabled" && everything == "off" && !have_ifs) {
  679. print "# undef PNG_on /*default off*/" >out
  680. } else {
  681. print "# ifdef PNG_NO_" i >out
  682. print "# undef PNG_on /*turned off*/" >out
  683. print "# endif" >out
  684. print "# ifdef PNG_NO_" i "_SUPPORTED" >out
  685. print "# undef PNG_on /*turned off*/" >out
  686. print "# endif" >out
  687. }
  688. print "# endif /*!enabled*/" >out
  689. print "# ifdef PNG_on" >out
  690. # The _SUPPORTED macro must be defined so that dependent
  691. # options output later work.
  692. print "# define PNG_" i "_SUPPORTED" >out
  693. print "# endif" >out
  694. print "# endif /*!command line*/" >out
  695. # If PNG_on is still set the option should be defined in
  696. # pnglibconf.h
  697. print "# ifdef PNG_on" >out
  698. if (i ~ /^fail_/) {
  699. print error, i, "is on: enabled by:" iffs[i] enabledby[i] ", requires" requires[i] end >out
  700. } else if (i !~ /^ok_/) {
  701. print def i sup >out
  702. # Supported option, set required settings
  703. nreqs = split(sets[i], r)
  704. for (j=1; j<=nreqs; ++j) {
  705. print "# ifdef PNG_set_" r[j] >out
  706. # Some other option has already set a value:
  707. print error, i, "sets", r[j] ": duplicate setting" end >out
  708. print error, " previous value: " end "PNG_set_" r[j] >out
  709. print "# else" >out
  710. # Else set the default: note that this won't accept arbitrary
  711. # values, the setval string must be acceptable to all the C
  712. # compilers we use. That means it must be VERY simple; a number,
  713. # a name or a string.
  714. print "# define PNG_set_" r[j], setval[i " " r[j]] >out
  715. print "# endif" >out
  716. }
  717. }
  718. print "# endif /* definition */" >out
  719. print "#endif /*requires, if*/" >out
  720. if (logunsupported || i ~ /^ok_/) {
  721. print "#ifndef PNG_on" >out
  722. if (logunsupported) {
  723. print und i une >out
  724. }
  725. if (i ~ /^ok_/) {
  726. print error, i, "not enabled: requires:" requires[i] ", enabled by:" iffs[i] enabledby[i] end >out
  727. }
  728. print "#endif" >out
  729. }
  730. done[i] = 1
  731. ++movement
  732. }
  733. if (!finished && !movement) {
  734. print "option: loop or missing option in dependency tree, cannot process:"
  735. for (i in option) if (!done[i]) {
  736. print " option", i, "depends on" tree[i], "needs:"
  737. nreqs = split(tree[i], r)
  738. if (nreqs > 0) for (j=1; j<=nreqs; ++j) if (!done[r[j]]) {
  739. print " " r[j]
  740. }
  741. }
  742. exit 1
  743. }
  744. }
  745. print "PNG_DFN_END_SORT" >out
  746. print comment, "end of options", cend >out
  747. # Do the 'setting' values second, the algorithm the standard
  748. # tree walk (O(1)) done in an O(2) while/for loop; interations
  749. # settings x depth, outputing the deepest required macros
  750. # first.
  751. print "" >out
  752. print "/* SETTINGS */" >out
  753. print comment, "settings", cend >out
  754. # Sort (in dfn.awk) on field 2, the setting name
  755. print "PNG_DFN_START_SORT 2" >out
  756. finished = 0
  757. while (!finished) {
  758. finished = 1
  759. movement = 0 # done nothing
  760. for (i in setting) if (!doneset[i]) {
  761. nreqs = split(setting[i], r)
  762. if (nreqs > 0) {
  763. # By default assume the requires values are options, but if there
  764. # is no option with that name check for a setting
  765. for (j=1; j<=nreqs; ++j) if (option[r[j]] == "" && !doneset[r[j]]) {
  766. break
  767. }
  768. if (j<=nreqs) {
  769. finished = 0
  770. continue # try a different setting
  771. }
  772. }
  773. # All the requirements have been processed, output
  774. # this setting.
  775. if (deb) print "setting", i
  776. deflt = defaults[i]
  777. # Remove any spurious trailing spaces
  778. sub(/ *$/,"",deflt)
  779. # A leading @ means leave it unquoted so the preprocessor
  780. # can substitute the build time value
  781. if (deflt ~ /^ @/)
  782. deflt = " " subs substr(deflt, 3) sube
  783. print "" >out
  784. print "/* setting: ", i >out
  785. print " * requires:" setting[i] >out
  786. print " * default: ", defaults[i] deflt, "*/" >out
  787. for (j=1; j<=nreqs; ++j) {
  788. if (option[r[j]] != "")
  789. print "#ifndef PNG_" r[j] "_SUPPORTED" >out
  790. else
  791. print "#ifndef PNG_" r[j] >out
  792. print error, i, "requires", r[j] end >out
  793. print "# endif" >out
  794. }
  795. # The precedence is:
  796. #
  797. # 1) External definition; trumps:
  798. # 2) Option 'sets' value; trumps:
  799. # 3) Setting 'default'
  800. #
  801. print "#ifdef PNG_" i >out
  802. # PNG_<i> is defined, so substitute the value:
  803. print def i, subs "PNG_" i sube end >out
  804. print "#else /* use default */" >out
  805. print "# ifdef PNG_set_" i >out
  806. # Value from an option 'sets' argument
  807. print def i, subs "PNG_set_" i sube end >out
  808. # This is so that subsequent tests on the setting work:
  809. print "# define PNG_" i, "1" >out
  810. if (defaults[i] != "") {
  811. print "# else /*default*/" >out
  812. print def i deflt end >out
  813. print "# define PNG_" i, "1" >out
  814. }
  815. print "# endif /* defaults */" >out
  816. print "#endif /* setting", i, "*/" >out
  817. doneset[i] = 1
  818. ++movement
  819. }
  820. if (!finished && !movement) {
  821. print "setting: loop or missing setting in 'requires', cannot process:"
  822. for (i in setting) if (!doneset[i]) {
  823. print " setting", i, "requires" setting[i]
  824. }
  825. exit 1
  826. }
  827. }
  828. print "PNG_DFN_END_SORT" >out
  829. print comment, "end of settings", cend >out
  830. # Regular end - everything looks ok
  831. if (protect != "") {
  832. print start "#endif", "/*", protect, "*/" end >out
  833. }
  834. }