Skip to content

Commit 58b76f1

Browse files
committed
Initial GCC toolset implementation
1 parent b297321 commit 58b76f1

File tree

17 files changed

+871
-114
lines changed

17 files changed

+871
-114
lines changed

core/modules/main/core_fields.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ Field.register({
117117
}
118118
})
119119

120+
Field.register({
121+
name = 'toolset',
122+
kind = 'string',
123+
allowed = {}
124+
})
125+
120126
Field.register({
121127
name = 'uuid',
122128
kind = 'string'

core/modules/main/core_modules.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ require('scripting')
33
register('gmake')
44
register('vstudio')
55
register('xcode')
6+
7+
register('gcc')

modules/exporters/gmake/gmake.lua

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,13 @@ local dom = require('dom')
66
local path = require('path')
77
local premake = require('premake')
88
local State = require('state')
9+
local Toolsets = require('toolsets')
910

1011
local gmake = {}
1112

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

15-
---
16-
-- Temporary variable until toolsets are implemented.
17-
---
18-
local _ARCHITECTURES = {
19-
x86 = 'x86',
20-
x86_64 = 'x86_64',
21-
arm = 'ARM',
22-
arm64 = 'ARM64'
23-
}
24-
25-
26-
---
27-
-- Table of all toolsets.
28-
--
29-
-- Temporary until toolsets are implemented.
30-
---
31-
gmake.toolsets = {
32-
compilers = {
33-
gcc = {
34-
getCFlags = function(cfg)
35-
local flags = {}
36-
if cfg.gmake_architecture == 'x86_64' then
37-
table.insert(flags, '-m64')
38-
elseif cfg.gmake_architecture == 'x86' then
39-
table.insert(flags, '-m32')
40-
end
41-
return flags
42-
end,
43-
getCppFlags = function (cfg)
44-
local flags = {}
45-
46-
return flags
47-
end,
48-
getCxxFlags = function (cfg)
49-
local flags = gmake.toolsets.compilers.gcc.getCFlags(cfg)
50-
-- TODO: Add C++ specific flags
51-
return flags
52-
end
53-
}
54-
},
55-
linkers = {
56-
gcc = {
57-
getLinkerFlags = function(cfg)
58-
local flags = {}
59-
if cfg.gmake_architecture == 'x86_64' then
60-
table.insert(flags, '-m64')
61-
-- should only link on linux
62-
table.insert(flags, '-L/usr/lib64')
63-
elseif cfg.gmake_architecture == 'x86' then
64-
table.insert(flags, '-m32')
65-
-- should only link on linux
66-
table.insert(flags, '-L/usr/lib32')
67-
end
68-
return flags
69-
end
70-
}
71-
}
72-
}
73-
7416

7517
---
7618
-- Exports the GNU makefile for all workspaces.
@@ -86,6 +28,23 @@ function gmake.export()
8628
print('Done.')
8729
end
8830

31+
32+
---
33+
-- Escape a string so it can be written to a makefile.
34+
---
35+
function gmake.esc(value)
36+
result = value:gsub("\\", "\\\\")
37+
result = result:gsub("\"", "\\\"")
38+
result = result:gsub(" ", "\\ ")
39+
result = result:gsub("%(", "\\(")
40+
result = result:gsub("%)", "\\)")
41+
42+
-- leave $(...) shell replacement sequences alone
43+
result = result:gsub("$\\%((.-)\\%)", "$(%1)")
44+
return result
45+
end
46+
47+
8948
---
9049
-- Query and build a DOM hierarchy from the contents of the user project script.
9150
--
@@ -160,9 +119,8 @@ function gmake.fetchProject(wks, name)
160119
prj.workspace = wks
161120
prj.uuid = prj.uuid or os.uuid(prj.name)
162121

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

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

@@ -218,9 +176,8 @@ function gmake.fetchProjectConfig(prj, build, platform)
218176
cfg.workspace = prj.workspace
219177
cfg.project = prj
220178

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

225182
return cfg
226183
end
@@ -258,7 +215,7 @@ function gmake.fetchConfig(state)
258215
local cfg = dom.Config.new(state)
259216

260217
-- translate the incoming architecture
261-
cfg.gmake_architecture = _ARCHITECTURES[cfg.architecture] or 'x86_64'
218+
cfg.gmake_architecture = cfg.architecture or 'x86_64'
262219
cfg.platform = cfg.platform or cfg.gmake_architecture
263220

264221
-- "Configuration|Platform or Architecture", e.g. "Debug|MyPlatform" or "Debug|Win32"
@@ -324,7 +281,7 @@ function gmake.getMakefileName(this, searchprjs, domroot)
324281
if count == 1 then
325282
return path.join(this.location, 'Makefile')
326283
else
327-
return path.join(this.location, this.name .. '.mak')
284+
return path.join(this.location, this.name:gsub(' ', '') .. '.mak')
328285
end
329286
end
330287

modules/exporters/gmake/src/proj.lua

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,40 @@ local function isProject(obj)
7676
end
7777

7878

79+
local function getCppFlags(cfg, toolset)
80+
return {}
81+
end
82+
83+
84+
local function getCFlags(cfg, toolset)
85+
local flags = {}
86+
table.insert(flags, toolset.getArchitectureFlags(cfg.gmake_architecture))
87+
return flags
88+
end
89+
90+
91+
local function getCxxFlags(cfg, toolset)
92+
local flags = getCFlags(cfg, toolset)
93+
94+
return flags
95+
end
96+
97+
98+
local function getLinkerFlags(cfg, toolset)
99+
local flags = {}
100+
101+
local sysLibDirs = toolset.getLibDirs({ toolset.getSystemLibDirs(cfg.gmake_architecture) })
102+
103+
table.insert(flags, toolset.getArchitectureFlags(cfg.gmake_architecture))
104+
105+
if sysLibDirs:len() > 0 then
106+
table.insert(flags, sysLibDirs)
107+
end
108+
109+
return flags
110+
end
111+
112+
79113
---
80114
-- Export the project's `.makefile` file.
81115
--
@@ -160,26 +194,25 @@ end
160194
---
161195
function proj.includeDirs(prj)
162196
if isProject(prj) then
163-
local includeDirs = prj.includeDirs
164-
if includeDirs ~= nil and #includeDirs > 0 then
165-
local includes = table.map(includeDirs, function(key, value)
166-
return '-I' .. path.getRelative(prj.location, value)
167-
end)
197+
local includeDirs = table.map(prj.includeDirs or {}, function(key, value)
198+
return path.getRelative(prj.location, value)
199+
end)
200+
201+
202+
if #includeDirs > 0 then
203+
local includes = prj.tools.getIncludes(includeDirs)
168204

169-
wl('INCLUDES = %s', table.concat(includes, ' '))
205+
wl('INCLUDES = %s', includes)
170206
else
171207
wl('INCLUDES =')
172208
end
173209
else
174-
local cfg = prj
175-
local configIncludeDirs = cfg.includeDirs
176-
if configIncludeDirs ~= nil and #configIncludeDirs > 0 then
177-
local includeDirString = table.concat(table.map(configIncludeDirs, function(key, value)
178-
local relative = path.getRelative(cfg.project.location, value)
179-
return '-I' .. relative
180-
end), ' ')
181-
182-
wl('INCLUDES += %s', includeDirString)
210+
local includeDirs = table.map(prj.includeDirs or {}, function(key, value)
211+
return path.getRelative(prj.project.location, value)
212+
end)
213+
214+
if #includeDirs > 0 then
215+
wl('INCLUDES += %s', prj.tools.getIncludes(includeDirs))
183216
end
184217
end
185218
end
@@ -192,8 +225,8 @@ end
192225
-- project to print defines of
193226
---
194227
function proj.defines(prj)
195-
local defs = proj.impl.definesString(prj)
196-
if defs ~= nil then
228+
local defs = prj.tools.getDefines(prj.defines or {})
229+
if #defs > 0 then
197230
wl('DEFINES += %s', defs)
198231
else
199232
wl('DEFINES +=')
@@ -266,18 +299,15 @@ end
266299
---
267300
function proj.cppFlags(prj)
268301
if isProject(prj) then
269-
local toolset = prj.compiler
270-
local flags = toolset.getCppFlags(prj)
302+
local flags = getCppFlags(prj, prj.tools)
271303

272304
if #flags > 0 then
273305
wl('ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP %s $(DEFINES) $(INCLUDES)', table.concat(flags, ' '))
274306
else
275307
wl('ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)')
276308
end
277309
else
278-
local cfg = prj
279-
local toolset = cfg.compiler
280-
local flags = toolset.getCppFlags(cfg)
310+
local flags = getCppFlags(prj, prj.tools)
281311

282312
if #flags > 0 then
283313
wl('ALL_CPPFLAGS += %s', table.concat(flags, ' '))
@@ -294,18 +324,15 @@ end
294324
---
295325
function proj.cFlags(prj)
296326
if isProject(prj) then
297-
local toolset = prj.compiler
298-
local flags = toolset.getCFlags(prj)
327+
local flags = getCFlags(prj, prj.tools)
299328

300329
if #flags > 0 then
301330
wl('ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) %s', table.concat(flags, ' '))
302331
else
303332
wl('ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS)')
304333
end
305334
else
306-
local cfg = prj
307-
local toolset = cfg.compiler
308-
local flags = toolset.getCFlags(cfg)
335+
local flags = getCFlags(prj, prj.tools)
309336

310337
if #flags > 0 then
311338
wl('ALL_CFLAGS += %s', table.concat(flags, ' '))
@@ -322,18 +349,15 @@ end
322349
---
323350
function proj.cxxFlags(prj)
324351
if isProject(prj) then
325-
local toolset = prj.compiler
326-
local flags = toolset.getCxxFlags(prj)
352+
local flags = getCxxFlags(prj, prj.tools)
327353

328354
if #flags > 0 then
329355
wl('ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) %s', table.concat(flags, ' '))
330356
else
331357
wl('ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS)')
332358
end
333359
else
334-
local cfg = prj
335-
local toolset = cfg.compiler
336-
local flags = toolset.getCxxFlags(cfg)
360+
local flags = getCxxFlags(prj, prj.tools)
337361

338362
if #flags > 0 then
339363
wl('ALL_CXXFLAGS += %s', table.concat(flags, ' '))
@@ -350,18 +374,15 @@ end
350374
---
351375
function proj.linkFlags(prj)
352376
if isProject(prj) then
353-
local toolset = prj.linker
354-
local flags = toolset.getLinkerFlags(prj)
377+
local flags = getLinkerFlags(prj, prj.tools)
355378

356379
if #flags > 0 then
357380
wl('ALL_LDFLAGS = $(LDFLAGS) %s', table.concat(flags, ' '))
358381
else
359382
wl('ALL_LDFLAGS = $(LDFLAGS)')
360383
end
361384
else
362-
local cfg = prj
363-
local toolset = cfg.linker
364-
local flags = toolset.getLinkerFlags(cfg)
385+
local flags = getLinkerFlags(prj, prj.tools)
365386

366387
if #flags then
367388
wl('ALL_LDFLAGS += %s', table.concat(flags, ' '))

modules/exporters/gmake/src/wks.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ end
139139
---
140140
function wks.projects(wk)
141141
local projects = table.map(wk.projects, function(key, value)
142-
return value.name
142+
return gmake.esc(value.name)
143143
end)
144144
if #projects > 0 then
145145
wl('PROJECTS := %s', table.concat(projects, ' '))
@@ -175,9 +175,9 @@ function wks.projectRules(wk)
175175
local deps = {}
176176

177177
if #deps > 0 then
178-
wl('%s: %s', prj.name, table.concat(deps, ' '))
178+
wl('%s: %s', gmake.esc(prj.name), table.concat(deps, ' '))
179179
else
180-
wl('%s:', prj.name)
180+
wl('%s:', gmake.esc(prj.name))
181181
end
182182
wl('ifneq (, $(config))')
183183
export.indent()
@@ -187,7 +187,7 @@ function wks.projectRules(wk)
187187
local prjName = path.getName(prjPath)
188188

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

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

0 commit comments

Comments
 (0)