Skip to content

Commit ea6e6bf

Browse files
authored
treewide: Auto-format CMake files (#26)
* treewide: Format CMake files with cmake-format Introduce a Makefile target as well.
1 parent da6842e commit ea6e6bf

File tree

7 files changed

+84
-69
lines changed

7 files changed

+84
-69
lines changed

.cmake-format.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"format": {
3+
"line_width": 100,
4+
"tab_size": 2,
5+
"separate_ctrl_name_with_space": true,
6+
"separate_fn_name_with_space": false,
7+
"dangle_parens": true,
8+
"dangle_align": "prefix",
9+
"line_ending": "unix"
10+
}
11+
}

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
run: |
2727
sudo apt-get update
2828
sudo apt-get install -y clang-format-9
29+
sudo pip3 install cmake-format
2930
3031
- name: lint
3132
run: make format

CMakeLists.txt

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
22

33
project(uthenticode)
44

5-
# NOTE(ww): CMake has bad defaults for install prefixes.
6-
# Instead of fussing over them, install everything to the build directory by default
7-
# and let the user set CMAKE_INSTALL_PREFIX explicitly for their own needs.
8-
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
9-
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "Default install directory" FORCE)
5+
# NOTE(ww): CMake has bad defaults for install prefixes. Instead of fussing over
6+
# them, install everything to the build directory by default and let the user
7+
# set CMAKE_INSTALL_PREFIX explicitly for their own needs.
8+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
9+
set(CMAKE_INSTALL_PREFIX
10+
"${CMAKE_BINARY_DIR}"
11+
CACHE PATH "Default install directory" FORCE)
1012
endif()
1113

12-
if (MSVC)
14+
if(MSVC)
1315
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
1416
endif()
1517

@@ -27,18 +29,17 @@ if(BUILD_SVCLI)
2729
add_subdirectory(src/svcli)
2830
endif()
2931

30-
# gtest setup.
31-
# Adapted roughly from the googletest README.
32+
# gtest setup. Adapted roughly from the googletest README.
3233
if(BUILD_TESTS)
3334
set(gtest_cmake_in "${CMAKE_CURRENT_SOURCE_DIR}/cmake/gtest.cmake.in")
34-
set(gtest_cmake "${CMAKE_CURRENT_BINARY_DIR}/googletest-download/CMakeLists.txt")
35+
set(gtest_cmake
36+
"${CMAKE_CURRENT_BINARY_DIR}/googletest-download/CMakeLists.txt")
3537
configure_file("${gtest_cmake_in}" "${gtest_cmake}")
3638

3739
execute_process(
3840
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
3941
RESULT_VARIABLE result
40-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
41-
)
42+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
4243

4344
if(result)
4445
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
@@ -47,27 +48,27 @@ if(BUILD_TESTS)
4748
execute_process(
4849
COMMAND ${CMAKE_COMMAND} --build .
4950
RESULT_VARIABLE result
50-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
51-
)
51+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
5252

5353
if(result)
5454
message(FATAL_ERROR "Build step for googletest failed: ${result}")
5555
endif()
5656

57-
# Prevent overriding the parent project's compiler/linker
58-
# settings on Windows
59-
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
60-
61-
# Add googletest directly to our build. This defines
62-
# the gtest and gtest_main targets.
63-
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
64-
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
65-
EXCLUDE_FROM_ALL)
66-
67-
# The gtest/gtest_main targets carry header search path
68-
# dependencies automatically when using CMake 2.8.11 or
69-
# later. Otherwise we have to add them here ourselves.
70-
if (CMAKE_VERSION VERSION_LESS 2.8.11)
57+
# Prevent overriding the parent project's compiler/linker settings on Windows
58+
set(gtest_force_shared_crt
59+
ON
60+
CACHE BOOL "" FORCE)
61+
62+
# Add googletest directly to our build. This defines the gtest and gtest_main
63+
# targets.
64+
add_subdirectory(
65+
${CMAKE_CURRENT_BINARY_DIR}/googletest-src
66+
${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
67+
68+
# The gtest/gtest_main targets carry header search path dependencies
69+
# automatically when using CMake 2.8.11 or later. Otherwise we have to add
70+
# them here ourselves.
71+
if(CMAKE_VERSION VERSION_LESS 2.8.11)
7172
include_directories("${gtest_SOURCE_DIR}/include")
7273
endif()
7374

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CLANG_FORMAT := clang-format
22
ALL_SRCS := $(shell find src test -type f \( -name '*.cpp' -o -name '*.h' \) -print)
3+
ALL_LISTFILES := $(shell find src test cmake -type f \( -name 'CMakeLists.txt' -o -name '*.cmake' \) -print)
34
VERSION := $(shell cat VERSION)
45

56
.PHONY: all
@@ -8,10 +9,17 @@ all:
89

910

1011
.PHONY: format
11-
format:
12+
format: clang-format cmake-format
13+
14+
.PHONY: clang-format
15+
clang-format:
1216
$(CLANG_FORMAT) -i -style=file $(ALL_SRCS)
1317
git diff --exit-code
1418

19+
.PHONY: cmake-format
20+
cmake-format:
21+
cmake-format -i $(ALL_LISTFILES)
22+
git diff --exit-code
1523

1624
.PHONY: doc
1725
doc:

cmake/gtest.cmake.in

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ cmake_minimum_required(VERSION 2.8.2)
33
project(googletest-download NONE)
44

55
include(ExternalProject)
6-
ExternalProject_Add(googletest
7-
GIT_REPOSITORY https://github.com/google/googletest.git
8-
GIT_TAG master
9-
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
10-
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
6+
ExternalProject_Add(
7+
googletest
8+
GIT_REPOSITORY https://github.com/google/googletest.git
9+
GIT_TAG master
10+
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
11+
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
1112
CONFIGURE_COMMAND ""
12-
BUILD_COMMAND ""
13-
INSTALL_COMMAND ""
14-
TEST_COMMAND ""
13+
BUILD_COMMAND ""
14+
INSTALL_COMMAND ""
15+
TEST_COMMAND ""
1516
)

src/CMakeLists.txt

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,51 @@ cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
22

33
project(uthenticode)
44

5-
find_package(OpenSSL 1.1 COMPONENTS Crypto REQUIRED)
5+
find_package(
6+
OpenSSL 1.1
7+
COMPONENTS Crypto
8+
REQUIRED
9+
)
610

711
add_library("${PROJECT_NAME}" uthenticode.cpp)
812

9-
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
10-
if(MSVC)
13+
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
14+
if (MSVC)
1115
target_compile_options("${PROJECT_NAME}" PUBLIC /W4 /WX)
12-
else()
16+
else ()
1317
target_compile_options("${PROJECT_NAME}" PUBLIC -Wall -Wextra -pedantic -Werror)
1418
target_compile_options("${PROJECT_NAME}" PUBLIC -fsanitize=address)
1519
target_link_options("${PROJECT_NAME}" PUBLIC -fsanitize=address)
16-
endif()
17-
endif()
20+
endif ()
21+
endif ()
1822

1923
target_include_directories(
20-
"${PROJECT_NAME}"
21-
PUBLIC
22-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
23-
$<INSTALL_INTERFACE:include>
24+
"${PROJECT_NAME}" PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
25+
$<INSTALL_INTERFACE:include>
2426
)
2527

26-
# NOTE(ww): More CMake dumbness: PUBLIC_HEADER doesn't get populated by
27-
# target_include_directories, even when set explicitly as PUBLIC.
28-
# We set it here so that it works correctly in the install step.
29-
set_target_properties(
30-
"${PROJECT_NAME}"
31-
PROPERTIES
32-
PUBLIC_HEADER "include/uthenticode.h"
33-
)
28+
# NOTE(ww): More CMake dumbness: PUBLIC_HEADER doesn't get populated by target_include_directories,
29+
# even when set explicitly as PUBLIC. We set it here so that it works correctly in the install step.
30+
set_target_properties("${PROJECT_NAME}" PROPERTIES PUBLIC_HEADER "include/uthenticode.h")
3431

3532
target_link_libraries("${PROJECT_NAME}" pe-parse::pe-parser-library)
3633
target_link_libraries("${PROJECT_NAME}" OpenSSL::Crypto)
3734

3835
install(
39-
TARGETS "${PROJECT_NAME}"
40-
EXPORT uthenticode-config
41-
RUNTIME
42-
DESTINATION "bin"
43-
LIBRARY
44-
DESTINATION "lib"
45-
ARCHIVE
46-
DESTINATION "lib"
47-
PUBLIC_HEADER
48-
DESTINATION "include"
36+
TARGETS "${PROJECT_NAME}"
37+
EXPORT uthenticode-config
38+
RUNTIME DESTINATION "bin"
39+
LIBRARY DESTINATION "lib"
40+
ARCHIVE DESTINATION "lib"
41+
PUBLIC_HEADER DESTINATION "include"
4942
)
5043
export(
5144
TARGETS "${PROJECT_NAME}"
5245
NAMESPACE uthenticode::
5346
FILE "${CMAKE_CURRENT_BINARY_DIR}/uthenticode-config.cmake"
5447
)
5548
install(
56-
EXPORT
57-
uthenticode-config
49+
EXPORT uthenticode-config
5850
DESTINATION "lib/cmake/uthenticode"
5951
NAMESPACE uthenticode::
6052
EXPORT_LINK_INTERFACE_LIBRARIES

test/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
33
project(uthenticode_test)
44

55
file(
6-
GLOB_RECURSE
7-
UTHENTICODE_TEST_SOURCES
6+
GLOB_RECURSE UTHENTICODE_TEST_SOURCES
87
LIST_DIRECTORIES false
98
*.h *.cpp
109
)
1110

1211
add_executable("${PROJECT_NAME}" ${UTHENTICODE_TEST_SOURCES})
1312
add_test(NAME "${PROJECT_NAME}" COMMAND "${PROJECT_NAME}")
1413
target_link_libraries("${PROJECT_NAME}" PUBLIC uthenticode gtest)
15-
target_compile_definitions("${PROJECT_NAME}" PRIVATE UTHENTICODE_TEST_ASSETS="${CMAKE_CURRENT_SOURCE_DIR}/assets")
14+
target_compile_definitions(
15+
"${PROJECT_NAME}" PRIVATE UTHENTICODE_TEST_ASSETS="${CMAKE_CURRENT_SOURCE_DIR}/assets"
16+
)

0 commit comments

Comments
 (0)