[CMake] How to get the evaluated path to a target - MSVC

David Cole david.cole at kitware.com
Sat May 23 07:37:53 EDT 2009


Dmitry, your suggestion will not work. He's asking how to achieve this from
a wrapper script that he has written where $(OutDir) does not evaluate in
the context of the script...
Steve, here's a technique that I use, but it has some caveats:
- it requires that the executable exist at the time of the script call
- it's a search for the exe in possible locations, so if multiple exist,
it's possible to get the "wrong" one

Anyhow, here it is:

# For an executable named "my"
#
# If you make this part of a cmake -P script, you will have to pass in
# CMAKE_BINARY_DIR and CMAKE_CONFIGURATION_TYPES with
# -D *before* the -P...
#
# If "my" does not exist in "bin" then find the first one that
# does exist in a configuration type subdir of "bin."
# my_CMAKE_CONFIGURATION_TYPES is a list of possible configuration
# types in "recommended" order. First existing one found wins.
#
SET(my_BASE_DIR "${CMAKE_BINARY_DIR}")
SET(my_CMAKE_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES})
#
# If you prefer a different search order than what is defined in
CMAKE_CONFIGURATION_TYPES,
# you may reorder them in my_CMAKE_CONFIGURATION_TYPES...

IF(NOT my_EXECUTABLE)
  SET(my_EXECUTABLE "${my_BASE_DIR}/bin/my${CMAKE_EXECUTABLE_SUFFIX}")
ENDIF(NOT my_EXECUTABLE)

IF(NOT EXISTS "${my_EXECUTABLE}")
  FOREACH(my_CONFIGURATION_TYPE ${my_CMAKE_CONFIGURATION_TYPES})
    IF(NOT EXISTS "${my_EXECUTABLE}")
    IF(EXISTS
"${my_BASE_DIR}/bin/${my_CONFIGURATION_TYPE}/my${CMAKE_EXECUTABLE_SUFFIX}")
      SET(my_EXECUTABLE
"${my_BASE_DIR}/bin/${my_CONFIGURATION_TYPE}/my${CMAKE_EXECUTABLE_SUFFIX}")
    ENDIF(EXISTS
"${my_BASE_DIR}/bin/${my_CONFIGURATION_TYPE}/my${CMAKE_EXECUTABLE_SUFFIX}")
    ENDIF(NOT EXISTS "${my_EXECUTABLE}")
  ENDFOREACH(my_CONFIGURATION_TYPE)
ENDIF(NOT EXISTS "${my_EXECUTABLE}")

MESSAGE(STATUS "my_EXECUTABLE='${my_EXECUTABLE}'")


Good luck!

Hope this helps,
David



On Sat, May 23, 2009 at 2:35 AM, Dmitry Bely <dmitry.bely at gmail.com> wrote:

> On Fri, May 22, 2009 at 11:46 PM, Steve Huston <shuston at riverace.com>
> wrote:
> > Hi John,
> >
> > Thanks for replying.
> >
> >> On Fri, May 22, 2009 at 12:18 PM, Steve Huston
> >> <shuston at riverace.com> wrote:
> >> > I'm using cmake for some cross-platform Linux/Windows stuff. When
> >> > ctest runs my tests, there is often the need to have a wrapper script
> >> > run the test - it sets up env variables (sometimes by reading the
> >> > content of a file), runs the test, scans log files, runs valgrind,
> >> > etc. So, I'm passing the actual test exe name to the script, and the
> >> > script runs it at the proper time.
> >> >
> >> > This works fine on Linux. On Windows, however, I'm having a problem
> >> > getting the actual path - when I get the LOCATION property, it has
> >> > $(OutDir) embedded. Visual Studio can substitute this in if
> > ctest/VS
> >> > is directly executing the test. However, if passed to the wrapper
> >> > script, the VS OutDir variable is not available. Is there a portable
> >> > way to get the test executable's path (either relative or complete) so
> >> > I can pass it to the wrapper script?
> >>
> >> Shouldn't you already know the path in your CMakeLists.txt?
> >>
> >> It should be somewhere off of
> >> ${PROJECT_BINARY_DIR}
> >
> > "somewhere off of" is the operative phrase... Exactly where is what I
> > need to know. For example, in a Windows Debug build, it's in
> > ${PROJECT_BINARY_DIR}\Debug.
>
> Why not to use something like this:
>
> if(MSVC_IDE)
>  set(out_dir "$(OutDir)") # expanded by Visual Studio
> else(MSVC_IDE)
>  set(out_dir ${PROJECT_BINARY_DIR})
> endif(MSVC_IDE)
>
> Nmake generator (instead of Visual Studio one) could be another option.
>
> - Dmitry Bely
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090523/30fb9602/attachment-0001.htm>


More information about the CMake mailing list