From b8d8d24d216a1031804a15ae782662ecb98c76d1 Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 12 Apr 2018 23:07:18 +0100 Subject: [PATCH 1/5] Get cmake working with msvc for basic game --- CMakeLists.txt | 98 +++++++++++++++++++--------------- src/openrct2-ui/CMakeLists.txt | 25 ++++++++- src/openrct2-ui/Ui.cpp | 2 +- src/openrct2/CMakeLists.txt | 33 +++++++----- 4 files changed, 100 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 60408d5872..f1428e704a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,9 @@ endif() project(openrct2 CXX) -include(FindPkgConfig) +if (NOT MSVC) + include(FindPkgConfig) +endif () include(CheckCXXCompilerFlag) include(GNUInstallDirs) @@ -72,52 +74,62 @@ function (ADD_CHECK_CXX_COMPILER_FLAG _CXXFLAGS _CACHE_VAR _FLAG) endif () endfunction () -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NULL_DEREFERENCE -Wnull-dereference) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_TYPES -Wsuggest-final-types) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_METHODS -Wsuggest-final-methods) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_OVERRIDE -Wsuggest-override) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_COND -Wduplicated-cond) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NON_VIRTUAL_DTOR -Wnon-virtual-dtor) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_MISSING_VARIABLE_DECLARATIONS -Wmissing-variable-declarations) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_BRANCHES -Wduplicated-branches) -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_RESTRICT -Wrestrict) +if (MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /std:c++latest /permissive- /Zc:externConstexpr") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244") # C4244: 'conversion_type': conversion from 'type1' to 'type2', possible loss of data -# Items below are not supported by ICC -if (NOT MINGW) - # Do not enable for MinGW, as its headers contain redundant declarations of builtin functions - ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_REDUNDANT_DECLS -Wredundant-decls) -endif () -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_IGNORED_QUALIFIERS -Wignored-qualifiers) - -# -Wstrict-overflow is only active when -fstrict-overflow is enabled, but -fstrict-overflow -# is enabled on -O2, -O3, -Os. This should help catch bugs locally before they reach Travis -# As of 2a435bf -Wstrict-overflow=1 passes, but higher values do not. -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-overflow") -ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_STRICT_OVERFLOW -Wstrict-overflow=1) - -# Compiler flags -set(DEBUG_LEVEL 0 CACHE STRING "Select debug level for compilation. Use value in range 0–3.") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-aliasing -Werror -Wundef -Wmissing-declarations -Winit-self -Wall -Wno-unknown-pragmas -Wno-unused-function -Wno-missing-braces ") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-comment -Wshadow -Wnonnull") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG=${DEBUG_LEVEL}") - -if(APPLE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=objc-method-access") -endif() - -# On mingw all code is already PIC, this will avoid compiler error on redefining this option -if (NOT MINGW) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") -endif () - -if (APPLE AND NOT USE_MMAP) - set(PIE_FLAG "-fno-pie") + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + add_definitions(-D_SCL_SECURE_NO_WARNINGS) + add_definitions(-D__SSE4_1__) + add_definitions(-D__AVX2__) else () - set(PIE_FLAG "-fpie") + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NULL_DEREFERENCE -Wnull-dereference) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_TYPES -Wsuggest-final-types) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_FINAL_METHODS -Wsuggest-final-methods) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_SUGGEST_OVERRIDE -Wsuggest-override) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_COND -Wduplicated-cond) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_NON_VIRTUAL_DTOR -Wnon-virtual-dtor) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_MISSING_VARIABLE_DECLARATIONS -Wmissing-variable-declarations) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_DUPLICATED_BRANCHES -Wduplicated-branches) + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_RESTRICT -Wrestrict) + + # Items below are not supported by ICC + if (NOT MINGW) + # Do not enable for MinGW, as its headers contain redundant declarations of builtin functions + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_REDUNDANT_DECLS -Wredundant-decls) + endif () + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_IGNORED_QUALIFIERS -Wignored-qualifiers) + + # -Wstrict-overflow is only active when -fstrict-overflow is enabled, but -fstrict-overflow + # is enabled on -O2, -O3, -Os. This should help catch bugs locally before they reach Travis + # As of 2a435bf -Wstrict-overflow=1 passes, but higher values do not. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-overflow") + ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS CXX_WARN_STRICT_OVERFLOW -Wstrict-overflow=1) + + # Compiler flags + set(DEBUG_LEVEL 0 CACHE STRING "Select debug level for compilation. Use value in range 0–3.") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-aliasing -Werror -Wundef -Wmissing-declarations -Winit-self -Wall -Wno-unknown-pragmas -Wno-unused-function -Wno-missing-braces ") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-comment -Wshadow -Wnonnull") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG=${DEBUG_LEVEL}") + + if(APPLE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=objc-method-access") + endif() + + # On mingw all code is already PIC, this will avoid compiler error on redefining this option + if (NOT MINGW) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + endif () + + if (APPLE AND NOT USE_MMAP) + set(PIE_FLAG "-fno-pie") + else () + set(PIE_FLAG "-fpie") + endif () + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14") endif () -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14") - # Defines if (USE_MMAP) add_definitions(-DUSE_MMAP) diff --git a/src/openrct2-ui/CMakeLists.txt b/src/openrct2-ui/CMakeLists.txt index 0336bc4393..86e7819d4e 100644 --- a/src/openrct2-ui/CMakeLists.txt +++ b/src/openrct2-ui/CMakeLists.txt @@ -8,8 +8,18 @@ endif () option(DISABLE_OPENGL "Disable OpenGL support.") # Third party libraries -PKG_CHECK_MODULES(SDL2 REQUIRED sdl2) -PKG_CHECK_MODULES(SPEEX REQUIRED speexdsp) +if (MSVC) + find_package(jansson REQUIRED) + + find_path(SDL2_INCLUDE_DIRS SDL2/SDL.h) + find_library(SDL2_LDFLAGS sdl2) + + find_library(SPEEX_LDFLAGS libspeexdsp) +else () + PKG_CHECK_MODULES(SDL2 REQUIRED sdl2) + PKG_CHECK_MODULES(SPEEX REQUIRED speexdsp) +endif () + if (NOT DISABLE_OPENGL) # GL doesn't work nicely with macOS, while find_package doesn't work with multiarch on Ubuntu. if (APPLE) @@ -64,6 +74,14 @@ if (WIN32) # tell it that it is set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__USE_MINGW_ANSI_STDIO=1") endif () +if (MSVC) + # Add DPI-aware manifest + # HACK Add /MANIFEST:NO first to prevent cmake from adding its own manifest arguments + if (NOT $ENV{VCToolsInstallDir} STREQUAL "") + set(MANIFEST_DPI "$ENV{VCToolsInstallDir}\\Include\\Manifest\\PerMonitorHighDPIAware.manifest") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /MANIFEST /manifest:embed /manifestinput:\"${MANIFEST_DPI}\"") + endif () +endif () # Defines if (DISABLE_OPENGL) @@ -72,3 +90,6 @@ else () # Makes OpenGL function get queried in run-time rather than linked-in add_definitions(-DOPENGL_NO_LINK) endif () +if (MSVC) + add_definitions(-D__DISABLE_DLL_PROXY__) +endif () diff --git a/src/openrct2-ui/Ui.cpp b/src/openrct2-ui/Ui.cpp index d17c9bb052..b94c3e6720 100644 --- a/src/openrct2-ui/Ui.cpp +++ b/src/openrct2-ui/Ui.cpp @@ -32,7 +32,7 @@ using namespace OpenRCT2::Ui; /** * Main entry point for non-Windows systems. Windows instead uses its own DLL proxy. */ -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__DISABLE_DLL_PROXY__) int NormalisedMain(int argc, const char * * argv) #else int main(int argc, const char * * argv) diff --git a/src/openrct2/CMakeLists.txt b/src/openrct2/CMakeLists.txt index 939f6c854f..ba91cd82e0 100644 --- a/src/openrct2/CMakeLists.txt +++ b/src/openrct2/CMakeLists.txt @@ -10,17 +10,26 @@ if (APPLE) endif () # Third party libraries -PKG_CHECK_MODULES(JANSSON REQUIRED jansson>=2.5) -PKG_CHECK_MODULES(LIBZIP REQUIRED libzip>=1.0) -PKG_CHECK_MODULES(ZLIB REQUIRED zlib) +if (MSVC) + find_package(jansson 2.5 REQUIRED) + find_package(png 1.6 REQUIRED) + find_package(zlib REQUIRED) -PKG_CHECK_MODULES(PNG libpng>=1.6) -if (NOT PNG_FOUND) - PKG_CHECK_MODULES(PNG libpng16) + find_path(LIBZIP_INCLUDE_DIRS zip.h) + find_library(LIBZIP_LIBRARIES zip) +else () + PKG_CHECK_MODULES(JANSSON REQUIRED jansson>=2.5) + PKG_CHECK_MODULES(LIBZIP REQUIRED libzip>=1.0) + PKG_CHECK_MODULES(ZLIB REQUIRED zlib) + + PKG_CHECK_MODULES(PNG libpng>=1.6) if (NOT PNG_FOUND) - PKG_CHECK_MODULES(PNG libpng>=1.2) + PKG_CHECK_MODULES(PNG libpng16) if (NOT PNG_FOUND) - PKG_CHECK_MODULES(PNG REQUIRED libpng12) + PKG_CHECK_MODULES(PNG libpng>=1.2) + if (NOT PNG_FOUND) + PKG_CHECK_MODULES(PNG REQUIRED libpng12) + endif () endif () endif () endif () @@ -92,7 +101,7 @@ if (NOT DISABLE_NETWORK) endif () endif () -if (NOT APPLE AND NOT MINGW) +if (NOT APPLE AND NOT MINGW AND NOT MSVC) # This is ugly hack to work around https://bugs.launchpad.net/ubuntu/+source/gcc-5/+bug/1568899. # Once C++17 is enabled (and thus old compilers are no longer supported, this needs to be gone. # We cannot simply detect the _compiler_ version, as the bug exists with the C++ _library_ @@ -152,7 +161,7 @@ if (NOT OPENRCT2_COMMIT_SHA1_SHORT STREQUAL "HEAD") COMPILE_DEFINITIONS OPENRCT2_COMMIT_SHA1_SHORT="${OPENRCT2_COMMIT_SHA1_SHORT}") endif() -if(X86 OR X86_64) -set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/drawing/SSE41Drawing.cpp PROPERTIES COMPILE_FLAGS -msse4.1) -set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/drawing/AVX2Drawing.cpp PROPERTIES COMPILE_FLAGS -mavx2) +if((X86 OR X86_64) AND NOT MSVC) + set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/drawing/SSE41Drawing.cpp PROPERTIES COMPILE_FLAGS -msse4.1) + set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/drawing/AVX2Drawing.cpp PROPERTIES COMPILE_FLAGS -mavx2) endif() From c59f342f2f2ae69f9981ae3f2e1fb90c471028de Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 12 Apr 2018 23:31:33 +0100 Subject: [PATCH 2/5] Use correct SDL2 library in debug builds --- src/openrct2-ui/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/openrct2-ui/CMakeLists.txt b/src/openrct2-ui/CMakeLists.txt index 86e7819d4e..88efe96987 100644 --- a/src/openrct2-ui/CMakeLists.txt +++ b/src/openrct2-ui/CMakeLists.txt @@ -12,7 +12,11 @@ if (MSVC) find_package(jansson REQUIRED) find_path(SDL2_INCLUDE_DIRS SDL2/SDL.h) - find_library(SDL2_LDFLAGS sdl2) + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + find_library(SDL2_LDFLAGS sdl2d) + else () + find_library(SDL2_LDFLAGS sdl2) + endif () find_library(SPEEX_LDFLAGS libspeexdsp) else () From f24b1bdfa696fb67bb6bef002a8dac11b9443575 Mon Sep 17 00:00:00 2001 From: Ted John Date: Fri, 13 Apr 2018 19:21:03 +0100 Subject: [PATCH 3/5] Enable curl, openssl and freetype libraries for msvc --- src/openrct2/CMakeLists.txt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/openrct2/CMakeLists.txt b/src/openrct2/CMakeLists.txt index ba91cd82e0..3d8d0206a2 100644 --- a/src/openrct2/CMakeLists.txt +++ b/src/openrct2/CMakeLists.txt @@ -36,17 +36,26 @@ endif () # Third party libraries (optional) if (NOT DISABLE_HTTP_TWITCH OR NOT DISABLE_NETWORK) - PKG_CHECK_MODULES(LIBCURL REQUIRED libcurl) + if (MSVC) + find_package(curl REQUIRED) + set(LIBCURL_LIBRARIES ${CURL_LIBRARIES}) + else () + PKG_CHECK_MODULES(LIBCURL REQUIRED libcurl) + endif () endif () if (NOT DISABLE_NETWORK) find_package(OpenSSL 1.0.0 REQUIRED) endif () if (NOT DISABLE_TTF) - if (UNIX AND NOT APPLE) + if (UNIX AND NOT APPLE AND NOT MSVC) PKG_CHECK_MODULES(FONTCONFIG REQUIRED fontconfig) endif () - PKG_CHECK_MODULES(FREETYPE REQUIRED freetype2) + if (MSVC) + find_package(freetype REQUIRED) + else () + PKG_CHECK_MODULES(FREETYPE REQUIRED freetype2) + endif () endif () # Sources From fc83f72fc6b98c39214e1ef801bf12e90f22e34c Mon Sep 17 00:00:00 2001 From: Ted John Date: Fri, 13 Apr 2018 21:56:58 +0100 Subject: [PATCH 4/5] Add a default / example CMakeSettings.json for VS --- .vs/CMakeSettings.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .vs/CMakeSettings.json diff --git a/.vs/CMakeSettings.json b/.vs/CMakeSettings.json new file mode 100644 index 0000000000..0e32f3a202 --- /dev/null +++ b/.vs/CMakeSettings.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "x64-Debug", + "inheritEnvironments": [ "msvc_x64_x64" ], + "generator": "Ninja", + "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}", + "configurationType": "Debug", + "variables": [ + { + "name": "VCPKG_TARGET_TRIPLET", + "value": "x64-windows-winssl" + }, + { + "name": "CMAKE_TOOLCHAIN_FILE", + "value": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake" + } + ] + } + ] +} From 0ea81d7df4918e700c7f121af3c47d2dd9c8b433 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 14 Apr 2018 17:50:32 +0100 Subject: [PATCH 5/5] Enable warnings as errors --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f1428e704a..424d32b5b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,7 +75,7 @@ function (ADD_CHECK_CXX_COMPILER_FLAG _CXXFLAGS _CACHE_VAR _FLAG) endfunction () if (MSVC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /std:c++latest /permissive- /Zc:externConstexpr") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /std:c++latest /permissive- /Zc:externConstexpr /WX") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244") # C4244: 'conversion_type': conversion from 'type1' to 'type2', possible loss of data add_definitions(-D_CRT_SECURE_NO_WARNINGS)