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

Merge static libraries in avif_apps #2153

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ if(AVIF_BUILD_APPS OR (AVIF_BUILD_TESTS AND (AVIF_ENABLE_FUZZTEST OR AVIF_ENABLE
# Instead of building avif_apps/avif_apps_internal and linking to avif/avif_internal, avif_apps_obj only does one compilation.
# Still, the compile definitions needs to be passed from avif to avif_apps. The following also passes them to avif_apps_internal but AVIF_DLL does not impact avif_apps_internal.
target_compile_definitions(avif_apps_obj PRIVATE $<BUILD_INTERFACE:$<TARGET_PROPERTY:avif,INTERFACE_COMPILE_DEFINITIONS>>)
target_link_libraries(avif_apps_obj PUBLIC avif_obj PNG::PNG ZLIB::ZLIB JPEG::JPEG)
target_link_libraries(avif_apps_obj PUBLIC avif PNG::PNG ZLIB::ZLIB JPEG::JPEG)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(avif_apps_obj PRIVATE m)
endif()
Expand All @@ -683,17 +683,21 @@ if(AVIF_BUILD_APPS OR (AVIF_BUILD_TESTS AND (AVIF_ENABLE_FUZZTEST OR AVIF_ENABLE
endif()

# Main avif_apps library.
add_library(avif_apps STATIC)
target_link_libraries(avif_apps PUBLIC avif PRIVATE avif_apps_obj)
target_include_directories(avif_apps INTERFACE apps/shared)
add_library(avif_apps_static STATIC)
target_link_libraries(avif_apps_static PUBLIC avif PRIVATE avif_apps_obj)
target_include_directories(avif_apps_static INTERFACE apps/shared)

# avif_apps_internal is to use when linking to avif_internal.
if(BUILD_SHARED_LIBS)
add_library(avif_apps_internal STATIC)
target_link_libraries(avif_apps_internal PUBLIC avif_internal PRIVATE avif_apps_obj)
target_include_directories(avif_apps_internal INTERFACE apps/shared)
add_library(avif_apps ALIAS avif_apps_static)
else()
add_library(avif_apps_internal ALIAS avif_apps)
set_target_properties(avif_apps_static PROPERTIES AVIF_LOCAL ON)
merge_static_libs(avif_apps avif_apps_static)
set_target_properties(avif_apps_static PROPERTIES OUTPUT_NAME avif_apps_internal EXPORT_NAME avif_apps_internal)
add_library(avif_apps_internal ALIAS avif_apps_static)
endif()
endif()

Expand Down
14 changes: 13 additions & 1 deletion cmake/Modules/merge_static_libs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function(merge_static_libs target in_target)
add_library(${target} STATIC ${source_file})

avif_collect_deps(${in_target} lib_deps)
get_target_property(include_dirs ${in_target} INTERFACE_INCLUDE_DIRECTORIES)
if(include_dirs)
target_include_directories(${target} PUBLIC ${include_dirs})
endif()

foreach(lib ${lib_deps})
get_target_property(child_target_type ${lib} TYPE)
Expand All @@ -67,11 +71,19 @@ function(merge_static_libs target in_target)
if(is_avif_local)
list(APPEND libs $<TARGET_FILE:${lib}>)
list(APPEND dependencies "${lib}")
else()
if(${child_target_type} STREQUAL "UNKNOWN_LIBRARY")
# This is a global library. It needs to be linked to.
get_target_property(include_dirs ${lib} INTERFACE_INCLUDE_DIRECTORIES)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I suggest not separaring the comment from the code (line 81: target_link_libraries(${target} ${lib})) that it describes.

if(include_dirs)
target_include_directories(${target} PUBLIC ${include_dirs})
endif()
target_link_libraries(${target} ${lib})
endif()
endif()
endforeach()

list(REMOVE_DUPLICATES libs)

add_custom_command(
OUTPUT ${source_file} DEPENDS ${dependencies} COMMAND ${CMAKE_COMMAND} -E echo \"const int dummy = 0\;\" > ${source_file}
)
Expand Down
Loading