dabc604f7644294031fe2be37ecd59a647b1c475
[lxde/liblxqt.git] / cmake / lxqt_use.cmake.in
1 # - Find the LXQt include and library dirs and define a some macros
2 #
3 # The module defines the following functions
4 #
5 # lxqt_translate_desktop(desktop_files
6 #                         SOURCES sources ...
7 #                         [TRANSLATION_DIR] translation_directory
8 #                        )
9 #     out: desktop_files
10 #     generates commands to create .desktop files from sources.
11 #     The generated filenames can be found in desktop_files.
12 #
13 #     in: sources
14 #     List of the desktop.in files
15 #
16 #     in: translation_directory
17 #     A relative path to the directory with translations files, it is
18 #     relative to the CMakeList.txt. By default is "translations"
19 #
20 #
21 # lxqt_set_default_value(VAR_NAME VAR_VALUE)
22 #
23 #
24 # lxqt_create_pkgconfig_file(name 
25 #                            desc 
26 #                            requires 
27 #                            include_rel_dir 
28 #                            version)
29 #     Write a pkg-config pc file for given "name" with "decription"
30 #     Arguments:
31 #       name: a library name (withoud "lib" prefix and "so" suffixes
32 #       desc: a desription string
33 #       requires: required libraries
34 #       include_rel_dir: include directory, relative to includedir
35 #       version: package version
36 #
37 #
38 #
39 # The module defines the following definitions
40 #
41 #  -DLXQT_SHARE_DIR      - This allows to install and read the configs from non-standard locations
42 #
43 #  -DLXQT_ETC_XDG_DIR    - XDG standards expects system-wide configuration files in the
44 #                          /etc/xdg/lxqt location. Unfortunately QSettings we are using internally
45 #                          can be overriden in the Qt compilation time to use different path for
46 #                          system-wide configs. (for example configure ... -sysconfdir /etc/settings ...)
47 #                          This path can be found calling Qt4's qmake:
48 #                             qmake -query QT_INSTALL_CONFIGURATION
49 #
50 #
51 #
52
53 include_directories(${LXQT_INCLUDE_DIRS})
54
55 link_directories(${LXQT_LIBRARY_DIRS})
56
57 #cmake_policy(SET CMP0005 NEW)
58 add_definitions(-DLXQT_RELATIVE_SHARE_DIR=\"${LXQT_RELATIVE_SHARE_DIR}\")
59 add_definitions(-DLXQT_SHARE_DIR=\"${LXQT_SHARE_DIR}\")
60 add_definitions(-DLXQT_RELATIVE_SHARE_TRANSLATIONS_DIR=\"${LXQT_RELATIVE_TRANSLATIONS_DIR}\")
61 add_definitions(-DLXQT_SHARE_TRANSLATIONS_DIR=\"${LXQT_TRANSLATIONS_DIR}\")
62 add_definitions(-DLXQT_ETC_XDG_DIR=\"${LXQT_ETC_XDG_DIR}\")
63 add_definitions(-DLXQT_VERSION=\"${LXQT_VERSION}\")
64
65 # for backward compatability ----->
66 add_definitions(-DLXQT_SHARE_DIR=\"${LXQT_SHARE_DIR}\")
67 add_definitions(-DLXQT_ETC_XDG_DIR=\"${LXQT_ETC_XDG_DIR}\")
68 add_definitions(-DLXQT_VERSION=\"${LXQT_VERSION}\")
69 # for backward compatability <-----
70
71 #if(NOT "${PROJECT_NAME}" STREQUAL "liblxqt")
72 #    message("#################${PROJECT_NAME} liblxqt${LXQT_QT_VERSION}")
73 #    add_dependencies("lxqt${LXQT_QT_VERSION}")
74 SET_SOURCE_FILES_PROPERTIES(LXQT_LIBRARIES PROPERTIES OBJECT_DEPENDS lxqt${LXQT_QT_VERSION})
75 #endif()
76
77
78 if (LXQT_QT_VERSION EQUAL "4")
79     find_package(Qt4 REQUIRED QUIET)
80     include(${QT_USE_FILE})
81
82     find_package(QTXDG REQUIRED)
83 elseif(LXQT_QT_VERSION EQUAL "5")
84     find_package(Qt5Widgets REQUIRED QUIET)
85     find_package(Qt5DBus REQUIRED QUIET)
86     find_package(Qt5X11Extras REQUIRED QUIET)
87     find_package(Qt5LinguistTools REQUIRED QUIET)
88     find_package(Qt5Xdg REQUIRED)
89 else()
90     message(FATAL "Qt${LXQT_QT_VERSION} is not supported.")
91 endif()
92
93 # Works for Qt4 and Qt5
94 include(${QTXDG_USE_FILE})
95
96 #else()
97 #    find_package(Qt4 REQUIRED QUIET)
98 #    include(${QT_USE_FILE})
99
100 #endif()
101
102 #**********************************************************
103 # DESKTOP files
104 #**********************************************************
105
106 function(lxqt_translate_desktop _RESULT)
107     set(_translationDir "translations")
108
109     # Parse arguments ***************************************
110     set(_state "")
111     foreach (_arg ${ARGN})
112         if (
113             ("${_arg}_I_HATE_CMAKE" STREQUAL "SOURCES_I_HATE_CMAKE") OR
114             ("${_arg}_I_HATE_CMAKE" STREQUAL "TRANSLATION_DIR_I_HATE_CMAKE")
115            )
116
117             set(_state ${_arg})
118
119         else()
120             if("${_state}" STREQUAL "SOURCES")
121                 get_filename_component (__file ${_arg} ABSOLUTE)
122                 set(_sources  ${_sources} ${__file})
123                 #set(_sources  ${_sources} ${_arg})
124
125             elseif("${_state}" STREQUAL "TRANSLATION_DIR")
126                 set(_translationDir ${_arg})
127                 set(_state "")
128
129             else()
130                 MESSAGE(FATAL_ERROR
131                   "Unknown argument '${_arg}'.\n"
132                   "See ${CMAKE_CURRENT_LIST_FILE} for more information.\n"
133                 )
134             endif()
135         endif()
136     endforeach(_arg)
137
138     get_filename_component (_translationDir ${_translationDir} ABSOLUTE)
139
140     foreach (_inFile ${_sources})
141         get_filename_component(_inFile   ${_inFile} ABSOLUTE)
142         get_filename_component(_fileName ${_inFile} NAME_WE)
143         #Extract the real extension ............
144         get_filename_component(_fileExt  ${_inFile} EXT)
145         string(REPLACE ".in" "" _fileExt ${_fileExt})
146         #.......................................
147         set(_outFile "${CMAKE_CURRENT_BINARY_DIR}/${_fileName}${_fileExt}")
148
149         file(GLOB _translations
150             ${_translationDir}/${_fileName}_*${_fileExt}
151             ${_translationDir}/local/${_fileName}_*${_fileExt}
152         )
153
154         set(_pattern "'\\[.*]\\s*='")
155         if (_translations)
156             add_custom_command(OUTPUT ${_outFile}
157                 COMMAND grep -v "'#TRANSLATIONS_DIR='" ${_inFile} > ${_outFile}
158                 COMMAND grep --no-filename ${_pattern} ${_translations} >> ${_outFile}
159                 COMMENT "Generating ${_fileName}${_fileExt}"
160             )
161         else()
162             add_custom_command(OUTPUT ${_outFile}
163                 COMMAND grep -v "'#TRANSLATIONS_DIR='" ${_inFile} > ${_outFile}
164                 COMMENT "Generating ${_fileName}${_fileExt}"
165             )
166         endif()
167
168         set(__result ${__result} ${_outFile})
169     endforeach()
170
171     set(${_RESULT} ${__result} PARENT_SCOPE)
172 endfunction()
173
174
175 macro(lxqt_set_default_value VAR_NAME VAR_VALUE)
176     if (NOT DEFINED ${VAR_NAME})
177         set (${VAR_NAME} ${VAR_VALUE})
178     endif ()
179 endmacro()
180
181
182 #
183 # Write a pkg-config pc file for given "name" with "decription"
184 # Arguments:
185 #   name: a library name (withoud "lib" prefix and "so" suffixes
186 #   desc: a desription string
187 #
188 macro(lxqt_create_pkgconfig_file name desc requires include_rel_dir version)
189     set(_pkgfname "${CMAKE_CURRENT_BINARY_DIR}/${name}.pc")
190     message(STATUS "${name}: writing pkgconfig file ${_pkgfname}")
191
192     file(WRITE "${_pkgfname}"
193             "# file generated by razor-qt cmake build\n"
194             "prefix=${CMAKE_INSTALL_PREFIX}\n"
195             "libdir=\${prefix}/${CMAKE_INSTALL_LIBDIR}\n"
196             "includedir=\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}\n"
197             "\n"
198             "Name: ${name}\n"
199             "Description: ${desc}\n"
200             "Version: ${version}\n"
201             "Requires: ${requires}\n"
202             "Libs: -L\${libdir} -l${name}\n"
203             "Cflags: -I\${includedir} -I\${includedir}/${include_rel_dir}\n"
204             "\n"
205         )
206
207     install(FILES ${_pkgfname} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
208 endmacro()