Untitled

 avatar
unknown
c_cpp
12 days ago
2.9 kB
6
Indexable
cmake_minimum_required(VERSION 3.8)
project(dummy_for_clangd)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(EXCLUDE_PATTERNS "test" "gtest" "mobile" "win32" "support" "private" "system" "setup" "wince" "version")

function(is_excluded result path)
    set(excluded FALSE)
    foreach(pattern ${EXCLUDE_PATTERNS})
        if(${path} MATCHES ".*/${pattern}(/|$)")
            set(excluded TRUE)
            break()
        endif()
    endforeach()
    set(${result} ${excluded} PARENT_SCOPE)
endfunction()

set(include_dirs "")
list(APPEND include_dirs "${CMAKE_SOURCE_DIR}")

file(GLOB_RECURSE all_dirs LIST_DIRECTORIES true RELATIVE ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/*")
foreach(dir ${all_dirs})
    if(IS_DIRECTORY "${CMAKE_SOURCE_DIR}/${dir}")
        is_excluded(should_exclude "${dir}")
        if(NOT should_exclude)
            list(APPEND include_dirs "${CMAKE_SOURCE_DIR}/${dir}")
        endif()
    endif()
endforeach()

file(GLOB_RECURSE all_source_files
    "${CMAKE_SOURCE_DIR}/*.c"
    "${CMAKE_SOURCE_DIR}/*.cc"
    "${CMAKE_SOURCE_DIR}/*.cpp"
    "${CMAKE_SOURCE_DIR}/*.cxx"
)

file(GLOB_RECURSE all_header_files
    "${CMAKE_SOURCE_DIR}/*.h"
    "${CMAKE_SOURCE_DIR}/*.hpp"
    "${CMAKE_SOURCE_DIR}/*.hxx"
    "${CMAKE_SOURCE_DIR}/*.inl"
)

set(valid_sources "")
foreach(src ${all_source_files})
    file(RELATIVE_PATH relpath "${CMAKE_SOURCE_DIR}" "${src}")
    is_excluded(should_exclude "${relpath}")
    if(NOT should_exclude)
        list(APPEND valid_sources "${src}")
    endif()
endforeach()

file(WRITE "${CMAKE_BINARY_DIR}/poc.cpp" "
#include <iostream>
int main() { 
    std::cout << \"poc target for clangd index generation\\n\";
    return 0; 
}
")
list(APPEND valid_sources "${CMAKE_BINARY_DIR}/poc.cpp")

add_executable(dummy_target ${valid_sources})

target_include_directories(dummy_target SYSTEM PUBLIC ${include_dirs})

set_target_properties(dummy_target PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED OFF
    CXX_EXTENSIONS OFF
    EXCLUDE_FROM_ALL TRUE
    EXCLUDE_FROM_DEFAULT_BUILD TRUE
)

message(STATUS "Include directories count: ${CMAKE_SOURCE_DIR}")
list(LENGTH include_dirs dir_count)
message(STATUS "Found ${dir_count} include directories")

file(WRITE "${CMAKE_SOURCE_DIR}/.clangd" "
CompileFlags:
  Add: [-Wall, -Wextra]
  
Index:
  Background: Build
  StandardLibrary: Yes
")

if(NOT EXISTS "${CMAKE_SOURCE_DIR}/.vscode")
    file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/.vscode")
endif()

file(WRITE "${CMAKE_SOURCE_DIR}/.vscode/settings.json" 
"{
    \"clangd.arguments\": [
        \"--compile-commands-dir=${CMAKE_BINARY_DIR}\",
        \"--header-insertion=iwyu\",
        \"--all-scopes-completion\",
        \"--completion-style=detailed\",
        \"--background-index\",
        \"--clang-tidy\",
        \"--pch-storage=memory\"
    ],
}")

message(STATUS "Configuration complete. Target created with ${dir_count} include directories")
Editor is loading...
Leave a Comment