[CMake] Get configuration-dependant LOCATION property

Mateusz Loskot mateusz at loskot.net
Tue Oct 5 17:28:21 EDT 2010


On 05/10/10 08:10, Michael Wild wrote:
> 
> On 5. Oct, 2010, at 3:52 , Michael Hertling wrote:
> 
>> On 10/04/2010 11:15 PM, Mateusz Loskot wrote:
>>> On 04/10/10 14:24, Michael Hertling wrote:
>>>> On 10/04/2010 10:53 AM, Mateusz Loskot wrote:
>>>>> On 04/10/10 07:33, J Decker wrote:
>>>>>> CMAKE_INSTALL_CONFIG_NAME I don't know if there's a simple flag like
>>>>>> 'project supports multiple targets' so I have an if(MSVC) set(
>>>>>> MULTI_TARGET) endif()  .... if( MULTI_TARGET ) Install( targets ...
>>>>>> ${CMAKE_INSTALL_CONFIG_NAME ) else() isntall( ... ${CMAKE_BUILD_TYPE}
>>>>>
>>>>>
>>>>> I need it before firing up install procedures.
>>>>> I build.
>>>>> I run tests (here I need to know location of build output per target).
>>>>
>>>> Have you already taken a look at ADD_TEST()'s generator expressions like
>>>> $<TARGET_FILE_DIR:tgt>? AFAIK, they've been introduced quite exactly to
>>>> meet requirements such as yours.
>>>
>>> Hmm, I've seen this expressions, but I'm not sure how it can be useful
>>> because the expressions are used to generate test input.
>>>
>>> My problem is about getting property to generate environment
>>> for test before it is executed:
>>>
>>> In one subtree of my sources, I have mylib target building mylib.dll
>>> In another subtree, I have test. Next, I build and run test:
>>>
>>> add_executable(mytest test.cpp)
>>>
>>> target_link_libraries(mytest mylib_import.lib)
>>>
>>> add_test(mytest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/mytest)
>>>
>>> set_tests_properties(mytest PROPERTIES
>>>  ENVIRONMENT "PATH=c:\path\to\dir\with\mylib.dll\")
>>>
>>> I need to figure out this: "c:\path\to\dir\with\mylib.dll\"
>>
>> You might consider to use a script as a test driver which takes
>> the desired path and the test executable as parameters, e.g.
>>
>> ADD_TEST(mytest
>>    testdriver $<TARGET_FILE_DIR:mylib> $<TARGET_FILE:mytest>)
>>
>> with testdriver setting the PATH and executing mytest.
>>
>> Regards,
>>
>> Michael
> 
> Ah, yes, this is even better than my idea of configuring. So, you could have a CMake script like the following:
> 
> testdriver.cmake
> ----------------
> # check arguments LIBDIR, TESTEXE and WIN32
> foreach(var LIBDIR TESTEXE WIN32)
>   if(NOT DEFINED ${var})
>     message(FATAL_ERROR "${var} not defined")
>   endif()
> endforeach()
> 
> # on Windows set PATH
> if(WIN32)
>   set($ENV{PATH} "${LIBDIR};$ENV{PATH}")
> endif(WIN32)
> 
> # run TESTEXE
> execute_process(
>   COMMAND "${TESTEXE}"
>   RESULT_VARIABLE result)
> if(result)
>   message(FATAL_ERROR "Running ${TESTEXE} failed")
> endif()
> # EOF
> 
> CMakeLists.txt
> --------------
> ...
> add_test(NAME bar COMMAND
>   "${CMAKE_COMMAND}" 
>     -DLIBDIR=$<TARGET_FILE_DIR:foo> -DTESTEXE=$<TARGET_FILE:bar> -DWIN32=${WIN32}
>     -P "${CMAKE_CURRENT_SOURCE_DIR}/testdriver.cmake"
>   )
> ...
> # EOF
> 
> I hope this helps

Wow! I had no idea about such CMake capabilities of CMake command run
from CMake script and passing another CMake script to execute.

This is brilliant!

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
Member of ACCU, http://accu.org


More information about the CMake mailing list