MantisBT - CMake
View Issue Details
0015963CMakeCMakepublic2016-02-09 16:522016-06-10 14:21
Bruce Adams 
 
normalminoralways
closedno change required 
LinuxRedHat5, 6 & 7
CMake 3.4.3 
 
0015963: strange behaviour running readlink via execute_process
I'm trying to use readlink -f to get the absolute path to a shared library following all symlinks.

>readlink -f /opt/gcc4.9.3/lib64/libstdc++.so.6
/opt/gcc4.9.3/lib64/libstdc++.so.6.0.20

but when I do this in cmake it doesn't expand the full path E.g.

set(CPP11_PATH ${CMAKE_CURRENT_BINARY_DIR}/)
execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print $3; }" OUTPUT_VARIABLE LIBSTDCPP_PATH)
message("LIBSTDCPP_PATH=${LIBSTDCPP_PATH}")
execute_process(COMMAND readlink -f ${LIBSTDCPP_PATH} OUTPUT_VARIABLE LIBSTDCPP_ABSPATH)
message("LIBSTDCPP_ABSPATH=${LIBSTDCPP_ABSPATH}")

prints:

LIBSTDCPP_PATH=/opt/gcc4.9.3/lib64/libstdc++.so.6
LIBSTDCPP_ABSPATH=/opt/gcc4.9.3/lib64/libstdc++.so.6

This also happens if I wrap it in a shell script:

execute_process(COMMAND doreadlink.sh ${LIBSTDCPP_PATH} OUTPUT_VARIABLE LIBSTDCPP_ABSPATH2)
message("LIBSTDCPP_ABSPATH2=${LIBSTDCPP_ABSPATH2}")

>LIBSTDCPP_ABSPATH2=/opt/gcc4.9.3/lib64/libstdc++.so.6


cmake_minimum_required(VERSION 2.8)
project(TEST CXX)
CMake script as below

set(CPP11_PATH ${CMAKE_CURRENT_BINARY_DIR}/cpp11)
execute_process(COMMAND g++ ${CMAKE_CURRENT_SOURCE_DIR}/cpp11.cpp -o${CPP11_PATH})

execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print $3; }" OUTPUT_VARIABLE LIBSTDCPP_PATH)
message("LIBSTDCPP_PATH=${LIBSTDCPP_PATH}")
execute_process(COMMAND readlink -f ${LIBSTDCPP_PATH} OUTPUT_VARIABLE LIBSTDCPP_ABSPATH)
message("LIBSTDCPP_ABSPATH=${LIBSTDCPP_ABSPATH}")
The following is an effective work around

execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print $3; }" COMMAND xargs readlink -f OUTPUT_VARIABLE LIBSTDCPP_PATH)

Originally raised on stackoverflow:

http://stackoverflow.com/questions/35299970/strange-behaviour-with-readlink-under-cmake [^]
No tags attached.
Issue History
2016-02-09 16:52Bruce AdamsNew Issue
2016-02-09 16:56Bruce AdamsNote Added: 0040456
2016-02-09 17:52Bruce AdamsNote Added: 0040457
2016-02-10 08:55Brad KingStatusnew => resolved
2016-02-10 08:55Brad KingResolutionopen => no change required
2016-06-10 14:21Kitware RobotNote Added: 0041259
2016-06-10 14:21Kitware RobotStatusresolved => closed

Notes
(0040456)
Bruce Adams   
2016-02-09 16:56   
I can't think how this could be a bug. It must be some misunderstanding on my part as to how cmake works but it is not at all obvious what.
(0040457)
Bruce Adams   
2016-02-09 17:52   
This is user error not a bug. Please close. Sorry for wasting bandwidth!

awk "{ print $3; }"

adds a newline which causes readlink to return the original string rather than
expanding it.

awk "{ printf($3); }"

makes everything work as expected.
The same behaviour occurs using get_filename_component realpath.
(0041259)
Kitware Robot   
2016-06-10 14:21   
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.