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

added gtest and cleaned up cmake #6

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 0 additions & 6 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,3 @@ jobs:
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
290 changes: 158 additions & 132 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,173 +1,199 @@
cmake_minimum_required(VERSION 3.10)
project(Kraken LANGUAGES CXX)
cmake_minimum_required(VERSION 3.16)
set(LIBRARY "Kraken")
set(TESTING "Kraken_Test")
project(${TESTING} LANGUAGES CXX)
project(${LIBRARY} LANGUAGES CXX)

file(GLOB SOURCES "src/*.cpp")
file(GLOB TESTS "test/*.cpp")
add_library(Kraken ${SOURCES})
add_executable(Kraken_Test EXCLUDE_FROM_ALL ${SOURCES} ${TESTS})

set_target_properties( ${LIBRARY} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )
set_target_properties( ${LIBRARY} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )

set_target_properties( ${TESTING} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )
set_target_properties( ${TESTING} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -m64")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

include_directories(include)
include(FetchContent)
include(GoogleTest)

# ================ <deps> ==============

set(FREETYPE_GIT_VERSION VER-2-13-2)
set(SDL_GIT_VERSION release-2.28.5)
set(SDL_TTF_GIT_VERSION release-2.20.2)
set(SDL_IMAGE_GIT_VERSION release-2.8.2)
set(SDL_MIXER_GIT_VERSION release-2.6.3)
set(TMX_LITE_VERSION v1.4.4)

set(SDL2MIXER_VENDORED TRUE)

if (NOT freetype_FOUND) # If there's none, fetch and build
include(FetchContent)
FetchContent_Declare(
freetype
GIT_REPOSITORY https://github.com/freetype/freetype.git
GIT_TAG ${FREETYPE_GIT_VERSION}
)
FetchContent_GetProperties(freetype)
if (NOT freetype_POPULATED)
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(freetype)
message("freetype_LIBRARY: " ${freetype_LIBRARY}) # fail but define in cmake
message("freetype_INCLUDE_DIR: " ${freetype_INCLUDE_DIR}) #pass
message("freetype_SOURCE_DIR: " ${freetype_SOURCE_DIR}) # pass
message("freetype_BINARY_DIR: " ${freetype_BINARY_DIR}) # pass

# build freetype
list(APPEND CPP_INCLUDE_DIRS ${freetype_SOURCE_DIR}/include)
list(APPEND CPP_INCLUDE_DIRS ${freetype_BINARY_DIR}/include)
list(APPEND CPP_HEADER_FILES ${freetype_SOURCE_DIR}/include/ft2build.h)

list(APPEND FREETYPE_INCLUDE_DIRS ${freetype_SOURCE_DIR}/include)
list(APPEND FREETYPE_INCLUDE_DIRS ${freetype_BINARY_DIR}/include)

set(FREETYPE_LIBRARY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

add_subdirectory(${freetype_SOURCE_DIR} ${freetype_BINARY_DIR}) #add to build in sub dir
add_library(Freetype::Freetype ALIAS freetype)

endif()
endif()
###############
# SDL2 #
###############

message("sdl_FOUND:" ${SDL_FOUND})
if (NOT sdl_FOUND) # If there's none, fetch and build sdl
include(FetchContent)
FetchContent_Declare(
sdl
set(SDL2_DISABLE_INSTALL OFF CACHE BOOL "Disable installation of SDL2") # i forget why this is necessary but im assuming it wont work without
set(SDL_STATIC OFF CACHE BOOL "Build a static library")
set(SDL_SHARED ON CACHE BOOL "Build a shared library")
set(SDL_TEST OFF CACHE BOOL "Build the test programs")
set(SDL_STATIC_PIC OFF CACHE BOOL "Build static libraries with PIC (Position Independent Code)")
FetchContent_Declare(
SDL2
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG ${SDL_GIT_VERSION}
)
FetchContent_GetProperties(sdl)
if (NOT SDL2_POPULATED)
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(sdl)
# build SDL2
list(APPEND CPP_INCLUDE_DIRS ${sdl_SOURCE_DIR}/include)

link_directories(${PROJECT_BINARY_DIR}/Debug)
add_subdirectory(${sdl_SOURCE_DIR} ${sdl_BINARY_DIR})

#needed for sub modules
list(APPEND SDL2_INCLUDE_DIR ${sdl_SOURCE_DIR}/include)

list(APPEND SDL2_LIBRARIES ${PROJECT_SOURCE_DIR}/build/Debug/SDL2maind.lib)
list(APPEND SDL2_LIBRARIES ${PROJECT_SOURCE_DIR}/build/Debug/SDL2-staticd.lib)
list(APPEND SDL2_LIBRARIES ${PROJECT_SOURCE_DIR}/build/Debug/SDL2d.lib)

list(APPEND CPP_LIBS "SDL2main")
list(APPEND CPP_LIBS "SDL2-static")
add_library(SDL2::SDL2 ALIAS SDL2)
add_library(SDL2::SDL2-static ALIAS SDL2-static)

endif()
endif()

set(SDL2TTF_INSTALL OFF CACHE STRING "" FORCE)
GIT_TAG ${SDL_GIT_VERSION}
GIT_PROGRESS TRUE
)
# set(SDL_SHARED OFF CACHE BOOL "Build a shared library")
FetchContent_MakeAvailable(SDL2)
target_include_directories(Kraken PRIVATE ${SDL2_SOURCE_DIR}/include)
target_include_directories(Kraken_Test PRIVATE ${SDL2_SOURCE_DIR}/include)

###############
# SDL2_ttf #
###############

set(BUILD_SHARED_LIBS ON CACHE BOOL "Build the library as a shared library")
# set(SDL2TTF_BUILD_SHARED_LIBS OFF CACHE BOOL "Build the library as a shared library")
set(SDL2TTF_SAMPLES OFF CACHE BOOL "Build the SDL2_ttf sample program(s)")
# let it know it needs to get its vendored deps
set(SDL2TTF_VENDORED ON CACHE BOOL "Use the vendored version of FreeType and HarfBuzz")
FetchContent_Declare(
SDL2_ttf
GIT_REPOSITORY https://github.com/libsdl-org/SDL_ttf
GIT_TAG ${SDL_TTF_GIT_VERSION}
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(SDL2_ttf)
target_include_directories(Kraken PRIVATE ${SDL2_ttf_SOURCE_DIR}) # include is in the root of the repo
target_include_directories(Kraken_Test PRIVATE ${SDL2_ttf_SOURCE_DIR}) # include is in the root of the repo

if (NOT sdl_ttf_FOUND) # If there's none, fetch and build sdl
include(FetchContent)
FetchContent_Declare(
sdl_ttf
GIT_REPOSITORY https://github.com/libsdl-org/SDL_ttf.git
GIT_TAG ${SDL_TTF_GIT_VERSION}
)
FetchContent_GetProperties(sdl_ttf)
if (NOT sdl_ttf_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(sdl_ttf)
add_subdirectory(${sdl_ttf_SOURCE_DIR} ${sdl_ttf_BINARY_DIR})
endif()
endif()
###############
# SDL_image #
###############

if (NOT sdl_image_FOUND) # If there's none, fetch and build sdl
include(FetchContent)
FetchContent_Declare(
sdl_image
FetchContent_Declare(
SDL2_image
GIT_REPOSITORY https://github.com/libsdl-org/SDL_image.git
GIT_TAG ${SDL_IMAGE_GIT_VERSION}
)
FetchContent_GetProperties(sdl_image)
if (NOT sdl_image_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(sdl_image)

set(SUPPORT_JPG OFF CACHE BOOL "" FORCE)
set(SUPPORT_PNG ON CACHE BOOL "" FORCE)
set(BUILD_SHOWIMAGE OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

# build sdl2 image
add_subdirectory(${sdl_image_SOURCE_DIR} ${sdl_image_BINARY_DIR})
endif()
endif()
GIT_TAG ${SDL_IMAGE_GIT_VERSION}
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(SDL2_image)
target_include_directories(Kraken PRIVATE ${SDL2_image_SOURCE_DIR}/include)
target_include_directories(Kraken_Test PRIVATE ${SDL2_image_SOURCE_DIR}/include)

if (NOT sdl_mixer_FOUND) # If there's none, fetch and build sdl
include(FetchContent)
FetchContent_Declare(
sdl_mixer
GIT_REPOSITORY https://github.com/libsdl-org/SDL_mixer.git
GIT_TAG ${SDL_MIXER_GIT_VERSION}
)
FetchContent_GetProperties(sdl_mixer)
###############
# SDL_mixer #
###############

if (NOT sdl_mixer_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(sdl_mixer)
# disable annoying codecs we dont need that require dependencies i dont have
set(SDL2MIXER_OPUS OFF CACHE BOOL "Enable Opus support")
set(SDL2MIXER_FLAC OFF CACHE BOOL "Enable FLAC support")
set(SDL2MIXER_MOD OFF CACHE BOOL "Enable Mod support")
set(SDL2MIXER_MIDI OFF CACHE BOOL "Enable MIDI support")

set(SUPPORT_OGG ON CACHE BOOL "" FORCE)
# mp3 enabled
set(SDL2MIXER_MP3 ON CACHE BOOL "Enable MP3 support")

# build SDL2 MIXER
list(APPEND CPP_INCLUDE_DIRS ${sdl_mixer_SOURCE_DIR}/include)
add_subdirectory(${sdl_mixer_SOURCE_DIR} ${sdl_mixer_BINARY_DIR})
list(APPEND CPP_LIBS "SDL2_mixer")
endif()
endif()
# wav enabled
set(SDL2MIXER_WAV ON CACHE BOOL "Enable WAV support")

FetchContent_Declare(
SDL2_mixer
GIT_REPOSITORY https://github.com/libsdl-org/SDL_mixer.git
GIT_TAG ${SDL_MIXER_GIT_VERSION}
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(SDL2_mixer)
target_include_directories(Kraken PRIVATE ${SDL2_mixer_SOURCE_DIR}/include)
target_include_directories(Kraken_Test PRIVATE ${SDL2_mixer_SOURCE_DIR}/include)

#############
# tmxlite #
#############

set(TMXLITE_STATIC_LIB OFF CACHE BOOL "Build tmxlite as static")
FetchContent_Declare(
tmxlite
GIT_REPOSITORY https://github.com/fallahn/tmxlite.git
GIT_TAG ${TMX_LITE_VERSION}
GIT_PROGRESS TRUE
)

FetchContent_MakeAvailable(tmxlite)
message(STATUS "tmxlite_SOURCE_DIR " ${tmxlite_SOURCE_DIR})

list(APPEND CPP_INCLUDE_DIRS ${tmxlite_SOURCE_DIR}/tmxlite/include)
add_subdirectory(${tmxlite_SOURCE_DIR}/tmxlite ${tmxlite_BINARY_DIR})
list(APPEND CPP_LIBS "tmxlite")

target_include_directories(Kraken PRIVATE ${tmxlite_SOURCE_DIR}/tmxlite/include)
target_include_directories(Kraken_Test PRIVATE ${tmxlite_SOURCE_DIR}/tmxlite/include)

###########
# gtest #
###########

FetchContent_Declare(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG f8d7d77c06936315286eb55f8de22cd23c188571
GIT_PROGRESS TRUE
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)

# ================ </deps> ==============

file(GLOB SOURCES "src/*.cpp")
add_library(${PROJECT_NAME} ${SOURCES})
enable_testing()

if (WIN32)
add_custom_command(
TARGET ${TESTING}
COMMENT "Copy gtest dlls"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bld/bin/libgtest.dll ${CMAKE_SOURCE_DIR}/bin/
VERBATIM
)
endif()
if (WIN32)
add_custom_command(
TARGET ${TESTING}
COMMENT "Copy gtest dlls"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bld/bin/libgmock.dll ${CMAKE_SOURCE_DIR}/bin/
VERBATIM
)
endif()
if (WIN32)
add_custom_command(
TARGET ${TESTING}
COMMENT "Copy gtest dlls"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bld/bin/libgtest_main.dll ${CMAKE_SOURCE_DIR}/bin/
VERBATIM
)
endif()
if (WIN32)
add_custom_command(
TARGET ${TESTING}
COMMENT "Copy gtest dlls"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bld/bin/libgmock_main.dll ${CMAKE_SOURCE_DIR}/bin/
VERBATIM
)
endif()

FetchContent_MakeAvailable(SDL SDL_image SDL_ttf SDL_mixer)
target_link_libraries(Kraken PRIVATE SDL2 SDL2_image SDL2_ttf SDL2_mixer tmxlite)
target_link_libraries(Kraken_Test PRIVATE SDL2 SDL2_image SDL2_ttf SDL2_mixer tmxlite gtest)

target_link_libraries(${PROJECT_NAME} PRIVATE SDL2 SDL2_image SDL2_ttf SDL2_mixer)
target_include_directories(Kraken PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(Kraken_Test PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(Kraken_Test PRIVATE ${tmxlite_SOURCE_DIR}/../googletest-src/googletest/include)
gtest_discover_tests(Kraken_Test)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR}/../include)
target_include_directories(${PROJECT_NAME} PRIVATE ${tmxlite_SOURCE_DIR}/tmxlite/include)
install(TARGETS ${PROJECT_NAME})
install(TARGETS Kraken)
Loading