<div>
<br>
</div>
<div></div>
<p style="color: #A0A0A8;">On Wednesday, January 1, 2014 at 4:27 PM, Michi Henning wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px;">
<span><div><div><div>I would like to add a target to format my code with clang-format. However, clang-format isn't installed as "clang-format" (at least not on Ubuntu). Instead, it installs itself as "clang-format-3.4", "clang-format-3.5", etc.</div><div><br></div><div>Is there a reasonable way to locate the binary with the largest version number >= than some minimum version? For example, I'd like to find clang-format-3.5 or later (even if the next version happens to be "clang-format-4.0"), and then create a symlink in the build tree for other scripts to use.</div></div></div></span></blockquote><div>I am a CMake newbie (like, just this week), and this is only a partial solution (I think I could probably flesh it out if we knew what the version string would look like in all situations), but maybe something like this:</div><div><br></div><div>CMakeLists.txt:</div><div><br></div><div><div>set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})</div><div>find_package(ClangFormat)</div><div><br></div><div>if(CLANG_FORMAT_FOUND)</div><div> message("clang-format executable: ${CLANG_FORMAT_EXECUTABLE}")</div><div> message("clang-format version: ${CLANG_FORMAT_VERSION}")</div><div>else()</div><div> message("clang-format executable not found")</div><div>endif()</div></div><div><br></div><div>FindClangFormat.cmake (in the CMAKE_SOURCE_DIR):</div><div><div><br></div><div>string(REPLACE ":" ";" _PATH $ENV{PATH})</div><div>foreach(p ${_PATH})</div><div> file(GLOB cand ${p}/clang-format*)</div><div> if(cand)</div><div> set(CLANG_FORMAT_EXECUTABLE ${cand})</div><div> set(CLANG_FORMAT_FOUND ON)</div><div> execute_process(COMMAND ${CLANG_FORMAT_EXECUTABLE} -version OUTPUT_VARIABLE clang_out )</div><div> string(REGEX MATCH .*\(version[^\n]*\)\n version ${clang_out})</div><div> set(CLANG_FORMAT_VERSION ${CMAKE_MATCH_1})</div><div> break()</div><div> else()</div><div> set(CLANG_FORMAT_FOUND OFF)</div><div> endif()</div><div><br></div><div>endforeach()</div></div><div><br></div><div>Matt</div><div><br></div>