MantisBT - CMake
View Issue Details
0014935CMakeCMakepublic2014-05-26 04:562016-06-10 14:31
th.thielemann 
Kitware Robot 
normalminoralways
closedmoved 
Vmware Player 6.0.2 on Win7Ubuntu12.04 LTS
 
 
0014935: file(APPEND "message...") writes nulls into file
I am using a shared folder between my Win7 and VmWare with Ubuntu 12.04.
Fore some reasons I write out some properties into a file during cmake run.
First line with file(WRITE "key=value"). All others with file(APPEND ...)
Sucessful tested on Windows, Linux using the local file system. But if I share a folder between Windows and Linux, the result is "00 00 00 00 ... 00 The appended message".
Create the environment Win7 + VmWare + Ubuntu 12.04 LTS + shared folder.

Here is my function

function(write_env_script Mode EnvKey EnvValue)
    # Possible option flags
    list(FIND ARGN EXTEND IsExtend)

    # Resulting script name
    if(CMAKE_HOST_WIN32)
        file(TO_NATIVE_PATH ${EnvValue} EnvValue)
    endif()
    set(ScriptName ${PROJECT_BINARY_DIR}/setprojectenv${SSP_SCRIPT_TYPE})

    # Reset mode to WRITE if file does not exist
    if(("${Mode}" STREQUAL "APPEND") AND (NOT EXISTS ${ScriptName}))
        set(Mode "WRITE")
    endif()

    # overwrite or extend the env variable's value
    set(EnvValueAlreadySet -1)
    if(NOT IsExtend EQUAL -1)
        if(NOT "$ENV{${EnvKey}}" STREQUAL "")
            if(CMAKE_HOST_UNIX)
                set(CurrentEnvValue "$ENV{${EnvKey}}:")
            elseif(CMAKE_HOST_WIN32)
                set(CurrentEnvValue "$ENV{${EnvKey}};")
            endif()
        endif()
        # Contains the env variable the value already?
        string(FIND "$ENV{${EnvKey}}" "${EnvValue}" EnvValueAlreadySet REVERSE)
    endif()

    if(EnvValueAlreadySet EQUAL -1)
        if(CMAKE_HOST_UNIX)
#### Error! Replace the following line with workaround
                        file(${Mode} ${ScriptName} "${PreviousScriptContent} export ${EnvKey}=${CurrentEnvValue}${EnvValue}\n" )
            execute_process(COMMAND chmod u+x ${ScriptName} WORKING_DIRECTORY "${PROJECT_BINARY_DIR}")
        elseif(CMAKE_HOST_WIN32)
            file(${Mode} ${ScriptName} "set ${EnvKey}=${CurrentEnvValue}${EnvValue}\n\r" )
        endif()
    endif()
endfunction()


The workaround for linux using VmWare share.
            if("${Mode}" STREQUAL "APPEND")
                file(READ ${ScriptName} PreviousScriptContent)
            endif()
            file(WRITE ${ScriptName} "${PreviousScriptContent} export ${EnvKey}=${CurrentEnvValue}${EnvValue}\n" )


Usage of the function:
    write_env_script(APPEND QT_PLUGIN_PATH "${QtLibs_PATH}/plugins")
    write_env_script(APPEND QML2_IMPORT_PATH "${QtLibs_PATH}/qml")
    write_env_script(APPEND PATH "${QtLibs_PATH}/bin" EXTEND)
CMake, linux, shared, Windows 7
Issue History
2014-05-26 04:56th.thielemannNew Issue
2014-05-26 07:22David ColeNote Added: 0035930
2014-05-26 08:50th.thielemannNote Added: 0035931
2014-05-26 08:53th.thielemannTag Attached: CMake
2014-05-26 08:53th.thielemannTag Attached: linux
2014-05-26 08:53th.thielemannTag Attached: shared
2014-05-26 08:53th.thielemannTag Attached: Windows 7
2016-06-10 14:29Kitware RobotNote Added: 0042552
2016-06-10 14:29Kitware RobotStatusnew => resolved
2016-06-10 14:29Kitware RobotResolutionopen => moved
2016-06-10 14:29Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0035930)
David Cole   
2014-05-26 07:22   
Are you certain the null characters are not coming from the value of some environment variable?
(0035931)
th.thielemann   
2014-05-26 08:50   
The values inserted are display on screen without any issues. In the case I use the workaround (Read previous file content into cmake variable, add the new content and write all back) everything is fine.
At first I assumed the reason is the file system only (the shared folder between Win7 and Linux). But I build in the shared folder too and no other file generated by cmake, compiler, linker shows this issue.
(0042552)
Kitware Robot   
2016-06-10 14:29   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.