From 0073788bfead1239d563e93f60dc8021bbf3dfaf Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 8 Oct 2018 15:15:43 +0200 Subject: Use compdb tool to include header files in compile_commands.json --- README.md | 2 +- backend/ninja/Ninja.cpp | 43 +++++++++++++++++++++++++++++++++---------- src/main.cpp | 4 ++++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ab43024..8312fc7 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Cross compilation currently only works from linux64 to win64 by using mingw-w64. Cross compilation does currently not work if you have zig files as zig doesn't support libc when cross compiling at the moment. You can run `scripts/mingw_package.py` to automatically copy dynamic library dependencies of your executable to the same directory as the executable, so the library can be found when running the executable on windows; this also allows you to bundle your application and distribute it without external dependencies. To run `scripts/mingw_package.py` you need to install pefile python library `sudo pip install pefile`. # IDE support -Sibs generates a compile_commands.json in the project root directory when executing `sibs build` and tools that support clang completion can be used, such as YouCompleteMe. +Sibs generates a compile_commands.json in the project root directory when executing `sibs build` and tools that support clang completion can be used, such as YouCompleteMe. To generate compile_commands.json that also finds header files (non-relative) you need to have compdb installed and availabile in your PATH environment variable: https://github.com/Sarcasm/compdb There are several editors that support YouCompleteMe, including Vim, Emacs and Visual Studio Code. Visual studio code now also supports clang completion with C/C++ extension by Microsoft; the extension will ask you which compile_commands.json file you want to use and you can choose the compile_commands.json in the project root directory. # Tests If your project contains a sub directory called "tests" then that directory will be used a test project. The test directory may contain a project.conf file which can contain \[dependencies] block for specifying test only dependencies. The test automatically includes the parent project as a dependency. diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp index a212280..ef6c365 100644 --- a/backend/ninja/Ninja.cpp +++ b/backend/ninja/Ninja.cpp @@ -979,7 +979,6 @@ namespace backend compileCCommand.insert(compileCCommand.end(), { ninja::NinjaArg::createRaw("-MMD"), - ninja::NinjaArg("-I" + config.getPackageName() + "@exe"), ninja::NinjaArg::createRaw("$globalIncDir") }); @@ -1816,20 +1815,18 @@ namespace backend Result Ninja::buildCompilationDatabase(const _tinydir_char_t *buildFilePath, const FileString &saveDir) { + Result compdbAvailableResult = exec(TINYDIR_STRING("compdb version"), false); + bool isCompdbAvailable = (compdbAvailableResult && compdbAvailableResult.unwrap().exitCode == 0); + FileString command = TINYDIR_STRING("ninja -C \""); - command += buildFilePath; + command += (isCompdbAvailable ? buildFilePath : saveDir); command += TINYDIR_STRING("\" -t compdb compile_c compile_cpp compile_zig > \""); - command += saveDir; + command += buildFilePath; command += TINYDIR_STRING("/compile_commands.json\""); Result execResult = exec(command.c_str(), false); - if(execResult.isOk()) + if(execResult) { - if(execResult.unwrap().exitCode == 0) - { - //printf("%s\n", execResult.unwrap().execStdout.c_str()); - return Result::Ok(true); - } - else + if(execResult.unwrap().exitCode != 0) { string errMsg = "Failed to build compilation database, reason: "; errMsg += execResult.unwrap().execStdout; @@ -1842,5 +1839,31 @@ namespace backend errMsg += execResult.getErrMsg(); return Result::Err(errMsg); } + + if(isCompdbAvailable) + { + command = TINYDIR_STRING("compdb -p \""); + command += buildFilePath; + command += TINYDIR_STRING("\" list > \""); + command += saveDir; + command += TINYDIR_STRING("/compile_commands.json\""); + execResult = exec(command.c_str(), false); + if(execResult) + { + if(execResult.unwrap().exitCode != 0) + { + string errMsg = "Failed to build compilation database, reason: "; + errMsg += execResult.unwrap().execStdout; + return Result::Err(errMsg); + } + } + else + { + string errMsg = "Failed to build compilation database, reason: "; + errMsg += execResult.getErrMsg(); + return Result::Err(errMsg); + } + } + return Result::Ok(true); } } diff --git a/src/main.cpp b/src/main.cpp index a3a7ed1..b05cded 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,6 +89,10 @@ using namespace std::chrono; // TODO: Support packaging with musl to reduce number of libraries needed for the package and also to reduce package size. +// TODO: Remove dependency on compdb tool, implement compile_commands.json generation inside sibs. +// Also compile_commands.json shouldn't update if no files have changed. This is easier to do when ninja is replaced so we can track changes +// in source/header files. + #if OS_FAMILY == OS_FAMILY_POSIX #define fout std::cout #define ferr std::cerr -- cgit v1.2.3