OpenLoco/CMakeLists.txt

203 lines
7.5 KiB
CMake
Raw Normal View History

2018-01-20 19:37:28 +01:00
cmake_minimum_required(VERSION 3.8)
2018-01-20 01:07:56 +01:00
set (PROJECT openloco)
project(${PROJECT} CXX)
2018-01-20 01:07:56 +01:00
if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt")
endif()
INCLUDE(FindPkgConfig)
set(USE_BOOST_FS_DEFAULT OFF)
2018-01-24 22:24:31 +01:00
if(APPLE OR MINGW)
set(USE_BOOST_FS_DEFAULT ON)
endif()
option(USE_BOOST_FILESYSTEM "Use Boost filesystem instead of C++17" ${USE_BOOST_FS_DEFAULT})
2018-01-20 01:07:56 +01:00
if (APPLE)
set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/usr/local/opt/openssl/lib/pkgconfig")
endif (APPLE)
# Define current git branch
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE OPENLOCO_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
# Define short commit hash
execute_process(
COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE OPENLOCO_COMMIT_SHA1_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
# To avoid unnecessary rebuilds, set the current branch and short sha1 hash
# only for the one file that uses these definitions: version.cpp
set_property(SOURCE ${CMAKE_SOURCE_DIR}/src/openloco/version.cpp
PROPERTY COMPILE_DEFINITIONS
OPENLOCO_BRANCH="${OPENLOCO_BRANCH}"
OPENLOCO_COMMIT_SHA1_SHORT="${OPENLOCO_COMMIT_SHA1_SHORT}")
2018-02-01 00:07:10 +01:00
# Set some compiler features
2018-02-08 22:42:30 +01:00
set(COMMON_COMPILE_OPTIONS "${COMMON_COMPILE_OPTIONS} -fstrict-aliasing -Werror -Wall")
# Poke some holes in -Wall:
set(COMMON_COMPILE_OPTIONS
"${COMMON_COMPILE_OPTIONS} \
-Wno-unknown-pragmas \
-Wno-unused-private-field \
")
2018-02-01 00:07:10 +01:00
# Set warnings common to supported compilers
set(COMMON_COMPILE_OPTIONS
"${COMMON_COMPILE_OPTIONS} \
-Waddress \
-Warray-bounds \
-Wchar-subscripts \
-Wenum-compare \
-Wformat \
-Wignored-qualifiers \
-Winit-self \
-Wmissing-declarations \
-Wnon-virtual-dtor \
-Wnull-dereference \
-Wstrict-aliasing \
-Wstrict-overflow=1 \
-Wundef \
-Wunreachable-code \
")
2018-01-20 01:07:56 +01:00
2018-01-20 19:37:28 +01:00
set(OBJ_FORMAT "elf32-i386")
set(LINKER_SCRIPT "ld_script_i386.xc")
2018-01-20 01:07:56 +01:00
# Handle creating the rct2 text and data files on OS X and Linux
# See details in src/openrct2.c:openrct2_setup_rct2_segment for how the values
# were derived.
if (UNIX)
set(OLOCO_EXE ${CMAKE_CURRENT_SOURCE_DIR}/loco.exe)
set(OLOCO_TEXT ${CMAKE_BINARY_DIR}/openloco_text)
set(OLOCO_DATA ${CMAKE_BINARY_DIR}/openloco_data)
2018-01-20 01:07:56 +01:00
add_custom_command(
2018-01-20 23:06:44 +01:00
OUTPUT ${OLOCO_TEXT}
COMMAND dd if="${OLOCO_EXE}" of="${OLOCO_TEXT}" bs=4096 skip=1 count=214
2018-01-20 01:07:56 +01:00
DEPENDS ${OLOCO_EXE}
)
add_custom_command(
2018-01-20 23:06:44 +01:00
OUTPUT ${OLOCO_DATA}
COMMAND dd if="${OLOCO_EXE}" of="${OLOCO_DATA}" bs=4096 skip=215 count=78
COMMAND dd if=/dev/zero of="${OLOCO_DATA}" bs=4096 seek=78 count=3133 conv=notrunc
2018-01-20 01:07:56 +01:00
DEPENDS ${OLOCO_EXE}
)
2018-01-20 23:06:44 +01:00
add_custom_target(segfiles DEPENDS ${OLOCO_TEXT} ${OLOCO_DATA})
if (APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sectcreate loco_text __text \"${OLOCO_TEXT}\" -sectcreate loco_data __data ${OLOCO_DATA} -segaddr loco_data 0x4d7000 -segprot loco_data rwx rwx -segaddr loco_text 0x401000 -segprot loco_text rwx rwx -segaddr __TEXT 0x2000000 -read_only_relocs suppress")
else ()
# For Linux we have to use objcopy to wrap regular binaries into a linkable
# format. We use specific section names which are then referenced in a
# bespoke linker script so they can be placed at predefined VMAs.
add_custom_command(
OUTPUT openloco_text_section.o
COMMAND objcopy --input binary --output ${OBJ_FORMAT} --binary-architecture i386 \"${OLOCO_TEXT}\" openloco_text_section.o --rename-section .data=.loco_text,contents,alloc,load,readonly,code
DEPENDS segfiles
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_command(
OUTPUT openloco_data_section.o
COMMAND objcopy --input binary --output ${OBJ_FORMAT} --binary-architecture i386 \"${OLOCO_DATA}\" openloco_data_section.o --rename-section .data=.loco_data,contents,alloc,load,readonly,data
DEPENDS segfiles
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_target(linkable_sections DEPENDS openloco_text_section.o openloco_data_section.o)
set_source_files_properties(
openloco_text_section.o openloco_data_section.o
PROPERTIES
EXTERNAL_OBJECT true
GENERATED true
)
# can't use GLOB here, as the files don't exist yet at cmake-time
set(LOCO_SECTIONS "${CMAKE_BINARY_DIR}/openloco_data_section.o" "${CMAKE_BINARY_DIR}/openloco_text_section.o")
2018-01-20 19:39:47 +01:00
set(LOCO_SEGMENT_LINKER_FLAGS "-Wl,-T,\"${CMAKE_CURRENT_SOURCE_DIR}/distribution/linux/${LINKER_SCRIPT}\"")
endif ()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LOCO_SEGMENT_LINKER_FLAGS}")
2018-01-20 01:07:56 +01:00
endif (UNIX)
set(DEBUG_LEVEL 0 CACHE STRING "Select debug level for compilation. Use value in range 03.")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG=${DEBUG_LEVEL}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG=${DEBUG_LEVEL}")
# include lib
include_directories("lib/")
# add source files
2018-01-20 23:03:43 +01:00
file(GLOB_RECURSE OLOCO_SOURCES "src/*.cpp" "src/*.h" "src/*.hpp")
2018-01-20 01:07:56 +01:00
2018-01-20 17:02:05 +01:00
if (APPLE)
file(GLOB_RECURSE OLOCO_MM_SOURCES "src/*.mm")
set_source_files_properties(${OLOCO_MM_SOURCES} PROPERTIES COMPILE_FLAGS "-x objective-c++ -fmodules")
endif ()
2018-01-20 01:07:56 +01:00
set(PIE_FLAG "-fno-pie")
set(TARGET_M "-m32")
# set necessary flags to compile code as is
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_M} -std=gnu99 ${COMMON_COMPILE_OPTIONS} -Wimplicit")
2018-01-20 19:37:28 +01:00
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TARGET_M} ${COMMON_COMPILE_OPTIONS} -Wnon-virtual-dtor")
2018-01-20 01:07:56 +01:00
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${TARGET_M}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS} ${TARGET_M} ${PIE_FLAG}")
2018-01-20 01:07:56 +01:00
PKG_CHECK_MODULES(SDL2 REQUIRED sdl2)
SET(SDL2LIBS ${SDL2_DEFINITIONS})
if (USE_BOOST_FILESYSTEM)
set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost COMPONENTS filesystem)
if (NOT Boost_FOUND)
message("Dynamic Boost not found, retrying with static")
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS filesystem REQUIRED)
endif ()
endif()
2018-01-20 01:07:56 +01:00
# Disable optimizations for interop.cpp for all compilers, to allow optimized
# builds without need for -fno-omit-frame-pointer
set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/src/openloco/interop/interop.cpp" PROPERTIES COMPILE_FLAGS -fno-omit-frame-pointer)
2018-01-20 19:37:28 +01:00
set(CMAKE_CXX_STANDARD 17)
2018-01-24 22:24:31 +01:00
if (MINGW)
add_library(${PROJECT} ${OLOCO_SOURCES})
target_link_libraries(${PROJECT} winmm)
else ()
add_executable(${PROJECT} ${OLOCO_SOURCES} ${OLOCO_MM_SOURCES} ${LOCO_SECTIONS})
endif ()
target_link_libraries(${PROJECT} SDL2 ${SDL2LIBS})
2018-01-24 22:24:31 +01:00
if (NOT MINGW)
add_dependencies(${PROJECT} segfiles)
endif()
2018-01-20 22:24:53 +01:00
target_compile_definitions(${PROJECT} PRIVATE _NO_LOCO_WIN32_=1)
if (USE_BOOST_FILESYSTEM)
target_link_libraries(${PROJECT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY})
target_compile_definitions(${PROJECT} PRIVATE BOOST_SYSTEM_NO_DEPRECATED=1 BOOST_FILESYSTEM_NO_DEPRECATED=1 _OPENLOCO_USE_BOOST_FS_=1)
else()
target_link_libraries(${PROJECT} stdc++fs)
endif()
2018-01-20 17:02:05 +01:00
2018-01-20 19:38:17 +01:00
if (MINGW)
target_compile_definitions(${PROJECT} PRIVATE _WIN32_WINNT=0x0600)
endif()
2018-01-20 17:02:05 +01:00
if (APPLE)
target_link_libraries(${PROJECT} "-framework Cocoa")
2018-01-24 06:30:48 +01:00
target_compile_definitions(${PROJECT} PRIVATE COMPAT_STD_BYTE=1)
2018-01-20 17:02:05 +01:00
endif ()