cmake_minimum_required(VERSION 3.1)
project(rizz)

set(SOURCE_FILES internal.h
                 app.c
                 profiler.c 
                 core.c 
                 plugin.cpp
                 graphics.c
                 reflect.c
                 asset.c 
                 camera.c 
                 http.c 
                 vfs.c 
                 android.c
                 json.c 
                 windows.cpp 
                 memory.c
                 demangle.cpp)

set(INCLUDE_FILES ../../include/rizz/config.h
                  ../../include/rizz/sg-types.h
                  ../../include/rizz/json.h 
                  ../../include/rizz/android.h
                  ../../include/rizz/ios.h 
                  ../../include/rizz/rizz.h)

set(TEXT_FILES  ../../README.md)

set(EXTERNAL_INCLUDES ../../3rdparty/sokol/sokol_app.h 
                      ../../3rdparty/sokol/sokol_gfx.h
                      ../../3rdparty/cr/cr.h
                      ../../include/cj5/cj5.h 
                      ../../include/dds-ktx/dds-ktx.h
                      ../../3rdparty/stb/stb_image.h
                      ../../3rdparty/stb/stb_image_resize.h
                      ../../3rdparty/sort/sort.h
                      ../../3rdparty/mattias/http.h 
                      ../../include/dmon/dmon.h 
                      ../../include/stackwalkerc/stackwalkerc.h)

if (APPLE)
    set_source_files_properties(app.c graphics.c PROPERTIES COMPILE_FLAGS "-fobjc-arc -fmodules -x objective-c") 
endif()

if (IOS)
    list(APPEND SOURCE_FILES ios.m)
endif()

# FIXME: due to some bug in msvc or cmake, I have to set this for all sources
if (MSVC)
    if (${MSVC_VERSION} GREATER_EQUAL 1928)
        set_source_files_properties(${SOURCE_FILES} PROPERTIES COMPILE_OPTIONS /std:c17)
    endif()
endif()

# bundle: make static lib to link it to the game.exe later
# no-bundle: make rizz.exe and run the game.dll as a plugin
if (BUNDLE)
    add_library(rizz STATIC ${SOURCE_FILES} ${EXTERNAL_INCLUDES} ${INCLUDE_FILES} ${TEXT_FILES})
    if (BUNDLE_PLUGINS)
        target_link_libraries(rizz PUBLIC ${BUNDLE_PLUGINS})
    endif()    
else()
    add_executable(rizz ${SOURCE_FILES} ${EXTERNAL_INCLUDES} ${INCLUDE_FILES} ${TEXT_FILES})
    
    if (WIN32)
        set_target_properties(rizz PROPERTIES WIN32_EXECUTABLE TRUE)
    endif()
endif()

# includes and extra compile flags
target_include_directories(rizz PRIVATE ../../3rdparty)
target_link_libraries(rizz PUBLIC sx PRIVATE remotery)
target_compile_definitions(rizz PRIVATE -DRIZZ_INTERNAL_API) # Expose internal API structs when build the library
if (ENABLE_HOT_LOADING)
    target_compile_definitions(rizz PRIVATE -DRIZZ_CONFIG_HOT_LOADING=1)
    if (APPLE)
        target_link_libraries(rizz PRIVATE "-framework CoreServices -framework CoreFoundation")
    endif()
else()
    target_compile_definitions(rizz PRIVATE -DRIZZ_CONFIG_HOT_LOADING=0)
endif()

if (ENABLE_PROFILER)
    target_compile_definitions(rizz PRIVATE -DRIZZ_CONFIG_PROFILER=1)
else()
    target_compile_definitions(rizz PRIVATE -DRIZZ_CONFIG_PROFILER=0)
endif()

# versioning
if (version)
    target_compile_definitions(rizz PRIVATE -DRIZZ_VERSION=${version})
endif()

if (RPI)
    target_include_directories(rizz PRIVATE ${CMAKE_SYSROOT}/opt/vc/include)
endif()

# Link system libs
if (ANDROID)
    target_link_libraries(rizz PRIVATE EGL android GLESv3)
elseif (RPI)
    target_link_directories(rizz PUBLIC ${CMAKE_SYSROOT}/opt/vc/lib)
    target_link_libraries(rizz PRIVATE brcmGLESv2 brcmEGL bcm_host atomic)

    # add_custom_command(
    #    OUTPUT ${output_filepath}
    #    COMMAND ${glslcc_path}
    #    ARGS ${args}
    #    DEPENDS ${source_files_sub}
    #    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    #    COMMENT "Compiling ${shader_lang_upper}: ${output_relpath}"
    #    VERBATIM)    
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    target_link_libraries(rizz PRIVATE flextGL X11 GL)
elseif (WIN32)
    target_link_libraries(rizz PRIVATE dxgi d3d11 Version)  
endif()

