Skip to content

Commit ad0b1a4

Browse files
author
Damian Duy
committed
Add automatic code formatting
1 parent c185e70 commit ad0b1a4

File tree

8 files changed

+161
-51
lines changed

8 files changed

+161
-51
lines changed

.clang-format

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
IndentWidth: 4
5+
...
6+

CMakeLists.txt

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2022 Intel Corporation
1+
# Copyright (C) 2022-2023 Intel Corporation
22
# SPDX-License-Identifier: MIT
33

44
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
@@ -8,6 +8,9 @@ include(GNUInstallDirs)
88
include(CMakePackageConfigHelpers)
99
include(CTest)
1010

11+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
12+
include(helpers)
13+
1114
find_package(Python3 COMPONENTS Interpreter)
1215

1316
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
@@ -17,17 +20,17 @@ if(MSVC)
1720
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
1821
endif()
1922

20-
# define rpath for libraries so that adapters can be found automatically
23+
# Define rpath for libraries so that adapters can be found automatically
2124
set(CMAKE_BUILD_RPATH "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
2225

23-
#Define a path for custom commands to work around MSVC
26+
# Define a path for custom commands to work around MSVC
2427
set(CUSTOM_COMMAND_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
2528
if(MSVC)
26-
#MSVC implicitly adds $<CONFIG> to the output path
29+
# MSVC implicitly adds $<CONFIG> to the output path
2730
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
2831
endif()
2932

30-
#CXX compiler support
33+
# CXX compiler support
3134
if(NOT MSVC)
3235
include(CheckCXXCompilerFlag)
3336
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
@@ -42,15 +45,15 @@ if(NOT MSVC)
4245
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive -fPIC")
4346
endif()
4447

45-
#MSVC compile flags
48+
# MSVC compile flags
4649
if(MSVC)
4750
string(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
4851
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
4952

50-
# treat warnings as errors
53+
# Treat warnings as errors
5154
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX /W3 /wd4996")
5255

53-
# enable multi-process compilation
56+
# Enable multi-process compilation
5457
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
5558
endif()
5659

@@ -62,6 +65,44 @@ endif()
6265
# Option to disable tests
6366
option(${PROJECT_NAME}_BUILD_TESTS "Build unit tests." ON)
6467

68+
# Option to run a code formatter
69+
option(${PROJECT_NAME}_FORMAT_CPP_STYLE "format code style of C++ sources" OFF)
70+
71+
find_program(CLANG_FORMAT NAMES clang-format-10 clang-format-10.0 clang-format)
72+
73+
set(CLANG_FORMAT_REQUIRED "10.0")
74+
75+
if(CLANG_FORMAT)
76+
get_program_version_major_minor(${CLANG_FORMAT} CLANG_FORMAT_VERSION)
77+
message(STATUS "Found clang-format: ${CLANG_FORMAT} (version: ${CLANG_FORMAT_VERSION})")
78+
endif()
79+
80+
# Check if the correct version of clang-format is available
81+
if(FORMAT_CPP_STYLE)
82+
if(CLANG_FORMAT)
83+
if(NOT (CLANG_FORMAT_VERSION VERSION_EQUAL CLANG_FORMAT_REQUIRED))
84+
message(FATAL_ERROR "required clang-format version is ${CLANG_FORMAT_REQUIRED}")
85+
endif()
86+
else()
87+
message(FATAL_ERROR "FORMAT_CPP_STYLE=ON, but clang-format not found (required version: ${CLANG_FORMAT_REQUIRED})")
88+
endif()
89+
endif()
90+
91+
# Obtain files for clang-format
92+
file(GLOB_RECURSE format_src
93+
"*.h"
94+
"*.cpp"
95+
)
96+
97+
# Exclude files from build directory to ensure they're not passed to the formatter
98+
list(FILTER format_src EXCLUDE REGEX "${CMAKE_CURRENT_BINARY_DIR}.")
99+
100+
# Add code formatter target
101+
add_custom_target(cppformat)
102+
103+
# Add files to the formatter
104+
add_cppformat(src-formatter ${format_src})
105+
65106
add_subdirectory(${THIRD_PARTY_DIR})
66107

67108
# A header only library to specify include directories in transitive
@@ -116,16 +157,18 @@ install(
116157

117158
set(API_JSON_FILE ${PROJECT_BINARY_DIR}/unified_runtime.json)
118159

119-
# generate source from the specification
160+
# Generate source from the specification
120161
add_custom_target(generate
121162
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/scripts
122163
COMMAND ${Python3_EXECUTABLE} run.py --api-json ${API_JSON_FILE}
123164
COMMAND ${Python3_EXECUTABLE} json2src.py --api-json ${API_JSON_FILE} ${PROJECT_SOURCE_DIR}
124165
)
125166

126-
# generate source and check for uncommitted diffs
167+
# Generate source and check for uncommitted diffs
127168
add_custom_target(check-generated
128169
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
129170
COMMAND git diff --exit-code
130171
DEPENDS generate
172+
# Uncomment line below after docker image is integrated
173+
#DEPENDS cppformat
131174
)

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ Tools can be acquired via instructions in [third_party](/third_party/README.md).
4949

5050
## Building
5151

52+
Requirements:
53+
- C++ compiler with C++14 support
54+
- cmake >= 3.14.0
55+
- clang-format-10.0 (for automatic code formatting)
56+
5257
Project is defined using [CMake](https://cmake.org/).
5358

5459
**Windows**:
@@ -72,6 +77,15 @@ $ cmake {path_to_source_dir}
7277
$ make
7378
~~~~
7479

80+
### CMake standard options
81+
82+
List of options provided by CMake:
83+
84+
| Name | Description | Values | Default |
85+
| - | - | - | - |
86+
| BUILD_TESTS | Build the tests | ON/OFF | ON |
87+
| FORMAT_CPP_STYLE | Format code style | ON/OFF | OFF |
88+
7589
**General**:
7690

7791
If you've made modifications to the specification, you can also run a custom `generate` target prior to building.
@@ -80,3 +94,8 @@ $ make generate
8094
~~~~
8195

8296
This call will automatically generate the source code.
97+
98+
To run automated code formatting build with option `FORMAT_CPP_STYLE` and then run a custom `cppformat` target:
99+
~~~~
100+
$ make cppformat
101+
~~~~

cmake/helpers.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (C) 2023 Intel Corporation
2+
# SPDX-License-Identifier: MIT
3+
4+
#
5+
# helpers.cmake -- helper functions for top-level CMakeLists.txt
6+
#
7+
8+
# Sets ${ret} to version of program specified by ${name} in major.minor format
9+
function(get_program_version_major_minor name ret)
10+
execute_process(COMMAND ${name} --version
11+
OUTPUT_VARIABLE cmd_ret
12+
ERROR_QUIET)
13+
STRING(REGEX MATCH "([0-9]+)\.([0-9]+)" VERSION ${cmd_ret})
14+
SET(${ret} ${VERSION} PARENT_SCOPE)
15+
endfunction()
16+
17+
# Generates cppformat-$name targets and attaches them
18+
# as dependencies of global "cppformat" target.
19+
# Arguments are used as files to be checked.
20+
# ${name} must be unique.
21+
function(add_cppformat name)
22+
if(NOT CLANG_FORMAT OR NOT (CLANG_FORMAT_VERSION VERSION_EQUAL CLANG_FORMAT_REQUIRED))
23+
return()
24+
endif()
25+
26+
if(${ARGC} EQUAL 0)
27+
return()
28+
else()
29+
add_custom_target(cppformat-${name}
30+
COMMAND ${CLANG_FORMAT}
31+
--style=file
32+
--i
33+
${ARGN}
34+
)
35+
endif()
36+
37+
add_dependencies(cppformat cppformat-${name})
38+
endfunction()

examples/hello_world/hello_world.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
/*
22
*
3-
* Copyright (C) 2020-2021 Intel Corporation
3+
* Copyright (C) 2020-2023 Intel Corporation
44
*
55
* SPDX-License-Identifier: MIT
66
*
77
*/
8-
#include <stdlib.h>
9-
#include <memory>
108
#include <iostream>
9+
#include <memory>
10+
#include <stdlib.h>
1111
#include <vector>
1212

1313
#include "ur_api.h"
1414

1515
//////////////////////////////////////////////////////////////////////////
16-
int main(int argc, char *argv[])
17-
{
16+
int main(int argc, char *argv[]) {
1817
ur_result_t status;
1918

2019
ur_platform_handle_t platform = nullptr;
2120
ur_device_handle_t pDevice = nullptr;
2221

2322
// Initialize the platform
2423
status = urInit(0, 0);
25-
if (status != UR_RESULT_SUCCESS)
26-
{
24+
if (status != UR_RESULT_SUCCESS) {
2725
std::cout << "urInit failed with return code: " << status << std::endl;
2826
return 1;
2927
}
@@ -33,65 +31,68 @@ int main(int argc, char *argv[])
3331
std::vector<ur_platform_handle_t> platforms;
3432

3533
status = urPlatformGet(1, nullptr, &platformCount);
36-
if (status != UR_RESULT_SUCCESS)
37-
{
38-
std::cout << "urPlatformGet failed with return code: " << status << std::endl;
34+
if (status != UR_RESULT_SUCCESS) {
35+
std::cout << "urPlatformGet failed with return code: " << status
36+
<< std::endl;
3937
goto out;
4038
}
4139

4240
platforms.resize(platformCount);
4341
status = urPlatformGet(platformCount, platforms.data(), nullptr);
44-
if (status != UR_RESULT_SUCCESS)
45-
{
46-
std::cout << "urPlatformGet failed with return code: " << status << std::endl;
42+
if (status != UR_RESULT_SUCCESS) {
43+
std::cout << "urPlatformGet failed with return code: " << status
44+
<< std::endl;
4745
goto out;
4846
}
4947

50-
for (auto p : platforms)
51-
{
48+
for (auto p : platforms) {
5249
ur_api_version_t api_version = {};
5350
status = urPlatformGetApiVersion(platform, &api_version);
54-
if (status != UR_RESULT_SUCCESS)
55-
{
56-
std::cout << "urPlatformGetApiVersion failed with return code: " << status << std::endl;
51+
if (status != UR_RESULT_SUCCESS) {
52+
std::cout << "urPlatformGetApiVersion failed with return code: "
53+
<< status << std::endl;
5754
goto out;
5855
}
59-
std::cout << "API version: " << UR_MAJOR_VERSION(api_version) << "." << UR_MINOR_VERSION(api_version) << std::endl;
56+
std::cout << "API version: " << UR_MAJOR_VERSION(api_version) << "."
57+
<< UR_MINOR_VERSION(api_version) << std::endl;
6058

6159
uint32_t deviceCount = 0;
6260
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, 0, nullptr, &deviceCount);
63-
if (status != UR_RESULT_SUCCESS)
64-
{
65-
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
61+
if (status != UR_RESULT_SUCCESS) {
62+
std::cout << "urDeviceGet failed with return code: " << status
63+
<< std::endl;
6664
goto out;
6765
}
6866

6967
std::vector<ur_device_handle_t> devices(deviceCount);
70-
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, deviceCount, devices.data(), nullptr);
71-
if (status != UR_RESULT_SUCCESS)
72-
{
73-
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
68+
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, deviceCount, devices.data(),
69+
nullptr);
70+
if (status != UR_RESULT_SUCCESS) {
71+
std::cout << "urDeviceGet failed with return code: " << status
72+
<< std::endl;
7473
goto out;
7574
}
76-
for (auto d : devices)
77-
{
75+
for (auto d : devices) {
7876
ur_device_type_t device_type;
79-
status = urDeviceGetInfo(d, UR_DEVICE_INFO_TYPE, sizeof(ur_device_type_t), static_cast<void *>(&device_type), nullptr);
80-
if (status != UR_RESULT_SUCCESS)
81-
{
82-
std::cout << "urDeviceGetInfo failed with return code: " << status << std::endl;
77+
status = urDeviceGetInfo(
78+
d, UR_DEVICE_INFO_TYPE, sizeof(ur_device_type_t),
79+
static_cast<void *>(&device_type), nullptr);
80+
if (status != UR_RESULT_SUCCESS) {
81+
std::cout << "urDeviceGetInfo failed with return code: "
82+
<< status << std::endl;
8383
goto out;
8484
}
8585
static const size_t DEVICE_NAME_MAX_LEN = 1024;
8686
char device_name[DEVICE_NAME_MAX_LEN] = {0};
87-
status = urDeviceGetInfo(d, UR_DEVICE_INFO_NAME, DEVICE_NAME_MAX_LEN - 1, static_cast<void *>(&device_name), nullptr);
88-
if (status != UR_RESULT_SUCCESS)
89-
{
90-
std::cout << "urDeviceGetInfo failed with return code: " << status << std::endl;
87+
status =
88+
urDeviceGetInfo(d, UR_DEVICE_INFO_NAME, DEVICE_NAME_MAX_LEN - 1,
89+
static_cast<void *>(&device_name), nullptr);
90+
if (status != UR_RESULT_SUCCESS) {
91+
std::cout << "urDeviceGetInfo failed with return code: "
92+
<< status << std::endl;
9193
goto out;
9294
}
93-
if (device_type == UR_DEVICE_TYPE_GPU)
94-
{
95+
if (device_type == UR_DEVICE_TYPE_GPU) {
9596
std::cout << "Found a " << device_name << " gpu.\n";
9697
}
9798
}

source/loader/ur_loader.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2022 Intel Corporation
3+
* Copyright (C) 2022-2023 Intel Corporation
44
*
55
* SPDX-License-Identifier: MIT
66
*
@@ -13,7 +13,6 @@
1313

1414
#include "ur_ddi.h"
1515

16-
#include "ur_util.h"
1716
#include "ur_object.h"
1817

1918
#include "ur_ldrddi.h"

source/loader/ur_object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2022 Intel Corporation
3+
* Copyright (C) 2022-2023 Intel Corporation
44
*
55
* SPDX-License-Identifier: MIT
66
*
@@ -12,6 +12,7 @@
1212
#define UR_OBJECT_H 1
1313

1414
#include "ur_singleton.h"
15+
#include "ur_util.h"
1516

1617
//////////////////////////////////////////////////////////////////////////
1718
struct dditable_t
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright (C) 2022-2023 Intel Corporation
2+
# SPDX-License-Identifier: MIT
3+
14
add_library(ur_testing INTERFACE)
25
target_include_directories(ur_testing INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
36
add_library(${PROJECT_NAME}::testing ALIAS ur_testing)

0 commit comments

Comments
 (0)