Added copylocal() API and NoCopyLocal build flag to control assembly reference copying in C# projects

This commit is contained in:
Jason Perkins 2013-08-11 09:51:19 -04:00
parent 4d653106e6
commit f94ccff6d0
6 changed files with 173 additions and 7 deletions

View File

@ -277,16 +277,22 @@
-- C# doesn't support per-configuration links (does it?) so just use
-- the settings from the first available config instead
local links = config.getlinks(project.getfirstconfig(prj), "system", "fullpath")
for _, link in ipairs(links) do
if link:find("/", nil, true) then
_x(2,'<Reference Include="%s">', path.getbasename(link))
_x(3,'<HintPath>%s</HintPath>', path.translate(link))
local cfg = project.getfirstconfig(prj)
config.getlinks(cfg, "system", function(original, decorated)
if decorated:find("/", nil, true) then
_x(2,'<Reference Include="%s">', path.getbasename(decorated))
_x(3,'<HintPath>%s</HintPath>', path.translate(decorated))
if not config.isCopyLocal(prj, original, true) then
_p(3,"<Private>False</Private>")
end
_p(2,'</Reference>')
else
_x(2,'<Reference Include="%s" />', path.getbasename(link))
_x(2,'<Reference Include="%s" />', path.getbasename(decorated))
end
end
end)
_p(1,'</ItemGroup>')
end
@ -306,6 +312,11 @@
_x(2,'<ProjectReference Include="%s">', path.translate(relpath))
_p(3,'<Project>{%s}</Project>', dep.uuid)
_x(3,'<Name>%s</Name>', dep.name)
if not config.isCopyLocal(prj, dep.name, true) then
_p(3,"<Private>False</Private>")
end
_p(2,'</ProjectReference>')
end
end

View File

@ -614,6 +614,13 @@
kind = "string-list",
}
api.register {
name = "copylocal",
scope = "config",
kind = "mixed-list",
tokens = true,
}
api.register {
name = "debugargs",
scope = "config",
@ -711,6 +718,7 @@
"MultiProcessorCompile",
"NativeWChar",
"No64BitChecks",
"NoCopyLocal",
"NoEditAndContinue",
"NoExceptions",
"NoFramePointer",

View File

@ -274,6 +274,9 @@
-- directory - just the directory, no name
-- fullpath - full path with decorated name
-- object - return the project object of the dependency
-- Or, a function(original, decorated) can be supplied, in which case it
-- will be called for each matching link, providing the original value as
-- it was specified in links(), and the decorated value.
-- @param linkage
-- Optional. For languages or environments that support different kinds of
-- linking (i.e. Managed/CLR C++, which can link both managed and unmanaged
@ -350,6 +353,8 @@
item = path.getname(item)
elseif part == "basename" then
item = path.getbasename(item)
elseif type(part) == "function" then
part(link, item)
end
end
@ -403,3 +408,34 @@
function config.gettargetinfo(cfg)
return buildtargetinfo(cfg, cfg.kind, "target")
end
--
-- Determine if the specified library or assembly reference should be copied
-- to the build's target directory. "Copy Local" is the terminology used by
-- Visual Studio C# projects for this feature.
--
-- @param cfg
-- The configuration to query. Can be a project (and will be for C#
-- projects).
-- @param linkname
-- The name of the library or assembly reference to check. This should
-- match the name as it was provided in the call to links().
-- @param default
-- The value to return if the library is not mentioned in any settings.
-- @return
-- True if the library should be copied local, false otherwise.
--
function config.isCopyLocal(cfg, linkname, default)
if cfg.flags.NoCopyLocal then
return false
end
if #cfg.copylocal > 0 then
return table.contains(cfg.copylocal, linkname)
end
return default
end

View File

@ -822,6 +822,7 @@
end
--
-- Returns true if the project uses a C/C++ language.
--

View File

@ -88,3 +88,57 @@
</ItemGroup>
]]
end
--
-- The assembly should not be copied to the target directory if the
-- NoCopyLocal flag has been set for the configuration.
--
function suite.markedPrivate_onNoCopyLocal()
links { "../Libraries/nunit.framework" }
flags { "NoCopyLocal" }
prepare()
test.capture [[
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\Libraries\nunit.framework.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
]]
end
--
-- If there are entries in the copylocal() list, then only those
-- specific libraries should be copied.
--
function suite.markedPrivate_onCopyLocalListExclusion()
links { "../Libraries/nunit.framework" }
copylocal { "SomeOtherProject" }
prepare()
test.capture [[
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\Libraries\nunit.framework.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
]]
end
function suite.notMarkedPrivate_onCopyLocalListInclusion()
links { "../Libraries/nunit.framework" }
copylocal { "../Libraries/nunit.framework" }
prepare()
test.capture [[
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\Libraries\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
]]
end

View File

@ -94,3 +94,59 @@
]]
end
--
-- The assembly should not be copied to the target directory if the
-- NoCopyLocal flag has been set for the configuration.
--
function suite.markedPrivate_onNoCopyLocal()
links { "MyProject" }
flags { "NoCopyLocal" }
prepare()
test.capture [[
<ItemGroup>
<ProjectReference Include="MyProject.vcproj">
<Project>{00112233-4455-6677-8888-99AABBCCDDEE}</Project>
<Name>MyProject</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
]]
end
--
-- If there are entries in the copylocal() list, then only those
-- specific libraries should be copied.
--
function suite.markedPrivate_onCopyLocalListExclusion()
links { "MyProject" }
copylocal { "SomeOtherProject" }
prepare()
test.capture [[
<ItemGroup>
<ProjectReference Include="MyProject.vcproj">
<Project>{00112233-4455-6677-8888-99AABBCCDDEE}</Project>
<Name>MyProject</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
]]
end
function suite.notMarkedPrivate_onCopyLocalListInclusion()
links { "MyProject" }
copylocal { "MyProject" }
prepare()
test.capture [[
<ItemGroup>
<ProjectReference Include="MyProject.vcproj">
<Project>{00112233-4455-6677-8888-99AABBCCDDEE}</Project>
<Name>MyProject</Name>
</ProjectReference>
</ItemGroup>
]]
end