1# genout.cmake.in
2# Generate .out from .c with awk (generic), 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(CMAKE_C_COMPILER "@CMAKE_C_COMPILER@")
17set(CMAKE_C_FLAGS "@CMAKE_C_FLAGS@")
18set(INCDIR "@CMAKE_CURRENT_BINARY_DIR@")
19set(PNG_PREFIX "@PNG_PREFIX@")
20set(PNGLIB_MAJOR "@PNGLIB_MAJOR@")
21set(PNGLIB_MINOR "@PNGLIB_MINOR@")
22set(PNGLIB_VERSION "@PNGLIB_VERSION@")
23set(ZLIBINCDIR "@ZLIB_INCLUDE_DIR@")
24
25get_filename_component(INPUTEXT "${INPUT}" EXT)
26get_filename_component(OUTPUTEXT "${OUTPUT}" EXT)
27get_filename_component(INPUTBASE "${INPUT}" NAME_WE)
28get_filename_component(OUTPUTBASE "${OUTPUT}" NAME_WE)
29get_filename_component(INPUTDIR "${INPUT}" PATH)
30get_filename_component(OUTPUTDIR "${OUTPUT}" PATH)
31
32if ("${INPUTEXT}" STREQUAL ".c" AND "${OUTPUTEXT}" STREQUAL ".out")
33  get_filename_component(GENDIR "${OUTPUT}" PATH)
34  file(MAKE_DIRECTORY "${GENDIR}")
35
36  file(REMOVE "${OUTPUT}.tf1" "${OUTPUT}.tf2")
37
38  set(INCLUDES "-I${INCDIR}")
39  if(ZLIBINCDIR)
40    list(APPEND INCLUDES "-I${ZLIBINCDIR}")
41  endif()
42
43  if(PNG_PREFIX)
44    set(PNG_PREFIX_DEF "-DPNG_PREFIX=${PNG_PREFIX}")
45  endif()
46
47  execute_process(COMMAND "${CMAKE_C_COMPILER}" "-E"
48                          ${CMAKE_C_FLAGS}
49                          "-I${SRCDIR}"
50                          "-I${BINDIR}"
51                          ${INCLUDES}
52                          "-DPNGLIB_LIBNAME=PNG${PNGLIB_MAJOR}${PNGLIB_MINOR}_0"
53                          "-DPNGLIB_VERSION=${PNGLIB_VERSION}"
54                          "-DSYMBOL_PREFIX=${SYMBOL_PREFIX}"
55                          "-DPNG_NO_USE_READ_MACROS"
56                          "-DPNG_BUILDING_SYMBOL_TABLE"
57                          ${PNG_PREFIX_DEF}
58                          "${INPUT}"
59                  OUTPUT_FILE "${OUTPUT}.tf1"
60                  WORKING_DIRECTORY "${BINDIR}"
61                  RESULT_VARIABLE CPP_FAIL)
62  if(CPP_FAIL)
63    message(FATAL_ERROR "Failed to generate ${OUTPUT}.tf1")
64  endif()
65
66  execute_process(COMMAND "${AWK}" -f "${SRCDIR}/scripts/dfn.awk"
67                          "out=${OUTPUT}.tf2" "${OUTPUT}.tf1"
68                  WORKING_DIRECTORY "${BINDIR}"
69                  RESULT_VARIABLE AWK_FAIL)
70  if(AWK_FAIL)
71    message(FATAL_ERROR "Failed to generate ${OUTPUT}.tf2")
72  endif()
73
74  file(REMOVE "${OUTPUT}.tf1")
75  file(RENAME "${OUTPUT}.tf2" "${OUTPUT}")
76else()
77  message(FATAL_ERROR "Unsupported conversion: ${INPUTEXT} to ${OUTPUTEXT}")
78endif()
79