From 5462c4ef4fb470c5878a46cb809203fc1459998f Mon Sep 17 00:00:00 2001 From: carllu Date: Fri, 5 Apr 2019 22:38:59 +0800 Subject: [PATCH] Add support to export compile_flags.txt --- xmake/plugins/project/clang/compile_flags.lua | 126 ++++++++++++++++++ xmake/plugins/project/main.lua | 2 + xmake/plugins/project/xmake.lua | 1 + 3 files changed, 129 insertions(+) create mode 100644 xmake/plugins/project/clang/compile_flags.lua diff --git a/xmake/plugins/project/clang/compile_flags.lua b/xmake/plugins/project/clang/compile_flags.lua new file mode 100644 index 00000000000..78e85e0b446 --- /dev/null +++ b/xmake/plugins/project/clang/compile_flags.lua @@ -0,0 +1,126 @@ +--!A cross-platform 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 - 2019, TBOOX Open Source Group. +-- +-- @author luzhlon +-- @file compile_flags.lua +-- + +-- imports +import("core.tool.compiler") +import("core.project.project") +import("core.language.language") + +-- make the object +function _make_object(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 = compiler.compflags(sourcefile, {target = target}) + for i, flag in ipairs(arguments) do + -- only export the -I*/-D* flags + if not g_flags[flag] and string.find(flag, '^-[ID]') then + g_flags[flag] = true + table.insert(g_flags, flag) + end + end + + -- clear first line marks + _g.firstline = false +end + +-- make each objects +function _make_each_objects(target, sourcekind, sourcebatch) + for index, objectfile in ipairs(sourcebatch.objectfiles) do + _make_object(target, sourcebatch.sourcefiles[index], objectfile) + end +end + +-- make single object +function _make_single_object(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(target) + + -- TODO + -- disable precompiled header first + target:set("pcheader", nil) + target:set("pcxxheader", nil) + + -- build source batches + for sourcekind, sourcebatch in pairs(target:sourcebatches()) do + if not sourcebatch.rulename then + if type(sourcebatch.objectfiles) == "string" then + _make_single_object(target, sourcekind, sourcebatch) + else + _make_each_objects(target, sourcekind, sourcebatch) + end + end + end +end + +-- make all +function _make_all() + -- make flags + _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(target) + end + end +end + +-- generate compilation databases for clang-based tools(compile_flags.txt) +-- +-- references: +-- - https://clang.llvm.org/docs/JSONCompilationDatabase.html +-- +function make(outputdir) + + -- enter project directory + local oldir = os.cd(os.projectdir()) + + -- make all + g_flags = {} + _make_all() + + -- write to file + local flagfile = io.open(path.join(outputdir, "compile_flags.txt"), "w") + for i, flag in ipairs(g_flags) do + flagfile:write(flag, '\n') + end + flagfile:close() + + -- leave project directory + os.cd(oldir) +end diff --git a/xmake/plugins/project/main.lua b/xmake/plugins/project/main.lua index fc55fd4a1d8..82fde03d8d7 100755 --- a/xmake/plugins/project/main.lua +++ b/xmake/plugins/project/main.lua @@ -38,6 +38,7 @@ import("vstudio.vs2013") import("vstudio.vs2015") import("vstudio.vs2017") import("vstudio.vs2019") +import("clang.compile_flags") import("clang.compile_commands") -- make project @@ -58,6 +59,7 @@ function _make(kind) , vs2015 = vs2015.make , vs2017 = vs2017.make , vs2019 = vs2019.make + , compile_flags = compile_flags.make , compile_commands = compile_commands.make } assert(maps[kind], "the project kind(%s) is not supported!", kind) diff --git a/xmake/plugins/project/xmake.lua b/xmake/plugins/project/xmake.lua index 78074567ed3..1d1fae16009 100644 --- a/xmake/plugins/project/xmake.lua +++ b/xmake/plugins/project/xmake.lua @@ -44,6 +44,7 @@ task("project") { {'k', "kind", "kv", "makefile", "Set the project kind." , " - makefile" + , " - compile_flags" , " - 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."