使用 premake 调整 g++ 位置

adjusting g++ location with premake

我想使用特定版本的 g++ installedvat /opt/blabla/bin/g++。

如何强制premake在makefile中添加CXX变量的初始化,使其指向特定位置?

我知道生成 makefile 后,我可以 'make CXX=...' 但我想在自动生成的 makefile 中设置 CXX。

使用 premake5,针对 gmake。

提前致谢

=============

更新:

通过查看示例和浏览代码,我发现我可以通过将这段代码添加到 premake5.lua:

local GCC_BIN_PATH = "/opt/blala/bin"

-- start: setting gcc version
-- todo: consider moving this instrumentation into a side lua script
local gcc = premake.tools.gcc

gcc.tools = {
   cc = GCC_BIN_PATH.."/gcc",
   cxx = GCC_BIN_PATH.."/g++",
   ar = GCC_BIN_PATH.."/ar"
}

function gcc.gettoolname(cfg, tool)
   return gcc.tools[tool]
end
-- finish: setting gcc version

有没有更好的方法来达到同样的效果?特别是,重新定义 gettoolname 函数是否有意义?

我认为官方方法是创建您自己的工具集。

下面的示例创建了一个名为 "arm_gcc" 的工具集:

premake.tools.arm_gcc         = {}

local arm_gcc                 = premake.tools.arm_gcc
local gcc                     = premake.tools.gcc

arm_gcc.getcflags             = gcc.getcflags
arm_gcc.getcxxflags           = gcc.getcxxflags
arm_gcc.getforceincludes      = gcc.getforceincludes
arm_gcc.getldflags            = gcc.getldflags
arm_gcc.getcppflags           = gcc.getcppflags
arm_gcc.getdefines            = gcc.getdefines
arm_gcc.getincludedirs        = gcc.getincludedirs
arm_gcc.getLibraryDirectories = gcc.getLibraryDirectories
arm_gcc.getlinks              = gcc.getlinks
arm_gcc.getmakesettings       = gcc.getmakesettings

function arm_gcc.gettoolname (cfg, tool)  
  local prefix = path.getabsolute ("../../arm_env")
  prefix       =  prefix .. "/arm_toolchain/bin/arm-linux-gnueabihf-"
  if     tool == "cc" then
    name = prefix .. "gcc"  
  elseif tool == "cxx" then
    name = prefix .. "g++"
  elseif tool == "ar" then
    name = prefix .. "ar"
  else
    name = nil
  end
  return name
end

那么你可以使用:

toolset "arm_gcc" 

在您的项目、过滤器等中

此方法的优点是您不会覆盖常规 gcc 工具集。如有必要,两者可以共存。

在我的例子中,我实际上发现将每个编译器添加到它自己的 lua 文件中,然后从主 (premake5.lua) 脚本中包含它。

与上述答案相同,但更通用。这个文件可以命名为"add_new_gcc_toolset.lua":

local function add_new_gcc_toolset (name, prefix)
  local gcc                         = premake.tools.gcc
  local new_toolset                 = {}  
  new_toolset.getcflags             = gcc.getcflags
  new_toolset.getcxxflags           = gcc.getcxxflags
  new_toolset.getforceincludes      = gcc.getforceincludes
  new_toolset.getldflags            = gcc.getldflags
  new_toolset.getcppflags           = gcc.getcppflags
  new_toolset.getdefines            = gcc.getdefines
  new_toolset.getincludedirs        = gcc.getincludedirs
  new_toolset.getLibraryDirectories = gcc.getLibraryDirectories
  new_toolset.getlinks              = gcc.getlinks
  new_toolset.getmakesettings       = gcc.getmakesettings
  new_toolset.toolset_prefix        = prefix

  function new_toolset.gettoolname (cfg, tool)  
    if     tool == "cc" then
      name = new_toolset.toolset_prefix .. "gcc"  
    elseif tool == "cxx" then
      name = new_toolset.toolset_prefix .. "g++"
    elseif tool == "ar" then
      name = new_toolset.toolset_prefix .. "ar"
    end
    return name
  end  

  premake.tools[name] = new_toolset
end

return add_new_gcc_toolset

作为解决方法,我找到了这个方法:

makesettings [[
    CC = gcc
]]

https://github.com/premake/premake-core/wiki/makesettings