Ported GMake link step to new APIs

This commit is contained in:
Jason Perkins 2012-06-13 15:21:22 -04:00
parent b9c09f6dd3
commit da4dbbc9de
12 changed files with 192 additions and 151 deletions

View File

@ -225,25 +225,10 @@
-- set up precompiled headers
cpp.pchconfig(cfg)
--[[
_p(' LIBS += %s', table.concat(cc.getlinkflags(cfg), " "))
_p(' LDDEPS += %s', table.concat(_MAKE.esc(premake.getlinks(cfg, "siblings", "fullpath")), " "))
-- write the link step
cpp.linkconfig(cfg, toolset)
if cfg.kind == "StaticLib" then
if cfg.platform:startswith("Universal") then
_p(' LINKCMD = libtool -o $(TARGET) $(OBJECTS)')
else
_p(' LINKCMD = $(AR) -rcs $(TARGET) $(OBJECTS)')
end
else
-- this was $(TARGET) $(LDFLAGS) $(OBJECTS)
-- but had trouble linking to certain static libs so $(OBJECTS) moved up
-- then $(LDFLAGS) moved to end
-- https://sourceforge.net/tracker/?func=detail&aid=3430158&group_id=71616&atid=531880
_p(' LINKCMD = $(%s) -o $(TARGET) $(OBJECTS) $(RESOURCES) $(ARCH) $(LIBS) $(LDFLAGS)', iif(cfg.language == "C", "CC", "CXX"))
end
--]]
-- write the custom build commands
_p(' define PREBUILDCMDS')
if #cfg.prebuildcommands > 0 then
_p('\t@echo Running pre-build commands')
@ -275,6 +260,33 @@
end
--
-- Link step
--
function cpp.linkconfig(cfg, toolset)
local flags = toolset.getlinks(cfg)
_p(' LIBS += %s', table.concat(flags, " "))
local deps = config.getlinks(cfg, "siblings", "fullpath")
_p(' LDDEPS += %s', table.concat(make.esc(deps), " "))
if cfg.kind == premake.STATICLIB then
if cfg.architecture == premake.UNIVERSAL then
_p(' LINKCMD = libtool -o $(TARGET) $(OBJECTS)')
else
_p(' LINKCMD = $(AR) -rcs $(TARGET) $(OBJECTS)')
end
else
-- This started as: $(TARGET) $(LDFLAGS) $(OBJECTS).
-- Had trouble linking to certain static libs, so $(OBJECTS) moved up.
-- $(LDFLAGS) moved: https://sf.net/tracker/?func=detail&aid=3430158&group_id=71616&atid=531880
local cc = iif(cfg.project.language == "C", "CC", "CXX")
_p(' LINKCMD = $(%s) -o $(TARGET) $(OBJECTS) $(RESOURCES) $(ARCH) $(LIBS) $(LDFLAGS)', cc)
end
end
--
-- Precompiled header support
--

View File

@ -323,8 +323,7 @@
_p(4,'UsePrecompiledHeader="%s"', iif(_ACTION > "vs2003" or cfg.flags.NoPCH, 0, 2))
end
_x(4,'ProgramDataBaseFileName="$(OutDir)\\%s.pdb"', config.gettargetinfo(cfg).basename)
vc200x.programDatabase(cfg)
vc200x.warnings(cfg)
_p(4,'DebugInformationFormat="%s"', vc200x.symbols(cfg))
@ -354,7 +353,7 @@
_p(4,'UsePrecompiledHeader="%s"', iif(_ACTION > "vs2003" or cfg.flags.NoPCH, 0, 2))
end
_x(4,'ProgramDataBaseFileName="$(OutDir)\\%s.pdb"', config.gettargetinfo(cfg).basename)
vc200x.programDatabase(cfg)
_p(4,'DebugInformationFormat="0"')
_p(4,'CompileAs="0"')
@ -936,6 +935,7 @@
--
-- Output the list of preprocessor symbols.
--
function vc200x.preprocessorDefinitions(cfg, defines)
if #defines > 0 then
@ -944,6 +944,16 @@
end
--
-- Output the program database filename.
--
function vc200x.programDatabase(cfg)
local target = config.gettargetinfo(cfg)
_x(4,'ProgramDataBaseFileName="$(OutDir)\\%s%s.pdb"', target.prefix, target.basename)
end
--
-- Output the correct project version attribute for the current action.
--

View File

@ -192,7 +192,7 @@
_x(2,'<IntDir>%s\\</IntDir>', path.translate(cfg.objdir))
_x(2,'<TargetName>%s</TargetName>', target.basename)
_x(2,'<TargetName>%s%s</TargetName>', target.prefix, target.basename)
_x(2,'<TargetExt>%s</TargetExt>', target.extension)
if cfg.flags.NoManifest then

View File

@ -336,6 +336,7 @@
scope = "config",
kind = "string",
allowed = {
"universal",
"x32",
"x64",
},

View File

@ -20,6 +20,7 @@
premake.PS3 = "ps3"
premake.SHAREDLIB = "SharedLib"
premake.STATICLIB = "StaticLib"
premake.UNIVERSAL = "universal"
premake.WINDOWEDAPP = "WindowedApp"
premake.WINDOWS = "windows"
premake.X32 = "x32"

View File

@ -65,8 +65,8 @@
local info = {}
info.directory = project.getrelative(cfg.project, directory)
info.basename = prefix .. basename .. suffix
info.name = info.basename .. extension
info.basename = basename .. suffix
info.name = prefix .. info.basename .. extension
info.extension = extension
info.abspath = path.join(directory, info.name)
info.fullpath = path.join(info.directory, info.name)

View File

@ -174,14 +174,14 @@
if not systemonly then
links = config.getlinks(cfg, "siblings", "object")
for _, link in ipairs(links) do
local linkinfo = config.getlinkinfo(link)
if link.kind == premake.STATICLIB then
-- Don't use "-l" flag when linking static libraries; instead use
-- path/libname.a to avoid linking a shared library of the same
-- name if one is present
local linkinfo = config.getlinkinfo(link)
table.insert(result, project.getrelative(cfg.project, linkinfo.abspath))
else
table.insert(result, "-l" .. link.basename)
table.insert(result, "-l" .. linkinfo.basename)
end
end
end

View File

@ -0,0 +1,127 @@
--
-- tests/actions/make/cpp/test_make_linking.lua
-- Validate the link step generation for makefiles.
-- Copyright (c) 2010-2012 Jason Perkins and the Premake project
--
T.make_linking = { }
local suite = T.make_linking
local cpp = premake.make.cpp
local project = premake5.project
--
-- Setup and teardown
--
local sln, prj
function suite.setup()
_OS = "linux"
sln, prj = test.createsolution()
end
local function prepare()
local cfg = project.getconfig(prj, "Debug")
cpp.linkconfig(cfg, premake.tools.gcc)
end
--
-- Check link command for a shared C++ library.
--
function suite.links_onCppSharedLib()
kind "SharedLib"
prepare()
test.capture [[
LIBS +=
LDDEPS +=
LINKCMD = $(CXX) -o $(TARGET) $(OBJECTS) $(RESOURCES) $(ARCH) $(LIBS) $(LDFLAGS)
]]
end
--
-- Check link command for a shared C library.
--
function suite.links_onCppSharedLib()
language "C"
kind "SharedLib"
prepare()
test.capture [[
LIBS +=
LDDEPS +=
LINKCMD = $(CC) -o $(TARGET) $(OBJECTS) $(RESOURCES) $(ARCH) $(LIBS) $(LDFLAGS)
]]
end
--
-- Check link command for a static library.
--
function suite.links_onStaticLib()
kind "StaticLib"
prepare()
test.capture [[
LIBS +=
LDDEPS +=
LINKCMD = $(AR) -rcs $(TARGET) $(OBJECTS)
]]
end
--
-- Check link command for a Mac OS X universal static library.
--
function suite.links_onStaticLib()
architecture "universal"
kind "StaticLib"
prepare()
test.capture [[
LIBS +=
LDDEPS +=
LINKCMD = libtool -o $(TARGET) $(OBJECTS)
]]
end
--
-- Check a linking to a sibling static library.
--
function suite.links_onSiblingStaticLib()
links "MyProject2"
test.createproject(sln)
kind "StaticLib"
location "build"
prepare()
test.capture [[
LIBS += build/libMyProject2.a
LDDEPS += build/libMyProject2.a
]]
end
--
-- Check a linking to a sibling shared library.
--
function suite.links_onSiblingSharedLib()
links "MyProject2"
test.createproject(sln)
kind "SharedLib"
location "build"
prepare()
test.capture [[
LIBS += -lMyProject2
LDDEPS += build/libMyProject2.so
]]
end

View File

@ -1,112 +0,0 @@
T.gcc_linking = { }
local suite = T.gcc_linking
local staticPrj
local linksToStaticProj
local sln
function suite.setup()
_ACTION = "gmake"
sln = solution "MySolution"
configurations { "Debug" }
platforms {}
staticPrj = project "staticPrj"
targetdir 'bar'
language 'C++'
kind "StaticLib"
linksToStaticProj = project "linksToStaticProj"
language 'C++'
kind 'ConsoleApp'
links{'staticPrj'}
end
function suite.teardown()
staticPrj = nil
linksToStaticProj = nil
sln = nil
end
local get_buffer = function(projectName)
io.capture()
premake.bake.buildconfigs()
local cfg = premake.getconfig(projectName, 'Debug', 'Native')
premake.gmake_cpp_config(cfg, premake.gcc)
local buffer = io.endcapture()
return buffer
end
function suite.projectLinksToStaticPremakeMadeLibrary_linksUsingTheFormat_pathNameExtension()
local buffer = get_buffer(linksToStaticProj)
local format_exspected = 'LIBS %+%= bar/libstaticPrj.a'
test.string_contains(buffer,format_exspected)
end
T.link_suite= { }
local firstProject = nil
local linksToFirstProject = nil
function T.link_suite.setup()
_ACTION = "gmake"
solution('dontCareSolution')
configurations{'Debug'}
end
function T.link_suite.teardown()
_ACTION = nil
firstProject = nil
linksToFirstProject = nil
end
function T.link_suite.projectLinksToSharedPremakeMadeLibrary_linksUsingFormat_dashLName()
firstProject = project 'firstProject'
kind 'SharedLib'
language 'C'
linksToFirstProject = project 'linksToFirstProject'
kind 'ConsoleApp'
language 'C'
links{'firstProject'}
local buffer = get_buffer(linksToFirstProject)
local format_exspected = 'LIBS %+%= %-lfirstProject'
test.string_contains(buffer,format_exspected)
end
function T.link_suite.projectLinksToPremakeMadeConsoleApp_doesNotLinkToConsoleApp()
firstProject = project 'firstProject'
kind 'ConsoleApp'
language 'C'
linksToFirstProject = project 'linksToFirstProject'
kind 'ConsoleApp'
language 'C'
links{'firstProject'}
local buffer = get_buffer(linksToFirstProject)
local format_exspected = 'LIBS %+%=%s+\n'
test.string_contains(buffer,format_exspected)
end
function T.link_suite.projectLinksToStaticPremakeMadeLibrary_projectDifferInDirectoryHeights_linksUsingCorrectRelativePath()
firstProject = project 'firstProject'
kind 'StaticLib'
language 'C'
linksToFirstProject = project 'linksToFirstProject'
kind 'ConsoleApp'
language 'C'
links{'firstProject'}
location './foo/bar'
local buffer = get_buffer(linksToFirstProject)
local format_exspected = 'LIBS %+%= ../../libfirstProject.a'
test.string_contains(buffer,format_exspected)
end

View File

@ -276,14 +276,3 @@
test.isequal("MyProject.self", i.name)
end
--
-- Base name should use prefix if set.
--
function suite.basenameHasPrefix_onPrefix()
targetprefix "lib"
i = prepare()
test.isequal("libMyProject", i.basename)
end

View File

@ -160,7 +160,7 @@
-- Makefile C/C++ projects
dofile("actions/make/cpp/test_make_pch.lua")
dofile("actions/make/test_make_linking.lua")
dofile("actions/make/cpp/test_make_linking.lua")
-- dofile("actions/make/test_makesettings.lua")
dofile("actions/make/test_wiidev.lua")

View File

@ -203,13 +203,26 @@
links { "MyProject2" }
test.createproject(sln)
kind "StaticLib"
location "MyProject2"
targetdir "lib"
prepare()
test.isequal({ "lib/libMyProject2.a" }, gcc.getlinks(cfg))
end
--
-- Use the -lname format when linking to sibling shared libraries.
--
function suite.links_onStaticSharedLibrary()
links { "MyProject2" }
test.createproject(sln)
kind "SharedLib"
targetdir "lib"
prepare()
test.isequal({ "-lMyProject2" }, gcc.getlinks(cfg))
end
--
-- When linking object files, leave off the "-l".
--