Source

premake-dev / src / project / project.lua

The default branch has multiple heads

Full commit
  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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
--
-- src/project/project.lua
-- Premake project object API
-- Copyright (c) 2011-2012 Jason Perkins and the Premake project
--

	premake5.project = {}
	local project = premake5.project
	local context = premake.context
	local oven = premake5.oven
	local configset = premake.configset


--
-- Create a new project object.
--
-- @param sln
--    The solution object to contain the new project.
-- @param name
--    The new project's name.
-- @return
--    A new project object, contained by the specified solution.
--

	function project.new(sln, name)
		local prj = {}

		prj.name = name
		prj.solution = sln
		prj.blocks = {}

		local cwd = os.getcwd()

		local cset = configset.new(sln.configset)
		cset.basedir = cwd
		cset.location = cwd
		cset.filename = name
		cset.uuid = os.uuid()
		prj.configset = cset

		-- attach a type descriptor
		setmetatable(prj, {
			__type = "project",
			__index = function(prj, key)
				return prj.configset[key]
			end,
		})

		return prj
	end


--
-- Flatten out a project and all of its configurations, merging all of the
-- values contained in the script-supplied configuration blocks.
--

	function project.bake(prj, sln)
		-- make sure I've got the actual project, and not the root configurations
		prj = prj.project or prj

		-- set up an environment for expanding tokens contained by this project
		local environ = {
			sln = sln,
			prj = prj,
		}

		-- create a context to represent the project's "root" configuration; some
		-- of the filter terms may be nil, so not safe to use a list
		local ctx = context.new(prj.configset, environ)
		context.addterms(ctx, _ACTION)
		context.addterms(ctx, prj.language)
		
		-- allow script to override system and architecture
		ctx.system = ctx.system or premake.action.current().os or os.get()
		context.addterms(ctx, ctx.system)
		context.addterms(ctx, ctx.architecture)

		-- if a kind is specified at the project level, use that too
		context.addterms(ctx, ctx.kind)
	
		-- attach a bit more local state
		ctx.solution = sln
		
		
		-- TODO: OLD, REMOVE: build an old-style configuration to wrap context, for now 
		local result = oven.merge(oven.merge({}, sln), prj)
		result.solution = sln
		result.blocks = prj.blocks
		result.baked = true

		-- prevent any default system setting from influencing configurations
		result.system = nil
		

		-- TODO: HACK, TRANSITIONAL, REMOVE: pass requests for missing values 
		-- through to the config context. Eventually all values will be in the 
		-- context and the cfg wrapper can be done away with
		setmetatable(prj, nil)

		result.context = ctx
		prj.context = ctx		
		setmetatable(result, {
			__index = function(prj, key)
				return prj.context[key]
			end,
		})
		setmetatable(prj, getmetatable(result))


		-- apply any mappings to the project's configuration set
		result.cfglist = project.bakeconfigmap(result)

		-- bake all configurations contained by the project
		local configs = {}
		for _, pairing in ipairs(result.cfglist) do
			local buildcfg = pairing[1]
			local platform = pairing[2]
			local cfg = project.bakeconfig(result, buildcfg, platform)
			
			-- make sure this config is supported by the action; skip if not
			if premake.action.supportsconfig(cfg) then
				configs[(buildcfg or "*") .. (platform or "")] = cfg
			end
		end
		result.configs = configs
		
		return result
	end


--
-- Flattens out the build settings for a particular build configuration and
-- platform pairing, and returns the result.
--

	function project.bakeconfig(prj, buildcfg, platform)
		-- set the default system and architecture values; for backward 
		-- compatibility, use platform if it would be a valid value
		local system = premake.action.current().os or os.get()
		local architecture
		if platform then
			system = premake.api.checkvalue(platform, premake.fields.system.allowed) or system
			architecture = premake.api.checkvalue(platform, premake.fields.architecture.allowed) or architecture
		end

		-- set up an environment for expanding tokens contained by this configuration
		local environ = {
			sln = sln,
			prj = prj,
		}

		-- create a context to represent this configuration; contains the terms
		-- that defines what belongs to this configuration, and controls access
		local ctx = context.new(prj.configset, environ)

		-- add base filters; some may be nil, so not safe to put in a list
		context.addterms(ctx, buildcfg)
		context.addterms(ctx, platform)
		context.addterms(ctx, _ACTION)
		context.addterms(ctx, prj.language)

		-- allow the project script to override the default system
		ctx.system = ctx.system or system
		context.addterms(ctx, ctx.system)

		-- allow the project script to override the default architecture
		ctx.architecture = ctx.architecture or architecture
		context.addterms(ctx, ctx.architecture)

		-- allow configuration to override the project kind
		context.addterms(ctx, ctx.kind)

		-- attach a bit more local state
		ctx.project = prj
		ctx.solution = prj.solution
		ctx.buildcfg = buildcfg
		ctx.platform = platform
		ctx.action = _ACTION
		ctx.language = prj.language


		
		-- TODO: OLD, REMOVE: build an old-style configuration to wrap context, for now 
		local filter = {
			["buildcfg"] = buildcfg,
			["platform"] = platform,
			["action"] = _ACTION,
			["system"] = ctx.system,
			["architecture"] = ctx.architecture,
		}
		
		local cfg = oven.bake(prj, prj.solution, filter)
		cfg.solution = prj.solution
		cfg.project = prj
		cfg.context = ctx

		-- File this under "too clever by half": I want path tokens (targetdir, etc.)
		-- to expand to relative paths, so they can be used in custom build rules and
		-- other places where it would be impractical to detect and convert them. So
		-- create a proxy object with an attached metatable that converts path fields
		-- on the fly as they are requested.
		local proxy = {}
		setmetatable(proxy, {
			__index = function(proxy, key)
				local field = premake.fields[key]
				if field and field.kind == "path" then
					return premake5.project.getrelative(cfg.project, cfg[key])
				end
				return cfg[key]
			end
		})

		environ.cfg = proxy


		-- TODO: HACK, TRANSITIONAL, REMOVE: pass requests for missing values 
		-- through to the config context. Eventually all values will be in the 
		-- context and the cfg wrapper can be done away with
		setmetatable(cfg, {
			__index = function(cfg, key)
				return cfg.context[key]
			end,
		})

		
		-- fill in any calculated values
		premake5.config.bake(cfg)

		return cfg
	end


--
-- Builds a list of build configuration/platform pairs for a project,
-- along with a mapping between the solution and project configurations.
-- @param prj
--    The project to query.
-- @return
--    Two values: 
--      - an array of the project's build configuration/platform
--        pairs, based on the result of the mapping
--      - a key-value table that maps solution build configuration/
--        platform pairs to project configurations.
--

	function project.bakeconfigmap(prj)
		local configs = table.fold(prj.configurations or {}, prj.platforms or {})
		for i, cfg in ipairs(configs) do
			configs[i] = project.mapconfig(prj, cfg[1], cfg[2])
		end
		
		-- walk through the result and remove duplicates
		local buildcfgs = {}
		local platforms = {}
		
		for _, pairing in ipairs(configs) do
			local buildcfg = pairing[1]
			local platform = pairing[2]
			
			if not table.contains(buildcfgs, buildcfg) then
				table.insert(buildcfgs, buildcfg)
			end
			
			if platform and not table.contains(platforms, platform) then
				table.insert(platforms, platform)
			end
		end

		-- merge these canonical lists back into pairs for the final result
		configs = table.fold(buildcfgs, platforms)	
		return configs
	end


--
-- Returns an iterator function for the configuration objects contained by
-- the project. Each configuration corresponds to a build configuration/
-- platform pair (i.e. "Debug|x32") as specified in the solution.
--
-- @param prj
--    The project object to query.
-- @return
--    An iterator function returning configuration objects.
--

	function project.eachconfig(prj)
		-- to make testing a little easier, allow this function to
		-- accept an unbaked project, and fix it on the fly
		if not prj.baked then
			prj = project.bake(prj, prj.solution)
		end

		local configs = prj.cfglist
		local count = #configs
		
		local i = 0
		return function ()
			i = i + 1
			if i <= count then
				return project.getconfig(prj, configs[i][1], configs[i][2])
			end
		end
	end


-- 
-- Locate a project by name; case insensitive.
--
-- @param name
--    The name of the project for which to search.
-- @return
--    The corresponding project, or nil if no matching project could be found.
--

	function project.findproject(name)
		for sln in premake.solution.each() do
			for _, prj in ipairs(sln.projects) do
				if (prj.name == name) then
					return  prj
				end
			end
		end
	end


--
-- Retrieve the project's configuration information for a particular build 
-- configuration/platform pair.
--
-- @param prj
--    The project object to query.
-- @param buildcfg
--    The name of the build configuration on which to filter.
-- @param platform
--    Optional; the name of the platform on which to filter.
-- @return
--    A configuration object.
--
	
	function project.getconfig(prj, buildcfg, platform)
		-- to make testing a little easier, allow this function to
		-- accept an unbaked project, and fix it on the fly
		if not prj.baked then
			prj = project.bake(prj, prj.solution)
		end
	
		-- if no build configuration is specified, return the "root" project
		-- configurations, which includes all configuration values that
		-- weren't set with a specific configuration filter
		if not buildcfg then
			return prj
		end
		
		-- apply any configuration mappings
		local pairing = project.mapconfig(prj, buildcfg, platform)
		buildcfg = pairing[1]
		platform = pairing[2]

		-- look up and return the associated config		
		local key = (buildcfg or "*") .. (platform or "")
		return prj.configs[key]
	end


--
-- Returns a list of sibling projects on which the specified project depends. 
-- This is used to list dependencies within a solution or workspace. Must 
-- consider all configurations because Visual Studio does not support per-config
-- project dependencies.
--
-- @param prj
--    The project to query.
-- @return
--    A list of dependent projects, as an array of project objects.
--

	function project.getdependencies(prj)
		local result = {}

		for cfg in project.eachconfig(prj) do
			for _, link in ipairs(cfg.links) do
				local dep = premake.solution.findproject(cfg.solution, link)
				if dep and not table.contains(result, dep) then
					table.insert(result, dep)
				end
			end
		end

		return result
	end


--
-- Builds a file configuration for a specific file from a project.
--
-- @param prj
--    The project to query.
-- @param filename
--    The absolute path of the file to query.
-- @return
--    A corresponding file configuration object.
--

	function project.getfileconfig(prj, filename)
		local fcfg = {}

		fcfg.abspath = filename
		fcfg.relpath = project.getrelative(prj, filename)

		local vpath = project.getvpath(prj, filename)
		if vpath ~= filename then
			fcfg.vpath = vpath
		else
			fcfg.vpath = fcfg.relpath
		end

		fcfg.name = path.getname(filename)
		fcfg.basename = path.getbasename(filename)
		fcfg.path = fcfg.relpath
		
		return fcfg
	end


--
-- Returns the file name for this project. Also works with solutions.
--
-- @param prj
--    The project object to query.
-- @param ext
--    An optional file extension to add, with the leading dot.
-- @return
--    The absolute path to the project's file.
--

	function project.getfilename(prj, ext)
		local fn = path.join(project.getlocation(prj), prj.filename)
		if ext then fn = fn .. ext end
		return fn
	end


--
-- Returns a unique object file name for a project source code file.
--
-- @param prj
--    The project object to query.
-- @param filename
--    The name of the file being compiled to the object file.
--

	function project.getfileobject(prj, filename)
		-- make sure I have the project, and not it's root configuration
		prj = prj.project or prj
		
		-- create a list of objects if necessary
		prj.fileobjects = prj.fileobjects or {}

		-- look for the corresponding object file		
		local basename = path.getbasename(filename)
		local uniqued = basename
		local i = 0
		
		while prj.fileobjects[uniqued] do
			-- found a match?
			if prj.fileobjects[uniqued] == filename then
				return uniqued
			end
			
			-- check a different name
			i = i + 1
			uniqued = basename .. i
		end
		
		-- no match, create a new one
		prj.fileobjects[uniqued] = filename
		return uniqued
	end


--
-- Return the first configuration of a project, which is used in some
-- actions to generate project-wide defaults.
--
-- @param prj
--    The project object to query.
-- @return
--    The first configuration in a project, as would be returned by
--    eachconfig().
--

	function project.getfirstconfig(prj)
		local iter = project.eachconfig(prj)
		local first = iter()
		return first
	end


--
-- Retrieve the project's file system location. Also works with solutions.
--
-- @param prj
--    The project object to query.
-- @param relativeto
--    Optional; if supplied, the project location will be made relative
--    to this path.
-- @return
--    The path to the project's file system location.
--

	function project.getlocation(prj, relativeto)
		local location = prj.location
		if not location and prj.solution then
			location = prj.solution.location
		end
		if not location then
			location = prj.basedir
		end	
		if relativeto then
			location = path.getrelative(relativeto, location)
		end
		return location
	end


--
-- Return the relative path from the project to the specified file.
--
-- @param prj
--    The project object to query.
-- @param filename
--    The file path, or an array of file paths, to convert.
-- @return
--    The relative path, or array of paths, from the project to the file.
--

	function project.getrelative(prj, filename)
		if type(filename) == "table" then
			local result = {}
			for i, name in ipairs(filename) do
				result[i] = project.getrelative(prj, name)
			end
			return result
		else
			if filename then
				return path.getrelative(project.getlocation(prj), filename)
			end
		end
	end


--
-- Create a tree from a project's list of source files.
--
-- @param prj
--    The project to query.
-- @return
--    A tree object containing the source file hierarchy. Leaf nodes
--    representing the individual files contain the fields:
--      abspath  - the absolute path of the file
--      relpath  - the relative path from the project to the file
--      vpath    - the file's virtual path
--    All nodes contain the fields:
--      path     - the node's path within the tree
--      realpath - the node's file system path (nil for virtual paths)
--      name     - the directory or file name represented by the node
--

	function project.getsourcetree(prj)
		-- make sure I have the project, and not it's root configuration
		prj = prj.project or prj
		
		-- check for a previously cached tree
		if prj.sourcetree then
			return prj.sourcetree
		end

		-- find *all* files referenced by the project, regardless of configuration
		local files = {}
		for cfg in project.eachconfig(prj) do
			for _, file in ipairs(cfg.files) do
				files[file] = file
			end
		end

		-- create a file config lookup cache
		prj.fileconfigs = {}
		
		-- create a tree from the file list
		local tr = premake.tree.new(prj.name)
		
		for file in pairs(files) do
			local fcfg = project.getfileconfig(prj, file)

			-- The tree represents the logical source code tree to be displayed
			-- in the IDE, not the physical organization of the file system. So
			-- virtual paths are used when adding nodes.
			local node = premake.tree.add(tr, fcfg.vpath, function(node)
				-- ...but when a real file system path is used, store it so that
				-- an association can be made in the IDE 
				if fcfg.vpath == fcfg.relpath then
					node.realpath = node.path
				end
			end)
			
			-- Store full file configuration in file (leaf) nodes
			for key, value in pairs(fcfg) do
				node[key] = value
			end
			
			prj.fileconfigs[node.abspath] = node
		end

		premake.tree.trimroot(tr)
		premake.tree.sort(tr)
		
		-- cache result and return
		prj.sourcetree = tr
		return tr
	end


--
-- Given a source file path, return a corresponding virtual path based on
-- the vpath entries in the project. If no matching vpath entry is found,
-- the original path is returned.
--

	function project.getvpath(prj, filename)
		-- if there is no match, return the input filename
		local vpath = filename
		
		for replacement,patterns in pairs(prj.vpaths or {}) do
			for _,pattern in ipairs(patterns) do

				-- does the filename match this vpath pattern?
				local i = filename:find(path.wildcards(pattern))
				if i == 1 then
					-- yes; trim the pattern out of the target file's path
					local leaf
					i = pattern:find("*", 1, true) or (pattern:len() + 1)
					if i < filename:len() then
						leaf = filename:sub(i)
					else
						leaf = path.getname(filename)
					end
					if leaf:startswith("/") then
						leaf = leaf:sub(2)
					end
					
					-- check for (and remove) stars in the replacement pattern.
					-- If there are none, then trim all path info from the leaf
					-- and use just the filename in the replacement (stars should
					-- really only appear at the end; I'm cheating here)
					local stem = ""
					if replacement:len() > 0 then
						stem, stars = replacement:gsub("%*", "")
						if stars == 0 then
							leaf = path.getname(leaf)
						end
					end
					
					vpath = path.join(stem, leaf)
				end
			end
		end
		
		return vpath
	end


--
-- Determines if a project contains a particular build configuration/platform pair.
--

	function project.hasconfig(prj, buildcfg, platform)
		if buildcfg and not prj.configurations[buildcfg] then
			return false
		end
		if platform and not prj.platforms[platform] then
			return false
		end
		return true
	end


--
-- Determines if a project contains a particular source code file.
--
-- @param prj
--    The project to query.
-- @param filename
--    The absolute path to the source code file being checked.
-- @return
--    True if the file belongs to the project, in any configuration.
--

	function project.hasfile(prj, filename)
		-- make sure I have the project, and not it's root configuration
		prj = prj.project or prj

		-- TODO: the file cache should be built during the baking process;
		-- I shouldn't need to fetch the tree to get it.
		project.getsourcetree(prj)

		return prj.fileconfigs[filename] ~= nil
	end


--
-- Given a build config/platform pairing, applies any project configuration maps
-- and returns a new (or the same) pairing.
--

	function project.mapconfig(prj, buildcfg, platform)
		local pairing = { buildcfg, platform }
		
		local testpattern = function(pattern, pairing, i)
			local j = 1
			while i <= #pairing and j <= #pattern do
				if pairing[i] ~= pattern[j] then
					return false
				end
				i = i + 1
				j = j + 1
			end
			return true
		end
		
		for pattern, replacements in pairs(prj.configmap or {}) do
			if type(pattern) ~= "table" then
				pattern = { pattern }
			end
			
			-- does this pattern match any part of the pair? If so,
			-- replace it with the corresponding values
			for i = 1, #pairing do
				if testpattern(pattern, pairing, i) then
					if #pattern == 1 and #replacements == 1 then
						pairing[i] = replacements[1]
					else
						pairing = { replacements[1], replacements[2] }
					end
				end
			end
		end
				
		return pairing
	end