CMake + atom + .clang_complete

A few months ago I moved from emacs to atom that looked like a more modern replacement. Most important for me – it had awesome CMake code completion that was very good. I soon made use of automplete and lint with clang. Both were good, but needed a .clang_autocomplete in your project root to work properly. Apparently if your project file get a bunch of compiler flags from the build-system managing this file by hand is not something I wanted, so I ended up with this snipplet in CMake that gets the job done:

message(STATUS "Generarating ${CMAKE_SOURCE_DIR}/.clang_complete")
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
file(WRITE ${CMAKE_SOURCE_DIR}/.clang_complete "")
foreach(dir ${dirs})
  file(APPEND ${CMAKE_SOURCE_DIR}/.clang_complete "-I${dir}\n")
endforeach()
string(REPLACE "'" "" CMAKE_C_FLAGS_SPLIT ${CMAKE_C_FLAGS})
string(REPLACE " " ";" CMAKE_C_FLAGS_SPLIT ${CMAKE_C_FLAGS_SPLIT})
foreach(flag ${CMAKE_C_FLAGS_SPLIT})
  file(APPEND ${CMAKE_SOURCE_DIR}/.clang_complete "${flag}\n")
endforeach()

If you use C++ in your project you need to use the CMAKE_CXX_FLAGS variable. This code also has the obvious limitation: If you pass via -D directive options that have spaces – this won’t quite work (e.g. -DRELEASE_CODENAME=”Black Burned Cookies”)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.