Skip to content
Open
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if(NOT BUILD_SDL)
set(BUILD_SDLGPU OFF)
endif()

if(NINTENDO_SWITCH)
if(NINTENDO_SWITCH AND NOT LIBRETRO_STATIC)
set(PREFER_SYSTEM_LIBRARIES ON)
endif()

Expand Down
89 changes: 86 additions & 3 deletions cmake/libretro.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,90 @@ if(BUILD_LIBRETRO)
)

if (LIBRETRO_STATIC)
# Collect sources from all dependencies to build a monolithic library.
# This ensures that consoles and Emscripten get a single, complete archive.
set(TIC80_MONOLITHIC_SRCS ${LIBRETRO_SRC})

# List of all potential core component targets to collect sources from
set(TIC80_CORE_OBJECT_DEPS
tic80core luaapi lua lpeg moon yue fennel wren squirrel pocketpy
quickjs mruby wasm scheme janet zip zlib png blipbuf giflib
wave argparse naett
)

set(TIC80_MONOLITHIC_INCLUDES "")
set(TIC80_MONOLITHIC_DEFINES "")

foreach(target ${TIC80_CORE_OBJECT_DEPS})
if(TARGET ${target})
get_target_property(type ${target} TYPE)
get_target_property(imported ${target} IMPORTED)

if(NOT type STREQUAL "INTERFACE_LIBRARY" AND NOT imported)
# Collect sources
get_target_property(target_sources ${target} SOURCES)
get_target_property(target_dir ${target} SOURCE_DIR)
if(target_sources)
foreach(src ${target_sources})
if(NOT IS_ABSOLUTE "${src}")
set(src "${target_dir}/${src}")
endif()
if(EXISTS "${src}")
list(APPEND TIC80_MONOLITHIC_SRCS "${src}")
endif()
endforeach()
endif()

# Collect Private include directories
get_target_property(includes ${target} INCLUDE_DIRECTORIES)
if(includes)
list(APPEND TIC80_MONOLITHIC_INCLUDES ${includes})
endif()

# Collect Private compile definitions
get_target_property(defines ${target} COMPILE_DEFINITIONS)
if(defines)
list(APPEND TIC80_MONOLITHIC_DEFINES ${defines})
endif()

# Collect Directory compile definitions if possible
# Only if target_dir is within project source tree to avoid errors
if(target_dir AND "${target_dir}" MATCHES "^${CMAKE_SOURCE_DIR}")
get_directory_property(dir_defines DIRECTORY ${target_dir} COMPILE_DEFINITIONS)
if(dir_defines)
list(APPEND TIC80_MONOLITHIC_DEFINES ${dir_defines})
endif()
endif()
endif()

# Collect Interface properties (available for all types, but we must use the INTERFACE_ variant)
get_target_property(iface_includes ${target} INTERFACE_INCLUDE_DIRECTORIES)
if(iface_includes)
list(APPEND TIC80_MONOLITHIC_INCLUDES ${iface_includes})
endif()

get_target_property(iface_defines ${target} INTERFACE_COMPILE_DEFINITIONS)
if(iface_defines)
list(APPEND TIC80_MONOLITHIC_DEFINES ${iface_defines})
endif()
endif()
endforeach()

# Remove duplicates from includes/defines to keep command lines manageable
if(TIC80_MONOLITHIC_INCLUDES)
list(REMOVE_DUPLICATES TIC80_MONOLITHIC_INCLUDES)
endif()
if(TIC80_MONOLITHIC_DEFINES)
list(REMOVE_DUPLICATES TIC80_MONOLITHIC_DEFINES)
endif()

add_library(tic80_libretro STATIC
${LIBRETRO_SRC}
${TIC80_MONOLITHIC_SRCS}
)

target_include_directories(tic80_libretro PRIVATE ${TIC80_MONOLITHIC_INCLUDES})
target_compile_definitions(tic80_libretro PRIVATE ${TIC80_MONOLITHIC_DEFINES})

if(EMSCRIPTEN)
set(LIBRETRO_EXTENSION "bc")
else()
Expand Down Expand Up @@ -46,6 +127,8 @@ if(BUILD_LIBRETRO)
target_compile_definitions(tic80_libretro PRIVATE
__LIBRETRO__=TRUE
)
target_link_libraries(tic80_libretro tic80core)
if(NOT LIBRETRO_STATIC)
target_link_libraries(tic80_libretro tic80core)
endif()
set_target_properties(tic80_libretro PROPERTIES PREFIX "")
endif()
endif()
Loading