diff options
| author | ruki <[email protected]> | 2017-07-19 14:56:46 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-07-19 14:56:46 +0800 |
| commit | f22b46466ebed2df42379172a31107e071b99979 (patch) | |
| tree | cb8dbafadd4bfc0fa85c40b2c239c1c1b0f91bf4 /xmake/plugins | |
| parent | 8119b93a66582b3b2f0443f5984b7a0336c30c8f (diff) | |
export compile_commands.json for clang tools
Diffstat (limited to 'xmake/plugins')
| -rw-r--r-- | xmake/plugins/project/clang/compile_commands.lua | 128 | ||||
| -rw-r--r-- | xmake/plugins/project/main.lua | 22 | ||||
| -rw-r--r-- | xmake/plugins/project/makefile/makefile.lua | 4 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs200x.lua | 4 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs201x.lua | 4 | ||||
| -rw-r--r-- | xmake/plugins/project/xmake.lua | 3 |
6 files changed, 149 insertions, 16 deletions
diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua new file mode 100644 index 000000000..3b3b65105 --- /dev/null +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -0,0 +1,128 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file compile_commands.lua +-- + +-- imports +import("core.tool.compiler") +import("core.project.project") +import("core.language.language") + +-- make the object +function _make_object(jsonfile, target, sourcefile, objectfile) + + -- get the source file kind + local sourcekind = language.sourcekind_of(sourcefile) + + -- make the object for the *.o/obj? ignore it directly + if sourcekind == "obj" or sourcekind == "lib" then + return + end + + -- get compile arguments + local arguments = table.join(compiler.compargv(sourcefile, {target = target, sourcekind = sourcekind})) + + -- make body + jsonfile:printf( +[[%s{ + "directory": "%s", + "arguments": ["%s"], + "file": "%s", + "output": "%s" +}]], ifelse(_g.firstline, "", ",\n"), os.projectdir(), table.concat(arguments, "\", \""), sourcefile, objectfile) + + -- clear first line marks + _g.firstline = false +end + +-- make each objects +function _make_each_objects(jsonfile, target, sourcekind, sourcebatch) + for index, objectfile in ipairs(sourcebatch.objectfiles) do + _make_object(jsonfile, target, sourcebatch.sourcefiles[index], objectfile) + end +end + +-- make single object +function _make_single_object(jsonfile, target, sourcekind, sourcebatch) + + -- not supported now, ignore it directly + for _, sourcefile in ipairs(table.wrap(sourcebatch.sourcefiles)) do + cprint("${bright yellow}warning: ${default yellow}ignore[%s]: %s", target:name(), sourcefile) + end +end + +-- make target +function _make_target(jsonfile, target) + + -- build source batches + for sourcekind, sourcebatch in pairs(target:sourcebatches()) do + if type(sourcebatch.objectfiles) == "string" then + _make_single_object(jsonfile, target, sourcekind, sourcebatch) + else + _make_each_objects(jsonfile, target, sourcekind, sourcebatch) + end + end +end + +-- make all +function _make_all(jsonfile) + + -- make header + jsonfile:print("[") + + -- make commands + _g.firstline = true + for _, target in pairs(project.targets()) do + local isdefault = target:get("default") + if not target:isphony() and (isdefault == nil or isdefault == true) then + _make_target(jsonfile, target) + end + end + + -- make tailer + jsonfile:print("]") +end + +-- generate compilation databases for clang-based tools(compile_commands.json) +-- +-- references: +-- - https://clang.llvm.org/docs/JSONCompilationDatabase.html +-- - https://sarcasm.github.io/notes/dev/compilation-database.html +-- - http://eli.thegreenplace.net/2014/05/21/compilation-databases-for-clang-based-tools +-- +function make(outputdir) + + -- enter project directory + local oldir = os.cd(os.projectdir()) + + -- open the jsonfile + local jsonfile = io.open(path.join(outputdir, "compile_commands.json"), "w") + + -- make all + _make_all(jsonfile) + + -- close the jsonfile + jsonfile:close() + + -- leave project directory + os.cd(oldir) +end diff --git a/xmake/plugins/project/main.lua b/xmake/plugins/project/main.lua index 1b4e6efe1..c949fa70f 100644 --- a/xmake/plugins/project/main.lua +++ b/xmake/plugins/project/main.lua @@ -36,6 +36,7 @@ import("vstudio.vs2012") import("vstudio.vs2013") import("vstudio.vs2015") import("vstudio.vs2017") +import("clang.compile_commands") -- make project function _make(kind) @@ -43,16 +44,17 @@ function _make(kind) -- the maps local maps = { - makefile = makefile.make - , vs2002 = vs2002.make - , vs2003 = vs2003.make - , vs2005 = vs2005.make - , vs2008 = vs2008.make - , vs2010 = vs2010.make - , vs2012 = vs2012.make - , vs2013 = vs2013.make - , vs2015 = vs2015.make - , vs2017 = vs2017.make + makefile = makefile.make + , vs2002 = vs2002.make + , vs2003 = vs2003.make + , vs2005 = vs2005.make + , vs2008 = vs2008.make + , vs2010 = vs2010.make + , vs2012 = vs2012.make + , vs2013 = vs2013.make + , vs2015 = vs2015.make + , vs2017 = vs2017.make + , compile_commands = compile_commands.make } assert(maps[kind], "the project kind(%s) is not supported!", kind) diff --git a/xmake/plugins/project/makefile/makefile.lua b/xmake/plugins/project/makefile/makefile.lua index 09b6d2e0f..d036c7240 100644 --- a/xmake/plugins/project/makefile/makefile.lua +++ b/xmake/plugins/project/makefile/makefile.lua @@ -387,7 +387,7 @@ end function make(outputdir) -- enter project directory - local olddir = os.cd(project.directory()) + local oldir = os.cd(os.projectdir()) -- remove the log makefile first os.rm(_logfile()) @@ -402,5 +402,5 @@ function make(outputdir) makefile:close() -- leave project directory - os.cd(olddir) + os.cd(oldir) end diff --git a/xmake/plugins/project/vstudio/impl/vs200x.lua b/xmake/plugins/project/vstudio/impl/vs200x.lua index 2f07f35a5..2c35534fe 100644 --- a/xmake/plugins/project/vstudio/impl/vs200x.lua +++ b/xmake/plugins/project/vstudio/impl/vs200x.lua @@ -31,7 +31,7 @@ import("vs200x_vcproj") function make(outputdir, vsinfo) -- enter project directory - local olddir = os.cd(project.directory()) + local oldir = os.cd(project.directory()) -- init solution directory vsinfo.solution_dir = path.join(outputdir, "vs" .. vsinfo.vstudio_version) @@ -47,5 +47,5 @@ function make(outputdir, vsinfo) end -- leave project directory - os.cd(olddir) + os.cd(oldir) end diff --git a/xmake/plugins/project/vstudio/impl/vs201x.lua b/xmake/plugins/project/vstudio/impl/vs201x.lua index 1807e9845..12d7ae4fb 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x.lua @@ -132,7 +132,7 @@ end function make(outputdir, vsinfo) -- enter project directory - local olddir = os.cd(project.directory()) + local oldir = os.cd(project.directory()) -- init solution directory vsinfo.solution_dir = path.join(outputdir, "vs" .. vsinfo.vstudio_version) @@ -224,5 +224,5 @@ function make(outputdir, vsinfo) end -- leave project directory - os.cd(olddir) + os.cd(oldir) end diff --git a/xmake/plugins/project/xmake.lua b/xmake/plugins/project/xmake.lua index 4de77ea2b..382d82411 100644 --- a/xmake/plugins/project/xmake.lua +++ b/xmake/plugins/project/xmake.lua @@ -44,9 +44,12 @@ task("project") { {'k', "kind", "kv", "makefile", "Set the project kind." , " - makefile" + , " - compile_commands (clang compilation database with json format)" , " - vs2002, vs2003, vs2005, vs2008, vs2010, vs2012, vs2013, vs2015, vs2017" } , {'m', "modes", "kv", nil, "Set the project modes." , " .e.g " + , " - xmake project -k makefile" + , " - xmake project -k compile_commands" , " - xmake project -k vs2015 -m \"release,debug\"" } , {nil, "outputdir", "v", ".", "Set the output directory." } } |
