Skip to content

Commit

Permalink
Replaced LXQt build tools with simplified version
Browse files Browse the repository at this point in the history
  • Loading branch information
redtide committed Jul 26, 2023
1 parent 7dde53a commit b64f4ad
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 53 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*build*
!lxqt-build-tools/
*.bak
*.diff
*.patch
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

4 changes: 2 additions & 2 deletions cmake/AppStream.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#===============================================================================
#=======================================================================================================
# Converts a domain string to a compatible AppStream ID
#
# E.G.: org.7-zip.7zip become org._7_zip._7zip
#
# See https://freedesktop.org/software/appstream/docs/chap-Metadata.html#tag-id-generic
#===============================================================================
#=======================================================================================================
macro(to_appstream_id INPUT OUTPUT)
set(temp_ "${INPUT}")
string(REPLACE "-" "_" temp_ ${temp_})
Expand Down
57 changes: 11 additions & 46 deletions cmake/QtAppResources.cmake
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#===============================================================================
#=======================================================================================================
# QtAppResources.cmake
#
# Configure and Installs:
# - About information markdown file
# - Appstream metainfo
# - Desktop file
# - Translation files via lxqt-build-tools
# - Translation files
#
# TODO: Windows and macOS
#===============================================================================
#=======================================================================================================
# Configure files
#===============================================================================
#=======================================================================================================
if (UNIX AND NOT APPLE)
include(GNUInstallDirs)
# "resources/about.md" is a gitignored file configured by cmake and then added
Expand All @@ -27,9 +27,6 @@ if (UNIX AND NOT APPLE)
configure_file("resources/in/linux/application.appdata.xml.in"
"${PROJECT_APPDATA_FILE_NAME}" @ONLY
)
# TODO: This file needs to be processed first by `configure_file()` for `@` variables,
# then the resulting file, the one below, to be processed by `lxqt_translate_desktop`.
# Would be better doing this in one step in `lxqt_translate_desktop` only.
set(DESKTOP_FILE_IN "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_ID}.desktop.in")
configure_file("resources/in/linux/application.desktop.in"
"${DESKTOP_FILE_IN}" @ONLY
Expand All @@ -38,57 +35,25 @@ if (UNIX AND NOT APPLE)
configure_file("resources/icons/application.icon"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_ICON_FILE_NAME}" COPYONLY
)
#===============================================================================
#=======================================================================================================
# Translations
#===============================================================================
# We currently use only 3 files from LXQt build tools:
#
# - LXQtTranslateDesktop.cmake
# - LXQtTranslateDesktop.pl
# - LXQtTranslateTs.cmake
#
# When it is necessary to include the tools as a Git submodule from a patched fork,
# we don't need/want to call `add_subdirectory()` to build and install the modules,
# so we can simply use the modules directory as is from their source directory.
#
# LXQtTranslateTs.cmake comes as a ".in" template file to configure, so we generate it
# in the same directory for convenience, and add that CMake modules source directory
# directly to CMAKE_MODULE_PATH as LXQT_CMAKE_MODULES_DIR
# (the configured file was previously added to .gitignore).
#===============================================================================
set(LXQT_MIN_LINGUIST_VERSION "${QT_VERSION}") # Required to configure the file.
set(LXQT_CMAKE_MODULES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/lxqt-build-tools/cmake/modules")
list(APPEND CMAKE_MODULE_PATH ${LXQT_CMAKE_MODULES_DIR})
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/external/lxqt-build-tools/cmake/modules/LXQtTranslateTs.cmake.in"
"${LXQT_CMAKE_MODULES_DIR}/LXQtTranslateTs.cmake"
@ONLY
)
message(STATUS "LXQT_CMAKE_MODULES_DIR: ${LXQT_CMAKE_MODULES_DIR}")

option(UPDATE_TRANSLATIONS "Update source translation files" OFF)

# TODO: include(LXQtPreventInSourceBuilds) ???
include(LXQtTranslateDesktop)
include(LXQtTranslateTs)

lxqt_translate_ts(PROJECT_QM_FILES
UPDATE_TRANSLATIONS ${UPDATE_TRANSLATIONS}
#=======================================================================================================
include(Translate)
qtls_translate(PROJECT_QM_FILES
SOURCES ${PROJECT_SOURCES} ${PROJECT_UI_FILES}
TEMPLATE "${PROJECT_ID}"
TRANSLATION_DIR "${PROJECT_TRANSLATIONS_DIR}"
INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/${PROJECT_ID}/translations"
)
lxqt_translate_desktop(PROJECT_DESKTOP_FILES
qtls_translate_desktop(PROJECT_DESKTOP_FILES
DESKTOP_FILE_STEM "${PROJECT_APPSTREAM_ID}"
SOURCES "${DESKTOP_FILE_IN}"
TRANSLATION_DIR "${PROJECT_TRANSLATIONS_DIR}"
USE_YAML
)
unset(DESKTOP_FILE_IN)
#===============================================================================
#=======================================================================================================
# Install
#===============================================================================
#=======================================================================================================
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_APPDATA_FILE_NAME}"
DESTINATION "${CMAKE_INSTALL_DATADIR}/metainfo"
)
Expand Down
159 changes: 159 additions & 0 deletions cmake/Translate.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#=======================================================================================================
# Copyright 2014 Luís Pereira <luis.artur.pereira@gmail.com> as LXQtTranslateTs.cmake.in
# Copyright 2023 Andrea Zanellato <redtid3@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#=======================================================================================================
#
# function qtls_translate(qmFiles OUTPUT: Compiled ".qm" translation files.
# SOURCES <sources>
# [TEMPLATE] translations base name (lower case project name)
# [TRANSLATION_DIR] ".ts" source directory path, relative to the CMakeList.txt.
# [INSTALL_DIR] ".qm" install directory
# )
# TEMPLATE Optional, Default: "${PROJECT_NAME}".
# TRANSLATION_DIR Optional, Default: "translations".
# INSTALL_DIR Optional, If not present no installation is performed.
#
#=======================================================================================================
find_package(Qt${QT_VERSION_MAJOR}LinguistTools REQUIRED)

function(qtls_translate qmFiles)
set(oneValueArgs
TEMPLATE
TRANSLATION_DIR
INSTALL_DIR
)
set(multiValueArgs SOURCES)
cmake_parse_arguments(TR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(NOT DEFINED TR_TEMPLATE)
set(TR_TEMPLATE "${PROJECT_NAME}")
endif()

if (NOT DEFINED TR_TRANSLATION_DIR)
set(TR_TRANSLATION_DIR "translations")
endif()
get_filename_component(TR_TRANSLATION_DIR "${TR_TRANSLATION_DIR}" ABSOLUTE)

if (EXISTS "${TR_TRANSLATION_DIR}")
file(GLOB tsFiles "${TR_TRANSLATION_DIR}/${TR_TEMPLATE}_*.ts")
set(templateFile "${TR_TRANSLATION_DIR}/${TR_TEMPLATE}.ts")
endif ()

qt_add_translation(QM ${tsFiles})

if(DEFINED TR_INSTALL_DIR)
install(FILES ${QM}
DESTINATION "${TR_INSTALL_DIR}"
COMPONENT "Runtime"
)
endif()

set(${qmFiles} ${QM} PARENT_SCOPE)
endfunction()
#=======================================================================================================
# Original Author: Alexander Sokolov <sokoloff.a@gmail.com>
#
# function(qtls_translate_desktop _RESULT)
#
# Output:
# _RESULT The generated .desktop file(s).
#
# Input:
# SOURCES `.desktop.in` file(s) to be merged and translated, relative to the CMakeList.txt.
#
# DESKTOP_FILE_STEM Optional, filename, without `.desktop` extension for the output file(s).
#
# TRANSLATION_DIR Optional, path to the directory with the .ts files, relative to the CMakeList.txt.
# Default: "translations".
#=======================================================================================================
find_package(Perl REQUIRED)

function(qtls_translate_desktop _RESULT)
# Parse arguments
set(oneValueArgs TRANSLATION_DIR DESKTOP_FILE_STEM)
set(multiValueArgs SOURCES)

cmake_parse_arguments(_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

# Check for unknown arguments
set(_UNPARSED_ARGS ${_ARGS_UNPARSED_ARGUMENTS})
if (NOT ${_UNPARSED_ARGS} STREQUAL "")
message(FATAL_ERROR
"Unknown arguments '${_UNPARSED_ARGS}'.\n"
"See qtls_translate_desktop() documentation for more information.\n"
)
endif()

if (NOT DEFINED _ARGS_SOURCES)
set(${_RESULT} "" PARENT_SCOPE)
return()
else()
set(_sources ${_ARGS_SOURCES})
endif()

if (NOT DEFINED _ARGS_TRANSLATION_DIR)
set(_translationDir "translations")
else()
set(_translationDir ${_ARGS_TRANSLATION_DIR})
endif()

get_filename_component (_translationDir ${_translationDir} ABSOLUTE)

foreach (_inFile ${_sources})
# File in full path string, E.g.: "/path/to/file_stem.desktop.in"
get_filename_component(_inFile ${_inFile} ABSOLUTE)

# File in name without extension, E.g.: "file_stem"
get_filename_component(_fileName ${_inFile} NAME_WE)

# File in long extension, e.g.: ".desktop.in"
get_filename_component(_fileExt ${_inFile} EXT)

# File output extension, e.g.: "desktop"
string(REPLACE ".in" "" _fileExt ${_fileExt})
string(REGEX REPLACE "^\\.([^.].*)$" "\\1" _fileExt ${_fileExt})

if(_ARGS_DESKTOP_FILE_STEM)
set(_outFile "${CMAKE_CURRENT_BINARY_DIR}/${_ARGS_DESKTOP_FILE_STEM}.${_fileExt}")
else()
set(_outFile "${CMAKE_CURRENT_BINARY_DIR}/${_fileName}.${_fileExt}")
endif()
get_filename_component(_outFileName ${_outFile} NAME)

add_custom_command(OUTPUT ${_outFile}
COMMAND ${PERL_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/TranslateDesktop.pl"
"${_inFile}"
"${_fileName}"
"${_translationDir}/${_fileName}[_.]*${_fileExt}.yaml" >> "${_outFile}"
VERBATIM
COMMENT "Generating ${_outFileName}"
)

set(__result ${__result} ${_outFile})
endforeach()

set(${_RESULT} ${__result} PARENT_SCOPE)
endfunction()
50 changes: 50 additions & 0 deletions cmake/TranslateDesktop.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use strict;

binmode(STDOUT, ":encoding(utf8)");
binmode(STDERR, ":encoding(utf8)");

my $desktop_in = $ARGV[0];
my $filename_base = $ARGV[1];
my @translation_files = glob($ARGV[2]);

my $section_re = qr/^\[([^\]]+)]/o;
my $lang_re = qr/^.*${filename_base}_([^.]+)\..+$/o;
my $strip_re = qr/#TRANSLATIONS_DIR=/o;

sub flush_translations {
my ($curr_section) = @_;
if (defined $curr_section) {
my $transl_yaml_re = qr/^${curr_section}\/([^: ]+) ?: *([^ ].*)$/;
foreach my $file (@translation_files) {
my $language = ($file =~ $lang_re ? "[$1]" : '');
open(my $trans_fh, '<:encoding(UTF-8)', $file) or next;
while (my $trans_l = <$trans_fh>) {
if ($trans_l =~ $transl_yaml_re)
{
my ($key, $value) = ($1, $2);
$value =~ s/^\s+|\s+$//; $value =~ s/^['"]//; $value =~ s/['"]$//;
if (length($value))
{
# Don't flush empty (untranslated) strings
print(STDOUT "$key$language=$value\n");
}
}
}
close($trans_fh);
}
}
}


open(my $fh, '<:encoding(UTF-8)', $desktop_in) or die "Could not open file '$desktop_in' $!";
my $curr_section = undef;
while (my $line = <$fh>) {
if ($line =~ $section_re) {
flush_translations($curr_section);
$curr_section = $1;
}
$line =~ $strip_re or print(STDOUT $line);
}
flush_translations($curr_section);

close($fh);
1 change: 0 additions & 1 deletion external/lxqt-build-tools
Submodule lxqt-build-tools deleted from 43d0f0

0 comments on commit b64f4ad

Please sign in to comment.