find_program: programs with shell and batch script in the same directory
Some tools install their Linux and Windows scripts in the same directory. This is possible because the shell script does not get an extension, and the batch script has the .bat extension, e.g.:
bin\cmake-bat-test (shell script)
bin\cmake-bat-test.bat (batch script)
With the bin directory in PATH, running "cmake-bat-test" on the command line will find bin\cmake-bat-test on Linux, and bin\cmake-bat-test.bat on Windows.
Now, the following code to execute cmake-bat-test from cmake will succeeds on Linux, but fails on Windows:
find_program(CMAKE_BAT_TEST cmake-bat-test)
if (CMAKE_BAT_TEST)
message("CMAKE_BAT_TEST: ${CMAKE_BAT_TEST}")
execute_process(COMMAND "${CMAKE_BAT_TEST}" RESULT_VARIABLE CMAKE_BAT_TEST_RESULT)
message("CMAKE_BAT_TEST_RESULT: ${CMAKE_BAT_TEST_RESULT}")
endif()
The output on Windows is:
CMAKE_BAT_TEST: C:\users\drizzd\bin\cmake-bat-test
CMAKE_BAT_TEST_RESULT: %1 is not a valid Win32 application
The error message is of course correct. The shell script at that exact path is not a valid Win32 application. We really want to execute cmake-bat-test.bat. We can avoid this problem by executing the batch script like this instead:
execute_process(COMMAND "${CMAKE_BAT_TEST}.bat")
execute_process(COMMAND cmd /c "${CMAKE_BAT_TEST}")
This requires a special case for the WIN32, however. It would be nice if find_program would (optionally) return the full path to the found executable, including the possible .bat or .exe extension. That way I can be sure to avoid such ambiguities. Would it be possible to implement this in Cmake?