Skip to content

Commit beaac48

Browse files
committed
Factoring out the patches into a separate .lua file
1 parent b0fdee6 commit beaac48

File tree

2 files changed

+116
-56
lines changed

2 files changed

+116
-56
lines changed

premake4-patches.lua

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

premake4.lua

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,12 @@
1515

1616
local action = _ACTION or ""
1717
local tgtname = "tommath"
18-
do
19-
-- This is mainly to support older premake4 builds
20-
if not premake.project.getbasename then
21-
print "Magic happens ..."
22-
-- override the function to establish the behavior we'd get after patching Premake to have premake.project.getbasename
23-
premake.project.getbasename = function(prjname, pattern)
24-
return pattern:gsub("%%%%", prjname)
25-
end
26-
-- obviously we also need to overwrite the following to generate functioning VS solution files
27-
premake.vstudio.projectfile = function(prj)
28-
local pattern
29-
if prj.language == "C#" then
30-
pattern = "%%.csproj"
31-
else
32-
pattern = iif(_ACTION > "vs2008", "%%.vcxproj", "%%.vcproj")
33-
end
34-
35-
local fname = premake.project.getbasename(prj.name, pattern)
36-
fname = path.join(prj.location, fname)
37-
return fname
38-
end
39-
-- we simply overwrite the original function on older Premake versions
40-
premake.project.getfilename = function(prj, pattern)
41-
local fname = premake.project.getbasename(prj.name, pattern)
42-
fname = path.join(prj.location, fname)
43-
return path.getrelative(os.getcwd(), fname)
44-
end
45-
end
46-
-- Name the project files after their VS version
47-
local orig_getbasename = premake.project.getbasename
48-
premake.project.getbasename = function(prjname, pattern)
49-
-- The below is used to insert the .vs(8|9|10|11|12|14|15|16) into the file names for projects and solutions
50-
if _ACTION then
51-
pattern = pattern:gsub("%%%%", "%%%%." .. _ACTION)
52-
end
53-
return orig_getbasename(prjname, pattern)
54-
end
55-
end
56-
57-
local function transformMN(input) -- transform the macro names for older Visual Studio versions
58-
local new_map = { vs2002 = 0, vs2003 = 0, vs2005 = 0, vs2008 = 0 }
59-
local replacements = { Platform = "PlatformName", Configuration = "ConfigurationName" }
60-
if new_map[action] ~= nil then
61-
for k,v in pairs(replacements) do
62-
if input:find(k) then
63-
input = input:gsub(k, v)
64-
end
65-
end
66-
end
67-
return input
68-
end
18+
dofile("premake4-patches.lua") -- can be commented out, if desired
19+
local transformMN = _G["transformMN"] or function(input) return input end
6920

7021
solution ("lib" .. tgtname)
7122
configurations ({"Debug", "Release"})
72-
platforms (iif(_ACTION < "vs2005", {"x32"}, {"x32", "x64"}))
23+
platforms (iif(action < "vs2005", {"x32"}, {"x32", "x64"}))
7324
location ('.')
7425

7526
project (tgtname)
@@ -83,7 +34,7 @@ solution ("lib" .. tgtname)
8334
objdir (outdir .. "\\Intermediate")
8435
libdirs {"$(IntDir)"}
8536
includedirs {"."} -- not really needed, but we try to stay true to makefile.msvc
86-
defines {"WIN32_LEAN_AND_MEAN", "WINVER=0x0501", "WIN32", "_CRT_SECURE_NO_WARNINGS", "_CRT_NONSTDC_NO_DEPRECATE",}-- "__STDC_WANT_SECURE_LIB__=1", "_CRT_HAS_CXX17=0",}
37+
defines {"WIN32_LEAN_AND_MEAN", "WINVER=0x0501", "WIN32", "_CRT_SECURE_NO_WARNINGS", "_CRT_NONSTDC_NO_DEPRECATE",}
8738
buildoptions {"/Z7", "/Wall", "/wd4146", "/wd4127", "/wd4668", "/wd4710", "/wd4711", "/wd4820",}
8839
-- /Z7 for a static lib includes all debug symbols inside the object files, meaning there is no need to distribute PDB _and_ .lib file
8940

@@ -126,12 +77,12 @@ solution ("lib" .. tgtname)
12677
configuration {"Release", "x64"}
12778
targetsuffix ("64")
12879

129-
configuration("vs2003 or vs2005 or vs2008")
130-
buildoptions {"/wd4255",}
131-
13280
configuration("vs2003 or vs2005")
13381
buildoptions {"/wd4242", "/wd4244",}
13482

83+
configuration("vs2003 or vs2005 or vs2008")
84+
buildoptions {"/wd4255",}
85+
13586
configuration("vs2019 or vs2022")
13687
buildoptions {"/wd5045",}
13788

0 commit comments

Comments
 (0)