From ec157c8e6e8163202e84158b9ad8467368342957 Mon Sep 17 00:00:00 2001 From: Ben Ratzlaff Date: Fri, 6 Feb 2015 16:48:59 -0700 Subject: [PATCH] Implement 'RelativeLinks" flag to effect makefile creation to use -l and -L instead of linking against the full path of a dynamic object library --- src/_premake_init.lua | 1 + src/tools/gcc.lua | 28 ++++++++++++++++---- tests/actions/make/cpp/test_make_linking.lua | 20 ++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/_premake_init.lua b/src/_premake_init.lua index fd40aeb6..4acef553 100644 --- a/src/_premake_init.lua +++ b/src/_premake_init.lua @@ -338,6 +338,7 @@ "Optimize", -- DEPRECATED "OptimizeSize", -- DEPRECATED "OptimizeSpeed", -- DEPRECATED + "RelativeLinks", "ReleaseRuntime", "SEH", "ShadowedVariables", diff --git a/src/tools/gcc.lua b/src/tools/gcc.lua index 4142ca8f..4b572a5a 100644 --- a/src/tools/gcc.lua +++ b/src/tools/gcc.lua @@ -213,6 +213,15 @@ table.insert(flags, '-L' .. project.getrelative(cfg.project, dir)) end + if cfg.flags.RelativeLinks then + for _, dir in ipairs(premake.config.getlinks(cfg, "siblings", "directory")) do + local libFlag = "-L" .. premake.project.getrelative(cfg.project, dir) + if not table.contains(flags, libFlag) then + table.insert(flags, libFlag) + end + end + end + return flags end @@ -225,12 +234,21 @@ function gcc.getlinks(cfg, systemonly) local result = {} - -- Don't use the -l form for sibling libraries, since they may have - -- custom prefixes or extensions that will confuse the linker. Instead - -- just list out the full relative path to the library. - if not systemonly then - result = config.getlinks(cfg, "siblings", "fullpath") + if cfg.flags.RelativeLinks then + local libFiles = premake.config.getlinks(cfg, "siblings", "basename") + for _, link in ipairs(libFiles) do + if string.find(link, "lib") == 1 then + link = link:sub(4) + end + table.insert(result, "-l" .. link) + end + else + -- Don't use the -l form for sibling libraries, since they may have + -- custom prefixes or extensions that will confuse the linker. Instead + -- just list out the full relative path to the library. + result = config.getlinks(cfg, "siblings", "fullpath") + end end -- The "-l" flag is fine for system libraries diff --git a/tests/actions/make/cpp/test_make_linking.lua b/tests/actions/make/cpp/test_make_linking.lua index 38f8ac09..c9ff4aa3 100644 --- a/tests/actions/make/cpp/test_make_linking.lua +++ b/tests/actions/make/cpp/test_make_linking.lua @@ -124,6 +124,26 @@ ]] end +-- +-- Check a linking to a sibling shared library using -l and -L. +-- + + function suite.links_onSiblingSharedLib() + links "MyProject2" + flags { "RelativeLinks" } + + test.createproject(sln) + kind "SharedLib" + location "build" + + prepare { "ldFlags", "libs", "ldDeps" } + test.capture [[ + ALL_LDFLAGS += $(LDFLAGS) -Lbuild -s + LIBS += -lMyProject2 + LDDEPS += build/libMyProject2.so + ]] + end + -- -- When referencing an external library via a path, the directory -- should be added to the library search paths, and the library