cmake: Fix the build on Unix with source files checked out on Windows

The issue is that, by default, Git for Windows checks out text files
with CRLF line endings. This is a problem for awk, which is expecting
Unix-style LF line endings. When cloning on Windows and attempting to
compile on WSL, Mingw or Cygwin, there may be an error from awk.

The fix is to leverage CMake's ability to configure a file and perform
EOL conversions. We copy the awk scripts from the source directory to
the build directory. This portable method ensures they have LF endings,
and the build logic is updated to use the build directory version.

Intentionally avoiding .gitattributes to avoid setting precedent.

Co-authored-by: Christopher Sean Morrison <brlcad@gmail.com>
Co-authored-by: Cosmin Truta <ctruta@gmail.com>
Signed-off-by: Cosmin Truta <ctruta@gmail.com>
This commit is contained in:
Christopher Sean Morrison 2022-03-10 01:08:09 -05:00 committed by Cosmin Truta
parent 8a354b41e9
commit 840af2eda6
2 changed files with 20 additions and 4 deletions

View File

@ -18,6 +18,7 @@
# Revised by Alex Gaynor, 2020 # Revised by Alex Gaynor, 2020
# Revised by Owen Rudge, 2020 # Revised by Owen Rudge, 2020
# Revised by Gleb Mazovetskiy, 2021 # Revised by Gleb Mazovetskiy, 2021
# Revised by Christopher Sean Morrison, 2022
# This code is released under the libpng license. # This code is released under the libpng license.
# For conditions of distribution and use, see the disclaimer # For conditions of distribution and use, see the disclaimer
@ -293,6 +294,20 @@ if(NOT AWK OR ANDROID OR IOS)
${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h) ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
add_custom_target(genfiles) # Dummy add_custom_target(genfiles) # Dummy
else() else()
# Copy the awk scripts, converting their line endings to Unix (LF)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/checksym.awk
${CMAKE_CURRENT_BINARY_DIR}/scripts/checksym.awk
@ONLY
NEWLINE_STYLE LF)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/options.awk
${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk
@ONLY
NEWLINE_STYLE LF)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/dfn.awk
${CMAKE_CURRENT_BINARY_DIR}/scripts/dfn.awk
@ONLY
NEWLINE_STYLE LF)
# Generate .chk from .out with awk: # Generate .chk from .out with awk:
# generate_chk(INPUT inputfile OUTPUT outputfile [DEPENDS dep1 [dep2...]]) # generate_chk(INPUT inputfile OUTPUT outputfile [DEPENDS dep1 [dep2...]])
include(CMakeParseArguments) include(CMakeParseArguments)
@ -377,14 +392,14 @@ else()
# Generate scripts/pnglibconf.h # Generate scripts/pnglibconf.h
generate_source(OUTPUT "scripts/pnglibconf.c" generate_source(OUTPUT "scripts/pnglibconf.c"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa"
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/options.awk" "${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h") "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h")
add_custom_target(scripts_pnglibconf_c DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/pnglibconf.c") add_custom_target(scripts_pnglibconf_c DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/pnglibconf.c")
# Generate pnglibconf.c # Generate pnglibconf.c
generate_source(OUTPUT "pnglibconf.c" generate_source(OUTPUT "pnglibconf.c"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa"
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/options.awk" "${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h") "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h")
add_custom_target(pnglibconf_c DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c") add_custom_target(pnglibconf_c DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c")
@ -446,7 +461,7 @@ else()
generate_chk(INPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out" generate_chk(INPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk"
DEPENDS scripts_symbols_out DEPENDS scripts_symbols_out
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/checksym.awk" "${CMAKE_CURRENT_BINARY_DIR}/scripts/checksym.awk"
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/symbols.def") "${CMAKE_CURRENT_SOURCE_DIR}/scripts/symbols.def")
add_custom_target(scripts_symbols_chk add_custom_target(scripts_symbols_chk

View File

@ -10,6 +10,7 @@
# Variables substituted from CMakeLists.txt # Variables substituted from CMakeLists.txt
set(SRCDIR "@CMAKE_CURRENT_SOURCE_DIR@") set(SRCDIR "@CMAKE_CURRENT_SOURCE_DIR@")
set(BINDIR "@CMAKE_CURRENT_BINARY_DIR@")
set(AWK "@AWK@") set(AWK "@AWK@")
@ -23,7 +24,7 @@ get_filename_component(OUTPUTDIR "${OUTPUT}" PATH)
if("${INPUTEXT}" STREQUAL ".out" AND "${OUTPUTEXT}" STREQUAL ".chk") if("${INPUTEXT}" STREQUAL ".out" AND "${OUTPUTEXT}" STREQUAL ".chk")
# Generate .chk from .out with awk (generic) # Generate .chk from .out with awk (generic)
file(REMOVE "${OUTPUT}" "${OUTPUTDIR}/${OUTPUTBASE}.new") file(REMOVE "${OUTPUT}" "${OUTPUTDIR}/${OUTPUTBASE}.new")
execute_process(COMMAND "${AWK}" -f "${SRCDIR}/scripts/checksym.awk" execute_process(COMMAND "${AWK}" -f "${BINDIR}/scripts/checksym.awk"
"${SRCDIR}/scripts/${INPUTBASE}.def" "${SRCDIR}/scripts/${INPUTBASE}.def"
"of=${OUTPUTDIR}/${OUTPUTBASE}.new" "of=${OUTPUTDIR}/${OUTPUTBASE}.new"
"${INPUT}" "${INPUT}"