|
| 1 | +--[[ |
| 2 | + This .lua file can be loaded via dofile() from premake4.lua in order to |
| 3 | + modify the behavior a bit. |
| 4 | +
|
| 5 | + It does the following (in order of appearence below): |
| 6 | +
|
| 7 | + - On older premake4 versions it will provide a premake.project.getbasename |
| 8 | + function, furthermore two other functions get patched to make use of it |
| 9 | + - premake.project.getbasename() gets overridden to insert a marker into the |
| 10 | + created file name, based on the chosen action |
| 11 | + Example: foobar.vcxproj becomes foobar.vs2022.vcxproj etc ... |
| 12 | + The purpose of this exercise is to allow for projects/solutions of several |
| 13 | + Visual Studio versions to reside in the same folder |
| 14 | + - Options "dotnet" gets removed |
| 15 | + - The "platform" option has some allowed values removed |
| 16 | + - The "os" option has some allowed values removed |
| 17 | + - The actions are trimmed to what we know can work |
| 18 | +
|
| 19 | + Consult the premake4.lua in the same folder for more details. |
| 20 | +
|
| 21 | + PS: There should be no adverse effects from commenting out the dofile() line |
| 22 | + in the main premake4.lua file. |
| 23 | +]] |
| 24 | +-- SPDX-License-Identifier: Unlicense |
| 25 | + |
| 26 | +do |
| 27 | + -- This is mainly to support older premake4 builds |
| 28 | + if not premake.project.getbasename then |
| 29 | + print "Magic happens ..." |
| 30 | + -- override the function to establish the behavior we'd get after patching Premake to have premake.project.getbasename |
| 31 | + premake.project.getbasename = function(prjname, pattern) |
| 32 | + return pattern:gsub("%%%%", prjname) |
| 33 | + end |
| 34 | + -- obviously we also need to overwrite the following to generate functioning VS solution files |
| 35 | + premake.vstudio.projectfile = function(prj) |
| 36 | + local pattern |
| 37 | + if prj.language == "C#" then |
| 38 | + pattern = "%%.csproj" |
| 39 | + else |
| 40 | + pattern = iif(action > "vs2008", "%%.vcxproj", "%%.vcproj") |
| 41 | + end |
| 42 | + |
| 43 | + local fname = premake.project.getbasename(prj.name, pattern) |
| 44 | + fname = path.join(prj.location, fname) |
| 45 | + return fname |
| 46 | + end |
| 47 | + -- we simply overwrite the original function on older Premake versions |
| 48 | + premake.project.getfilename = function(prj, pattern) |
| 49 | + local fname = premake.project.getbasename(prj.name, pattern) |
| 50 | + fname = path.join(prj.location, fname) |
| 51 | + return path.getrelative(os.getcwd(), fname) |
| 52 | + end |
| 53 | + end |
| 54 | + -- Name the project files after their VS version |
| 55 | + local orig_getbasename = premake.project.getbasename |
| 56 | + premake.project.getbasename = function(prjname, pattern) |
| 57 | + -- The below is used to insert the .vs(2003..2022) into the file names for projects and solutions |
| 58 | + if _ACTION and string.find(_ACTION, "vs") ~= nil then |
| 59 | + pattern = pattern:gsub("%%%%", "%%%%." .. _ACTION) |
| 60 | + end |
| 61 | + return orig_getbasename(prjname, pattern) |
| 62 | + end |
| 63 | + |
| 64 | + -- Remove an option altogether or some otherwise accepted values for that option |
| 65 | + local function remove_allowed_optionvalues(option, values_toremove) |
| 66 | + if premake.option.list[option] ~= nil then |
| 67 | + if values_toremove == nil then |
| 68 | + premake.option.list[option] = nil |
| 69 | + return |
| 70 | + end |
| 71 | + if premake.option.list.platform["allowed"] ~= nil then |
| 72 | + local allowed = premake.option.list[option].allowed |
| 73 | + for i = #allowed, 1, -1 do |
| 74 | + if values_toremove[allowed[i][1]] then |
| 75 | + table.remove(allowed, i) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + local function remove_action(action) |
| 83 | + if premake.action.list[action] ~= nil then |
| 84 | + premake.action.list[action] = nil |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + -- Remove some unwanted/outdated options |
| 89 | + remove_allowed_optionvalues("dotnet") |
| 90 | + remove_allowed_optionvalues("platform", { universal = 0, universal32 = 0, universal64 = 0, ps3 = 0, xbox360 = 0, }) |
| 91 | + remove_allowed_optionvalues("os", { haiku = 0, solaris = 0, }) |
| 92 | + -- ... and actions (mainly because they are untested) |
| 93 | + for k,v in pairs({codeblocks = 0, codelite = 0, xcode3 = 0, xcode4 = 0, vs2002 = 0}) do |
| 94 | + remove_action(k) |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +_G["transformMN"] = function (input) -- transform the macro names for older Visual Studio versions |
| 99 | + local new_map = { vs2002 = 0, vs2003 = 0, vs2005 = 0, vs2008 = 0 } |
| 100 | + local replacements = { Platform = "PlatformName", Configuration = "ConfigurationName" } |
| 101 | + if new_map[action] ~= nil then |
| 102 | + for k,v in pairs(replacements) do |
| 103 | + if input:find(k) then |
| 104 | + input = input:gsub(k, v) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + return input |
| 109 | +end |
0 commit comments