Skip to content

Commit

Permalink
Merge pull request #189 from aminya/windows-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jan 17, 2023
2 parents 57971f7 + 85d1057 commit 25196f0
Show file tree
Hide file tree
Showing 25 changed files with 153 additions and 144 deletions.
39 changes: 30 additions & 9 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
version: 3

includes:
windows: ./Taskfile_windows.yml
docker: ./docker/Taskfile.yml
examples: ./examples/Taskfile.yml
myproj:
Expand All @@ -28,16 +27,38 @@ tasks:
- task: myproj:test.release
- task: install

lint.cspell: npx -y cspell lint --no-progress --show-suggestions

lint.clang-format:
- cmd: powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { if ((get-item $file).Extension -in ".cpp", ".hpp", ".c", ".cc", ".cxx", ".hxx", ".ixx") { clang-format -i -style=file $file } }'
platforms: [windows]
- cmd: git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$' | xargs clang-format -i -style=file
platforms: [linux, darwin]

lint.cmake:
cmds:
- cmd: powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { $item=(get-item $file); if (($item.Name -eq "CMakeLists.txt") -or ($item.Extension -in ".cmake")) { cmake-format --in-place $file; cmake-lint $file {{.CMAKE_LINT_FLAGS}} } }'
platforms: [windows]
- cmd: git ls-files --exclude-standard | grep -E '(CMakeLists\.txt)|(\.(cmake))$' | xargs cmake-format --in-place | xargs cmake-lint {{.CMAKE_LINT_FLAGS}}
platforms: [linux, darwin]
vars:
CMAKE_LINT_FLAGS: --disabled-codes C0103 C0301 R0912 R0915 R0913 --suppress-decorations

lint:
- git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$' | xargs clang-format -i -style=file
- git ls-files --exclude-standard | grep -E '(CMakeLists\.txt)|(\.(cmake))$' | xargs cmake-format --in-place | xargs cmake-lint --disabled-codes C0103 C0301 R0912 R0915 R0913 --suppress-decorations
- task: myproj:lint
- task: install:lint
- task: minimal:lint
- task: emscripten:lint
- npx -y cspell lint --no-progress --show-suggestions
deps:
- lint.cspell
- lint.clang-format
- lint.cmake
- myproj:lint
- install:lint
- minimal:lint
- emscripten:lint

clean:
- cmd: powershell -c 'function rmrf($path) { if (test-path $path) { rm -r -force $path }}; rmrf ./install'
platforms: [windows]
- cmd: rm -rf ./install
platforms: [linux, darwin]

- task: myproj:clean
- task: install:clean
- rm -rf {{.CWD}}/install
12 changes: 0 additions & 12 deletions Taskfile_windows.yml

This file was deleted.

4 changes: 3 additions & 1 deletion cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ words:
- CPATH
- Cppcheck
- cppcoreguidelines
- cppreference
- cppdbg
- CPPFLAGS
- cpprc
Expand All @@ -40,8 +41,8 @@ words:
- DOPT
- dyld
- dylib
- Eigen
- egor-tensin
- Eigen
- emcc
- emcmake
- emscripten
Expand Down Expand Up @@ -82,6 +83,7 @@ words:
- pwsh
- rmrf
- rpath
- rtti
- sccache
- setx
- shlib
Expand Down
4 changes: 2 additions & 2 deletions src/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include_guard()
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
function(
set_project_warnings
project_name
_project_name
WARNINGS_AS_ERRORS
MSVC_WARNINGS
CLANG_WARNINGS
Expand Down Expand Up @@ -116,7 +116,7 @@ function(
set(PROJECT_WARNINGS_CUDA "${CUDA_WARNINGS}")

target_compile_options(
${project_name}
${_project_name}
INTERFACE # C++ warnings
$<$<COMPILE_LANGUAGE:CXX>:${PROJECT_WARNINGS_CXX}>
# C warnings
Expand Down
2 changes: 2 additions & 0 deletions src/CrossCompiler.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include_guard()

# Enable cross-compiling
macro(enable_cross_compiler)
include("${ProjectOptions_SRC_DIR}/Utilities.cmake")
detect_architecture(_arch)
Expand Down Expand Up @@ -159,6 +160,7 @@ macro(enable_cross_compiler)
message(STATUS "Toolchain File: ${CMAKE_TOOLCHAIN_FILE}")
endmacro()

# Get the toolchain file
function(get_toolchain_file value)
include("${ProjectOptions_SRC_DIR}/Utilities.cmake")
detect_architecture(_arch)
Expand Down
4 changes: 2 additions & 2 deletions src/DetectCompiler.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
include_guard()

# includes a separate CMakeLists.txt file to detect the CXX/C compilers before project is called
# Using a separate file ensures that the current scope is not contaminated by the variable
macro(detect_compiler)
# includes a separate CMakeLists.txt file to detect the CXX/C compilers before project is called
# Using a separate file ensures that the current scope is not contaminated by the variable
find_program(CMAKE_EXECUTABLE cmake)
execute_process(
COMMAND "${CMAKE_EXECUTABLE}" -S "${ProjectOptions_SRC_DIR}/detect_compiler" -B
Expand Down
4 changes: 2 additions & 2 deletions src/Linker.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include_guard()

# Set the linker to use for the linking phase
macro(configure_linker project_name linker)
macro(configure_linker _project_name linker)
if(NOT
"${linker}"
STREQUAL
Expand All @@ -13,7 +13,7 @@ macro(configure_linker project_name linker)

check_cxx_compiler_flag(${_linker_flag} _cxx_supports_linker)
if(_cxx_supports_linker)
target_compile_options(${project_name} INTERFACE ${_linker_flag})
target_compile_options(${_project_name} INTERFACE ${_linker_flag})
endif()
endif()
endmacro()
29 changes: 17 additions & 12 deletions src/Optimization.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include_guard()

macro(enable_interprocedural_optimization project_name)
# Enable Interprocedural Optimization (Link Time Optimization, LTO) in the release build
macro(enable_interprocedural_optimization _project_name)
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
include(CheckIPOSupported)
check_ipo_supported(RESULT result OUTPUT output)
Expand All @@ -13,32 +14,36 @@ macro(enable_interprocedural_optimization project_name)
"Interprocedural optimization is enabled. In other projects, linking with the compiled libraries of this project might require `set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)`"
)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
set_target_properties(${project_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
set_target_properties(${_project_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
else()
message(WARNING "Interprocedural Optimization is not supported. Not using it. Here is the error log: ${output}")
endif()
endif()
endmacro()

macro(enable_native_optimization project_name)
# Enable the optimizations specific to the build machine (e.g. SSE4_1, AVX2, etc.).
macro(enable_native_optimization _project_name)
detect_architecture(_arch)
if("${_arch}" STREQUAL "x64")
message(STATUS "Enabling the optimizations specific to the current build machine (less portable)")
if(MSVC)
# TODO It seems it only accepts the exact instruction set like AVX https://docs.microsoft.com/en-us/cpp/build/reference/arch-x64
# target_compile_options(${project_name} INTERFACE /arch:native)
# target_compile_options(${_project_name} INTERFACE /arch:native)
else()
target_compile_options(${project_name} INTERFACE -march=native)
target_compile_options(${_project_name} INTERFACE -march=native)
endif()
endif()
endmacro()

macro(disable_exceptions project_name)
target_compile_options(${project_name} INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/EHs-c- /D_HAS_EXCEPTIONS=0>)
target_compile_options(${project_name} INTERFACE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-exceptions -fno-unwind-tables>)
# Disable C++ exceptions for the given project.
macro(disable_exceptions _project_name)
target_compile_options(${_project_name} INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/EHs-c- /D_HAS_EXCEPTIONS=0>)
target_compile_options(${_project_name} INTERFACE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-exceptions
-fno-unwind-tables>)
endmacro()

macro(disable_rtti project_name)
target_compile_options(${project_name} INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/GR->)
target_compile_options(${project_name} INTERFACE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-rtti>)
endmacro()
# Disable C++ RTTI (Run-Time Type Information) for the given project.
macro(disable_rtti _project_name)
target_compile_options(${_project_name} INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/GR->)
target_compile_options(${_project_name} INTERFACE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-rtti>)
endmacro()
11 changes: 6 additions & 5 deletions src/Sanitizers.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
include_guard()

# Enable the sanitizers for the given project
function(
enable_sanitizers
project_name
_project_name
ENABLE_SANITIZER_ADDRESS
ENABLE_SANITIZER_LEAK
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
Expand Down Expand Up @@ -69,8 +70,8 @@ function(
STREQUAL
"")
if(NOT MSVC)
target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
target_link_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
target_compile_options(${_project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
target_link_options(${_project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
else()
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
if("${index_of_vs_install_dir}" STREQUAL "-1")
Expand All @@ -79,8 +80,8 @@ function(
"Using MSVC sanitizers requires setting the MSVC environment before building the project. Please manually open the MSVC command prompt and rebuild the project."
)
endif()
target_compile_options(${project_name} INTERFACE /fsanitize=${LIST_OF_SANITIZERS} /Zi /INCREMENTAL:NO)
target_link_options(${project_name} INTERFACE /INCREMENTAL:NO)
target_compile_options(${_project_name} INTERFACE /fsanitize=${LIST_OF_SANITIZERS} /Zi /INCREMENTAL:NO)
target_link_options(${_project_name} INTERFACE /INCREMENTAL:NO)
endif()
endif()
endif()
Expand Down
6 changes: 3 additions & 3 deletions src/Tests.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
include_guard()

# Enable coverage reporting for gcc/clang
function(enable_coverage project_name)
function(enable_coverage _project_name)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
target_compile_options(${project_name} INTERFACE --coverage -O0 -g)
target_link_libraries(${project_name} INTERFACE --coverage)
target_compile_options(${_project_name} INTERFACE --coverage -O0 -g)
target_link_libraries(${_project_name} INTERFACE --coverage)
endif()
endfunction()
40 changes: 20 additions & 20 deletions src/Utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,60 @@ function(
endfunction()

# A function to set environment variables of CMake from the output of `cmd /c set`
function(set_env_from_string env_string)
function(set_env_from_string _env_string)
# replace ; in paths with __sep__ so we can split on ;
string(
REGEX
REPLACE ";"
"__sep__"
env_string_sep_added
"${env_string}")
_env_string_sep_added
"${_env_string}")

# the variables are separated by \r?\n
string(
REGEX
REPLACE "\r?\n"
";"
env_list
"${env_string_sep_added}")
_env_list
"${_env_string_sep_added}")

foreach(env_var ${env_list})
foreach(_env_var ${_env_list})
# split by =
string(
REGEX
REPLACE "="
";"
env_parts
"${env_var}")
_env_parts
"${_env_var}")

list(LENGTH env_parts env_parts_length)
if("${env_parts_length}" EQUAL "2")
list(LENGTH _env_parts _env_parts_length)
if("${_env_parts_length}" EQUAL "2")
# get the variable name and value
list(
GET
env_parts
_env_parts
0
env_name)
_env_name)
list(
GET
env_parts
_env_parts
1
env_value)
_env_value)

# recover ; in paths
string(
REGEX
REPLACE "__sep__"
";"
env_value
"${env_value}")
_env_value
"${_env_value}")

# set env_name to env_value
set(ENV{${env_name}} "${env_value}")
# set _env_name to _env_value
set(ENV{${_env_name}} "${_env_value}")

# update cmake program path
if("${env_name}" EQUAL "PATH")
list(APPEND CMAKE_PROGRAM_PATH ${env_value})
if("${_env_name}" EQUAL "PATH")
list(APPEND CMAKE_PROGRAM_PATH ${_env_value})
endif()
endif()
endforeach()
Expand Down
4 changes: 2 additions & 2 deletions tests/emscripten/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
#endif

int main() {
printf("hello, world!\n");
return 0;
printf("hello, world!\n");
return 0;
}
6 changes: 4 additions & 2 deletions tests/install/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
version: 3

includes:
windows: ./Taskfile_windows.yml
myproj:
taskfile: ../myproj/Taskfile.yml
dir: ../myproj
Expand Down Expand Up @@ -33,4 +32,7 @@ tasks:
- ~/vcpkg/vcpkg format-manifest ./vcpkg.json

clean:
- rm -rf ./build
- cmd: powershell -c 'function rmrf($path) { if (test-path $path) { rm -r -force $path }}; rmrf ./build'
platforms: [windows]
- cmd: rm -rf ./build
platforms: [linux, darwin]
5 changes: 0 additions & 5 deletions tests/install/Taskfile_windows.yml

This file was deleted.

4 changes: 2 additions & 2 deletions tests/install/src/another_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#include <mylib2/lib.hpp>

int main() {
some_fun2();
return some_fun2();
some_fun2();
return some_fun2();
}
Loading

0 comments on commit 25196f0

Please sign in to comment.