Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active October 2, 2024 16:11
Show Gist options
  • Save scivision/af5b4da75830d56e56a9e28cbf7a5428 to your computer and use it in GitHub Desktop.
Save scivision/af5b4da75830d56e56a9e28cbf7a5428 to your computer and use it in GitHub Desktop.
find_program vis CMAKE_EXECUTABLE_SUFFIX

CMake find_program with non .exe suffix on Windows

More details

Because Windows programs may not necessarily be executable and numerous extensions for scripts exist on Windows e.g. .vbs etc. .bat file are not searched by find_program() unless explicitly specified as in this example. If a script (say a Bash script) exists with the same name, find_program() may find that first on Windows, even if the script wouldn't work on Windows.

If you know a program publishes both a non-Windows script without filename suffix and with a .bat suffix, it's best to be specific for Windows with the filename including suffix as in this example.

Reveal the search behavior with:

cmake -Bbuild --fresh --debug-find-var=scr
cmake_minimum_required(VERSION 3.19)
project(dummy LANGUAGES NONE)
# --- create test script
set(name "helloUniqueName1234")
set(content "echo \"hello\"\n")
if(WIN32)
string(APPEND name ".bat")
else()
string(PREPEND content "#!/bin/sh\n")
endif()
file(CONFIGURE OUTPUT ${name} CONTENT "${content}" @ONLY)
file(CHMOD ${CMAKE_CURRENT_BINARY_DIR}/${name} FILE_PERMISSIONS OWNER_READ OWNER_EXECUTE)
# --- find test script
find_program(scr
NAMES ${name}
PATHS ${CMAKE_CURRENT_BINARY_DIR}
REQUIRED
)
message(STATUS "script found: ${scr}")
# --- example of testing
enable_testing()
add_test(NAME test COMMAND ${scr})
set_property(TEST test PROPERTY PASS_REGULAR_EXPRESSION "hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment