Search code examples
clangclangd

How to add OS-specific complier flags in .clangd file?


I configured clangd using nvim-lspconfig. Now I would like to enable AVX support for certain projects in Linux.

How can I detect OS type in .clangd file, and then add -mavx flag only when it is Linux?

CompileFlags:
  Add: [-xavx]

Solution

  • I think it would be good to do that by leveraging an external script or a build system which is able to detect OS and construct if. As for Makefile:

    arch := $(shell uname -m)
    
    ifeq ($(arch), arm64)
    define CLANG_FLAGS
    CompileFlags:
        Add: [-Wall]
    endef
    else
    define CLANG_FLAGS
    CompileFlags:
        Add: [-Wall, -mavx]
    endef
    endif
    
    config:
        @$(file > .clangd,$(CLANG_FLAGS))
    

    To generate an OS-specific .clangd, just run make config.