Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.x - Initial GCC toolset implementation #1931

Draft
wants to merge 1 commit into
base: 6.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/modules/main/core_fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ Field.register({
}
})

Field.register({
name = 'toolset',
kind = 'string',
allowed = {}
})

Field.register({
name = 'uuid',
kind = 'string'
Expand Down
2 changes: 2 additions & 0 deletions core/modules/main/core_modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ require('scripting')
register('gmake')
register('vstudio')
register('xcode')

register('gcc')
91 changes: 24 additions & 67 deletions modules/exporters/gmake/gmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,13 @@ local dom = require('dom')
local path = require('path')
local premake = require('premake')
local State = require('state')
local Toolsets = require('toolsets')

local gmake = {}

gmake.wks = doFile('./src/wks.lua', gmake)
gmake.proj = doFile('./src/proj.lua', gmake)

---
-- Temporary variable until toolsets are implemented.
---
local _ARCHITECTURES = {
x86 = 'x86',
x86_64 = 'x86_64',
arm = 'ARM',
arm64 = 'ARM64'
}


---
-- Table of all toolsets.
--
-- Temporary until toolsets are implemented.
---
gmake.toolsets = {
compilers = {
gcc = {
getCFlags = function(cfg)
local flags = {}
if cfg.gmake_architecture == 'x86_64' then
table.insert(flags, '-m64')
elseif cfg.gmake_architecture == 'x86' then
table.insert(flags, '-m32')
end
return flags
end,
getCppFlags = function (cfg)
local flags = {}

return flags
end,
getCxxFlags = function (cfg)
local flags = gmake.toolsets.compilers.gcc.getCFlags(cfg)
-- TODO: Add C++ specific flags
return flags
end
}
},
linkers = {
gcc = {
getLinkerFlags = function(cfg)
local flags = {}
if cfg.gmake_architecture == 'x86_64' then
table.insert(flags, '-m64')
-- should only link on linux
table.insert(flags, '-L/usr/lib64')
elseif cfg.gmake_architecture == 'x86' then
table.insert(flags, '-m32')
-- should only link on linux
table.insert(flags, '-L/usr/lib32')
end
return flags
end
}
}
}


---
-- Exports the GNU makefile for all workspaces.
Expand All @@ -86,6 +28,23 @@ function gmake.export()
print('Done.')
end


---
-- Escape a string so it can be written to a makefile.
---
function gmake.esc(value)
result = value:gsub("\\", "\\\\")
result = result:gsub("\"", "\\\"")
result = result:gsub(" ", "\\ ")
result = result:gsub("%(", "\\(")
result = result:gsub("%)", "\\)")

-- leave $(...) shell replacement sequences alone
result = result:gsub("$\\%((.-)\\%)", "$(%1)")
return result
end


---
-- Query and build a DOM hierarchy from the contents of the user project script.
--
Expand Down Expand Up @@ -160,9 +119,8 @@ function gmake.fetchProject(wks, name)
prj.workspace = wks
prj.uuid = prj.uuid or os.uuid(prj.name)

-- TODO: Compiler/Linker from DOM
prj.compiler = gmake.toolsets.compilers.gcc
prj.linker = gmake.toolsets.linkers.gcc
local tsname = prj.toolset or 'gcc'
prj.tools = Toolsets.get(tsname)

prj.configs = prj:fetchConfigs(gmake.fetchProjectConfig)

Expand Down Expand Up @@ -218,9 +176,8 @@ function gmake.fetchProjectConfig(prj, build, platform)
cfg.workspace = prj.workspace
cfg.project = prj

-- TODO: Compiler/Linker from DOM
cfg.compiler = gmake.toolsets.compilers.gcc
cfg.linker = gmake.toolsets.linkers.gcc
local tsname = cfg.toolset or 'gcc'
cfg.tools = Toolsets.get(tsname)

return cfg
end
Expand Down Expand Up @@ -258,7 +215,7 @@ function gmake.fetchConfig(state)
local cfg = dom.Config.new(state)

-- translate the incoming architecture
cfg.gmake_architecture = _ARCHITECTURES[cfg.architecture] or 'x86_64'
cfg.gmake_architecture = cfg.architecture or 'x86_64'
cfg.platform = cfg.platform or cfg.gmake_architecture

-- "Configuration|Platform or Architecture", e.g. "Debug|MyPlatform" or "Debug|Win32"
Expand Down Expand Up @@ -324,7 +281,7 @@ function gmake.getMakefileName(this, searchprjs, domroot)
if count == 1 then
return path.join(this.location, 'Makefile')
else
return path.join(this.location, this.name .. '.mak')
return path.join(this.location, this.name:gsub(' ', '') .. '.mak')
end
end

Expand Down
95 changes: 58 additions & 37 deletions modules/exporters/gmake/src/proj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ local function isProject(obj)
end


local function getCppFlags(cfg, toolset)
return {}
end


local function getCFlags(cfg, toolset)
local flags = {}
table.insert(flags, toolset.getArchitectureFlags(cfg.gmake_architecture))
return flags
end


local function getCxxFlags(cfg, toolset)
local flags = getCFlags(cfg, toolset)

return flags
end


local function getLinkerFlags(cfg, toolset)
local flags = {}

local sysLibDirs = toolset.getLibDirs({ toolset.getSystemLibDirs(cfg.gmake_architecture) })

table.insert(flags, toolset.getArchitectureFlags(cfg.gmake_architecture))

if sysLibDirs:len() > 0 then
table.insert(flags, sysLibDirs)
end

return flags
end


---
-- Export the project's `.makefile` file.
--
Expand Down Expand Up @@ -160,26 +194,25 @@ end
---
function proj.includeDirs(prj)
if isProject(prj) then
local includeDirs = prj.includeDirs
if includeDirs ~= nil and #includeDirs > 0 then
local includes = table.map(includeDirs, function(key, value)
return '-I' .. path.getRelative(prj.location, value)
end)
local includeDirs = table.map(prj.includeDirs or {}, function(key, value)
return path.getRelative(prj.location, value)
end)


if #includeDirs > 0 then
local includes = prj.tools.getIncludes(includeDirs)

wl('INCLUDES = %s', table.concat(includes, ' '))
wl('INCLUDES = %s', includes)
else
wl('INCLUDES =')
end
else
local cfg = prj
local configIncludeDirs = cfg.includeDirs
if configIncludeDirs ~= nil and #configIncludeDirs > 0 then
local includeDirString = table.concat(table.map(configIncludeDirs, function(key, value)
local relative = path.getRelative(cfg.project.location, value)
return '-I' .. relative
end), ' ')

wl('INCLUDES += %s', includeDirString)
local includeDirs = table.map(prj.includeDirs or {}, function(key, value)
return path.getRelative(prj.project.location, value)
end)

if #includeDirs > 0 then
wl('INCLUDES += %s', prj.tools.getIncludes(includeDirs))
end
end
end
Expand All @@ -192,8 +225,8 @@ end
-- project to print defines of
---
function proj.defines(prj)
local defs = proj.impl.definesString(prj)
if defs ~= nil then
local defs = prj.tools.getDefines(prj.defines or {})
if #defs > 0 then
wl('DEFINES += %s', defs)
else
wl('DEFINES +=')
Expand Down Expand Up @@ -266,18 +299,15 @@ end
---
function proj.cppFlags(prj)
if isProject(prj) then
local toolset = prj.compiler
local flags = toolset.getCppFlags(prj)
local flags = getCppFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP %s $(DEFINES) $(INCLUDES)', table.concat(flags, ' '))
else
wl('ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)')
end
else
local cfg = prj
local toolset = cfg.compiler
local flags = toolset.getCppFlags(cfg)
local flags = getCppFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_CPPFLAGS += %s', table.concat(flags, ' '))
Expand All @@ -294,18 +324,15 @@ end
---
function proj.cFlags(prj)
if isProject(prj) then
local toolset = prj.compiler
local flags = toolset.getCFlags(prj)
local flags = getCFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) %s', table.concat(flags, ' '))
else
wl('ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS)')
end
else
local cfg = prj
local toolset = cfg.compiler
local flags = toolset.getCFlags(cfg)
local flags = getCFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_CFLAGS += %s', table.concat(flags, ' '))
Expand All @@ -322,18 +349,15 @@ end
---
function proj.cxxFlags(prj)
if isProject(prj) then
local toolset = prj.compiler
local flags = toolset.getCxxFlags(prj)
local flags = getCxxFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) %s', table.concat(flags, ' '))
else
wl('ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS)')
end
else
local cfg = prj
local toolset = cfg.compiler
local flags = toolset.getCxxFlags(cfg)
local flags = getCxxFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_CXXFLAGS += %s', table.concat(flags, ' '))
Expand All @@ -350,18 +374,15 @@ end
---
function proj.linkFlags(prj)
if isProject(prj) then
local toolset = prj.linker
local flags = toolset.getLinkerFlags(prj)
local flags = getLinkerFlags(prj, prj.tools)

if #flags > 0 then
wl('ALL_LDFLAGS = $(LDFLAGS) %s', table.concat(flags, ' '))
else
wl('ALL_LDFLAGS = $(LDFLAGS)')
end
else
local cfg = prj
local toolset = cfg.linker
local flags = toolset.getLinkerFlags(cfg)
local flags = getLinkerFlags(prj, prj.tools)

if #flags then
wl('ALL_LDFLAGS += %s', table.concat(flags, ' '))
Expand Down
12 changes: 6 additions & 6 deletions modules/exporters/gmake/src/wks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ end
---
function wks.projects(wk)
local projects = table.map(wk.projects, function(key, value)
return value.name
return gmake.esc(value.name)
end)
if #projects > 0 then
wl('PROJECTS := %s', table.concat(projects, ' '))
Expand Down Expand Up @@ -175,9 +175,9 @@ function wks.projectRules(wk)
local deps = {}

if #deps > 0 then
wl('%s: %s', prj.name, table.concat(deps, ' '))
wl('%s: %s', gmake.esc(prj.name), table.concat(deps, ' '))
else
wl('%s:', prj.name)
wl('%s:', gmake.esc(prj.name))
end
wl('ifneq (, $(config))')
export.indent()
Expand All @@ -187,7 +187,7 @@ function wks.projectRules(wk)
local prjName = path.getName(prjPath)

wl('@echo "==== Building %s ($(config)) ===="', prj.name)
wl('@${MAKE} --no-print-directory -C %s -f %s config=$(config)', prjDir, prjName)
wl('@${MAKE} --no-print-directory -C %s -f %s config=$(config)', gmake.esc(prjDir), gmake.esc(prjName))
export.outdent()
wl('endif')
wl()
Expand All @@ -209,7 +209,7 @@ function wks.cleanRule(wk)
local prjDir = path.getDirectory(path.getRelative(wk.location, prjPath))
local prjName = path.getName(prjPath)

wl('@${MAKE} --no-print-directory -C %s -f %s clean', prjDir, prjName)
wl('@${MAKE} --no-print-directory -C %s -f %s clean', gmake.esc(prjDir), gmake.esc(prjName))
end
export.outdent()
wl()
Expand Down Expand Up @@ -239,7 +239,7 @@ function wks.helpRule(wk)
wl('@echo " clean"')
wl('@echo " help [Prints this message]"')
for _, prj in ipairs(wk.projects) do
wl('@echo " %s"', prj.name)
wl('@echo " %s"', gmake.esc(prj.name))
end
wl('@echo ""')
wl('@echo "For more information, see https://premake.github.io"')
Expand Down
Loading