Skip to content

Commit

Permalink
Factoring out the patches into a separate .lua file
Browse files Browse the repository at this point in the history
  • Loading branch information
assarbad committed Feb 14, 2022
1 parent b0fdee6 commit beaac48
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 56 deletions.
109 changes: 109 additions & 0 deletions premake4-patches.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
--[[
This .lua file can be loaded via dofile() from premake4.lua in order to
modify the behavior a bit.
It does the following (in order of appearence below):
- On older premake4 versions it will provide a premake.project.getbasename
function, furthermore two other functions get patched to make use of it
- premake.project.getbasename() gets overridden to insert a marker into the
created file name, based on the chosen action
Example: foobar.vcxproj becomes foobar.vs2022.vcxproj etc ...
The purpose of this exercise is to allow for projects/solutions of several
Visual Studio versions to reside in the same folder
- Options "dotnet" gets removed
- The "platform" option has some allowed values removed
- The "os" option has some allowed values removed
- The actions are trimmed to what we know can work
Consult the premake4.lua in the same folder for more details.
PS: There should be no adverse effects from commenting out the dofile() line
in the main premake4.lua file.
]]
-- SPDX-License-Identifier: Unlicense

do
-- This is mainly to support older premake4 builds
if not premake.project.getbasename then
print "Magic happens ..."
-- override the function to establish the behavior we'd get after patching Premake to have premake.project.getbasename
premake.project.getbasename = function(prjname, pattern)
return pattern:gsub("%%%%", prjname)
end
-- obviously we also need to overwrite the following to generate functioning VS solution files
premake.vstudio.projectfile = function(prj)
local pattern
if prj.language == "C#" then
pattern = "%%.csproj"
else
pattern = iif(action > "vs2008", "%%.vcxproj", "%%.vcproj")
end

local fname = premake.project.getbasename(prj.name, pattern)
fname = path.join(prj.location, fname)
return fname
end
-- we simply overwrite the original function on older Premake versions
premake.project.getfilename = function(prj, pattern)
local fname = premake.project.getbasename(prj.name, pattern)
fname = path.join(prj.location, fname)
return path.getrelative(os.getcwd(), fname)
end
end
-- Name the project files after their VS version
local orig_getbasename = premake.project.getbasename
premake.project.getbasename = function(prjname, pattern)
-- The below is used to insert the .vs(2003..2022) into the file names for projects and solutions
if _ACTION and string.find(_ACTION, "vs") ~= nil then
pattern = pattern:gsub("%%%%", "%%%%." .. _ACTION)
end
return orig_getbasename(prjname, pattern)
end

-- Remove an option altogether or some otherwise accepted values for that option
local function remove_allowed_optionvalues(option, values_toremove)
if premake.option.list[option] ~= nil then
if values_toremove == nil then
premake.option.list[option] = nil
return
end
if premake.option.list.platform["allowed"] ~= nil then
local allowed = premake.option.list[option].allowed
for i = #allowed, 1, -1 do
if values_toremove[allowed[i][1]] then
table.remove(allowed, i)
end
end
end
end
end

local function remove_action(action)
if premake.action.list[action] ~= nil then
premake.action.list[action] = nil
end
end

-- Remove some unwanted/outdated options
remove_allowed_optionvalues("dotnet")
remove_allowed_optionvalues("platform", { universal = 0, universal32 = 0, universal64 = 0, ps3 = 0, xbox360 = 0, })
remove_allowed_optionvalues("os", { haiku = 0, solaris = 0, })
-- ... and actions (mainly because they are untested)
for k,v in pairs({codeblocks = 0, codelite = 0, xcode3 = 0, xcode4 = 0, vs2002 = 0}) do
remove_action(k)
end
end

_G["transformMN"] = function (input) -- transform the macro names for older Visual Studio versions
local new_map = { vs2002 = 0, vs2003 = 0, vs2005 = 0, vs2008 = 0 }
local replacements = { Platform = "PlatformName", Configuration = "ConfigurationName" }
if new_map[action] ~= nil then
for k,v in pairs(replacements) do
if input:find(k) then
input = input:gsub(k, v)
end
end
end
return input
end
63 changes: 7 additions & 56 deletions premake4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,12 @@

local action = _ACTION or ""
local tgtname = "tommath"
do
-- This is mainly to support older premake4 builds
if not premake.project.getbasename then
print "Magic happens ..."
-- override the function to establish the behavior we'd get after patching Premake to have premake.project.getbasename
premake.project.getbasename = function(prjname, pattern)
return pattern:gsub("%%%%", prjname)
end
-- obviously we also need to overwrite the following to generate functioning VS solution files
premake.vstudio.projectfile = function(prj)
local pattern
if prj.language == "C#" then
pattern = "%%.csproj"
else
pattern = iif(_ACTION > "vs2008", "%%.vcxproj", "%%.vcproj")
end

local fname = premake.project.getbasename(prj.name, pattern)
fname = path.join(prj.location, fname)
return fname
end
-- we simply overwrite the original function on older Premake versions
premake.project.getfilename = function(prj, pattern)
local fname = premake.project.getbasename(prj.name, pattern)
fname = path.join(prj.location, fname)
return path.getrelative(os.getcwd(), fname)
end
end
-- Name the project files after their VS version
local orig_getbasename = premake.project.getbasename
premake.project.getbasename = function(prjname, pattern)
-- The below is used to insert the .vs(8|9|10|11|12|14|15|16) into the file names for projects and solutions
if _ACTION then
pattern = pattern:gsub("%%%%", "%%%%." .. _ACTION)
end
return orig_getbasename(prjname, pattern)
end
end

local function transformMN(input) -- transform the macro names for older Visual Studio versions
local new_map = { vs2002 = 0, vs2003 = 0, vs2005 = 0, vs2008 = 0 }
local replacements = { Platform = "PlatformName", Configuration = "ConfigurationName" }
if new_map[action] ~= nil then
for k,v in pairs(replacements) do
if input:find(k) then
input = input:gsub(k, v)
end
end
end
return input
end
dofile("premake4-patches.lua") -- can be commented out, if desired
local transformMN = _G["transformMN"] or function(input) return input end

solution ("lib" .. tgtname)
configurations ({"Debug", "Release"})
platforms (iif(_ACTION < "vs2005", {"x32"}, {"x32", "x64"}))
platforms (iif(action < "vs2005", {"x32"}, {"x32", "x64"}))
location ('.')

project (tgtname)
Expand All @@ -83,7 +34,7 @@ solution ("lib" .. tgtname)
objdir (outdir .. "\\Intermediate")
libdirs {"$(IntDir)"}
includedirs {"."} -- not really needed, but we try to stay true to makefile.msvc
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",}
defines {"WIN32_LEAN_AND_MEAN", "WINVER=0x0501", "WIN32", "_CRT_SECURE_NO_WARNINGS", "_CRT_NONSTDC_NO_DEPRECATE",}
buildoptions {"/Z7", "/Wall", "/wd4146", "/wd4127", "/wd4668", "/wd4710", "/wd4711", "/wd4820",}
-- /Z7 for a static lib includes all debug symbols inside the object files, meaning there is no need to distribute PDB _and_ .lib file

Expand Down Expand Up @@ -126,12 +77,12 @@ solution ("lib" .. tgtname)
configuration {"Release", "x64"}
targetsuffix ("64")

configuration("vs2003 or vs2005 or vs2008")
buildoptions {"/wd4255",}

configuration("vs2003 or vs2005")
buildoptions {"/wd4242", "/wd4244",}

configuration("vs2003 or vs2005 or vs2008")
buildoptions {"/wd4255",}

configuration("vs2019 or vs2022")
buildoptions {"/wd5045",}

Expand Down

0 comments on commit beaac48

Please sign in to comment.