Skip to content

Commit

Permalink
improve parse header deps
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Dec 23, 2020
1 parent b64f0d5 commit 243ddec
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 34 deletions.
8 changes: 8 additions & 0 deletions tests/projects/other/parse_headerdeps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


9 changes: 9 additions & 0 deletions tests/projects/other/parse_headerdeps/common/test1.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// ../common/test1.hpp
#include <iostream>

void f()
{
std::cout << "f()" << std::endl;
}


8 changes: 8 additions & 0 deletions tests/projects/other/parse_headerdeps/src/test1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test1.cpp
#include "common/test1.hpp"

int main(int argc, char** argv)
{
f();
return 0;
}
12 changes: 12 additions & 0 deletions tests/projects/other/parse_headerdeps/src/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- xmake.lua
-- global
add_rules('mode.debug', 'mode.release')

set_version('0.1.0')

set_kind('binary')
add_includedirs('..')
set_warnings('all')
--set_languages('cxx20')
target('test1')
add_files('test1.cpp')
72 changes: 58 additions & 14 deletions xmake/modules/private/tools/cl/parse_deps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,71 @@
import("core.project.project")
import("core.base.hashset")
import("parse_include")
import("core.tool.toolchain")

-- get $VCInstallDir
function _VCInstallDir()
local VCInstallDir = _g.VCInstallDir
if not VCInstallDir then
local msvc = toolchain.load("msvc")
if msvc then
local vcvars = msvc:config("vcvars")
if vcvars and vcvars.VCInstallDir then
VCInstallDir = vcvars.VCInstallDir
_g.VCInstallDir = VCInstallDir
end
end
end
return VCInstallDir
end

-- get $WindowsSdkDir
function _WindowsSdkDir()
local WindowsSdkDir = _g.WindowsSdkDir
if not WindowsSdkDir then
local msvc = toolchain.load("msvc")
if msvc then
local vcvars = msvc:config("vcvars")
if vcvars and vcvars.WindowsSdkDir then
WindowsSdkDir = vcvars.WindowsSdkDir
_g.WindowsSdkDir = WindowsSdkDir
end
end
end
return WindowsSdkDir
end


-- normailize path of a dependecy
function _normailize_dep(dep, projectdir)
if path.is_absolute(dep) then
dep = path.translate(dep)
else
dep = path.absolute(dep, projectdir)
end
local VCInstallDir = _VCInstallDir()
local WindowsSdkDir = _WindowsSdkDir()
if (VCInstallDir and dep:startswith(VCInstallDir)) or (WindowsSdkDir and dep:startswith(WindowsSdkDir)) then
-- we ignore headerfiles in vc install directory
return
end
if dep:startswith(projectdir) then
return path.relative(dep, projectdir)
else
-- we need also check header files outside project
-- https://github.com/xmake-io/xmake/issues/1154
return dep
end
end

-- parse depsfiles from string
function main(depsdata)

-- translate it
local results = hashset.new()
for _, line in ipairs(depsdata:split("\n", {plain = true})) do

-- get includefile
local includefile = parse_include(line:trim())
if includefile then

-- get the relative
includefile = path.relative(includefile, project.directory())
includefile = path.absolute(includefile)

-- save it if belong to the project
if includefile:startswith(os.projectdir()) then

-- insert it and filter repeat
includefile = path.relative(includefile, project.directory())
includefile = _normailize_dep(includefile, os.projectdir())
if includefile then
results:insert(includefile)
end
end
Expand Down
68 changes: 58 additions & 10 deletions xmake/modules/private/tools/cl/parse_deps_json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,62 @@
import("core.project.project")
import("core.base.hashset")
import("core.base.json")
import("core.tool.toolchain")

-- get $VCInstallDir
function _VCInstallDir()
local VCInstallDir = _g.VCInstallDir
if not VCInstallDir then
local msvc = toolchain.load("msvc")
if msvc then
local vcvars = msvc:config("vcvars")
if vcvars and vcvars.VCInstallDir then
VCInstallDir = vcvars.VCInstallDir:lower() -- @note we need lower case for json/deps
_g.VCInstallDir = VCInstallDir
end
end
end
return VCInstallDir
end

-- get $WindowsSdkDir
function _WindowsSdkDir()
local WindowsSdkDir = _g.WindowsSdkDir
if not WindowsSdkDir then
local msvc = toolchain.load("msvc")
if msvc then
local vcvars = msvc:config("vcvars")
if vcvars and vcvars.WindowsSdkDir then
WindowsSdkDir = vcvars.WindowsSdkDir:lower() -- @note we need lower case for json/deps
_g.WindowsSdkDir = WindowsSdkDir
end
end
end
return WindowsSdkDir
end

-- normailize path of a dependecy
function _normailize_dep(dep, projectdir)
if path.is_absolute(dep) then
dep = path.translate(dep)
else
dep = path.absolute(dep, projectdir)
end
dep = dep:lower()
local VCInstallDir = _VCInstallDir()
local WindowsSdkDir = _WindowsSdkDir()
if (VCInstallDir and dep:startswith(VCInstallDir)) or (WindowsSdkDir and dep:startswith(WindowsSdkDir)) then
-- we ignore headerfiles in vc install directory
return
end
if dep:startswith(projectdir) then
return path.relative(dep, projectdir)
else
-- we need also check header files outside project
-- https://github.com/xmake-io/xmake/issues/1154
return dep
end
end

-- parse depsfiles from string
function main(depsdata)
Expand All @@ -39,16 +95,8 @@ function main(depsdata)
local results = hashset.new()
local projectdir = os.projectdir():lower() -- we need generate lower string, because json values are all lower
for _, includefile in ipairs(includes) do

-- get the absolute path
if not path.is_absolute(includefile) then
includefile = path.absolute(includefile, projectdir):lower()
end

-- save it if belong to the project
if includefile:startswith(projectdir) then
-- insert it and filter repeat
includefile = path.relative(includefile, projectdir)
includefile = _normailize_dep(includefile, projectdir)
if includefile then
results:insert(includefile)
end
end
Expand Down
8 changes: 4 additions & 4 deletions xmake/modules/private/tools/gcc/parse_deps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ local space_placeholder = "\001"

-- normailize path of a dependecy
function _normailize_dep(dep, projectdir)

-- tranlate dep path
if path.is_absolute(dep) then
dep = path.translate(dep)
else
dep = path.absolute(dep, projectdir)
end

-- save it if belong to the project
if dep:startswith(projectdir) then
return path.relative(dep, projectdir)
else
-- we need also check header files outside project
-- https://github.com/xmake-io/xmake/issues/1154
return dep
end
end

Expand Down
6 changes: 2 additions & 4 deletions xmake/modules/private/tools/rc/parse_deps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ import("core.base.hashset")

-- normailize path of a dependecy
function _normailize_dep(dep, projectdir)

-- tranlate dep path
if path.is_absolute(dep) then
dep = path.translate(dep)
else
dep = path.absolute(dep, projectdir)
end

-- save it if belong to the project
if dep:startswith(projectdir) then
return path.relative(dep, projectdir)
else
return deps
end
end

Expand Down
4 changes: 2 additions & 2 deletions xmake/rules/qt/install/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ function main(target, opt)
local msvc = toolchain.load("msvc", {plat = target:plat(), arch = target:arch()})
if msvc then
local vcvars = msvc:config("vcvars")
if vcvars and vcvars.VSInstallDir then
envs = {VCINSTALLDIR = path.join(vcvars.VSInstallDir, "VC")}
if vcvars and vcvars.VCInstallDir then
envs = {VCINSTALLDIR = vcvars.VCInstallDir}
end
end

Expand Down

0 comments on commit 243ddec

Please sign in to comment.