Skip to content

Commit 06f4c39

Browse files
Introducing ClangFormat tool for code formatting (#379)
* first impl of clang format * added clang format in CI * fixe var name * fixed path * test non-formatting code * reverted changes * fixed include headers sorting * fixed pipeline
1 parent 3449d18 commit 06f4c39

File tree

225 files changed

+2612
-2169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+2612
-2169
lines changed

.github/workflows/Engine-CI.yml

+10
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ on:
55
branches: [ master, develop ]
66

77
jobs:
8+
clang-format:
9+
strategy:
10+
matrix:
11+
directories: [ZEngine, Tetragrama]
12+
uses: ./.github/workflows/job-clangformat.yml
13+
with:
14+
srcDirectory: ${{ matrix.directories }}
15+
816
windows:
17+
needs: clang-format
918
uses: ./.github/workflows/windows-build.yml
1019

1120
macOS:
21+
needs: clang-format
1222
uses: ./.github/workflows/macOS-build.yml
1323

1424
# linux:

.github/workflows/job-clangformat.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: ZEngine Code Formatting
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
srcDirectory:
7+
type: string
8+
9+
jobs:
10+
format:
11+
name: clang-format
12+
runs-on: windows-2022
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Checking formatting
18+
run: .\Scripts\ClangFormat.ps1 -SourceDirectory ${{ github.workspace }}/${{ inputs.srcDirectory }} -RunAsCheck 1
19+
shell: pwsh

.github/workflows/job-cmakebuild-macOS.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/checkout@v4
2222

2323
- name: CMake Build
24-
run: .\Scripts\BuildEngine.ps1 -Configurations ${{inputs.configuration}}
24+
run: .\Scripts\BuildEngine.ps1 -Configurations ${{inputs.configuration}} -RunClangFormat 0
2525
shell: pwsh
2626

2727
- name: Publish Build Artifacts

.github/workflows/job-cmakebuild-windows.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/checkout@v4
1919

2020
- name: CMake Build
21-
run: .\Scripts\BuildEngine.ps1 -Configurations ${{inputs.configuration}}
21+
run: .\Scripts\BuildEngine.ps1 -Configurations ${{inputs.configuration}} -RunClangFormat 0
2222
shell: pwsh
2323

2424
- name: Publish Build Artifacts

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![ZEngine Windows Build](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/windows-build.yml/badge.svg)](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/windows-build.yml) [![ZEngine Linux Build](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/linux-build.yml/badge.svg)](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/linux-build.yml) [![ZEngine macOS Build](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/macOS-build.yml/badge.svg)](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/macOS-build.yml)
1+
[![Engine Build and Tests](https://github.com/JeanPhilippeKernel/RendererEngine/actions/workflows/Engine-CI.yml/badge.svg)]
22

33
[![Discord Server](https://discord.com/api/guilds/1249429728624906405/widget.png?style=banner2)](https://discord.gg/jC3GPVKKsW)
44

@@ -25,6 +25,7 @@ Before building, make sure your setup is correct :
2525
- Install [Python](https://www.python.org/ftp/python/3.12.4/python-3.12.4-amd64.exe)
2626
- Install [CMake](https://cmake.org/download/) 3.20 or later.
2727
- Install [DOTNET SDK 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)
28+
- Install [LLVM](https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/LLVM-18.1.8-win64.exe)
2829

2930
### Setup macOS machine
3031

@@ -53,6 +54,12 @@ Before building, make sure your setup is correct :
5354
```
5455
- Install [DOTNET SDK 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)
5556

57+
- Install ClangFormat through Homebrew:
58+
```bash
59+
brew update
60+
brew install clang-format@18
61+
```
62+
5663
## Building the engine & launcher
5764

5865
1. Start `Powershell Core` and make sure that you can run CMake, You can type `cmake --version` to simply output the current CMake version installed.

Scripts/BuildEngine.ps1

+27-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ param (
3131
[Parameter(HelpMessage = "Whether to run build, default to True")]
3232
[bool] $RunBuilds = $True,
3333

34+
[Parameter(HelpMessage = "Whether to run clang format to format the code, default to True")]
35+
[bool] $RunClangFormat = $True,
36+
37+
[Parameter(HelpMessage = "Whether to check code formatting correctness, default to False")]
38+
[bool] $VerifyFormatting = $False,
39+
3440
[Parameter(HelpMessage = "Whether to rebuild shader files")]
3541
[switch] $ForceShaderRebuild,
3642

@@ -233,8 +239,28 @@ function Build([string]$configuration, [int]$VsVersion , [bool]$runBuild) {
233239
}
234240
}
235241

236-
# Run Shader Compilation
242+
237243
if(-Not $LauncherOnly) {
244+
245+
# Run Clang format
246+
if ($RunClangFormat) {
247+
[string]$clangFormatScript = Join-Path $PSScriptRoot -ChildPath "ClangFormat.ps1"
248+
[string[]]$srcDirectories = @(
249+
(Join-Path $repositoryRootPath -ChildPath "ZEngine"),
250+
(Join-Path $repositoryRootPath -ChildPath "Tetragrama")
251+
)
252+
253+
foreach ($directory in $srcDirectories) {
254+
& pwsh -File $clangFormatScript -SourceDirectory $directory -RunAsCheck:$VerifyFormatting
255+
256+
if ($LASTEXITCODE -ne 0) {
257+
Write-Error "Stopped build process..." -ErrorAction Stop
258+
}
259+
}
260+
}
261+
262+
263+
# Run Shader Compilation
238264
[bool]$forceRebuild = If ($ForceShaderRebuild) { $True } Else { $False }
239265
foreach ($config in $Configurations) {
240266
$shaderCompileScript = Join-Path $PSScriptRoot -ChildPath "ShaderCompile.ps1"

Scripts/ClangFormat.ps1

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# MIT License
2+
3+
# Copyright (c) 2024 Jean Philippe & Contributors
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
#Requires -PSEdition Core
24+
25+
param (
26+
[Parameter(Mandatory, HelpMessage = "The directory that contains source code to format")]
27+
[string]$SourceDirectory,
28+
29+
[Parameter(HelpMessage = "Whether clang-format should only check if the source code is well-formatted")]
30+
[bool]$RunAsCheck=$False
31+
)
32+
33+
$ErrorActionPreference = "Stop"
34+
35+
. (Join-Path $PSScriptRoot Shared.ps1)
36+
37+
$srcFiles = Get-ChildItem -Path $SourceDirectory -Recurse -File | Where-Object { $_.Name -notlike "CMakeLists*" -and $_.Name -notlike "*.json" }
38+
39+
if ($srcFiles.Count -eq 0) {
40+
Write-Host "No source files found in the specified directory."
41+
return
42+
}
43+
44+
$srcFiles = $srcFiles | ForEach-Object { $_.FullName }
45+
46+
Write-Host "Running clang-format on $SourceDirectory..."
47+
48+
[string[]] $clangFormatArgument = "-i", "--ferror-limit=0", "-fallback-style=none", "--style=file"
49+
50+
if ($RunAsCheck) {
51+
$clangFormatArgument += "--dry-run", "--Werror"
52+
}
53+
54+
$clangFormatProgram = Find-ClangFormat
55+
56+
$process = Start-Process $clangFormatProgram -ArgumentList "$clangFormatArgument $srcFiles" -NoNewWindow -PassThru
57+
$handle = $process.Handle
58+
$process.WaitForExit()
59+
$exitCode = $process.ExitCode
60+
61+
if ($exitCode -ne 0) {
62+
Write-Error "clang-format failed formatting source with exit code '$exitCode'" -ErrorAction Stop
63+
}
64+
else {
65+
Write-Host "clang-format source formatting succeeded"
66+
}

Scripts/Shared.ps1

+29
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,36 @@ function Find-GlslangValidator () {
183183
throw "Failed to find glslangValidator. Tried: " + ($GlslangValidatorCandidates -join ', ')
184184
}
185185

186+
function Find-ClangFormat () {
187+
$repoConfiguration = Get-RepositoryConfiguration
188+
$LLVMMinimumVersion = $repoConfiguration.Requirements.LLVM.Version
189+
$LLVMMaximumVersion = $repoConfiguration.Requirements.LLVM.MaximumVersion
190+
191+
$candidates = @(
192+
'clang-format'
193+
if ($IsMacOS) {
194+
$brewPrefixPath = Invoke-Expression -Command "& brew --prefix"
195+
Join-Path $brewPrefixPath -ChildPath 'opt/llvm/bin/clang-format'
196+
}
197+
if ($IsWindows) {
198+
Join-Path -Path $env:ProgramFiles -ChildPath 'LLVM\bin\clang-format.exe'
199+
}
200+
)
186201

202+
foreach ($candidate in $candidates) {
203+
$clangFormatCommand = Get-Command $candidate -ErrorAction SilentlyContinue
204+
if ($clangFormatCommand) {
205+
if ((& $clangFormatCommand --version | Out-String) -match "clang-format version ([\d\.]*)") {
206+
[Version] $clangFormatVersion = $Matches[1]
207+
if ((CompareVersion $clangFormatVersion $LLVMMinimumVersion) -and (CompareVersion $LLVMMaximumVersion $clangFormatVersion)) {
208+
return $clangFormatCommand.Source
209+
}
210+
}
211+
}
212+
}
213+
214+
throw "Failed to find clang-format min $LLVMMinimumVersion max $LLVMMaximumVersion. Tried: " + ($candidates -join ', ')
215+
}
187216

188217
function Setup-ShaderCCompilerTool () {
189218
$repoConfiguration = Get-RepositoryConfiguration

Tetragrama/src/Components/AboutUIComponent.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#pragma once
22
#include <ZEngine/ZEngine.h>
33

4-
namespace Tetragrama::Components {
5-
class AboutUIComponent : public ZEngine::Components::UI::UIComponent {
4+
namespace Tetragrama::Components
5+
{
6+
class AboutUIComponent : public ZEngine::Components::UI::UIComponent
7+
{
68
public:
79
AboutUIComponent(std::string_view name = "AboutUIComponent", bool visibility = true) : UIComponent(name, visibility, true) {}
810
virtual ~AboutUIComponent() = default;
911

10-
virtual void Render() override {
12+
virtual void Render() override
13+
{
1114
ImGui::ShowAboutWindow(&m_is_open);
1215
}
1316

Tetragrama/src/Components/DemoUIComponent.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#pragma once
22
#include <ZEngine/ZEngine.h>
33

4-
namespace Tetragrama::Components {
5-
class DemoUIComponent : public ZEngine::Components::UI::UIComponent {
4+
namespace Tetragrama::Components
5+
{
6+
class DemoUIComponent : public ZEngine::Components::UI::UIComponent
7+
{
68
public:
79
DemoUIComponent(std::string_view name = "DemoUIComponent", bool visibility = true) : UIComponent(name, visibility, true) {}
810
virtual ~DemoUIComponent() = default;
911

10-
virtual void Render() override {
12+
virtual void Render() override
13+
{
1114
ImGui::ShowDemoWindow(&m_is_open);
1215
}
1316

Tetragrama/src/Components/DockspaceUIComponent.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#include <pch.h>
22
#include <DockspaceUIComponent.h>
3-
#include <ZEngine/Logging/LoggerDefinition.h>
3+
#include <Editor.h>
44
#include <Event/EventDispatcher.h>
5-
#include <imgui/src/imgui_internal.h>
5+
#include <Helpers/UIDispatcher.h>
6+
#include <Helpers/WindowsHelper.h>
7+
#include <Importers/AssimpImporter.h>
68
#include <MessageToken.h>
79
#include <Messengers/Messenger.h>
8-
#include <Helpers/WindowsHelper.h>
9-
#include <Editor.h>
10-
#include <Helpers/UIDispatcher.h>
11-
12-
10+
#include <ZEngine/Logging/LoggerDefinition.h>
1311
#include <fmt/format.h>
14-
#include <Importers/AssimpImporter.h>
12+
#include <imgui/src/imgui_internal.h>
1513

1614
namespace fs = std::filesystem;
1715
using namespace ZEngine::Components::UI::Event;
@@ -24,7 +22,6 @@ namespace Tetragrama::Components
2422
std::string DockspaceUIComponent::s_asset_importer_report_msg = "";
2523
float DockspaceUIComponent::s_editor_scene_serializer_progress = 0.0f;
2624

27-
2825
DockspaceUIComponent::DockspaceUIComponent(std::string_view name, bool visibility)
2926
: UIComponent(name, visibility, false), m_asset_importer(ZEngine::CreateScope<Importers::AssimpImporter>()),
3027
m_editor_serializer(ZEngine::CreateScope<Serializers::EditorSceneSerializer>())
@@ -68,7 +65,7 @@ namespace Tetragrama::Components
6865

6966
#ifdef _WIN32
7067
std::replace(scene_fullname.begin(), scene_fullname.end(), '/', '\\'); // Todo : Move this replace into an helper function....
71-
#endif // _WIN32
68+
#endif // _WIN32
7269

7370
m_editor_serializer->Deserialize(scene_fullname);
7471
}
@@ -384,7 +381,7 @@ namespace Tetragrama::Components
384381
ImGui::SameLine();
385382
if (ImGui::Button("Cancel", ImVec2(120, 0)))
386383
{
387-
m_open_exit = false;
384+
m_open_exit = false;
388385
m_pending_shutdown = false;
389386
ImGui::CloseCurrentPopup();
390387
}

Tetragrama/src/Components/DockspaceUIComponent.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
2-
#include <ZEngine/ZEngine.h>
3-
#include <Message.h>
42
#include <Importers/IAssetImporter.h>
3+
#include <Message.h>
54
#include <Serializers/EditorSceneSerializer.h>
5+
#include <ZEngine/ZEngine.h>
66

77
namespace Tetragrama::Components
88
{

Tetragrama/src/Components/Events/SceneTextureAvailableEvent.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
#pragma once
22
#include <ZEngine/ZEngine.h>
33

4-
namespace Tetragrama::Components::Event {
4+
namespace Tetragrama::Components::Event
5+
{
56

6-
class SceneTextureAvailableEvent : public ZEngine::Components::UI::Event::UIComponentEvent {
7+
class SceneTextureAvailableEvent : public ZEngine::Components::UI::Event::UIComponentEvent
8+
{
79
public:
810
SceneTextureAvailableEvent(uint32_t texture_identifier) : m_texture_identifier(texture_identifier) {}
911

1012
EVENT_TYPE(SceneTextureAvailable)
1113

12-
uint32_t GetSceneTexture() const {
14+
uint32_t GetSceneTexture() const
15+
{
1316
return m_texture_identifier;
1417
}
1518

16-
virtual ZEngine::Event::EventType GetType() const override {
19+
virtual ZEngine::Event::EventType GetType() const override
20+
{
1721
return GetStaticType();
1822
}
1923

20-
virtual int GetCategory() const override {
24+
virtual int GetCategory() const override
25+
{
2126
return GetStaticCategory();
2227
}
2328

Tetragrama/src/Components/Events/SceneViewportFocusedEvent.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#pragma once
22
#include <ZEngine/ZEngine.h>
33

4-
namespace Tetragrama::Components::Event {
4+
namespace Tetragrama::Components::Event
5+
{
56

6-
class SceneViewportFocusedEvent : public ZEngine::Components::UI::Event::UIComponentEvent {
7+
class SceneViewportFocusedEvent : public ZEngine::Components::UI::Event::UIComponentEvent
8+
{
79
public:
810
SceneViewportFocusedEvent() = default;
911
~SceneViewportFocusedEvent() = default;
1012

1113
EVENT_TYPE(SceneViewportFocused)
1214

13-
virtual ZEngine::Event::EventType GetType() const override {
15+
virtual ZEngine::Event::EventType GetType() const override
16+
{
1417
return GetStaticType();
1518
}
1619

17-
virtual int GetCategory() const override {
20+
virtual int GetCategory() const override
21+
{
1822
return GetStaticCategory();
1923
}
2024
};

0 commit comments

Comments
 (0)