Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for building on native windows with cmake #2052

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
71b7391
Made cmake building on windows somewhat possible
Rob2309 Mar 7, 2024
2d4c7a5
Merge remote-tracking branch 'upstream/master' into windows-fixes2
Rob2309 Mar 7, 2024
dafe7cd
Fixed build in release mode
Rob2309 Mar 7, 2024
0fa2f37
Added portable version of gen_version.sh to cmake
Rob2309 Mar 7, 2024
efe5f1d
miniunz.cpp does not require c++17 anymore
Rob2309 Mar 7, 2024
274c7a4
Simplify win32 macro checking
Rob2309 Mar 7, 2024
2df4d08
Fixed compilation on linux gcc
Rob2309 Mar 8, 2024
f1cd42f
Added install commands, build now copies required DLLs to build folder
Rob2309 Mar 8, 2024
2382348
cmake now builds all dependencies from source
Rob2309 Mar 8, 2024
d6a6489
Fixed build on linux and cmake < 3.25
Rob2309 Mar 8, 2024
5f72a06
Fixed OpenAL header includes, fixed ogg dependency not found
Rob2309 Mar 9, 2024
bb598b6
Merge remote-tracking branch 'upstream/master' into windows-fixes2
Rob2309 Mar 9, 2024
8ac65bb
Added modification notice
Rob2309 Mar 15, 2024
4d59af0
Support building without steamworks
Rob2309 Mar 15, 2024
5d34e66
Fixed crash on world creation with disabled steamworks
Rob2309 Mar 15, 2024
e3efcce
Revert "Fixed crash on world creation with disabled steamworks"
Rob2309 Mar 16, 2024
daf6906
Revert "Support building without steamworks"
Rob2309 Mar 16, 2024
a39e829
More robust building without steamworks
Rob2309 Mar 16, 2024
d3ef188
Merge remote-tracking branch 'upstream/master' into windows-fixes2
Rob2309 Mar 16, 2024
dde4da1
Removed requirement for C++17 when building on native windows
Rob2309 Mar 21, 2024
296fd27
Merge branch 'master' into windows-fixes2
Rob2309 Mar 21, 2024
91eb85c
Merge branch 'master' into windows-fixes2
Rob2309 Mar 22, 2024
fededf9
Merge branch 'master' into windows-fixes2
Rob2309 Mar 22, 2024
b720e8c
Fixed MSVC assuming wrong source file encoding
Rob2309 Mar 22, 2024
4530fd3
Added option to build OpenAL-soft from source
Rob2309 Mar 22, 2024
6e95a67
Included windows resource file in CMakeLists.txt
Rob2309 Mar 30, 2024
9f9c5ed
Merge branch 'master' into windows-fixes2
Rob2309 Apr 20, 2024
5a47bb8
Merge commit '0c9d2e0682beb52f466e543953ac489730d53fe4' into windows-…
Rob2309 May 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern/steamworks/
extern/deps/
mods/
build/
build_kde/
Expand Down
130 changes: 80 additions & 50 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
cmake_minimum_required (VERSION 2.6)
cmake_minimum_required (VERSION 3.20)

project(keeper)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# set default cxxflags
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++1y -Wno-sign-compare -Wno-unused-variable -Wno-shift-count-overflow -ftemplate-depth=512 -DUSE_STEAMWORKS -I /home/michal/keeperrl/extern/steamworks/public")
# clang specific cflags
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-tautological-constant-out-of-range-compare -Wno-mismatched-tags")

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 14)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED on)

set(STDAFX_H "${CMAKE_CURRENT_SOURCE_DIR}/stdafx.h")
set(STDAFX_H_GCH "${CMAKE_CURRENT_BINARY_DIR}/stdagx.h.gch")
option(ENABLE_STEAMWORKS "Enable steamworks integration" off)

# set debug cxxflags
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wimplicit-fallthrough -Wno-unused-function")
#if ((${CMAKE_BUILD_TYPE} MATCHES "Debug") AND (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang"))
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -L ${CMAKE_CURRENT_SOURCE_DIR}/extern/steamworks/redistributable_bin/linux64/ -L /usr/local/lib -lsteam_api -Wl,-rpath=. -Wl,--gdb-index")
#endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(STDAFX_H "${CMAKE_CURRENT_SOURCE_DIR}/stdafx.h")
set(STDAFX_H_GCH "${CMAKE_CURRENT_BINARY_DIR}/stdafx.h.gch")

# additional cmake modules directory
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

if(ENABLE_STEAMWORKS)
add_library(steamworks SHARED IMPORTED)
set_target_properties(steamworks PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/extern/steamworks/redistributable_bin/win64/steam_api64.dll")
set_target_properties(steamworks PROPERTIES IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/extern/steamworks/redistributable_bin/win64/steam_api64.lib")
target_include_directories(steamworks INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/extern/steamworks/public")
endif()

# find required libraries
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
Expand All @@ -43,67 +49,91 @@ find_package(VorbisFile REQUIRED)
find_package(THEORA REQUIRED)

# generate version.h
file(GLOB GENERATE_VERSION_SH "gen_version.sh")
set(KEEPER_VERSION_H "${CMAKE_CURRENT_SOURCE_DIR}/version.h")
add_custom_command(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/
COMMAND ${GENERATE_VERSION_SH}
DEPENDS ${GENERATE_VERSION_SH}
OUTPUT ${KEEPER_VERSION_H}
COMMENT "Generating version.h"
)
if(ENABLE_GEN_VERSION)
file(GLOB GENERATE_VERSION_SH "gen_version.sh")
set(KEEPER_VERSION_H "${CMAKE_CURRENT_SOURCE_DIR}/version.h")
add_custom_command(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/
COMMAND ${GENERATE_VERSION_SH}
DEPENDS ${GENERATE_VERSION_SH}
OUTPUT ${KEEPER_VERSION_H}
COMMENT "Generating version.h"
)
endif()

# check member serialization
# the stamp file is not actually generated, but it doesn't seem to matter
file(GLOB CHECK_SERIAL_SH "check_serial.sh")
set(CHECK_SERIAL_STAMP "${CMAKE_CURRENT_SOURCE_DIR}/check_serial.stamp")
add_custom_command(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/
COMMAND bash ${CHECK_SERIAL_SH}
DEPENDS ${CHECK_SERIAL_SH}
OUTPUT ${CHECK_SERIAL_STAMP}
COMMENT "Checking serialization"
)
if(ENABLE_CHECK_SERIAL)
file(GLOB CHECK_SERIAL_SH "check_serial.sh")
set(CHECK_SERIAL_STAMP "${CMAKE_CURRENT_SOURCE_DIR}/check_serial.stamp")
add_custom_command(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/
COMMAND bash ${CHECK_SERIAL_SH}
DEPENDS ${CHECK_SERIAL_SH}
OUTPUT ${CHECK_SERIAL_STAMP}
COMMENT "Checking serialization"
)
endif()

# rules to create `keeper` binary
file(GLOB SOURCES "*.cpp" "extern/*.cpp")
add_executable(keeper ${SOURCES} ${KEEPER_VERSION_H} ${CHECK_SERIAL_STAMP})
target_include_directories(keeper PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(keeper PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/extern)
target_include_directories(keeper PRIVATE ${SDL2_INCLUDE_DIRS})
target_include_directories(keeper PRIVATE ${OPENAL_INCLUDE_DIRS})
target_link_libraries(keeper PRIVATE
${SDL2_LIBRARIES}
SDL2::SDL2
${SDL2_IMAGE_LIBRARY}
${OPENGL_gl_LIBRARY}
OpenGL::GL
${CMAKE_THREAD_LIBS_INIT}
${CURL_LIBRARIES}
CURL::libcurl
${ZLIB_LIBRARIES}
${OPENAL_LIBRARY}
${VORBIS_LIBRARIES}
${OGG_LIBRARIES}
${THEORA_dec_LIBRARY}
${VorbisFile_LIBRARIES}
)
if(ENABLE_STEAMWORKS)
target_link_libraries(keeper PRIVATE steamworks)
target_compile_definitions(keeper PRIVATE USE_STEAMWORKS)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_compile_definitions(keeper PRIVATE WINDOWS)
target_link_libraries(keeper PRIVATE dbghelp)
endif()
target_compile_options(keeper PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/w>)
target_compile_definitions(keeper PRIVATE $<$<CXX_COMPILER_ID:MSVC>:_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING>)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_source_files_properties(
content_factory.cpp
player_control.cpp
gui_builder.cpp
pretty_printing.cpp
task.cpp
PROPERTIES
COMPILE_FLAGS /bigobj
)
else()
set_source_files_properties(
content_factory.cpp
player_control.cpp
gui_builder.cpp
pretty_printing.cpp
task.cpp
PROPERTIES
COMPILE_FLAGS -Wa,-mbig-obj
)
endif()
endif()

# set up definitions
if ((${CMAKE_BUILD_TYPE} MATCHES "Release"))
target_compile_definitions(keeper PRIVATE RELEASE=1)
else()
# generate stdafx.h.gch for debug build
# workaround to unquote the cxxflags
set(CXX_FLAGS_LIST ${CMAKE_CXX_FLAGS})
separate_arguments(CXX_FLAGS_LIST)
add_custom_command(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/
COMMAND ${CMAKE_CXX_COMPILER} ${CXX_FLAGS_LIST} -x c++-header ${STDAFX_H} -MMD -o ${STDAFX_H_GCH}
DEPENDS ${STDAFX_H}
OUTPUT ${STDAFX_H_GCH}
COMMENT "Generating ${STDAFX_H_GCH}"
)
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include-pch ${STDAFX_H_GCH}")
endif()
target_sources(keeper PRIVATE ${STDAFX_H_GCH})
target_precompile_headers(keeper PRIVATE ${STDAFX_H})
endif()

if(ENABLE_LOCAL_USER_DIR)
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindTHEORA.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mark_as_advanced(THEORA_dec_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(THEORA
REQUIRED_VARS THEORA_LIBRARY THEORA_enc_LIBRARY THEORA_dec_LIBRARY THEORA_INCLUDE_DIR)
REQUIRED_VARS THEORA_dec_LIBRARY THEORA_INCLUDE_DIR)

if (THEORA_FOUND)
set(THEORA_LIBRARIES "${THEORA_LIBRARY}" "${THEORA_enc_LIBRARY}" "${THEORA_dec_LIBRARY}")
Expand Down
2 changes: 1 addition & 1 deletion collective.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CollectiveWarnings;
class Immigration;
class PositionMatching;
class MinionActivities;
class ResourceInfo;
struct ResourceInfo;
class StoragePositions;
class Furnace;
class Dancing;
Expand Down
2 changes: 1 addition & 1 deletion collective_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void CollectiveConfig::addBedRequirementToImmigrants(vector<ImmigrantInfo>& immi
[&](const auto&) {}
));
if (!hasBed) {
info.addRequirement(AttractionInfo(1, factory->furniture.getBedFurniture(*bedType)[0]));
info.addRequirement(AttractionInfo(1, AttractionType(factory->furniture.getBedFurniture(*bedType)[0])));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions content_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

class KeyVerifier;
class BuildInfo;
class ExternalEnemy;
class ResourceDistribution;
class EnemyInfo;
struct ExternalEnemy;
struct ResourceDistribution;
struct EnemyInfo;
class ImmigrantInfo;
class ZLevelInfo;
class BuildingInfo;
struct ZLevelInfo;
struct BuildingInfo;
struct LayoutGenerator;
struct TileGasInfo;
struct PromotionInfo;
Expand Down
59 changes: 53 additions & 6 deletions directory_path.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "stdafx.h"
#include "directory_path.h"
#include "file_path.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "dirent.h"
#ifndef _MSC_VER
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include "dirent.h"
#else
#include <filesystem>
#endif

DirectoryPath::DirectoryPath(string p) : path(std::move(p)) {
}
Expand All @@ -17,21 +21,31 @@ DirectoryPath DirectoryPath::subdirectory(const std::string& s) const {
return DirectoryPath(path + "/" + s);
}

#ifndef _MSC_VER
static bool isDirectory(const string& path) {
struct stat path_stat;
if (stat(path.c_str(), &path_stat))
return false;
else
return S_ISDIR(path_stat.st_mode);
}
#else
static bool isDirectory(const string& path) {
std::error_code err;
return std::filesystem::is_directory(path, err);
}
#endif

bool DirectoryPath::exists() const {
return isDirectory(getPath());
}

void DirectoryPath::createIfDoesntExist() const {
if (!exists()) {
#ifndef WINDOWS
#ifdef _MSC_VER
std::error_code err;
USER_CHECK(!std::filesystem::create_directory(path.data(), err));
#elif !defined(WINDOWS)
USER_CHECK(!mkdir(path.data(), 0750)) << "Unable to create directory \"" + path + "\": " + strerror(errno);
#else
USER_CHECK(!mkdir(path.data())) << "Unable to create directory \"" + path + "\": " + strerror(errno);
Expand All @@ -45,19 +59,30 @@ void DirectoryPath::removeRecursively() const {
remove(file.getPath());
for (auto subdir : getSubDirs())
subdirectory(subdir).removeRecursively();
rmdir(getPath());
#ifdef _MSC_VER
std::error_code err;
std::filesystem::remove(getPath(), err);
#else
rmdir(getPath());
#endif
}
}

static bool isRegularFile(const string& path) {
#ifndef _MSC_VER
struct stat path_stat;
if (stat(path.c_str(), &path_stat))
return false;
else
return S_ISREG(path_stat.st_mode);
#else
std::error_code err;
return std::filesystem::is_regular_file(path, err);
#endif
}

vector<FilePath> DirectoryPath::getFiles() const {
#ifndef _MSC_VER
vector<FilePath> ret;
if (DIR* dir = opendir(path.data())) {
while (dirent* ent = readdir(dir))
Expand All @@ -66,9 +91,18 @@ vector<FilePath> DirectoryPath::getFiles() const {
closedir(dir);
}
return ret;
#else
vector<FilePath> res;
for(const auto& entry : std::filesystem::directory_iterator{path.data()}) {
if(entry.is_regular_file())
res.push_back(FilePath(entry.path().filename().string(), entry.path().string()));
}
return res;
#endif
}

vector<string> DirectoryPath::getSubDirs() const {
#ifndef _MSC_VER
vector<string> ret;
if (DIR* dir = opendir(path.data())) {
while (dirent* ent = readdir(dir))
Expand All @@ -77,6 +111,15 @@ vector<string> DirectoryPath::getSubDirs() const {
closedir(dir);
}
return ret;
#else
vector<string> res;
std::error_code err;
for(const auto& entry : std::filesystem::directory_iterator{path.data(), err}) {
if(entry.is_directory())
res.push_back(entry.path().filename().string());
}
return res;
#endif
}

const char* DirectoryPath::getPath() const {
Expand Down Expand Up @@ -107,10 +150,14 @@ string getAbsolute(const char* path) {
}

DirectoryPath DirectoryPath::current() {
#ifndef _MSC_VER
char buffer[2048];
char* name = getcwd(buffer, sizeof(buffer) - 1);
CHECK(name && "getcwd error");
return DirectoryPath(name);
#else
return DirectoryPath(std::filesystem::current_path().string());
#endif
}

optional<string> DirectoryPath::copyRecursively(DirectoryPath to) {
Expand Down
2 changes: 1 addition & 1 deletion extern/iomanip.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <cstddef>

#ifdef _LIBCPP_VERSION
#if defined(_LIBCPP_VERSION) || defined(_WIN32)
#include <iomanip>
#else

Expand Down
1 change: 1 addition & 0 deletions extern/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#if defined(__has_include) && !defined(STX_NO_STD_VARIANT)
# if __has_include(<variant>) && (__cplusplus > 201402)
#include <variant>
namespace STX_NAMESPACE_NAME {
using std::variant;
using std::visit;
Expand Down
Loading