From 4741afd2cfc1c38ae0a81c1b71134b1a323b9ac1 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Wed, 13 Jan 2021 12:45:10 +0100 Subject: [PATCH] Port cmake improvements to miami --- CMakeLists.txt | 94 +++++++++++++++------- cmake/FindMPG123.cmake | 28 ------- cmake/FindSndFile.cmake | 84 +++++++++---------- cmake/Findmpg123.cmake | 38 +++++++++ src/CMakeLists.txt | 174 +++++++++++++++++++++------------------- 5 files changed, 237 insertions(+), 181 deletions(-) delete mode 100644 cmake/FindMPG123.cmake create mode 100644 cmake/Findmpg123.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 387400b3..7282135d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,44 +1,82 @@ cmake_minimum_required(VERSION 3.8) -project(reVC C CXX) +set(EXECUTABLE reVC) +set(PROJECT REVC) + +project(${EXECUTABLE} C CXX) list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") if(WIN32) - set(REVC_AUDIOS "NULL" "OAL" "MSS") + set(${PROJECT}_AUDIOS "OAL" "MSS") else() - set(REVC_AUDIOS "NULL" "OAL") + set(${PROJECT}_AUDIOS "OAL") endif() -set(REVC_AUDIO "OAL" CACHE STRING "Audio") +set(${PROJECT}_AUDIO "OAL" CACHE STRING "Audio") -set_property(CACHE REVC_AUDIO PROPERTY STRINGS ${REVC_AUDIOS}) -message(STATUS "REVC_AUDIO = ${REVC_AUDIO} (choices=${REVC_AUDIOS})") -set("REVC_AUDIO_${REVC_AUDIO}" ON) -if(NOT REVC_AUDIO IN_LIST REVC_AUDIOS) - message(FATAL_ERROR "Illegal REVC_AUDIO=${REVC_AUDIO}") +option(${PROJECT}_WITH_OPUS "Build ${EXECUTABLE} with opus support" OFF) +option(${PROJECT}_WITH_LIBSNDFILE "Build ${EXECUTABLE} with libsndfile (instead of internal decoder)" OFF) + +set_property(CACHE ${PROJECT}_AUDIO PROPERTY STRINGS ${${PROJECT}_AUDIOS}) +message(STATUS "${PROJECT}_AUDIO = ${${PROJECT}_AUDIO} (choices=${${PROJECT}_AUDIOS})") +set("${PROJECT}_AUDIO_${${PROJECT}_AUDIO}" ON) +if(NOT ${PROJECT}_AUDIO IN_LIST ${PROJECT}_AUDIOS) + message(FATAL_ERROR "Illegal ${PROJECT}_AUDIO=${${PROJECT}_AUDIO}") endif() -if(REVC_INSTALL) - include(GNUInstallDirs) - set(REVC_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/reVC") +option(${PROJECT}_VENDORED_LIBRW "Use vendored librw" ON) +if(${PROJECT}_VENDORED_LIBRW) + add_subdirectory(vendor/librw) +else() + find_package(librw REQUIRED) endif() - -add_subdirectory("vendor/librw") add_subdirectory(src) -if(REVC_INSTALL) - include(CMakePackageConfigHelpers) - configure_package_config_file(reVC-config.cmake.in reVC-config.cmake - INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}" - ) - install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/reVC-config.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" - ) - install( - EXPORT reVC-targets - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" - ) +if(${PROJECT}_INSTALL) + install(DIRECTORY gamefiles/ DESTINATION ".") + if(LIBRW_PLATFORM_NULL) + set(platform "-null") + elseif(LIBRW_PLATFORM_PS2) + set(platform "-ps2") + elseif(LIBRW_PLATFORM_GL3) + if(LIBRW_GL3_GFXLIB STREQUAL "GLFW") + set(platform "-gl3-glfw") + else() + set(platform "-gl3-sdl2") + endif() + elseif(LIBRW_PLATFORM_D3D9) + set(platform "-d3d9") + endif() + if(${PROJECT}_AUDIO_OAL) + set(audio "-oal") + elseif(${PROJECT}_AUDIO_MSS) + set(audio "-mss") + endif() + if(${PROJECT}_WITH_OPUS) + set(audio "${audio}-opus") + endif() + if(NOT LIBRW_PLATFORM_PS2) + if(WIN32) + set(os "-win") + elseif(APPLE) + set(os "-apple") + elseif(UNIX) + set(os "-linux") + else() + set(compiler "-UNK") + message(WARNING "Unknown os. Created cpack package will be wrong. (override using cpack -P)") + endif() + endif() - include(CMakeCPack.cmake) + set(CPACK_PACKAGE_NAME "${PROJECT_NAME}${platform}${audio}${os}${compiler}") + set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "GTA III reversed") + set(CPACK_PACKAGE_VENDOR "GTAModding") + # FIXME: missing license (https://github.com/GTAmodding/re3/issues/794) + # set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/LICENSE") + # set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE") + set(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}") + set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}") + set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}") + set(CPACK_GENERATOR "TXZ") + include(CPack) endif() diff --git a/cmake/FindMPG123.cmake b/cmake/FindMPG123.cmake deleted file mode 100644 index a9b6dd8b..00000000 --- a/cmake/FindMPG123.cmake +++ /dev/null @@ -1,28 +0,0 @@ -# - Find mpg123 -# Find the native mpg123 includes and library -# -# MPG123_INCLUDE_DIR - where to find mpg123.h -# MPG123_LIBRARIES - List of libraries when using mpg123. -# MPG123_FOUND - True if mpg123 found. - -IF(MPG123_INCLUDE_DIR AND MPG123_LIBRARIES) - # Already in cache, be silent - SET(MPG123_FIND_QUIETLY TRUE) -ENDIF(MPG123_INCLUDE_DIR AND MPG123_LIBRARIES) - -FIND_PATH(MPG123_INCLUDE_DIR mpg123.h - PATHS "${MPG123_DIR}" - PATH_SUFFIXES include - ) - -FIND_LIBRARY(MPG123_LIBRARIES NAMES mpg123 mpg123-0 - PATHS "${MPG123_DIR}" - PATH_SUFFIXES lib - ) - -# MARK_AS_ADVANCED(MPG123_LIBRARIES MPG123_INCLUDE_DIR) - -# handle the QUIETLY and REQUIRED arguments and set MPG123_FOUND to TRUE if -# all listed variables are TRUE -INCLUDE(FindPackageHandleStandardArgs) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(MPG123 DEFAULT_MSG MPG123_LIBRARIES MPG123_INCLUDE_DIR) diff --git a/cmake/FindSndFile.cmake b/cmake/FindSndFile.cmake index 8ae47b70..5381af48 100644 --- a/cmake/FindSndFile.cmake +++ b/cmake/FindSndFile.cmake @@ -4,9 +4,11 @@ # # Once done this will define # -# SNDFILE_FOUND - system has libsndfile +# SNDFILE_FOUND - system has libsndfile # SNDFILE_INCLUDE_DIRS - the libsndfile include directory -# SNDFILE_LIBRARIES - Link these to use libsndfile +# SNDFILE_LIBRARIES - Link these to use libsndfile +# SNDFILE_CFLAGS - Compile options to use libsndfile +# SndFile::SndFile - Imported library of libsndfile # # Copyright (C) 2006 Wengo # @@ -15,53 +17,51 @@ # For details see the accompanying COPYING-CMAKE-SCRIPTS file. # -if (SNDFILE_LIBRARIES AND SNDFILE_INCLUDE_DIRS) - # in cache already - set(SNDFILE_FOUND TRUE) -else (SNDFILE_LIBRARIES AND SNDFILE_INCLUDE_DIRS) +find_package(PkgConfig QUIET) +if(PKG_CONFIG_FOUND) + pkg_search_module(PKG_SNDFILE "sndfile") +endif() - find_path(SNDFILE_INCLUDE_DIR +find_path(SNDFILE_INCLUDE_DIR NAMES - sndfile.h + sndfile.h + HINTS + ${PKG_SNDFILE_INCLUDE_DIRS} PATHS - /usr/include - /usr/local/include - /opt/local/include - /sw/include - ) - - find_library(SNDFILE_LIBRARY + /usr/include + /usr/local/include + /opt/local/include + /sw/include + ) + +find_library(SNDFILE_LIBRARY NAMES - sndfile + sndfile + HINTS + ${PKG_SNDFILE_LIBRARIES} PATHS - /usr/lib - /usr/local/lib - /opt/local/lib - /sw/lib - ) + /usr/lib + /usr/local/lib + /opt/local/lib + /sw/lib +) - set(SNDFILE_INCLUDE_DIRS - ${SNDFILE_INCLUDE_DIR} - ) - set(SNDFILE_LIBRARIES - ${SNDFILE_LIBRARY} - ) +set(SNDFILE_CFLAGS "${PKG_SNDFILE_CFLAGS_OTHER}" CACHE STRING "CFLAGS of libsndfile") - if (SNDFILE_INCLUDE_DIRS AND SNDFILE_LIBRARIES) +set(SNDFILE_INCLUDE_DIRS "${SNDFILE_INCLUDE_DIR}") +set(SNDFILE_LIBRARIES "${SNDFILE_LIBRARY}") + +if(SNDFILE_INCLUDE_DIRS AND SNDFILE_LIBRARIES) set(SNDFILE_FOUND TRUE) - endif (SNDFILE_INCLUDE_DIRS AND SNDFILE_LIBRARIES) +endif() - if (SNDFILE_FOUND) - if (NOT SndFile_FIND_QUIETLY) - message(STATUS "Found libsndfile: ${SNDFILE_LIBRARIES}") - endif (NOT SndFile_FIND_QUIETLY) - else (SNDFILE_FOUND) - if (SndFile_FIND_REQUIRED) - message(FATAL_ERROR "Could not find libsndfile") - endif (SndFile_FIND_REQUIRED) - endif (SNDFILE_FOUND) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(SndFile DEFAULT_MSG SNDFILE_INCLUDE_DIRS SNDFILE_LIBRARIES) - # show the SNDFILE_INCLUDE_DIRS and SNDFILE_LIBRARIES variables only in the advanced view - mark_as_advanced(SNDFILE_INCLUDE_DIRS SNDFILE_LIBRARIES) - -endif (SNDFILE_LIBRARIES AND SNDFILE_INCLUDE_DIRS) +if(NOT TARGET SndFile::SndFile) + add_library(__SndFile INTERFACE) + target_compile_options(__SndFile INTERFACE ${SNDFILE_CFLAGS}) + target_include_directories(__SndFile INTERFACE ${SNDFILE_INCLUDE_DIRS}) + target_link_libraries(__SndFile INTERFACE ${SNDFILE_LIBRARIES}) + add_library(SndFile::SndFile ALIAS __SndFile) +endif() diff --git a/cmake/Findmpg123.cmake b/cmake/Findmpg123.cmake new file mode 100644 index 00000000..c6fe56bb --- /dev/null +++ b/cmake/Findmpg123.cmake @@ -0,0 +1,38 @@ +# - Find mpg123 +# Find the native mpg123 includes and library +# +# mpg123_INCLUDE_DIR - Where to find mpg123.h +# mpg123_LIBRARIES - List of libraries when using mpg123. +# mpg123_CFLAGS - Compile options to use mpg123 +# mpg123_FOUND - True if mpg123 found. +# MPG123::libmpg123 - Imported library of libmpg123 + +find_package(PkgConfig QUIET) +if(PKG_CONFIG_FOUND) + pkg_search_module(PKG_MPG123 mpg123) +endif() + +find_path(mpg123_INCLUDE_DIR mpg123.h + HINTS ${PKG_MPG123_INCLUDE_DIRS} + PATHS "${mpg123_DIR}" + PATH_SUFFIXES include +) + +find_library(mpg123_LIBRARIES NAMES mpg123 mpg123-0 + HINTS ${PKG_MPG123_LIBRARIES} + PATHS "${mpg123_DIR}" + PATH_SUFFIXES lib +) + +set(mpg123_CFLAGS "${PKG_MPG123_CFLAGS_OTHER}" CACHE STRING "CFLAGS of mpg123") + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(mpg123 DEFAULT_MSG mpg123_LIBRARIES mpg123_INCLUDE_DIR) + +if(NOT TARGET MPG123::libmpg123) + add_library(__libmpg123 INTERFACE) + target_compile_options(__libmpg123 INTERFACE ${mpg123_CFLAGS}) + target_include_directories(__libmpg123 INTERFACE ${mpg123_INCLUDE_DIR}) + target_link_libraries(__libmpg123 INTERFACE ${mpg123_LIBRARIES}) + add_library(MPG123::libmpg123 ALIAS __libmpg123) +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e87878a8..dc204d17 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,92 +1,105 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -if(${REVC_AUDIO} STREQUAL "OAL") - find_package(OpenAL REQUIRED) - find_package(MPG123 REQUIRED) - find_package(SndFile REQUIRED) -endif() +file(GLOB_RECURSE ${PROJECT}_SOURCES "*.cpp" "*.h" "*.rc") -file(GLOB_RECURSE Sources "*.cpp" "*.h") +function(header_directories RETURN_LIST) + file(GLOB_RECURSE ALL_SRCS *.h *.cpp *.c) + set(RELDIRS) + foreach(SRC ${ALL_SRCS}) + file(RELATIVE_PATH RELSRC "${CMAKE_CURRENT_SOURCE_DIR}" "${SRC}") + get_filename_component(RELDIR "${RELSRC}" DIRECTORY) + list(APPEND RELDIRS ${RELDIR}) + endforeach() + list(REMOVE_DUPLICATES RELDIRS) + set(${RETURN_LIST} ${RELDIRS} PARENT_SCOPE) +endfunction() -MACRO(HEADER_DIRECTORIES return_list) - FILE(GLOB_RECURSE new_list *.cpp) - SET(dir_list "animation" - "audio" - "collision" - "control" - "core" - "entities" - "extras" - "fakerw" - "math" - "modelinfo" - "objects" - "peds" - "render" - "rw" - "save" - "skel" - "text" - "vehicles" - "weapons") - FOREACH(file_path ${new_list}) - GET_FILENAME_COMPONENT(dir_path ${file_path} PATH) - SET(dir_list ${dir_list} ${dir_path}) - ENDFOREACH() - LIST(REMOVE_DUPLICATES dir_list) - SET(${return_list} ${dir_list}) -ENDMACRO() +header_directories(${PROJECT}_INCLUDES) -HEADER_DIRECTORIES(header_list) -include_directories(${header_list}) +add_executable(${EXECUTABLE} WIN32 + ${${PROJECT}_SOURCES} +) +target_link_libraries(${EXECUTABLE} PRIVATE + librw::librw + Threads::Threads +) -add_executable(reVC ${Sources}) -target_link_libraries(reVC librw) -target_link_libraries(reVC Threads::Threads) - -if(${REVC_AUDIO} STREQUAL "OAL") - target_link_libraries(reVC ${OPENAL_LIBRARY}) - target_link_libraries(reVC ${MPG123_LIBRARIES}) - target_link_libraries(reVC ${SNDFILE_LIBRARIES}) -endif() - -target_include_directories(reVC - INTERFACE - $ - ) - -target_compile_definitions(reVC +target_include_directories(${EXECUTABLE} PRIVATE - "$,DEBUG,NDEBUG>" - PUBLIC - "RW_${REVC_PLATFORM}" - ) + $ + $ +) -target_compile_definitions(reVC PRIVATE LIBRW=1 AUDIO_OAL=1) +target_compile_definitions(${EXECUTABLE} + PRIVATE + $,DEBUG,NDEBUG> + LIBRW + ${PROJECT}_NO_AUTOLINK +) + +if(LIBRW_PLATFORM_D3D9) + target_compile_definitions(${EXECUTABLE} + PUBLIC + USE_D3D9 + ) +endif() + +if(${PROJECT}_AUDIO STREQUAL "OAL") + find_package(OpenAL REQUIRED) + target_include_directories(${EXECUTABLE} PRIVATE ${OPENAL_INCLUDE_DIR}) + target_link_libraries(${EXECUTABLE} PRIVATE ${OPENAL_LIBRARY}) + target_compile_definitions(${EXECUTABLE} PRIVATE ${OPENAL_DEFINITIONS}) + target_compile_definitions(${EXECUTABLE} PRIVATE AUDIO_OAL) +elseif(${PROJECT}_AUDIO STREQUAL "MSS") + find_package(MilesSDK REQUIRED) + target_compile_definitions(${EXECUTABLE} PRIVATE AUDIO_MSS) + target_link_libraries(${EXECUTABLE} PRIVATE MilesSDK::MilesSDK) +endif() + +find_package(mpg123 REQUIRED) +target_link_libraries(${EXECUTABLE} PRIVATE + MPG123::libmpg123 +) +if(${PROJECT}_WITH_OPUS) + find_package(opusfile REQUIRED) + target_link_libraries(${EXECUTABLE} PRIVATE + opusfile::opusfile + ) + target_compile_definitions(${EXECUTABLE} PRIVATE AUDIO_OPUS) +endif() +if(${PROJECT}_WITH_LIBSNDFILE) + find_package(SndFile REQUIRED) + target_link_libraries(${EXECUTABLE} PRIVATE + SndFile::SndFile + ) + target_compile_definitions(${EXECUTABLE} PRIVATE AUDIO_OAL_USE_SNDFILE) +endif() + +target_compile_definitions(${EXECUTABLE} PRIVATE ) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") - target_compile_options(reVC + target_compile_options(${EXECUTABLE} PRIVATE "-Wall" - ) - if (NOT REVC_PLATFORM_PS2) - target_compile_options(reVC + ) + if (NOT LIBRW_PLATFORM_PS2) + target_compile_options(${EXECUTABLE} PRIVATE - "-Wextra" - "-Wdouble-promotion" - "-Wpedantic" - ) + -Wextra + -Wdouble-promotion + -Wpedantic + ) endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - target_compile_options(reVC + target_compile_options(${EXECUTABLE} PUBLIC - /wd4996 /wd4244 - ) + /Zc:sizedDealloc- + ) endif() -set_target_properties(reVC +set_target_properties(${EXECUTABLE} PROPERTIES C_STANDARD 11 C_EXTENSIONS OFF @@ -94,20 +107,15 @@ set_target_properties(reVC CXX_STANDARD 11 CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON - PREFIX "" - ) - -if(REVC_INSTALL) - target_include_directories(reVC - INTERFACE - $ - ) +) +if(${PROJECT}_INSTALL) install( - TARGETS reVC - EXPORT reVC-targets - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ) + TARGETS ${EXECUTABLE} + EXPORT ${EXECUTABLE}-targets + RUNTIME DESTINATION "." + ) + if(MSVC) + install(FILES $ DESTINATION "." OPTIONAL) + endif() endif()