Automatically determine build flags by parsing 'make -n' output #1167

This commit is contained in:
roel0 2018-03-19 21:55:59 +01:00
parent 68b9399d4c
commit c47b5fd4b8
3 changed files with 81 additions and 4 deletions

View File

@ -3,20 +3,27 @@
call ale#Set('c_clang_executable', 'clang') call ale#Set('c_clang_executable', 'clang')
call ale#Set('c_clang_options', '-std=c11 -Wall') call ale#Set('c_clang_options', '-std=c11 -Wall')
call ale#Set('c_gcc_parse_makefile', 0)
function! ale_linters#c#clang#GetExecutable(buffer) abort function! ale_linters#c#clang#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'c_clang_executable') return ale#Var(a:buffer, 'c_clang_executable')
endfunction endfunction
function! ale_linters#c#clang#GetCommand(buffer) abort function! ale_linters#c#clang#GetCommand(buffer) abort
let l:paths = ale#c#FindLocalHeaderPaths(a:buffer) let l:cflags = []
if g:ale_c_parse_makefile
let l:cflags = join(ale#c#ParseMakefile(a:buffer), ' ')
endif
if empty(l:cflags)
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
endif
" -iquote with the directory the file is in makes #include work for " -iquote with the directory the file is in makes #include work for
" headers in the same directory. " headers in the same directory.
return ale#Escape(ale_linters#c#clang#GetExecutable(a:buffer)) return ale#Escape(ale_linters#c#clang#GetExecutable(a:buffer))
\ . ' -S -x c -fsyntax-only ' \ . ' -S -x c -fsyntax-only '
\ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' ' \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' '
\ . ale#c#IncludeOptions(l:paths) \ . l:cflags . ' '
\ . ale#Var(a:buffer, 'c_clang_options') . ' -' \ . ale#Var(a:buffer, 'c_clang_options') . ' -'
endfunction endfunction

View File

@ -3,20 +3,27 @@
call ale#Set('c_gcc_executable', 'gcc') call ale#Set('c_gcc_executable', 'gcc')
call ale#Set('c_gcc_options', '-std=c11 -Wall') call ale#Set('c_gcc_options', '-std=c11 -Wall')
call ale#Set('c_parse_makefile', 0)
function! ale_linters#c#gcc#GetExecutable(buffer) abort function! ale_linters#c#gcc#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'c_gcc_executable') return ale#Var(a:buffer, 'c_gcc_executable')
endfunction endfunction
function! ale_linters#c#gcc#GetCommand(buffer) abort function! ale_linters#c#gcc#GetCommand(buffer) abort
let l:paths = ale#c#FindLocalHeaderPaths(a:buffer) let l:cflags = []
if g:ale_c_parse_makefile
let l:cflags = join(ale#c#ParseMakefile(a:buffer), ' ')
endif
if empty(l:cflags)
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
endif
" -iquote with the directory the file is in makes #include work for " -iquote with the directory the file is in makes #include work for
" headers in the same directory. " headers in the same directory.
return ale#Escape(ale_linters#c#gcc#GetExecutable(a:buffer)) return ale#Escape(ale_linters#c#gcc#GetExecutable(a:buffer))
\ . ' -S -x c -fsyntax-only ' \ . ' -S -x c -fsyntax-only '
\ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' ' \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' '
\ . ale#c#IncludeOptions(l:paths) \ . l:cflags . ' '
\ . ale#Var(a:buffer, 'c_gcc_options') . ' -' \ . ale#Var(a:buffer, 'c_gcc_options') . ' -'
endfunction endfunction

View File

@ -22,6 +22,69 @@ function! ale#c#FindProjectRoot(buffer) abort
return '' return ''
endfunction endfunction
function! ale#c#ParseCFlags(project_root, stdout_make) abort
let l:cflags_list = []
let l:cflags = split(a:stdout_make, " ")
let l:shell_option = 0
let l:macro_option = 0
let l:previous_option = ''
for l:option in l:cflags
" Check if cflag contained spaces
if l:shell_option || stridx(l:option, "=`") >= 0
" Cflag contained shell command with spaces (ex. -D='date +%s')
let l:shell_option = 1
let l:previous_option .= l:option . ' '
if l:option[-1: -1] != "`"
continue
endif
let l:shell_option = 0
elseif l:macro_option || stridx(l:option, "$((") > 0
" Cflag contained macro with spaces (ex -Da=$(( 4 * 20 )))
let l:macro_option = 1
let l:previous_option .= l:option . ' '
if stridx(l:option, "))") < 0
continue
endif
let l:macro_option = 0
endif
if l:previous_option != ''
let l:option = l:previous_option
let l:previous_option = ''
endif
" Fix relative paths if needed
if stridx(l:option, "-I") >= 0
if stridx(l:option, "-I/") < 0
let l:option = '-I' . a:project_root . s:sep . l:option[2:]
endif
endif
" Parse the cflag
if stridx(l:option, "-I") >= 0 ||
\ stridx(l:option, "-D") >= 0
if index(l:cflags_list, l:option) < 0
call add(l:cflags_list, l:option)
endif
endif
endfor
return l:cflags_list
endfunction
function! ale#c#ParseMakefile(buffer) abort
let l:project_root = ale#c#FindProjectRoot(a:buffer)
let l:project_cflags = []
if !empty(l:project_root)
if !empty(globpath(l:project_root, 'Makefile', 0))
let stdout_make = system('cd '. l:project_root . ' && make -n')
for l:object in split(l:stdout_make, '\n')
if stridx(l:object, expand("%t"))
return ale#c#ParseCFlags(l:project_root, l:object)
endif
endfor
endif
endif
return []
endfunction
" Given a buffer number, search for a project root, and output a List " Given a buffer number, search for a project root, and output a List
" of directories to include based on some heuristics. " of directories to include based on some heuristics.
" "