Skip to content

Commit

Permalink
CMake: Generate/copy qtbase translations on Linux/Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Sep 17, 2023
1 parent 3a3a9af commit 9dad1d7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/scripts/linux/appimage-qt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ rm -fr "$DEPSDIR"
mv "$DEPSDIR.bak" "$DEPSDIR"

# Fix up translations.
rm -fr "$OUTDIR/usr/bin/translations"
mv "$OUTDIR/usr/translations" "$OUTDIR/usr/bin"
rm -fr "$OUTDIR/usr/bin/translations" "$OUTDIR/usr/translations"
cp -a "$BUILDDIR/bin/translations" "$OUTDIR/usr/bin"

# Generate AppStream meta-info.
Expand Down
53 changes: 53 additions & 0 deletions cmake/CopyBaseTranslations.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function(copy_base_translations target)
get_target_property(MOC_EXECUTABLE_LOCATION Qt6::moc IMPORTED_LOCATION)
get_filename_component(QT_BINARY_DIRECTORY "${MOC_EXECUTABLE_LOCATION}" DIRECTORY)
find_program(LCONVERT_EXE lconvert HINTS "${QT_BINARY_DIRECTORY}")
set(BASE_TRANSLATIONS_DIR "${QT_BINARY_DIRECTORY}/../translations")

if(NOT APPLE)
add_custom_command(TARGET ${target} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E make_directory "$<TARGET_FILE_DIR:${target}>/translations")
endif()

file(GLOB qmFiles "${BASE_TRANSLATIONS_DIR}/qt_*.qm")
foreach(path IN LISTS qmFiles)
get_filename_component(file ${path} NAME)

# qt_help_<lang> just has to ruin everything.
if(file MATCHES "qt_help_" OR NOT file MATCHES "qt_([^.]+).qm")
continue()
endif()

# If qtbase_<lang>.qm exists, merge all qms for that language into a single qm.
set(lang "${CMAKE_MATCH_1}")
set(baseQmPath "${BASE_TRANSLATIONS_DIR}/qtbase_${lang}.qm")
if(EXISTS "${baseQmPath}")
set(outPath "${CMAKE_CURRENT_BINARY_DIR}/qt_${lang}.qm")
set(srcQmFiles)
file(GLOB langQmFiles "${BASE_TRANSLATIONS_DIR}/qt*${lang}.qm")
foreach(qmFile IN LISTS langQmFiles)
get_filename_component(file ${qmFile} NAME)
if(file STREQUAL "qt_${lang}.qm")
continue()
endif()
LIST(APPEND srcQmFiles "${qmFile}")
endforeach()
add_custom_command(OUTPUT ${outPath}
COMMAND "${LCONVERT_EXE}" -verbose -of qm -o "${outPath}" ${srcQmFiles}
DEPENDS ${srcQmFiles}
)
set(path "${outPath}")
endif()

target_sources(${target} PRIVATE ${path})
if(APPLE)
set_source_files_properties(${path} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/translations)
elseif(WIN32)
# TODO: Set the correct binary instead of relying on make install on Windows...
install(FILES "${path}" DESTINATION "${CMAKE_SOURCE_DIR}/bin/translations")
else()
add_custom_command(TARGET ${target} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${path}" "$<TARGET_FILE_DIR:${target}>/translations")
endif()
endforeach()
endfunction()
6 changes: 5 additions & 1 deletion pcsx2-qt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include(CopyBaseTranslations)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
Expand Down Expand Up @@ -200,6 +202,7 @@ target_compile_definitions(pcsx2-qt PRIVATE QT_NO_EXCEPTIONS)
if(WIN32)
set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION "${CMAKE_SOURCE_DIR}/bin/translations")
qt_add_lrelease(pcsx2-qt TS_FILES ${TS_FILES})
copy_base_translations(pcsx2-qt)
elseif(APPLE)
qt_add_lrelease(pcsx2-qt TS_FILES ${TS_FILES} QM_FILES_OUTPUT_VARIABLE QM_FILES)
set(PCSX2_MACOS_LOCALIZATIONS)
Expand All @@ -218,15 +221,16 @@ elseif(APPLE)
target_sources(pcsx2-qt PRIVATE ${QM_FILE})
set_source_files_properties(${QM_FILE} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/translations/)
endforeach()
copy_base_translations(pcsx2-qt)
else()
# TODO: Copy base translations.
qt_add_lrelease(pcsx2-qt TS_FILES ${TS_FILES} QM_FILES_OUTPUT_VARIABLE QM_FILES)
set(QM_OUTPUT_DIR "$<TARGET_FILE_DIR:pcsx2-qt>/translations")
add_custom_command(TARGET pcsx2-qt POST_BUILD COMMAND "${CMAKE_COMMAND}" -E make_directory "${QM_OUTPUT_DIR}")
foreach (QM_FILE IN LISTS QM_FILES)
get_filename_component(QM_FILE_NAME ${QM_FILE} NAME)
add_custom_command(TARGET pcsx2-qt POST_BUILD COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${QM_FILE}" "${QM_OUTPUT_DIR}/${QM_FILE_NAME}")
endforeach()
copy_base_translations(pcsx2-qt)
endif()


Expand Down
2 changes: 1 addition & 1 deletion pcsx2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ function(setup_main_executable target)
endif()
endif()
find_program(WINDEPLOYQT_EXE windeployqt HINTS "${QT_BINARY_DIRECTORY}")
install(CODE "execute_process(COMMAND \"${WINDEPLOYQT_EXE}\" \"${CMAKE_SOURCE_DIR}/bin/$<TARGET_FILE_NAME:${target}>\" --plugindir \"${CMAKE_SOURCE_DIR}/bin/QtPlugins\" --no-compiler-runtime --no-system-d3d-compiler COMMAND_ERROR_IS_FATAL ANY)")
install(CODE "execute_process(COMMAND \"${WINDEPLOYQT_EXE}\" \"${CMAKE_SOURCE_DIR}/bin/$<TARGET_FILE_NAME:${target}>\" --plugindir \"${CMAKE_SOURCE_DIR}/bin/QtPlugins\" --no-compiler-runtime --no-system-d3d-compiler --no-translations COMMAND_ERROR_IS_FATAL ANY)")
install(CODE "file(WRITE \"${CMAKE_SOURCE_DIR}/bin/qt.conf\" \"[Paths]\\nPlugins = ./QtPlugins\")")
endif()

Expand Down

0 comments on commit 9dad1d7

Please sign in to comment.