From fada840909bf5e61e3d5c8bed84722166f5adda0 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 4 Aug 2017 15:37:44 +0800 Subject: improve precompiled header --- xmake/plugins/project/vstudio/impl/vs200x.lua | 3 +- xmake/plugins/project/vstudio/impl/vs201x.lua | 15 ++-- .../project/vstudio/impl/vs201x_vcxproj.lua | 97 +++++++++++++--------- .../vstudio/impl/vs201x_vcxproj_filters.lua | 23 ++--- 4 files changed, 77 insertions(+), 61 deletions(-) (limited to 'xmake/plugins/project/vstudio/impl') diff --git a/xmake/plugins/project/vstudio/impl/vs200x.lua b/xmake/plugins/project/vstudio/impl/vs200x.lua index 6a14613d7..10207467b 100644 --- a/xmake/plugins/project/vstudio/impl/vs200x.lua +++ b/xmake/plugins/project/vstudio/impl/vs200x.lua @@ -42,7 +42,8 @@ function make(outputdir, vsinfo) -- TODO -- disable precompiled header first for _, target in pairs(project.targets()) do - target:set("precompiled_header", nil) + target:set("pcheader", nil) + target:set("pcxxheader", nil) end -- make vsprojs diff --git a/xmake/plugins/project/vstudio/impl/vs201x.lua b/xmake/plugins/project/vstudio/impl/vs201x.lua index 7b470e94e..bf638d031 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x.lua @@ -47,9 +47,11 @@ function _make_targetinfo(mode, arch, target) targetinfo.sdkver = (vcvarsall[arch] or {}).sdkver end - -- save precompiled header file (.pch) - targetinfo.pcheaderfile = target:pcheaderfile() - target:set("precompiled_header", nil) + -- save c/c++ precompiled output file (.pch) + targetinfo.pcoutputfile = target:pcoutputfile("c") + targetinfo.pcxxoutputfile = target:pcoutputfile("cxx") + target:set("pcheader", nil) + target:set("pcxxheader", nil) -- save symbols targetinfo.symbols = target:get("symbols") @@ -197,10 +199,9 @@ function make(outputdir, vsinfo) targets[targetname] = targets[targetname] or {} local _target = targets[targetname] - -- save precompiled header and source - local precompiled_header, precompiled_source = target:pcsourcefile() - _target.pcheader = precompiled_header -- header.h - _target.pcsourcefile = precompiled_source -- header.cpp + -- save c/c++ precompiled header + _target.pcheader = target:pcheaderfile("c") -- header.h + _target.pcxxheader = target:pcheaderfile("cxx") -- header.[hpp|inl] -- init target info _target.name = targetname diff --git a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua index d302b1bf4..7abe20a54 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua @@ -24,6 +24,7 @@ -- imports import("core.project.config") +import("core.language.language") import("vsfile") -- make compiling flags @@ -376,26 +377,16 @@ function _make_common_item(vcxprojfile, vsinfo, target, targetinfo, vcxprojdir) vcxprojfile:print("%s", path.relative(path.absolute(symbolfile), vcxprojdir)) end - -- use precompiled header - if target.pcheader then - - -- get precompiled header file - local pcheader = path.filename(target.pcheader) - if target.pcsourcefile and os.isfile(target.pcsourcefile) then - local sourcedata = io.readfile(target.pcsourcefile) - if sourcedata then - local includefile = sourcedata:match("#include%s+[<\"](.+)[>\"]") - if includefile then - pcheader = includefile - end - end - end - + -- use c or c++ precompiled header + local pcheader = target.pcxxheader or target.pcheader + if pcheader then + -- make precompiled header and outputfile vcxprojfile:print("Use") - vcxprojfile:print("%s", pcheader) - if targetinfo.pcheaderfile then - vcxprojfile:print("%s", path.relative(path.absolute(targetinfo.pcheaderfile), vcxprojdir)) + vcxprojfile:print("%s", path.filename(pcheader)) + local pcoutputfile = targetinfo.pcxxoutputfile or targetinfo.pcoutputfile + if pcoutputfile then + vcxprojfile:print("%s", path.relative(path.absolute(pcoutputfile), vcxprojdir)) end end @@ -476,7 +467,7 @@ function _make_header_file(vcxprojfile, includefile, vcxprojdir) end -- make source file for all modes -function _make_source_file_forall(vcxprojfile, vsinfo, sourcefile, sourceinfo, vcxprojdir) +function _make_source_file_forall(vcxprojfile, vsinfo, target, sourcefile, sourceinfo, vcxprojdir) -- get object file and source kind local sourcekind = nil @@ -542,10 +533,20 @@ function _make_source_file_forall(vcxprojfile, vsinfo, sourcefile, sourceinfo, v end end + -- disable the precompiled header if sourcekind ~= headerkind + local pcheader = target.pcxxheader or target.pcheader + local pcheader_disable = false + if pcheader and language.sourcekind_of(sourcefile) ~= ifelse(target.pcxxheader, "cxx", "cc") then + pcheader_disable = true + end + -- all modes and archs exist? if count == #vsinfo.modes then if #key > 0 then vcxprojfile:print("<%s>%s", itemname, iteminfo.value(key), itemname) + if pcheader_disable then + vcxprojfile:print("NotUsing") + end end else for cond, _ in pairs(mergeinfos) do @@ -553,11 +554,17 @@ function _make_source_file_forall(vcxprojfile, vsinfo, sourcefile, sourceinfo, v -- for mode | arch if #key > 0 then vcxprojfile:print("<%s Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s\'\">%s", itemname, cond, iteminfo.value(key), itemname) + if pcheader_disable then + vcxprojfile:print("NotUsing", cond) + end end else -- only for mode if #key > 0 then vcxprojfile:print("<%s Condition=\"\'%$(Configuration)\'==\'%s\'\">%s", itemname, cond, iteminfo.value(key), itemname) + if pcheader_disable then + vcxprojfile:print("NotUsing", cond) + end end end end @@ -571,7 +578,7 @@ function _make_source_file_forall(vcxprojfile, vsinfo, sourcefile, sourceinfo, v end -- make source file for specific modes -function _make_source_file_forspec(vcxprojfile, vsinfo, sourcefile, sourceinfo, vcxprojdir) +function _make_source_file_forspec(vcxprojfile, vsinfo, target, sourcefile, sourceinfo, vcxprojdir) -- add source file sourcefile = path.relative(path.absolute(sourcefile), vcxprojdir) @@ -590,6 +597,12 @@ function _make_source_file_forspec(vcxprojfile, vsinfo, sourcefile, sourceinfo, -- for *.c/cpp files else + + -- disable the precompiled header if sourcekind ~= headerkind + local pcheader = target.pcxxheader or target.pcheader + if pcheader and language.sourcekind_of(sourcefile) ~= ifelse(target.pcxxheader, "cxx", "cc") then + vcxprojfile:print("NotUsing") + end vcxprojfile:print("%s", objectfile) vcxprojfile:print("%s %%(AdditionalOptions)", os.args(info.flags)) end @@ -600,18 +613,31 @@ function _make_source_file_forspec(vcxprojfile, vsinfo, sourcefile, sourceinfo, end -- make source file for precompiled header -function _make_source_file_forpch(vcxprojfile, vsinfo, pcsourcefile, targetinfo, vcxprojdir) +function _make_source_file_forpch(vcxprojfile, vsinfo, target, vcxprojdir) -- add precompiled source file - local sourcefile = path.relative(path.absolute(pcsourcefile), vcxprojdir) - vcxprojfile:enter("", sourcefile) - vcxprojfile:print("Create") - vcxprojfile:print(" %%(AdditionalOptions)") - for _, info in ipairs(targetinfo) do - local objectfile = path.relative(path.absolute(info.pcheaderfile .. ".obj"), vcxprojdir) - vcxprojfile:print("%s", info.mode, info.arch, objectfile) - end - vcxprojfile:leave("") + local pcheader = target.pcxxheader or target.pcheader + if pcheader then + local sourcefile = path.relative(path.absolute(pcheader), vcxprojdir) + vcxprojfile:enter("", sourcefile) + vcxprojfile:print("Create") + vcxprojfile:print("") + vcxprojfile:print(" %%(AdditionalOptions)") + for _, info in ipairs(target.info) do + + -- compile as c/c++ + local compileas = ifelse(target.pcxxheader, "CompileAsCpp", "CompileAsC") + vcxprojfile:print("%s", info.mode, info.arch, compileas) + + -- add object file + local pcoutputfile = info.pcxxoutputfile or info.pcoutputfile + if pcoutputfile then + local objectfile = path.relative(path.absolute(pcoutputfile .. ".obj"), vcxprojdir) + vcxprojfile:print("%s", info.mode, info.arch, objectfile) + end + end + vcxprojfile:leave("") + end end -- make source files @@ -639,16 +665,14 @@ function _make_source_files(vcxprojfile, vsinfo, target, vcxprojdir) -- make source files for sourcefile, sourceinfo in pairs(sourceinfos) do if #sourceinfo == #target.info then - _make_source_file_forall(vcxprojfile, vsinfo, sourcefile, sourceinfo, vcxprojdir) + _make_source_file_forall(vcxprojfile, vsinfo, target, sourcefile, sourceinfo, vcxprojdir) else - _make_source_file_forspec(vcxprojfile, vsinfo, sourcefile, sourceinfo, vcxprojdir) + _make_source_file_forspec(vcxprojfile, vsinfo, target, sourcefile, sourceinfo, vcxprojdir) end end -- make precompiled source file - if target.pcsourcefile then - _make_source_file_forpch(vcxprojfile, vsinfo, target.pcsourcefile, target.info, vcxprojdir) - end + _make_source_file_forpch(vcxprojfile, vsinfo, target, vcxprojdir) vcxprojfile:leave("") @@ -657,9 +681,6 @@ function _make_source_files(vcxprojfile, vsinfo, target, vcxprojdir) for _, includefile in ipairs(target.headerfiles) do _make_header_file(vcxprojfile, includefile, vcxprojdir) end - if target.pcheader then - _make_header_file(vcxprojfile, target.pcheader, vcxprojdir) - end vcxprojfile:leave("") end diff --git a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua index c2432b4df..f15fa9c5f 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua @@ -98,13 +98,14 @@ function _make_sources(filtersfile, vsinfo, target, vcxprojdir) filtersfile:print("%s", filter) filtersfile:leave("", ifelse(as, "CustomBuild", "ClCompile")) end - end - if target.pcsourcefile then - local filter = _make_filter(target.pcsourcefile, target, vcxprojdir) - if filter then - filtersfile:enter("", path.relative(path.absolute(target.pcsourcefile), vcxprojdir)) - filtersfile:print("%s", filter) - filtersfile:leave("") + local pcheader = target.pcxxheader or target.pcheader + if pcheader then + local filter = _make_filter(pcheader, target, vcxprojdir) + if filter then + filtersfile:enter("", path.relative(path.absolute(pcheader), vcxprojdir)) + filtersfile:print("%s", filter) + filtersfile:leave("") + end end end filtersfile:leave("") @@ -123,14 +124,6 @@ function _make_headers(filtersfile, vsinfo, target, vcxprojdir) filtersfile:leave("") end end - if target.pcheader then - local filter = _make_filter(target.pcheader, target, vcxprojdir) - if filter then - filtersfile:enter("", path.relative(path.absolute(target.pcheader), vcxprojdir)) - filtersfile:print("%s", filter) - filtersfile:leave("") - end - end filtersfile:leave("") end -- cgit v1.3.1