nickdiego / compiledb

Tool for generating Clang's JSON Compilation Database files for make-based build systems.
GNU General Public License v3.0
1.36k stars 122 forks source link

compiledb for clang and/or MinGW GCC generates empty compile_commands.json #96

Open wireless-road opened 4 years ago

wireless-road commented 4 years ago

Hi! Have pretty simple C project containing just one source file hello.c:

#include <stdio.h>

int main()
{
    printf("Hello world!\n");    
    return 0;
}

here is Makefilefor this project:

clang:
    @echo 'clangg'
    clang.exe -IC:\tools\MinGW\i686-w64-mingw32\include hello.c -v -o hello.exe

mingw:
    C:\tools\MinGW\bin\gcc.exe -IC:\tools\MinGW\i686-w64-mingw32\include hello.c -v -o hello.exe

I've compiledbinstalled by: pip install compiledb

So, when I'm trying to run compiledb make clang: It shows me following output:

clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: c:\Program Files\LLVM\bin
 "c:\\Program Files\\LLVM\\bin\\clang.exe" -cc1 -triple x86_64-pc-windows-msvc19.24.28315 -emit-obj -mrelax-all -mincremental-linker-compatible -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.c -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -momit-leaf-frame-pointer -v -resource-dir "c:\\Program Files\\LLVM\\lib\\clang\\9.0.0" -I "C:\\tools\\MinGW\\i686-w64-mingw32\\include" -internal-isystem "c:\\Program Files\\LLVM\\lib\\clang\\9.0.0\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\include" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\ucrt" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\shared" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\um" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\winrt" -fdebug-compilation-dir "C:\\cms_new\\cms_iot_sw_package\\sources\\iot\\hello\\src" -ferror-limit 19 -fmessage-length 210 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.24.28315 -fdelayed-template-parsing -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o "C:\\Users\\ALMAZ~1.KHA\\AppData\\Local\\Temp\\hello-c576e2.o" -x c hello.c
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
 C:\tools\MinGW\i686-w64-mingw32\include
 c:\Program Files\LLVM\lib\clang\9.0.0\include
 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include
 C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt
 C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared
 C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um
 C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt
End of search list.
 "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\Hostx64\\x64\\link.exe" -out:hello.exe -defaultlib:libcmt "-libpath:C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\lib\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\ucrt\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\um\\x64" -nologo "C:\\Users\\ALMAZ~1.KHA\\AppData\\Local\\Temp\\hello-c576e2.o"

But generated compile_commands.json contains just square brackets: []

Same if I try to compile by compiledb make mingw.

So is compiledb can work at Windows OS?

TheGreatRambler commented 4 years ago

I have the same error, but in my case, the file is completely empty.

EDIT: I fixed it by running a fake build, ie with the -n command. This could solve your problem.

wireless-road commented 4 years ago

Unfortunately, -n didn't help. I run compiledb from source with enabled debug info and it shows me following:

## Processing build commands from <fdopen>
Line 1: Failed to parse build command [Details: (<class 'bashlex.tokenizer.MatchedPairError'>) unexpected EOF while looking for matching '`' (position 78)]. Ignoring: 'make: Entering directory `/c/sw_package/sources/iot/hello/src''
New command: C:\tools\MinGW\bin\gcc.exe -IC:\tools\MinGW\i686-w64-mingw32\include hello.c -v --debug -o hello.exe
Line 3: Failed to parse build command [Details: (<class 'bashlex.tokenizer.MatchedPairError'>) unexpected EOF while looking for matching '`' (position 77)]. Ignoring: 'make: Leaving directory `/c/sw_package/sources/iot/hello/src''
## Loaded compilation database with 0 entries from compile_commands.json
## Writing compilation database with 0 entries to compile_commands.json
## Done.

Seems that quote symbol before /c/sw_package breaks everything but when I run compiledb make mingw from command line I didn't see any Entering directory and Leaving directory messages at all. Can't understand how to fix that.

TheGreatRambler commented 4 years ago

Can't clang generate compile_commands.json files natively?

misterb0407 commented 4 years ago

I have the same issue, my compile_commands.json just show: []

sonulohani commented 4 years ago

Same with me. I am getting compile_commands.json with only [] in Windows

J-keaper commented 4 years ago

I encountered the same error on MacOS 10.15. I fix this through using make | compiledb instead of compiledb make. See this Issue

imran27 commented 4 years ago

Same issue with me too.

I am working on a huge project that generally uses a couple of different compilers to compile for different embedded platforms, all on a remote ubuntu machine.

All I wanted to do is have a compilation db to get proper autocompletion, code jumping/navigation etc.

I exported the make -Bnwk output to a build.log, copied it to my local machine and used compiledb.exe --parse build.log and it didn't work. Also tried cat build.log | compiledb.exe for each of the following cases.

I tried removing all binary characters, removing the entire path of compilers and replacing them with just gcc/clang/CC & g++/clang++/CXX, no avail.

I keep getting just [] in my compiled_commands.json

I hope there is some workaround.

andrewstevens-infineon commented 3 years ago

I ran into this Issue working under msys on a Windows host. As a python package compiledb relies on the standard python pathname processing built-ins. These, of course, do not handle the special pathname conventions Posix shell environments like MSYS or Cygwin use to represent Windows drive-letters.

As a result joining the current working dir and the filename path captured from make does not produce a correct legal windows pathname. This causes the check that the file corresponding to the joined pathname exists to fail(unless you've specifiied -S) no entry is generated. Even if you specify -S the pathnames are mostly mangled/useless.

I've created a PR (#110) with a small patch that fixes this. Feedback from folks using cygwin etc welcome!

AJigsawnHalo commented 3 years ago

Same issue here. compiledb only produces a compile_commands.json that only contains [ ] when run in a build folder. Interestingly when I ran it at the project's root folder it produces a compile_commands.json file that contains an entry for the main.cpp file in there but not for .cpp files inside other folders.

Also I tried running compiledb on the same project inside WSL2 and it works perfectly.

aqilc commented 1 year ago

I also have this issue. Is there any way compiledb can support MSVC? I would love to see some sort of output after trying at least 5 diffirent ways to try to generate this illusive file.

ericLemanissier commented 1 year ago

I noticed that compiledb puts in compilation database only the files which are at the root of the project, but not the files which are in subdirectories. I guess this could be because it does not handle properly windows' path separator \ ?

Rixium commented 1 year ago

I had the same issue. Removing the .exe after my compiler path worked for me.

You'll have to add it back in manually to the generated compile_commands.json after though.

Your makefile would become:

clang:
    @echo 'clangg'
    clang -IC:\tools\MinGW\i686-w64-mingw32\include hello.c -v -o hello.exe

mingw:
    C:\tools\MinGW\bin\gcc -IC:\tools\MinGW\i686-w64-mingw32\include hello.c -v -o hello.exe
aljohnston112 commented 3 months ago

I have this issue building a package with a custom Makefile for EDK2.

Here it is in case it helps.

all: build

.PHONY: build

build: Application/BootLib \
       FFPkg.dec \
       FFPkg.dsc
    cd .. && \
    . ./edksetup.sh && \
    build && \
    cp ./Build/FFPkg/DEBUG_GCC/X64/FFPkg.efi ./Build/EmulatorX64/DEBUG_GCC5/X64/FFFinger.efi && \
    echo "run" > gdb_commands && \
    echo "y" >> gdb_commands && \
    EmulatorPkg/build.sh run < gdb_commands && \
    rm gdb_commands

I did remove the gdb run, and it still did not work. The pipe method did not work because of some custom logging not working.

Turns out the build command does not work with compiledb either

aljohnston112 commented 3 months ago

I found a great solution on StackOverflow

make clean;make VERBOSE=y all &> make_output.txt
compiledb --parse make_output.txt

https://stackoverflow.com/questions/21134120/how-to-turn-makefile-into-json-compilation-database