Skip to content

Instantly share code, notes, and snippets.

@Hexlord
Last active May 16, 2020 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexlord/90b857047c527fd178dbfe5de39dd276 to your computer and use it in GitHub Desktop.
Save Hexlord/90b857047c527fd178dbfe5de39dd276 to your computer and use it in GitHub Desktop.
CMake notes regarding how to parse AST of your project via libclang if you use CMake MSVS toolchain with lots of dependencies
// One would want to fetch all include directories from all the targets your main executable is linked against (and executable itself)
// Then you need to provide WinSDK include paths (the ones MSVS uses)
// Finally feed them in format of -I<argN> args in function clang_parseTranslationUnit in your LIBCLANG_APP
// Important part of my CMakeLists.txt is provided below
// Find script is available here https://github.com/rpavlik/cmake-modules/blob/master/FindWindowsSDK.cmake
// Example of your LinkedTargets: it is identical to linking!
target_link_libraries (MainExecutable SDL2-static freetype icuuc cAudio sfmt OpenAL)
set(LinkedTargets MainExecutable SDL2-static freetype icuuc cAudio sfmt OpenAL)
set(includePaths "")
message("Iterating linked targets: ${LinkedTargets}")
foreach(Target ${LinkedTargets})
get_target_property(dirs ${Target} INCLUDE_DIRECTORIES)
if(dirs)
message("includes of target ${Target} are: ${dirs}")
foreach(dir ${dirs})
# message("include found: ${dir}")
list(APPEND includePaths ${dir})
endforeach()
endif()
endforeach()
find_package(WindowsSDK REQUIRED)
message("WinSDK found: ${WINDOWSSDK_LATEST_DIR}")
get_windowssdk_include_dirs(${WINDOWSSDK_LATEST_DIR} winsdk_dirs)
foreach(dir ${winsdk_dirs})
#if(NOT dir MATCHES ".*/ucrt.*")
message("WinSDK include found: ${dir}")
list(APPEND includePaths ${dir})
#endif()
endforeach()
set(CodeGenArgs "")
foreach(dir ${includePaths})
# message("Include: ${dir}")
set(CodeGenArgs "${CodeGenArgs} \"${dir}\"")
endforeach()
message(${CodeGenArgs})
// Or any other way you plan to execute your libclang AST parsing app :)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/Code Generation"
COMMAND "$<TARGET_FILE:LIBCLANG_APP>" "${CodeGenArgs}"
# MAIN_DEPENDENCY ${ARG_FILE}
COMMENT "Generating Code"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment