Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wireless-road opened this issue Jan 22, 2020 · 14 comments

Comments

@wireless-road
Copy link

wireless-road commented Jan 22, 2020

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 Makefile for 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 compiledb installed 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
Copy link

TheGreatRambler commented Jan 22, 2020

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
Copy link
Author

wireless-road commented Jan 22, 2020

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
Copy link

TheGreatRambler commented Jan 22, 2020

Can't clang generate compile_commands.json files natively?

@misterb0407
Copy link

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

@sonulohani
Copy link

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

@J-keaper
Copy link

J-keaper commented Jun 30, 2020

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

@imran27
Copy link

imran27 commented Aug 6, 2020

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
Copy link

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
Copy link

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
Copy link

aqilc commented Dec 21, 2022

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
Copy link

ericLemanissier commented Jan 6, 2023

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
Copy link

Rixium commented Mar 11, 2023

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
Copy link

aljohnston112 commented Jul 31, 2024

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
Copy link

aljohnston112 commented Jul 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests