add cmake-format.py and formating all cmake files

This commit is contained in:
Stepanov Igor 2025-01-09 16:32:54 +03:00
parent 7bbd8bab34
commit bb15d17892
5 changed files with 105 additions and 69 deletions

3
.cmake-format.py Normal file
View File

@ -0,0 +1,3 @@
tab_size = 4
enable_sort = True
autosort = True

View File

@ -8,18 +8,20 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows") if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# We use two types linking: for clang build is static (vcpkg triplet x64-windows-static) # We use two types linking: for clang build is static (vcpkg triplet
# and for msvc build is dynamic linking (vcpkg triplet x64-windows) # x64-windows-static) and for msvc build is dynamic linking (vcpkg triplet
# By default CMAKE_MSVC_RUNTIME_LIBRARY set by MultiThreaded$<$<CONFIG:Debug>:Debug>DLL # x64-windows) By default CMAKE_MSVC_RUNTIME_LIBRARY set by
if (VCPKG_TARGET_TRIPLET MATCHES "static") # MultiThreaded$<$<CONFIG:Debug>:Debug>DLL
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") if(VCPKG_TARGET_TRIPLET MATCHES "static")
endif() set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endif() endif()
add_subdirectory(src) add_subdirectory(src)
add_executable(${PROJECT_NAME} src/main.cpp) add_executable(${PROJECT_NAME} src/main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
if(VOXELENGINE_BUILD_APPDIR) if(VOXELENGINE_BUILD_APPDIR)
include(${CMAKE_CURRENT_SOURCE_DIR}/dev/cmake/BuildAppdir.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/dev/cmake/BuildAppdir.cmake)
@ -27,33 +29,44 @@ endif()
if(MSVC) if(MSVC)
if(NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Build type" FORCE)
endif() endif()
if((CMAKE_BUILD_TYPE EQUAL "Release") OR (CMAKE_BUILD_TYPE EQUAL "RelWithDebInfo")) if((CMAKE_BUILD_TYPE EQUAL "Release") OR (CMAKE_BUILD_TYPE EQUAL
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2) "RelWithDebInfo"))
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2)
else() else()
target_compile_options(${PROJECT_NAME} PRIVATE /W4) target_compile_options(${PROJECT_NAME} PRIVATE /W4)
endif() endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /source-charset:UTF-8 /D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR") set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} /source-charset:UTF-8 /D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR"
)
else() else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra target_compile_options(
# additional warnings ${PROJECT_NAME}
-Wformat-nonliteral -Wcast-align PRIVATE -Wall
-Wpointer-arith -Wundef -Wextra
-Wwrite-strings -Wno-unused-parameter) # additional warnings
if (CMAKE_BUILD_TYPE MATCHES "Debug") -Wformat-nonliteral
target_compile_options(${PROJECT_NAME} PRIVATE -Og) -Wcast-align
endif() -Wpointer-arith
if (WIN32) -Wundef
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") -Wwrite-strings
endif() -Wno-unused-parameter)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_options(${PROJECT_NAME} PRIVATE -Og)
endif()
if(WIN32)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()
endif() endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie -lstdc++fs") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie -lstdc++fs")
endif() endif()
if (WIN32) if(WIN32)
target_link_libraries(${PROJECT_NAME} VoxelEngineSrc winmm) target_link_libraries(${PROJECT_NAME} VoxelEngineSrc winmm)
endif() endif()
@ -61,14 +74,13 @@ target_link_libraries(${PROJECT_NAME} VoxelEngineSrc ${CMAKE_DL_LIBS})
# Deploy res to build dir # Deploy res to build dir
add_custom_command( add_custom_command(
TARGET ${PROJECT_NAME} TARGET ${PROJECT_NAME}
POST_BUILD POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different COMMAND
${CMAKE_CURRENT_SOURCE_DIR}/res ${CMAKE_COMMAND} -E copy_directory_if_different
$<TARGET_FILE_DIR:${PROJECT_NAME}>/res ${CMAKE_CURRENT_SOURCE_DIR}/res $<TARGET_FILE_DIR:${PROJECT_NAME}>/res)
)
if (VOXELENGINE_BUILD_TESTS) if(VOXELENGINE_BUILD_TESTS)
enable_testing() enable_testing()
add_subdirectory(test) add_subdirectory(test)
endif() endif()

View File

@ -10,7 +10,7 @@ add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED) find_package(GLEW REQUIRED)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows") if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# specific for vcpkg # specific for vcpkg
find_package(OpenAL CONFIG REQUIRED) find_package(OpenAL CONFIG REQUIRED)
set(OPENAL_LIBRARY OpenAL::OpenAL) set(OPENAL_LIBRARY OpenAL::OpenAL)
@ -20,17 +20,19 @@ endif()
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED) find_package(PNG REQUIRED)
find_package(CURL REQUIRED) find_package(CURL REQUIRED)
if (NOT APPLE) if(NOT APPLE)
find_package(EnTT REQUIRED) find_package(EnTT REQUIRED)
endif() endif()
set(LIBS "") set(LIBS "")
if (CMAKE_SYSTEM_NAME STREQUAL "Windows") if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# Use directly linking to lib instead PkgConfig (because pkg-config dont install on windows as default) # Use directly linking to lib instead PkgConfig (because pkg-config dont
# TODO: Do it with findLua. # install on windows as default) TODO: Do it with findLua.
if (MSVC) if(MSVC)
set(LUA_INCLUDE_DIR "$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/include/luajit") set(LUA_INCLUDE_DIR
"$ENV{VCPKG_ROOT}/packages/luajit_${VCPKG_TARGET_TRIPLET}/include/luajit"
)
find_package(Lua REQUIRED) find_package(Lua REQUIRED)
else() else()
# Used for mingw-clang cross compiling from msys2 # Used for mingw-clang cross compiling from msys2
@ -49,7 +51,7 @@ elseif(APPLE)
set(LUA_LIBRARIES "/opt/homebrew/lib/libluajit-5.1.a") set(LUA_LIBRARIES "/opt/homebrew/lib/libluajit-5.1.a")
message(STATUS "LUA Libraries: ${LUA_LIBRARIES}") message(STATUS "LUA Libraries: ${LUA_LIBRARIES}")
message(STATUS "LUA Include Dir: ${LUA_INCLUDE_DIR}") message(STATUS "LUA Include Dir: ${LUA_INCLUDE_DIR}")
set(VORBISLIB ${VORBIS_LDFLAGS}) set(VORBISLIB ${VORBIS_LDFLAGS})
message(STATUS "Vorbis Lib: ${VORBIS_LDFLAGS}") message(STATUS "Vorbis Lib: ${VORBIS_LDFLAGS}")
else() else()
@ -70,4 +72,16 @@ endif()
include_directories(${LUA_INCLUDE_DIR}) include_directories(${LUA_INCLUDE_DIR})
include_directories(${CURL_INCLUDE_DIR}) include_directories(${CURL_INCLUDE_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ZLIB::ZLIB PNG::PNG CURL::libcurl ${VORBISLIB} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS}) target_link_libraries(
${PROJECT_NAME}
${LIBS}
glfw
OpenGL::GL
${OPENAL_LIBRARY}
GLEW::GLEW
ZLIB::ZLIB
PNG::PNG
CURL::libcurl
${VORBISLIB}
${LUA_LIBRARIES}
${CMAKE_DL_LIBS})

View File

@ -8,23 +8,19 @@ find_package(GTest)
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) target_include_directories(${PROJECT_NAME}
target_link_libraries( PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} VoxelEngineSrc GTest::gtest_main)
VoxelEngineSrc
GTest::gtest_main
)
# HACK: copy res to test/ folder for fixing problem compatibility MultiConfig and non # HACK: copy res to test/ folder for fixing problem compatibility MultiConfig
# MultiConfig builds. Delete in future and use only root res folder # and non MultiConfig builds. Delete in future and use only root res folder Also
# Also this resolve problem with ctests, because it set cwd to CMAKE_CURRENT_BINARY_DIR # this resolve problem with ctests, because it set cwd to
# CMAKE_CURRENT_BINARY_DIR
add_custom_command( add_custom_command(
TARGET ${PROJECT_NAME} TARGET ${PROJECT_NAME}
POST_BUILD POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
${CMAKE_SOURCE_DIR}/res ${CMAKE_SOURCE_DIR}/res ${CMAKE_CURRENT_BINARY_DIR}/res)
${CMAKE_CURRENT_BINARY_DIR}/res
)
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}) gtest_discover_tests(${PROJECT_NAME})

View File

@ -8,24 +8,35 @@ add_executable(${PROJECT_NAME} ${SOURCES})
if(MSVC) if(MSVC)
if(NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Build type" FORCE)
endif() endif()
if((CMAKE_BUILD_TYPE EQUAL "Release") OR (CMAKE_BUILD_TYPE EQUAL "RelWithDebInfo")) if((CMAKE_BUILD_TYPE EQUAL "Release") OR (CMAKE_BUILD_TYPE EQUAL
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Release>:Release>") "RelWithDebInfo"))
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2) set(CMAKE_MSVC_RUNTIME_LIBRARY
"MultiThreaded$<$<CONFIG:Release>:Release>")
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /MT /O2)
else() else()
target_compile_options(${PROJECT_NAME} PRIVATE /W4) target_compile_options(${PROJECT_NAME} PRIVATE /W4)
endif() endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else() else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra target_compile_options(
-Wformat-nonliteral -Wcast-align ${PROJECT_NAME}
-Wpointer-arith -Wundef PRIVATE -Wall
-Wwrite-strings -Wno-unused-parameter) -Wextra
-Wformat-nonliteral
-Wcast-align
-Wpointer-arith
-Wundef
-Wwrite-strings
-Wno-unused-parameter)
endif() endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie -lstdc++fs") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie -lstdc++fs")
endif() endif()
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src ${CMAKE_DL_LIBS}) target_include_directories(
${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src ${CMAKE_DL_LIBS})