summaryrefslogtreecommitdiffstats
path: root/src/linguist/GenerateLUpdateProject.cmake
blob: 64c3838679cfeae708ab9007a36fe90917d5e64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause

# Generate an lupdate project file in JSON format.
#
# This file is to be used in CMake script mode with the following variables set:
#
# IN_FILE: .cmake file that sets lupdate_* variables
# OUT_FILE: lupdate project .json file

include("${IN_FILE}")

# Converts a CMake list into a string containing a JSON array
#    a,b,c -> [ "a", "b", "c" ]
function(list_to_json_array srcList jsonList)
    set(quotedList "")
    foreach(entry ${srcList})
        list(APPEND quotedList "\"${entry}\"")
    endforeach()
    list(JOIN quotedList ", " joinedList)
    set(${jsonList} "[ ${joinedList} ]" PARENT_SCOPE)
endfunction()

# Remove all nonexistent files from ARGN and store the result in out_var.
#    filter_nonexistent_files(existing_files foo.txt bar.cpp)
#    -> foo.txt (if foo.txt exists and bar.cpp does not)
function(filter_nonexistent_files out_var)
    # Filter out non-existent (generated) source files
    set(existing_sources "")
    foreach(path IN LISTS ARGN)
        if(EXISTS "${path}")
            list(APPEND existing_sources "${path}")
        endif()
    endforeach()
    set("${out_var}" "${existing_sources}" PARENT_SCOPE)
endfunction()

# Remove source files that are unsuitable input for lupdate.
#    filter_unsuitable_lupdate_input(sources main.cpp foo_de.qm bar.qml whatever_metatypes.json)
#    -> main.cpp bar.qml
function(filter_unsuitable_lupdate_input out_var)
    set(result ${ARGN})
    list(FILTER result EXCLUDE REGEX "\\.(qm|json)$")
    set("${out_var}" "${result}" PARENT_SCOPE)
endfunction()

# Remove ui_foo.h for each foo.ui file found in the sources.
#    filter_generated_ui_headers(existing_files .../src/foo.ui .../target_autogen/include/ui_foo.h)
#    -> .../src/foo.ui
function(filter_generated_ui_headers out_var)
    set(ui_file_paths ${ARGN})
    list(FILTER ui_file_paths INCLUDE REGEX "/[^/]+\\.ui$")

    set(filter_regex "")
    foreach(file_path IN LISTS ui_file_paths)
        get_filename_component(file_name "${file_path}" NAME_WLE)
        if(NOT "${filter_regex}" STREQUAL "")
            string(APPEND filter_regex "|")
        endif()
        string(APPEND filter_regex "(/ui_${file_name}\\.h$)")
    endforeach()

    set(result ${ARGN})
    if(NOT "${filter_regex}" STREQUAL "")
        list(FILTER result EXCLUDE REGEX ${filter_regex})
    endif()

    set("${out_var}" "${result}" PARENT_SCOPE)
endfunction()

function(prepare_json_sources out_var)
    filter_nonexistent_files(sources ${ARGN})
    filter_unsuitable_lupdate_input(sources ${sources})
    filter_generated_ui_headers(sources ${sources})
    list_to_json_array("${sources}" result)
    set("${out_var}" "${result}" PARENT_SCOPE)
endfunction()

get_filename_component(project_root "${lupdate_project_file}" DIRECTORY)

# Make relative paths absolute to the project root
set(path_variables sources include_paths translations)
if(lupdate_subproject_count GREATER 0)
    foreach(i RANGE 1 ${lupdate_subproject_count})
        list(APPEND path_variables
            subproject${i}_include_paths
            subproject${i}_sources
            subproject${i}_excluded
        )
    endforeach()
endif()
foreach(path_var IN LISTS path_variables)
    set(absolute_${path_var} "")
    foreach(path IN LISTS lupdate_${path_var})
        if(NOT IS_ABSOLUTE "${path}")
            if(path_var MATCHES "^subproject[0-9]+_")
                set(base_dir "${lupdate_${CMAKE_MATCH_0}source_dir}")
            else()
                set(base_dir "${project_root}")
            endif()
            get_filename_component(path "${path}" ABSOLUTE BASE_DIR "${base_dir}")
        endif()
        list(APPEND absolute_${path_var} "${path}")
    endforeach()
endforeach()

prepare_json_sources(json_sources ${absolute_sources})
list_to_json_array("${absolute_include_paths}" json_include_paths)
list_to_json_array("${absolute_translations}" json_translations)

set(content "{
  \"projectFile\": \"${lupdate_project_file}\",
  \"includePaths\": ${json_include_paths},
  \"sources\": ${json_sources},
  \"translations\": ${json_translations},
  \"subProjects\": [
")
if(lupdate_subproject_count GREATER 0)
    foreach(i RANGE 1 ${lupdate_subproject_count})
        prepare_json_sources(json_sources ${absolute_subproject${i}_sources})
        list_to_json_array("${absolute_subproject${i}_include_paths}" json_include_paths)
        list_to_json_array("${absolute_subproject${i}_excluded}" json_sources_exclusions)
        string(APPEND content "    {
      \"projectFile\": \"${lupdate_subproject${i}_source_dir}/CMakeLists.txt\",
      \"includePaths\": ${json_include_paths},
      \"sources\": ${json_sources},
      \"excluded\": ${json_sources_exclusions}
    }")
        if(i LESS lupdate_subproject_count)
            string(APPEND content ",")
        endif()
        string(APPEND content "\n")
    endforeach()
endif()
string(APPEND content "  ]
}
")
file(WRITE "${OUT_FILE}" "${content}")