Skip to content

Commit

Permalink
Iteration on CMake scripts
Browse files Browse the repository at this point in the history
- consolidate compile flags - enable /MP globally
- moved ASan/sanitizer to separate file
  • Loading branch information
DarkWanderer committed Feb 5, 2023
1 parent ce75508 commit b1fe251
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 48 deletions.
49 changes: 9 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ else()
endif()

if (NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
message(STATUS "USING: MultiCore VS Solution")
set(CompileFlags "/MP")
else()
message(STATUS "USING: Ninja Configuration")
set(CompileFlags "")
add_compile_options(/MP)
endif()

# Define directories and file paths for some common binaries
Expand Down Expand Up @@ -131,8 +127,8 @@ set(Qt5_x64_DIR
)

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /we4311")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:\"LIBCMTD\"")
add_compile_options(/we4311)
add_link_options("/NODEFAULTLIB:\"LIBCMTD\"")
endif()

# Copy data and binary assets from source to binary tree to provide a working Orbiter environment
Expand Down Expand Up @@ -281,7 +277,7 @@ option(ORBITER_MAKE_SDK_SAMPLES
OFF
)

option(ORBITER_ENABLE_ASAN
option(ORBITER_SANITIZER
"Build binaries with Address Sanitizer"
OFF
)
Expand All @@ -296,17 +292,15 @@ if(ORBITER_BUILD_XRSOUND)
endif()

if(ORBITER_BUILD_D3D9CLIENT)
find_package(DXSDK
)
find_package(DXSDK REQUIRED)
set(DXSDK_DIR
${DXSDK_DIR}
CACHE
PATH
"Installation directory of DirectX SDK (June 2010)"
)
if(${DXSDK_DIR} STREQUAL "DXSDK_DIR-NOTFOUND")
message(WARNING "DirectX SDK not found. The D3D9 graphics client will not be built.")
set(ORBITER_BUILD_D3D9CLIENT OFF)
message(FATAL_ERROR "DirectX SDK not found. The D3D9 graphics client will not be built.")
endif()
endif()

Expand Down Expand Up @@ -339,30 +333,9 @@ if (ORBITER_MAKE_DOC)
endif()

# Enable Address SANitizer
if (ORBITER_ENABLE_ASAN AND MSVC)
set(asan_binary_dir "$ENV{VCToolsInstallDir}bin\\Host${ARCH}\\${ARCH}")
if (NOT EXISTS "${asan_binary_dir}")
message( FATAL_ERROR "ASAN: Visual Studio tools directory ${asan_binary_dir} not valid, check VCToolsInstallDir environment variable" )
endif()

if (BUILD64)
set(asan_arch_id "x86_64")
else()
set(asan_arch_id "i386")
endif()
set(asan_dll "clang_rt.asan_dynamic-${asan_arch_id}.dll")
set(asan_dbg_dll "clang_rt.asan_dbg_dynamic-${asan_arch_id}.dll")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fsanitize=address /EHsc")
add_custom_command(
COMMENT "Copying ASAN Clang runtime from ${asan_binary_dir}"
OUTPUT ${asan_dll}
OUTPUT ${asan_dbg_dll}
COMMAND ${CMAKE_COMMAND} -E copy ${asan_binary_dir}\\${asan_dll} ${ORBITER_BINARY_ROOT_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${asan_binary_dir}\\${asan_dbg_dll} ${ORBITER_BINARY_ROOT_DIR}
)
add_custom_target(AsanBinaries DEPENDS ${asan_dll})
message("Address Sanitizer Enabled")
include(cmake/sanitizer.cmake)
if (ORBITER_SANITIZER AND MSVC)
enable_sanitizer(${ORBITER_SANITIZER})
endif()

# For a given argument string template (odt_to_pdf_arglist) and source file name (infile)
Expand Down Expand Up @@ -517,7 +490,3 @@ if (ORBITER_MAKE_TESTS)
enable_testing()
add_subdirectory(Tests)
endif()

if (ORBITER_ENABLE_ASAN AND MSVC)
add_dependencies(Orbiter_server AsanBinaries)
endif()
1 change: 0 additions & 1 deletion OVP/D3D7Client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ target_link_libraries(D3D7Client

set_target_properties(D3D7Client
PROPERTIES
COMPILE_FLAGS "${CompileFlags}"
COMPILE_DEFINITIONS "DIRECTINPUT_VERSION=0x0800"
FOLDER OVP
)
Expand Down
1 change: 0 additions & 1 deletion OVP/D3D9Client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ target_link_libraries(D3D9Client

set_target_properties(D3D9Client
PROPERTIES
COMPILE_FLAGS "${CompileFlags}"
LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Modules/Plugin
LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Modules/Plugin
COMPILE_DEFINITIONS D3D9CLIENT_EXPORTS
Expand Down
2 changes: 0 additions & 2 deletions Src/Orbiter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ if(BUILD_ORBITER_DX7)
set_target_properties(Orbiter
PROPERTIES
LINK_FLAGS "/SUBSYSTEM:WINDOWS"
COMPILE_FLAGS "${CompileFlags}"
RUNTIME_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
COMPILE_DEFINITIONS "INLINEGRAPHICS;DIRECTINPUT_VERSION=0x0800"
ENABLE_EXPORTS 1
Expand Down Expand Up @@ -252,7 +251,6 @@ if(BUILD_ORBITER_SERVER)
set_target_properties(Orbiter_server
PROPERTIES
LINK_FLAGS "/SUBSYSTEM:CONSOLE /ENTRY:WinMainCRTStartup"
COMPILE_FLAGS "${CompileFlags}"
RUNTIME_OUTPUT_DIRECTORY ${ORBITER_BINARY_MODULE_DIR}/Server
OUTPUT_NAME Orbiter
VS_DEBUGGER_WORKING_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
Expand Down
4 changes: 4 additions & 0 deletions Src/Orbiter/Orbiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ int _matherr(struct _exception *except )

INT WINAPI WinMain (HINSTANCE hInstance, HINSTANCE, LPSTR strCmdLine, INT nCmdShow)
{
#ifdef _CRTDBG_MAP_ALLOC
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif

#ifndef INLINEGRAPHICS
// Verify working directory
char dir[1024];
Expand Down
4 changes: 0 additions & 4 deletions Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ ExternalProject_Add(plsplit
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/plsplit
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/plsplit
INSTALL_DIR ${ORBITER_INSTALL_UTILS_DIR}
CMAKE_GENERATOR "Visual Studio 16 2019"
CMAKE_GENERATOR_PLATFORM "x64"
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} -DINSTALLDIR=${ORBITER_INSTALL_UTILS_DIR}
BUILD_COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/plsplit --config Release
INSTALL_COMMAND ""
Expand All @@ -39,8 +37,6 @@ ExternalProject_Add(tileedit_proj
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/tileedit/qt
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tileedit/qt
INSTALL_DIR ${ORBITER_INSTALL_UTILS_DIR}
CMAKE_GENERATOR "Visual Studio 16 2019"
CMAKE_GENERATOR_PLATFORM "x64"
CMAKE_ARGS -DCMAKE_CFG_INTDIR=Release -DQTDIR:PATH=${Qt5_x64_DIR} -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} -DINSTALLDIR=${ORBITER_INSTALL_UTILS_DIR}
BUILD_COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/tileedit/qt --config Release
INSTALL_COMMAND ""
Expand Down
57 changes: 57 additions & 0 deletions cmake/sanitizer.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function(enable_sanitizer SANITIZER)
if(NOT MSVC)
add_compile_options(-fsanitize=${SANITIZER} -fno-omit-frame-pointer)
add_link_options(-fsanitize=${SANITIZER})
elseif("${SANITIZER}" STREQUAL "leak") # MSVC CRT Debug Heap
add_compile_definitions("_CRTDBG_MAP_ALLOC")
elseif("${SANITIZER}" STREQUAL "address")
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit build
set(ASAN_ARCHITECTURE "x86_64")
set(ASAN_LIBRARY_HINT_DIR $ENV{VCToolsInstallDir}/bin/Hostx64/x64)
else()
set(ASAN_ARCHITECTURE "i386")
set(ASAN_LIBRARY_HINT_DIR $ENV{VCToolsInstallDir}/bin/Hostx86/x86)
endif()

if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(ASAN_LIBRARY_NAME "clang_rt.asan_dbg_dynamic-${ASAN_ARCHITECTURE}.dll")
else()
set(ASAN_LIBRARY_NAME "clang_rt.asan_dynamic-${ASAN_ARCHITECTURE}.dll")
endif()
set(LLVM_SYMBOLIZER_NAME "llvm-symbolizer.exe")

find_file (ASAN_LIBRARY_SOURCE
NAMES ${ASAN_LIBRARY_NAME}
REQUIRED
HINTS ${ASAN_LIBRARY_HINT_DIR} $ENV{LIBPATH}
DOC "Clang AddressSanitizer runtime"
)

find_file (LLVM_SYMBOLIZER_SOURCE
NAMES ${LLVM_SYMBOLIZER_NAME}
REQUIRED
HINTS ${ASAN_LIBRARY_HINT_DIR} $ENV{LIBPATH}
DOC "LLVM symbolizer executable"
)

add_custom_command(
COMMENT "Copying ${ASAN_LIBRARY_SOURCE} to ${CMAKE_BINARY_DIR}}"
OUTPUT ${CMAKE_BINARY_DIR}/${ASAN_LIBRARY_NAME}
MAIN_DEPENDENCY ${ASAN_LIBRARY_SOURCE}
COMMAND ${CMAKE_COMMAND} -E copy ${ASAN_LIBRARY_SOURCE} ${CMAKE_BINARY_DIR}
)

add_custom_command(
COMMENT "Copying ${LLVM_SYMBOLIZER_SOURCE} to ${CMAKE_BINARY_DIR}}"
OUTPUT ${CMAKE_BINARY_DIR}/${LLVM_SYMBOLIZER_NAME}
MAIN_DEPENDENCY ${LLVM_SYMBOLIZER_SOURCE}
COMMAND ${CMAKE_COMMAND} -E copy ${LLVM_SYMBOLIZER_SOURCE} ${CMAKE_BINARY_DIR}
)

add_custom_target(CopyAsanBinaries ALL DEPENDS ${ASAN_LIBRARY_NAME} ${LLVM_SYMBOLIZER_NAME})
add_compile_options(/fsanitize=${SANITIZER} /Zi /Oy-)
else()
message(FATAL_ERROR "MSVC does not support sanitizer ${SANITIZER}")
endif()
message(STATUS "Enabled sanitize=${SANITIZER}")
endfunction()

0 comments on commit b1fe251

Please sign in to comment.