From da4dbbc9de50776d49bf3ae06162fcb917b49ad5 Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Wed, 13 Jun 2012 15:21:22 -0400 Subject: [PATCH] Ported GMake link step to new APIs --- src/actions/make/make_cpp.lua | 48 ++++--- src/actions/vstudio/vs200x_vcproj.lua | 16 ++- src/actions/vstudio/vs2010_vcxproj.lua | 2 +- src/base/api.lua | 1 + src/base/premake.lua | 1 + src/project/config.lua | 4 +- src/tools/gcc.lua | 4 +- tests/actions/make/cpp/test_make_linking.lua | 127 +++++++++++++++++++ tests/actions/make/test_make_linking.lua | 112 ---------------- tests/config/test_targetinfo.lua | 11 -- tests/premake4.lua | 2 +- tests/tools/test_gcc.lua | 15 ++- 12 files changed, 192 insertions(+), 151 deletions(-) create mode 100644 tests/actions/make/cpp/test_make_linking.lua delete mode 100644 tests/actions/make/test_make_linking.lua diff --git a/src/actions/make/make_cpp.lua b/src/actions/make/make_cpp.lua index 294f9bfe..aeced279 100644 --- a/src/actions/make/make_cpp.lua +++ b/src/actions/make/make_cpp.lua @@ -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 -- diff --git a/src/actions/vstudio/vs200x_vcproj.lua b/src/actions/vstudio/vs200x_vcproj.lua index a9f78b0d..da405b3c 100644 --- a/src/actions/vstudio/vs200x_vcproj.lua +++ b/src/actions/vstudio/vs200x_vcproj.lua @@ -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. -- diff --git a/src/actions/vstudio/vs2010_vcxproj.lua b/src/actions/vstudio/vs2010_vcxproj.lua index a8cf293e..3440ccc3 100644 --- a/src/actions/vstudio/vs2010_vcxproj.lua +++ b/src/actions/vstudio/vs2010_vcxproj.lua @@ -192,7 +192,7 @@ _x(2,'%s\\', path.translate(cfg.objdir)) - _x(2,'%s', target.basename) + _x(2,'%s%s', target.prefix, target.basename) _x(2,'%s', target.extension) if cfg.flags.NoManifest then diff --git a/src/base/api.lua b/src/base/api.lua index c0e78e65..c8f7b2f7 100644 --- a/src/base/api.lua +++ b/src/base/api.lua @@ -336,6 +336,7 @@ scope = "config", kind = "string", allowed = { + "universal", "x32", "x64", }, diff --git a/src/base/premake.lua b/src/base/premake.lua index 5cbb839c..75838653 100644 --- a/src/base/premake.lua +++ b/src/base/premake.lua @@ -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" diff --git a/src/project/config.lua b/src/project/config.lua index 20e45109..0806571b 100755 --- a/src/project/config.lua +++ b/src/project/config.lua @@ -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) diff --git a/src/tools/gcc.lua b/src/tools/gcc.lua index 26f43458..d8dfbafd 100644 --- a/src/tools/gcc.lua +++ b/src/tools/gcc.lua @@ -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 diff --git a/tests/actions/make/cpp/test_make_linking.lua b/tests/actions/make/cpp/test_make_linking.lua new file mode 100644 index 00000000..0057b05a --- /dev/null +++ b/tests/actions/make/cpp/test_make_linking.lua @@ -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 diff --git a/tests/actions/make/test_make_linking.lua b/tests/actions/make/test_make_linking.lua deleted file mode 100644 index 7a346e6e..00000000 --- a/tests/actions/make/test_make_linking.lua +++ /dev/null @@ -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 - diff --git a/tests/config/test_targetinfo.lua b/tests/config/test_targetinfo.lua index 950587c8..4d822ea7 100755 --- a/tests/config/test_targetinfo.lua +++ b/tests/config/test_targetinfo.lua @@ -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 - diff --git a/tests/premake4.lua b/tests/premake4.lua index e50fd31f..166c864c 100644 --- a/tests/premake4.lua +++ b/tests/premake4.lua @@ -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") diff --git a/tests/tools/test_gcc.lua b/tests/tools/test_gcc.lua index d989dd96..efb092e8 100644 --- a/tests/tools/test_gcc.lua +++ b/tests/tools/test_gcc.lua @@ -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". --