adjusting g++ location with premake

2019-05-31 06:40发布

问题:

I would like to use a specific version of g++ installedvat /opt/blabla/bin/g++.

How do I force premake to add initialization of CXX variable in makefile, such that it will point to the specific location?

I do realize that once makefile is generated, I can to 'make CXX=...' but I would like to have CXX set inside auto-generated makefile.

Using premake5, targeting gmake.

Thanks in advance

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

Update:

By poking examples and browsing the code, I figured out I can do it by adding this code into 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

Is there a better way to achieve the same? In particular, does it make sense to redefine gettoolname function?

回答1:

I think that official way is to create your own toolset.

The example below creates a toolset with the "arm_gcc" name:

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

Then you can use:

toolset "arm_gcc" 

Inside your projects, filters, etc.

This method has the advantage that you aren't overwriting the regular gcc toolset. Both can coexist if necessary.

In my case I actually find cleaner to add each compiler in its own lua file and then include it from the main (premake5.lua) script.



回答2:

Same answer as above but more generic. This file can be named "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


回答3:

as a workaround, I found this method:

makesettings [[
    CC = gcc
]]

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