MantisBT - CMake
View Issue Details
0015694CMakeCMakepublic2015-08-12 17:322016-01-04 11:52
Scott 
 
highmajoralways
closedno change required 
VS 2013Windows8.1
CMake 3.3 
 
0015694: STRING(LENGTH ...) fails when loading a file larger than 17 KB
Attached is our cmake command (makeheader.cmake), which loads in a text file and creates a C++ header during the compiler phase.

It works great until files larger than 17 KBs are used. The error it produces is very strange, it fails to return the size of the FILEDATA string, which should never be more than 40 bytes in size.

while(NOT DONE)
    FILE(READ ${FILENAME} FILEDATA LIMIT 20 OFFSET ${CURRENT_OFFSET} HEX)
    STRING(LENGTH ${FILEDATA} READLEN)
Using this code in a CMake project for VS 2013 x64:

foreach(COMPILED_IN_FILE ManualPages.txt)
    get_filename_component(BASENAME ${COMPILED_IN_FILE} NAME_WE)
    get_filename_component(EXT ${COMPILED_IN_FILE} EXT)
    string(SUBSTRING ${EXT} 1 1 CAMEL_CHAR)
    string(TOUPPER ${CAMEL_CHAR} CAMEL_CHAR)
    string(SUBSTRING ${EXT} 2 -1 EXT)
    set(OUT_HEADER "${BASENAME}${CAMEL_CHAR}${EXT}.h")
    set(OUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${OUT_HEADER})
    set(IN_PATH ${CMAKE_SOURCE_DIR}/NwConsole/${COMPILED_IN_FILE})
    
    list(APPEND HEADER_FILES ${OUT_PATH})
    
    add_custom_command(OUTPUT ${OUT_PATH}
        COMMAND ${CMAKE_COMMAND}
            -DFILENAME=${IN_PATH}
            -DDESTINATION=${OUT_PATH}
            -P "${CMAKE_SOURCE_DIR}/CMake/makeheader.cmake"
        MAIN_DEPENDENCY ${IN_PATH}
        DEPENDS ${CMAKE_SOURCE_DIR}/CMake/makeheader.cmake)
endforeach()

Use any text file you have that is more than 17 KBs in size. Even though the string read in from the file should never be more than 40 bytes, it eventually returns this error:

3> Generating ManualPagesTxt.h
3> CMake Error at C:/Dev/ngtrunk/src/CMake/makeheader.cmake:18 (STRING):
3> string sub-command LENGTH requires two arguments.
3>
3>
3>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets(170,5): error MSB6006: "cmd.exe" exited with code 1.
It fails on 3.3 and 2.8.12.2 as well.
No tags attached.
? makeheader.cmake (1,069) 2015-08-12 17:32
https://public.kitware.com/Bug/file/5507/makeheader.cmake
Issue History
2015-08-12 17:32ScottNew Issue
2015-08-12 17:32ScottFile Added: makeheader.cmake
2015-08-13 09:02Brad KingNote Added: 0039276
2015-08-13 09:02Brad KingStatusnew => resolved
2015-08-13 09:02Brad KingResolutionopen => no change required
2015-08-13 10:21ScottNote Added: 0039277
2016-01-04 11:52Robert MaynardNote Added: 0040114
2016-01-04 11:52Robert MaynardStatusresolved => closed

Notes
(0039276)
Brad King   
2015-08-13 09:02   
> STRING(LENGTH ${FILEDATA} READLEN)

When FILEDATA is empty this will expand to

 STRING(LENGTH READLEN)

which is the incorrect number of arguments. You need to quote the argument:

 STRING(LENGTH "${FILEDATA}" READLEN)

See the cmake-language(7) manual for details:

 http://www.cmake.org/cmake/help/v3.3/manual/cmake-language.7.html#command-arguments [^]
(0039277)
Scott   
2015-08-13 10:21   
You are right, it works now. Thanks
(0040114)
Robert Maynard   
2016-01-04 11:52   
Closing resolved issues that have not been updated in more than 4 months.