summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/genie/src/actions/make/make_vala.lua
blob: 308605802ef634be37df4b7ae0dc6c4759b2b398 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
--
-- make_vala.lua
-- Generate a Vala project makefile.
--

	premake.make.vala = { }
	premake.make.makefile_ignore = false

	local vala = premake.make.vala
	local make = premake.make

	function premake.make_vala(prj)

		-- create a shortcut to the compiler interface
		local valac = premake.gettool(prj)

		-- build a list of supported target platforms that also includes a generic build
		local platforms = premake.filterplatforms(prj.solution, valac.platforms, "Native")

		-- output build configurations
		premake.gmake_vala_header(prj, valac, platforms)

		-- write configuration blocks
		for _, platform in ipairs(platforms) do
			for cfg in premake.eachconfig(prj, platform) do
				premake.gmake_valac_config(prj, cfg, valac)
			end
		end

		-- list object directories
		local objdirs = {}
		local additionalobjdirs = {}
		for _, file in ipairs(prj.allfiles) do
			if path.issourcefile(file) or path.isgresource(file) then
				objdirs[_MAKE.esc(path.getdirectory(path.trimdots(file)))] = 1
			end
		end

		_p('OBJDIRS := \\')
		_p('\t$(OBJDIR) \\')
		for dir, _ in iter.sortByKeys(objdirs) do
			_p('\t$(OBJDIR)/%s \\', dir)
		end
		for dir, _ in iter.sortByKeys(additionalobjdirs) do
			_p('\t%s \\', dir)
		end
		_p('')

		-- main build rule(s)
		_p('.PHONY: clean prebuild prelink')
		_p('')

		_p('all: $(OBJDIRS) $(TARGETDIR) prebuild prelink $(TARGET)')
		_p('\t@:')
		_p('')

		-- target build rule
		_p('$(TARGET): $(OBJECTS) | $(TARGETDIR)')
		_p('\t@echo Linking %s', prj.name)
		_p('\t$(SILENT) $(LINKCMD)')

		_p('\t$(POSTBUILDCMDS)')
		_p('')

		-- Create destination directories. Can't use $@ for this because it loses the
		-- escaping, causing issues with spaces and parenthesis
		_p('$(TARGETDIR):')
		premake.make_mkdirrule("$(TARGETDIR)")

		_p('$(OBJDIRS):')
		if (not prj.solution.messageskip) or (not table.contains(prj.solution.messageskip, "SkipCreatingMessage")) then
		_p('\t@echo Creating $(@)')
		end
		_p('\t-$(call MKDIR,$@)')
		_p('')

		-- clean target
		_p('clean:')
		if (not prj.solution.messageskip) or (not table.contains(prj.solution.messageskip, "SkipCleaningMessage")) then
			_p('\t@echo Cleaning %s', prj.name)
		end
		_p('ifeq (posix,$(SHELLTYPE))')
		_p('\t$(SILENT) rm -f  $(TARGET)')
		_p('else')
		_p('\t$(SILENT) if exist $(subst /,\\\\,$(TARGET)) del $(subst /,\\\\,$(TARGET))')
		_p('endif')
		_p('')

		-- custom build step targets
		_p('prebuild:')
		_p('\t$(PREBUILDCMDS)')
		_p('')

		_p('prelink:')
		_p('\t$(PRELINKCMDS)')
		_p('')

		vala.fileRules(prj, valac)
	end



--
-- Write the makefile header
--

	function premake.gmake_vala_header(prj, valac, platforms)
		_p('# %s project makefile autogenerated by GENie', premake.action.current().shortname)
		_p('')

		_p('.SUFFIXES:') -- Delete the default suffix rules.
		_p('')

		-- set up the environment
		_p('ifndef config')
		_p('  config=%s', _MAKE.esc(premake.getconfigname(prj.solution.configurations[1], platforms[1], true)))
		_p('endif')
		_p('')

		_p('ifndef verbose')
		_p('  SILENT = @')
		_p('endif')
		_p('')

		-- identify the shell type
		_p('SHELLTYPE := msdos')
		_p('ifeq (,$(ComSpec)$(COMSPEC))')
		_p('  SHELLTYPE := posix')
		_p('endif')
		_p('ifeq (/bin,$(findstring /bin,$(SHELL)))')
		_p('  SHELLTYPE := posix')
		_p('endif')
		_p('ifeq (/bin,$(findstring /bin,$(MAKESHELL)))')
		_p('  SHELLTYPE := posix')
		_p('endif')
		_p('')

		_p('ifeq (posix,$(SHELLTYPE))')
		_p('  MKDIR = $(SILENT) mkdir -p "$(1)"')
		_p('  COPY  = $(SILENT) cp -fR "$(1)" "$(2)"')
		_p('  RM    = $(SILENT) rm -f "$(1)"')
		_p('else')
		_p('  MKDIR = $(SILENT) mkdir "$(subst /,\\\\,$(1))" 2> nul || exit 0')
		_p('  COPY  = $(SILENT) copy /Y "$(subst /,\\\\,$(1))" "$(subst /,\\\\,$(2))"')
		_p('  RM    = $(SILENT) del /F "$(subst /,\\\\,$(1))" 2> nul || exit 0')
		_p('endif')
		_p('')

		_p('VALAC  = %s', valac.valac)
		_p('CC     = %s', valac.cc)
		_p('GLIBRC = %s', valac.glibrc)
		_p('')

		if (not premake.make.makefile_ignore) then
			_p('MAKEFILE = %s', _MAKE.getmakefilename(prj, true))
			_p('')
		end
	end



--
-- Write a block of configuration settings.
--

	local function is_excluded(prj, cfg, file)
		if table.icontains(prj.excludes, file) then
			return true
		end

		if table.icontains(cfg.excludes, file) then
			return true
		end

		return false
	end

	function premake.gmake_valac_config(prj, cfg, valac)

		_p('ifeq ($(config),%s)', _MAKE.esc(cfg.shortname))

		_p('  BASEDIR     = %s', _MAKE.esc(path.getrelative(cfg.location, _WORKING_DIR)))
		_p('  OBJDIR      = %s', _MAKE.esc(cfg.objectsdir))
		_p('  TARGETDIR   = %s', _MAKE.esc(cfg.buildtarget.directory))
		_p('  TARGET      = $(TARGETDIR)/%s', _MAKE.esc(cfg.buildtarget.name))
		_p('  DEFINES    +=%s', make.list(valac.getdefines(cfg.defines)))
		_p('  VAPIDIRS   +=%s', make.list(valac.getvapidirs(cfg.vapidirs)))
		_p('  PKGS       +=%s', make.list(valac.getlinks(cfg.links)))
		_p('  FLAGS      += $(DEFINES) $(VAPIDIRS) $(PKGS)%s', make.list(table.join(valac.getvalaflags(cfg), cfg.buildoptions_vala)))
		_p('  VALA_LDFLAGS= $(shell pkg-config --libs%s)', make.list(cfg.links))
		_p('  ALL_LDFLAGS+= $(LDFLAGS)%s $(VALA_LDFLAGS)', make.list(table.join(cfg.linkoptions)))
		_p('  LINKOBJS    = %s', "$(OBJECTS)")
		_p('  VALA_CFLAGS = $(shell pkg-config --cflags%s)%s', make.list(cfg.links), make.list(valac.getvalaccflags(cfg)))
		_p('  ALL_CFLAGS += $(CFLAGS) $(ARCH)%s $(VALA_CFLAGS)', make.list(table.join(cfg.buildoptions, cfg.buildoptions_c)))
		_p('  LINKCMD     = $(CC) -o $(TARGET) $(LINKOBJS) $(ARCH) $(ALL_LDFLAGS)');

		table.sort(cfg.files)

		-- add objects for compilation, and remove any that are excluded per config.
		_p('  OBJECTS := \\')
		for _, file in ipairs(cfg.files) do
			if path.issourcefile(file) or path.isgresource(file) then
				if not is_excluded(prj, cfg, file) then
					_p('\t$(OBJDIR)/%s.o \\'
						, _MAKE.esc(path.trimdots(path.removeext(file)))
						)
				end
			end
		end
		_p('')

		_p('  SOURCES := \\')
		for _, file in ipairs(cfg.files) do
			if path.issourcefile(file) and path.isvalafile(file) then
				if not is_excluded(prj, cfg, file) then
					_p('\t%s \\', _MAKE.esc(file))
				end
			end
		end
		_p('')

		_p('  GRESOURCES := \\')
		for _, file in ipairs(cfg.files) do
			if path.isgresource(file) then
				if not is_excluded(prj, cfg, file) then
					_p('\t%s \\', _MAKE.esc(file))
				end
			end
		end
		_p('')

		_p('  define PREBUILDCMDS')
		if #cfg.prebuildcommands > 0 then
			_p('\t@echo Running pre-build commands')
			_p('\t%s', table.implode(cfg.prebuildcommands, "", "", "\n\t"))
		end
		_p('  endef')

		_p('  define PRELINKCMDS')
		if #cfg.prelinkcommands > 0 then
			_p('\t@echo Running pre-link commands')
			_p('\t%s', table.implode(cfg.prelinkcommands, "", "", "\n\t"))
		end
		_p('  endef')

		_p('  define POSTBUILDCMDS')
		if #cfg.postbuildcommands > 0 then
			_p('\t@echo Running post-build commands')
			_p('\t%s', table.implode(cfg.postbuildcommands, "", "", "\n\t"))
		end
		_p('  endef')

		_p('endif')
		_p('')
	end



--
-- Build command for a single file.
--

	function vala.fileRules(prj, cc)
		local platforms = premake.filterplatforms(prj.solution, cc.platforms, "Native")

		_p('ifneq (,$(OBJRESP))')
		_p('$(OBJRESP): $(OBJECTS) | $(TARGETDIR) $(OBJDIRS)')
		_p('\t$(SILENT) echo $^')
		_p('\t$(SILENT) echo $^ > $@')
		_p('endif')
		_p('')

		_p('ifneq (,$(LDRESP))')
		_p('$(LDRESP): $(LDDEPS) | $(TARGETDIR) $(OBJDIRS)')
		_p('\t$(SILENT) echo $^')
		_p('\t$(SILENT) echo $^ > $@')
		_p('endif')
		_p('')

		local pattern_targets = ""
		table.sort(prj.allfiles)
		for _, file in ipairs(prj.allfiles or {}) do
			if path.issourcefile(file) or path.isgresource(file) then
				if path.isvalafile(file) or path.iscfile(file) or path.isgresource(file) then
					if path.isvalafile(file) or path.isgresource(file) then
						_p('$(OBJDIR)/%s.o: $(OBJDIR)/%s.c $(GCH) $(MAKEFILE) | $(OBJDIR)/%s'
							, _MAKE.esc(path.trimdots(path.removeext(file)))
							, _MAKE.esc(path.trimdots(path.removeext(file)))
							, _MAKE.esc(path.getdirectory(path.trimdots(file)))
							)
						if not path.isgresource(file) then
							pattern_targets = pattern_targets
								.. "$(OBJDIR)/"
								.. _MAKE.esc(path.trimdots(path.removeext(file)))
								.. ".% \\\n" -- Pattern rule: https://stackoverflow.com/a/3077254
						end
					else
						_p('$(OBJDIR)/%s.o: %s $(GCH) $(MAKEFILE) | $(OBJDIR)/%s'
							, _MAKE.esc(path.trimdots(path.removeext(file)))
							, file
							, _MAKE.esc(path.getdirectory(path.trimdots(file)))
							)
					end
					if prj.msgcompile then
						_p('\t@echo ' .. prj.msgcompile)
					else
						_p('\t@echo $(notdir $<)')
					end
					_p('\t$(SILENT) %s $(FORCE_INCLUDE) -o "$@" -c "$<"'
						, "$(CC) $(ALL_CFLAGS)"
						, _MAKE.esc(path.getbasename(file))
						)
					for _, task in ipairs(prj.postcompiletasks or {}) do
						_p('\t$(SILENT) %s', task)
						_p('')
					end
					_p('')

					if path.isgresource(file) then
						_p('$(OBJDIR)/%s.c: %s $(GCH) $(MAKEFILE) | $(OBJDIR)/%s'
							, _MAKE.esc(path.trimdots(path.removeext(file)))
							, _MAKE.esc(file)
							, _MAKE.esc(path.getdirectory(path.trimdots(file)))
							)
						if prj.msgcompile then
							_p('\t@echo ' .. prj.msgcompile)
						else
							_p('\t@echo $(notdir $<)')
						end
						_p('\t$(SILENT) %s "$<" --target="$@" --sourcedir="%s" --generate'
							, "$(GLIBRC)"
							, _MAKE.esc(path.getdirectory(file))
							)
						for _, task in ipairs(prj.postcompiletasks or {}) do
							_p('\t$(SILENT) %s', task)
							_p('')
						end
					end

					_p('')
				end
			end
		end

		if pattern_targets ~= "" then
			-- Generate .c from corresponding .vala
			_p('%s: $(SOURCES) $(GRESOURCES) $(GCH) $(MAKEFILE)', pattern_targets)
			if prj.msgcompile then
				_p('\t@echo ' .. prj.msgcompile)
			else
				_p('\t@echo [GEN] valac')
			end
			_p('\t$(SILENT) %s $(SOURCES) --directory $(OBJDIR) --basedir $(BASEDIR) --gresources=$(GRESOURCES) -C'
				, "$(VALAC) $(FLAGS)"
				)
			for _, task in ipairs(prj.postcompiletasks or {}) do
				_p('\t$(SILENT) %s', task)
				_p('')
			end
		end
	end