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
25set(PLATFORM_C_FLAGS)
26if(APPLE)
27  set(CMAKE_OSX_ARCHITECTURES "@CMAKE_OSX_ARCHITECTURES@")
28  set(CMAKE_OSX_SYSROOT "@CMAKE_OSX_SYSROOT@")
29  if(CMAKE_OSX_ARCHITECTURES)
30    set(PLATFORM_C_FLAGS ${PLATFORM_C_FLAGS} -arch ${CMAKE_OSX_ARCHITECTURES})
31  endif()
32  if(CMAKE_OSX_SYSROOT)
33    set(PLATFORM_C_FLAGS ${PLATFORM_C_FLAGS} -isysroot ${CMAKE_OSX_SYSROOT})
34  endif()
35endif()
36
37get_filename_component(INPUTEXT "${INPUT}" EXT)
38get_filename_component(OUTPUTEXT "${OUTPUT}" EXT)
39get_filename_component(INPUTBASE "${INPUT}" NAME_WE)
40get_filename_component(OUTPUTBASE "${OUTPUT}" NAME_WE)
41get_filename_component(INPUTDIR "${INPUT}" PATH)
42get_filename_component(OUTPUTDIR "${OUTPUT}" PATH)
43
44if ("${INPUTEXT}" STREQUAL ".c" AND "${OUTPUTEXT}" STREQUAL ".out")
45  get_filename_component(GENDIR "${OUTPUT}" PATH)
46  file(MAKE_DIRECTORY "${GENDIR}")
47
48  file(REMOVE "${OUTPUT}.tf1" "${OUTPUT}.tf2")
49
50  set(INCLUDES "-I${INCDIR}")
51  if(ZLIBINCDIR)
52    foreach(dir ${ZLIBINCDIR})
53      list(APPEND INCLUDES "-I${dir}")
54    endforeach()
55  endif()
56
57  if(PNG_PREFIX)
58    set(PNG_PREFIX_DEF "-DPNG_PREFIX=${PNG_PREFIX}")
59  endif()
60
61  execute_process(COMMAND "${CMAKE_C_COMPILER}" "-E"
62                          ${CMAKE_C_FLAGS}
63                          ${PLATFORM_C_FLAGS}
64                          "-I${SRCDIR}"
65                          "-I${BINDIR}"
66                          ${INCLUDES}
67                          "-DPNGLIB_LIBNAME=PNG${PNGLIB_MAJOR}${PNGLIB_MINOR}_0"
68                          "-DPNGLIB_VERSION=${PNGLIB_VERSION}"
69                          "-DSYMBOL_PREFIX=${SYMBOL_PREFIX}"
70                          "-DPNG_NO_USE_READ_MACROS"
71                          "-DPNG_BUILDING_SYMBOL_TABLE"
72                          ${PNG_PREFIX_DEF}
73                          "${INPUT}"
74                  OUTPUT_FILE "${OUTPUT}.tf1"
75                  WORKING_DIRECTORY "${BINDIR}"
76                  RESULT_VARIABLE CPP_FAIL)
77  if(CPP_FAIL)
78    message(FATAL_ERROR "Failed to generate ${OUTPUT}.tf1")
79  endif()
80
81  execute_process(COMMAND "${AWK}" -f "${SRCDIR}/scripts/dfn.awk"
82                          "out=${OUTPUT}.tf2" "${OUTPUT}.tf1"
83                  WORKING_DIRECTORY "${BINDIR}"
84                  RESULT_VARIABLE AWK_FAIL)
85  if(AWK_FAIL)
86    message(FATAL_ERROR "Failed to generate ${OUTPUT}.tf2")
87  endif()
88
89  file(REMOVE "${OUTPUT}.tf1")
90  file(RENAME "${OUTPUT}.tf2" "${OUTPUT}")
91else()
92  message(FATAL_ERROR "Unsupported conversion: ${INPUTEXT} to ${OUTPUTEXT}")
93endif()
94