1# gensrc.cmake.in
2# Generate source files with awk, based upon the automake logic.
3
4# Copyright (C) 2016 Glenn Randers-Pehrson
5# Written by Roger Leigh, 2016
6
7# This code is released under the libpng license.
8# For conditions of distribution and use, see the disclaimer
9# and license in png.h
10
11# Variables substituted from CMakeLists.txt
12set(SRCDIR "@CMAKE_CURRENT_SOURCE_DIR@")
13set(BINDIR "@CMAKE_CURRENT_BINARY_DIR@")
14
15set(AWK "@AWK@")
16set(DFA_XTRA "@DFA_XTRA@")
17set(PNG_PREFIX "@PNG_PREFIX@")
18set(PNGLIB_VERSION "@PNGLIB_VERSION@")
19
20if("${OUTPUT}" STREQUAL "scripts/pnglibconf.c")
21  # Generate scripts/pnglibconf.c
22
23  file(REMOVE "${BINDIR}/pnglibconf.tf6" "${BINDIR}/pnglibconf.tf7")
24
25  execute_process(COMMAND "${CMAKE_COMMAND}" -E echo "com ${PNGLIB_VERSION} STANDARD API DEFINITION"
26                  COMMAND "${AWK}" -f "${SRCDIR}/scripts/options.awk"
27                          "out=pnglibconf.tf6" "logunsupported=1" "version=search"
28                          "${SRCDIR}/pngconf.h" "-"
29                          "${SRCDIR}/scripts/pnglibconf.dfa"
30                  WORKING_DIRECTORY "${BINDIR}"
31                  RESULT_VARIABLE AWK_FAIL)
32  if(AWK_FAIL)
33    message(FATAL_ERROR "Failed to generate pnglibconf.tf6")
34  endif()
35
36  execute_process(COMMAND "${AWK}" -f "${SRCDIR}/scripts/options.awk"
37                  "out=pnglibconf.tf7" "pnglibconf.tf6"
38                  WORKING_DIRECTORY "${BINDIR}"
39                  RESULT_VARIABLE AWK_FAIL)
40  if(AWK_FAIL)
41    message(FATAL_ERROR "Failed to generate pnglibconf.tf7")
42  endif()
43
44  file(REMOVE "pnglibconf.tf6")
45  file(MAKE_DIRECTORY "${BINDIR}/scripts")
46  file(RENAME "pnglibconf.tf7" "${BINDIR}/scripts/pnglibconf.c")
47
48elseif ("${OUTPUT}" STREQUAL "pnglibconf.c")
49  # Generate pnglibconf.c
50
51  file(REMOVE "${BINDIR}/pnglibconf.tf4" "${BINDIR}/pnglibconf.tf5")
52
53  execute_process(COMMAND "${AWK}" -f "${SRCDIR}/scripts/options.awk"
54                  out=pnglibconf.tf4 version=search
55                  ${SRCDIR}/pngconf.h ${SRCDIR}/scripts/pnglibconf.dfa
56                  ${SRCDIR}/pngusr.dfa ${DFA_XTRA}
57                  WORKING_DIRECTORY "${BINDIR}"
58                  RESULT_VARIABLE AWK_FAIL)
59  if(AWK_FAIL)
60    message(FATAL_ERROR "Failed to generate pnglibconf.tf4")
61  endif()
62
63  execute_process(COMMAND "${AWK}" -f "${SRCDIR}/scripts/options.awk"
64                  out=pnglibconf.tf5 pnglibconf.tf4
65                  WORKING_DIRECTORY "${BINDIR}"
66                  RESULT_VARIABLE AWK_FAIL)
67  if(AWK_FAIL)
68    message(FATAL_ERROR "Failed to generate pnglibconf.tf5")
69  endif()
70
71  file(REMOVE "pnglibconf.tf4")
72  file(MAKE_DIRECTORY "${BINDIR}/scripts")
73  file(RENAME "pnglibconf.tf5" "${BINDIR}/pnglibconf.c")
74
75elseif ("${OUTPUT}" STREQUAL "pnglibconf.h")
76  # Generate pnglibconf.h
77
78  file(REMOVE "${BINDIR}/${OUTPUT}")
79  if(PNG_PREFIX)
80    file(REMOVE "pnglibconf.tf8")
81
82    execute_process(COMMAND "${AWK}" "s==0 && NR>1{print prev}
83                             s==0{prev=\$0}
84                             s==1{print \"#define\", \$1, \"${PNG_PREFIX}\" \$1}
85                             s==2{print \"#define ${PNG_PREFIX}png_\" \$1, \"PNG_\" \$1}
86                             END{print prev}" s=0 pnglibconf.out s=1 "${BINDIR}/scripts/prefix.out"
87                             s=2 "${SRCDIR}/scripts/macro.lst"
88                    OUTPUT_FILE pnglibconf.tf8
89                    RESULT_VARIABLE AWK_FAIL)
90    if(AWK_FAIL)
91      message(FATAL_ERROR "Failed to generate pnglibconf.tf8")
92    endif()
93
94    file(RENAME "pnglibconf.tf8" "${BINDIR}/${OUTPUT}")
95  else()
96    execute_process(COMMAND "${CMAKE_COMMAND}" -E copy "${BINDIR}/pnglibconf.out"
97                                                       "${BINDIR}/${OUTPUT}"
98                    RESULT_VARIABLE COPY_FAIL)
99    if(COPY_FAIL)
100      message(FATAL_ERROR "Failed to create pnglibconf.h")
101    endif()
102  endif()
103
104elseif ("${OUTPUT}" STREQUAL "pngprefix.h")
105  # Generate pngprefix.h
106
107  file(REMOVE "${BINDIR}/${OUTPUT}")
108
109  if(PNG_PREFIX)
110    file(REMOVE "pngprefix.tf1")
111
112    execute_process(COMMAND "${AWK}"
113                            "{print \"#define\", \$1, \"${PNG_PREFIX}\" \$1}"
114                            "${BINDIR}/scripts/intprefix.out"
115                    OUTPUT_FILE "pngprefix.tf1"
116                    RESULT_VARIABLE AWK_FAIL)
117    if(AWK_FAIL)
118      message(FATAL_ERROR "Failed to generate pngprefix.tf1")
119    endif()
120
121    file(RENAME "pngprefix.tf1" "${BINDIR}/${OUTPUT}")
122  else()
123    file(WRITE "${BINDIR}/${OUTPUT}" "/* No libpng symbol prefix configured. */")
124  endif()
125
126elseif("${OUTPUT}" STREQUAL "scripts/pnglibconf.h.prebuilt")
127  # Generate scripts/pnglibconf.h.prebuilt (fails build)
128
129  message(STATUS "Attempting to build scripts/pnglibconf.h.prebuilt")
130  message(STATUS "This is a machine generated file, but if you want to make")
131  message(STATUS "a new one simply build the 'genfiles' target, and copy")
132  message(STATUS "scripts/pnglibconf.out to scripts/pnglibconf.h.prebuilt")
133  message(STATUS "AND set PNG_ZLIB_VERNUM to 0 (you MUST do this)")
134  message(FATAL_ERROR "Stopping build")
135
136else()
137  message(FATAL_ERROR "Unsupported output: ${OUTPUT}")
138endif()
139