Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active June 8, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/135e83803ed29162817fce4098dec144 to your computer and use it in GitHub Desktop.
Save Mic92/135e83803ed29162817fce4098dec144 to your computer and use it in GitHub Desktop.
Ruby script to generate a .clang_complete file for NixOS using cmake's compilation database (compile_commands.json)
 $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .
 $ ruby gen-clang-complete.rb compile_commands.json
 $ cat compile_commands.json
-Iinclude
-I../include
-I/nix/store/5vcs34znmbsiwpc965jaab3ffvklxpf6-llvm-3.4.2/include
-I/nix/store/hw4i6ydaqxkdp6i94z6rfh90xklwslw7-z3-4.5.0/include
-I/nix/store/wfiz5lx24rr3r6c70523zw5rxv4pg38z-zlib-1.2.11-dev/include
-I/home/joerg/git/klee/include
-I/nix/store/9v42ppf2dpwc8247w34mywj89m0a1l88-googletest-release-1.8.0-src/googletest/include
-I/nix/store/9v42ppf2dpwc8247w34mywj89m0a1l88-googletest-release-1.8.0-src/googletest
-DKLEE_UCLIBC_BCA_NAME=\"klee-uclibc.bca\"
-D_GNU_SOURCE
-D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS
-D__STDC_LIMIT_MACROS
-DkleeRuntest_EXPORTS
-DKLEE_UCLIBC_BCA_NAME="klee-uclibc.bca"
-DGTEST_HAS_PTHREAD=1
-isystem /nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0/include/c++/5.4.0
-isystem /nix/store/dyj2k6ch35r1ips4vr97md2i0yvl4r5c-gcc-5.4.0/include/c++/5.4.0/x86_64-unknown-linux-gnu
#!/usr/bin/env ruby
require "json"
require "set"
def main()
if ARGV.size < 1
$stderr.puts "USAGE: #{$PROGRAM_NAME} compile_commands.json"
exit 1
end
data = JSON.load(open(ARGV[0]))
includes = Set.new
defs = Set.new
data.map do |entry|
args = entry["command"].split(/\s+/)
includes.merge(args.find_all {|v| v.start_with?("-I")} )
defs.merge(args.find_all {|v| v.start_with?("-D")} )
end
# Please pretend the following ugly hack does not exists and in the line after
# somehow we magically have C++ std header includes
cxx_stdlib = `eval echo $(nix-instantiate --eval --expr 'with (import <nixpkgs>) {}; clang.default_cxx_stdlib_compile')`
cxx_stdlib_args = cxx_stdlib.scan(/-isystem \S+/)
open(".clang_complete", "w+") do |f|
includes.each {|e| f.puts e}
defs.each {|e| f.puts e}
cxx_stdlib_args.each {|e| f.puts e}
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment