XMake: Add header guard check
This commit is contained in:
parent
ed7ab31933
commit
b85f6b68ac
|
|
@ -66,7 +66,7 @@ on_run(function ()
|
||||||
local checks = {}
|
local checks = {}
|
||||||
|
|
||||||
table.insert(checks, {
|
table.insert(checks, {
|
||||||
Name = "remove empty lines at the beginning",
|
Name = "empty lines",
|
||||||
Check = function (moduleName)
|
Check = function (moduleName)
|
||||||
local files = table.join(
|
local files = table.join(
|
||||||
os.files("include/Nazara/" .. moduleName .. "/**.hpp"),
|
os.files("include/Nazara/" .. moduleName .. "/**.hpp"),
|
||||||
|
|
@ -107,6 +107,166 @@ on_run(function ()
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
table.insert(checks, {
|
||||||
|
Name = "header guards",
|
||||||
|
Check = function (moduleName)
|
||||||
|
local files = table.join(
|
||||||
|
os.files("include/Nazara/" .. moduleName .. "/**.hpp"),
|
||||||
|
os.files("src/Nazara/" .. moduleName .. "/**.hpp")
|
||||||
|
)
|
||||||
|
|
||||||
|
local fixes = {}
|
||||||
|
for _, filePath in pairs(files) do
|
||||||
|
local lines = GetFile(filePath)
|
||||||
|
|
||||||
|
local pragmaLine
|
||||||
|
local ifndefLine
|
||||||
|
local defineLine
|
||||||
|
local endifLine
|
||||||
|
local macroName
|
||||||
|
|
||||||
|
local pathMacro = filePath:gsub("[/\\]", "_")
|
||||||
|
do
|
||||||
|
pathMacro = pathMacro:sub(pathMacro:lastof(moduleName .. "_", true) + #moduleName + 1)
|
||||||
|
local i = pathMacro:lastof(".", true)
|
||||||
|
if i then
|
||||||
|
pathMacro = pathMacro:sub(1, i - 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local pathHeaderGuard = (moduleName ~= pathMacro) and "NAZARA_" .. moduleName:upper() .. "_" .. pathMacro:upper() .. "_HPP" or "NAZARA_" .. moduleName:upper() .. "_HPP"
|
||||||
|
|
||||||
|
local canFix = true
|
||||||
|
local ignored = false
|
||||||
|
|
||||||
|
-- Fetch pragma once, ifdef and define lines
|
||||||
|
for i = 1, #lines do
|
||||||
|
if lines[i] == "// no header guards" then
|
||||||
|
canFix = false
|
||||||
|
ignored = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
if lines[i] == "#pragma once" then
|
||||||
|
if pragmaLine then
|
||||||
|
print(filePath .. ": multiple #pragma once found")
|
||||||
|
canFix = false
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
pragmaLine = i
|
||||||
|
elseif not ifndefLine and lines[i]:startswith("#ifndef") then
|
||||||
|
ifndefLine = i
|
||||||
|
|
||||||
|
macroName = lines[i]:match("^#ifndef%s+(.+)$")
|
||||||
|
if not macroName then
|
||||||
|
print(filePath .. ": failed to identify header guard macro (ifndef)")
|
||||||
|
canFix = false
|
||||||
|
break
|
||||||
|
end
|
||||||
|
elseif ifndefLine and not defineLine and lines[i]:startswith("#define") then
|
||||||
|
defineLine = i
|
||||||
|
|
||||||
|
local defineMacroName = lines[i]:match("^#define%s+(.+)$")
|
||||||
|
if not defineMacroName then
|
||||||
|
print(filePath .. ": failed to identify header guard macro (define)")
|
||||||
|
canFix = false
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
if defineMacroName ~= macroName then
|
||||||
|
print(filePath .. ": failed to identify header guard macro (define macro doesn't match ifdef)")
|
||||||
|
canFix = false
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if ifndefLine and defineLine then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not ignored then
|
||||||
|
if not ifndefLine or not defineLine or not macroName then
|
||||||
|
print(filePath .. ": failed to identify header guard macro")
|
||||||
|
canFix = false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Fetch endif line
|
||||||
|
if canFix then
|
||||||
|
local shouldFixEndif = false
|
||||||
|
|
||||||
|
for i = #lines, 1, -1 do
|
||||||
|
if lines[i]:startswith("#endif") then
|
||||||
|
local macro = lines[i]:match("#endif // (.+)")
|
||||||
|
if macro ~= macroName then
|
||||||
|
shouldFixEndif = true
|
||||||
|
end
|
||||||
|
|
||||||
|
endifLine = i
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not endifLine then
|
||||||
|
print(filePath .. ": failed to identify header guard macro (endif)")
|
||||||
|
canFix = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if canFix then
|
||||||
|
if macroName ~= pathHeaderGuard then
|
||||||
|
print(filePath .. ": header guard mismatch (got " .. macroName .. ", expected " .. pathHeaderGuard .. ")")
|
||||||
|
|
||||||
|
shouldFixEndif = false
|
||||||
|
|
||||||
|
table.insert(fixes, {
|
||||||
|
File = filePath,
|
||||||
|
Func = function (lines)
|
||||||
|
lines[ifndefLine] = "#ifndef " .. pathHeaderGuard
|
||||||
|
lines[defineLine] = "#define " .. pathHeaderGuard
|
||||||
|
lines[endifLine] = "#endif // " .. pathHeaderGuard
|
||||||
|
|
||||||
|
return lines
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if shouldFixEndif then
|
||||||
|
print(filePath .. ": #endif was missing comment")
|
||||||
|
|
||||||
|
table.insert(fixes, {
|
||||||
|
File = filePath,
|
||||||
|
Func = function (lines)
|
||||||
|
lines[endifLine] = "#endif // " .. pathHeaderGuard
|
||||||
|
|
||||||
|
return lines
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
if not pragmaLine then
|
||||||
|
print(filePath .. ": no #pragma once found")
|
||||||
|
table.insert(fixes, {
|
||||||
|
File = filePath,
|
||||||
|
Func = function (lines)
|
||||||
|
table.insert(lines, ifndefLine - 1, "#pragma once")
|
||||||
|
table.insert(lines, ifndefLine - 1, "")
|
||||||
|
|
||||||
|
return lines
|
||||||
|
end
|
||||||
|
})
|
||||||
|
elseif pragmaLine > ifndefLine then
|
||||||
|
print(filePath .. ": #pragma once is after header guard (should be before)")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return fixes
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
table.insert(checks, {
|
table.insert(checks, {
|
||||||
Name = "copyright",
|
Name = "copyright",
|
||||||
Check = function (moduleName)
|
Check = function (moduleName)
|
||||||
|
|
@ -255,7 +415,7 @@ on_run(function ()
|
||||||
table.insert(newLines, lines[i])
|
table.insert(newLines, lines[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
UpdateFile(configFilePath, newLines)
|
return newLines
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
@ -332,7 +492,7 @@ on_run(function ()
|
||||||
table.insert(lines, #copyrightLines + 1, "")
|
table.insert(lines, #copyrightLines + 1, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
UpdateFile(filePath, lines)
|
return lines
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
@ -359,7 +519,9 @@ on_run(function ()
|
||||||
if shouldFix then
|
if shouldFix then
|
||||||
for _, fix in pairs(fixes) do
|
for _, fix in pairs(fixes) do
|
||||||
print("Fixing " .. fix.File)
|
print("Fixing " .. fix.File)
|
||||||
fix.Func(assert(fileLines[fix.File]))
|
UpdateFile(fix.File, fix.Func(assert(fileLines[fix.File])))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for filePath, _ in pairs(updatedFiles) do
|
for filePath, _ in pairs(updatedFiles) do
|
||||||
|
|
@ -371,6 +533,4 @@ on_run(function ()
|
||||||
print("Saving changes to " .. filePath)
|
print("Saving changes to " .. filePath)
|
||||||
io.writefile(filePath, table.concat(lines, "\n"))
|
io.writefile(filePath, table.concat(lines, "\n"))
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ on_run(function ()
|
||||||
paths[moduleName .. "_Components"] = {
|
paths[moduleName .. "_Components"] = {
|
||||||
Excludes = table.copy(excludedFiles),
|
Excludes = table.copy(excludedFiles),
|
||||||
Header = head,
|
Header = head,
|
||||||
HeaderGuard = "NAZARA_GLOBAL_" .. moduleName:upper() .. "_COMPONENTS_HPP",
|
HeaderGuard = "NAZARA_" .. moduleName:upper() .. "_COMPONENTS_HPP",
|
||||||
Name = "Nazara" .. moduleName .. " components",
|
Name = "Nazara" .. moduleName .. " components",
|
||||||
SearchDir = modulePath .. "/Components",
|
SearchDir = modulePath .. "/Components",
|
||||||
Target = modulePath .. "/Components.hpp"
|
Target = modulePath .. "/Components.hpp"
|
||||||
|
|
@ -64,7 +64,7 @@ on_run(function ()
|
||||||
paths[moduleName .. "_Systems"] = {
|
paths[moduleName .. "_Systems"] = {
|
||||||
Excludes = table.copy(excludedFiles),
|
Excludes = table.copy(excludedFiles),
|
||||||
Header = head,
|
Header = head,
|
||||||
HeaderGuard = "NAZARA_GLOBAL_" .. moduleName:upper() .. "_SYSTEMS_HPP",
|
HeaderGuard = "NAZARA_" .. moduleName:upper() .. "_SYSTEMS_HPP",
|
||||||
Name = "Nazara" .. moduleName .. " systems",
|
Name = "Nazara" .. moduleName .. " systems",
|
||||||
SearchDir = modulePath .. "/Systems",
|
SearchDir = modulePath .. "/Systems",
|
||||||
Target = modulePath .. "/Systems.hpp"
|
Target = modulePath .. "/Systems.hpp"
|
||||||
|
|
@ -85,7 +85,7 @@ on_run(function ()
|
||||||
["InstanceFunctions.hpp"] = true,
|
["InstanceFunctions.hpp"] = true,
|
||||||
},
|
},
|
||||||
Header = paths["OpenGLRenderer"].Header,
|
Header = paths["OpenGLRenderer"].Header,
|
||||||
HeaderGuard = "NAZARA_GLOBAL_OPENGLRENDERER_WRAPPER_HPP",
|
HeaderGuard = "NAZARA_OPENGLRENDERER_WRAPPER_HPP",
|
||||||
Name = "OpenGL wrapper",
|
Name = "OpenGL wrapper",
|
||||||
SearchDir = "include/Nazara/OpenGLRenderer/Wrapper",
|
SearchDir = "include/Nazara/OpenGLRenderer/Wrapper",
|
||||||
Target = "include/Nazara/OpenGLRenderer/Wrapper.hpp"
|
Target = "include/Nazara/OpenGLRenderer/Wrapper.hpp"
|
||||||
|
|
@ -99,7 +99,7 @@ on_run(function ()
|
||||||
["InstanceFunctions.hpp"] = true,
|
["InstanceFunctions.hpp"] = true,
|
||||||
},
|
},
|
||||||
Header = paths["VulkanRenderer"].Header,
|
Header = paths["VulkanRenderer"].Header,
|
||||||
HeaderGuard = "NAZARA_GLOBAL_VULKANRENDERER_WRAPPER_HPP",
|
HeaderGuard = "NAZARA_VULKANRENDERER_WRAPPER_HPP",
|
||||||
Name = "Vulkan wrapper",
|
Name = "Vulkan wrapper",
|
||||||
SearchDir = "include/Nazara/VulkanRenderer/Wrapper",
|
SearchDir = "include/Nazara/VulkanRenderer/Wrapper",
|
||||||
Target = "include/Nazara/VulkanRenderer/Wrapper.hpp"
|
Target = "include/Nazara/VulkanRenderer/Wrapper.hpp"
|
||||||
|
|
|
||||||
|
|
@ -72,11 +72,11 @@ on_run(function()
|
||||||
assert(headerFile, "failed to open Spir-V header")
|
assert(headerFile, "failed to open Spir-V header")
|
||||||
|
|
||||||
headerFile:write([[
|
headerFile:write([[
|
||||||
// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq
|
// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
// This file is part of the "Nazara Engine - Shader module"
|
// This file is part of the "Nazara Engine - Shader module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp"
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
// This file was generated automatically, please do not edit
|
// this file was automatically generated and should not be edited
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
@ -211,12 +211,12 @@ headerFile:write([[
|
||||||
assert(sourceFile, "failed to open Spir-V source")
|
assert(sourceFile, "failed to open Spir-V source")
|
||||||
|
|
||||||
sourceFile:write([[
|
sourceFile:write([[
|
||||||
// this file was automatically generated and should not be edited
|
// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
|
||||||
// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq
|
|
||||||
// This file is part of the "Nazara Engine - Shader module"
|
// This file is part of the "Nazara Engine - Shader module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
// this file was automatically generated and should not be edited
|
||||||
|
|
||||||
#include <Nazara/Shader/SpirvData.hpp>
|
#include <Nazara/Shader/SpirvData.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
|
||||||
|
|
@ -179,6 +179,7 @@ local DirectionToString = {}
|
||||||
|
|
||||||
file:write([[
|
file:write([[
|
||||||
// this file was automatically generated and should not be edited
|
// this file was automatically generated and should not be edited
|
||||||
|
// no header guards
|
||||||
|
|
||||||
// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq
|
// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq
|
||||||
// This file is part of the "Nazara Engine - Core module"
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue