summaryrefslogtreecommitdiffstats
path: root/modules/TDEMacros.cmake
blob: 97b1f1b43b73db1c89461e8d65cb6ed924cd9451 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
#################################################
#
#  (C) 2010-2012 Serghei Amelian
#  serghei (DOT) amelian (AT) gmail.com
#
#  (C) 2011-2012 Timothy Pearson
#  kb9vqf (AT) pearsoncomputing.net
#
#  (C) 2012-2020 Slávek Banko
#  slavek (DOT) banko (AT) axis.cz
#
#  Improvements and feedback are welcome
#
#  This file is released under GPL >= 2
#
#################################################

include( CheckCXXCompilerFlag )
include( TDEVersion )


#################################################
#####
##### initialization...

if( NOT TDE_CMAKE_ROOT )
  if( "${CMAKE_VERSION}" VERSION_LESS "3.1" )
    message( FATAL_ERROR "CMake >= 3.1.0 required" )
  endif()

  if( ${CMAKE_CURRENT_LIST_DIR} STREQUAL ${CMAKE_ROOT}/Modules )

    # TDE CMake is installed in the system directory
    set( TDE_CMAKE_ROOT ${CMAKE_ROOT}
         CACHE FILEPATH "TDE CMake root" )
    set( TDE_CMAKE_MODULES ${TDE_CMAKE_ROOT}/Modules
         CACHE FILEPATH "TDE CMake modules" )
    set( TDE_CMAKE_TEMPLATES ${TDE_CMAKE_ROOT}/Templates
         CACHE FILEPATH "TDE CMake templates" )

  else()

    # TDE CMake is part of the source code
    get_filename_component( TDE_CMAKE_ROOT ${CMAKE_CURRENT_LIST_DIR} PATH )
    set( TDE_CMAKE_ROOT ${TDE_CMAKE_ROOT}
         CACHE FILEPATH "TDE CMake root" )
    set( TDE_CMAKE_MODULES ${TDE_CMAKE_ROOT}/modules
         CACHE FILEPATH "TDE CMake modules" )
    set( TDE_CMAKE_TEMPLATES ${TDE_CMAKE_ROOT}/templates
         CACHE FILEPATH "TDE CMake templates" )

  endif()


  option( FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." FALSE )
  if( ${FORCE_COLORED_OUTPUT} )
    if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
      if( NOT "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "4.9" )
        add_compile_options (-fdiagnostics-color=always)
      endif()
    elseif( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" )
      add_compile_options (-fcolor-diagnostics)
    endif()
  endif()

endif()


#################################################
#####
##### tde_message_fatal

macro( tde_message_fatal )
  message( FATAL_ERROR
    "#################################################\n"
    " ${ARGV}\n"
    "#################################################" )
endmacro( tde_message_fatal )


#################################################
#####
##### tde_get_arg( <ARG_NAME> <COUNT> <RETURN> <REST> <ARGS...> )
##### ARG_NAME(string): name of an argument to find in ARGS
##### COUNT(number): argument dimension, a number of items returned in RETURN
##### RETURN(list ref): items returned for argument as they found in ARGS
##### REST(list ref): rest of items except argument name and items returned in RETURN
##### ARGS(list): source list of arguments

macro( tde_get_arg ARG_NAME COUNT RETURN REST )
  unset( ${RETURN} )
  unset( ${REST} )
  list( APPEND ${REST} ${ARGN} )
  list( FIND ${REST} ${ARG_NAME} _arg_idx)
  if( NOT ${_arg_idx} EQUAL -1 )
    list( REMOVE_AT ${REST} ${_arg_idx} )
    set( _i 0 )
    while( ${_i} LESS ${COUNT} )
      list( GET ${REST} ${_arg_idx} _arg )
      list( REMOVE_AT ${REST} ${_arg_idx} )
      list( APPEND ${RETURN} ${_arg} )
      math( EXPR _i "${_i} + 1" )
    endwhile()
  endif()
endmacro( tde_get_arg )


################################################
#####
##### tde_execute_process( <ARGS...> [MESSAGE <MSG>] )
##### MSG: fatal error message (standard message will be written if not supplied)
##### ARGS: execute_process arguments

macro( tde_execute_process )
  tde_get_arg( MESSAGE 1 _message _rest_args ${ARGV} )
  tde_get_arg( RESULT_VARIABLE 1 _result_variable _tmp ${_rest_args} )
  tde_get_arg( COMMAND 1 _command _tmp ${_rest_args} )
  tde_get_arg( OUTPUT_VARIABLE 1 _output_variable _tmp ${_rest_args} )
  tde_get_arg( CACHE 3 _cache _rest_args2 ${_rest_args} )

  # handle optional FORCE parameter
  if( DEFINED _cache )
    list( GET _cache 2 _tmp )
    if( _tmp STREQUAL FORCE )
      set( _rest_args ${_rest_args2} )
    else()
      tde_get_arg( CACHE 2 _cache _rest_args ${_rest_args} )
    endif()
  endif()

  if( NOT DEFINED _result_variable )
    list( APPEND _rest_args RESULT_VARIABLE _exec_result )
    set( _result_variable _exec_result )
  endif()

  execute_process( ${_rest_args} )

  if( DEFINED _output_variable AND DEFINED _cache )
    set( ${_output_variable} ${${_output_variable}} CACHE ${_cache} )
  endif()

  if( ${_result_variable} )
    if( DEFINED _message )
      tde_message_fatal( ${_message} )
    else()
      if( ${${_result_variable}} MATCHES "^[0-9]+$" )
        set( ${_result_variable} "status ${${_result_variable}} returned!" )
      endif()
      tde_message_fatal( "Error executing '${_command}': ${${_result_variable}}" )
    endif()
  endif()
endmacro( tde_execute_process )


################################################
#####
##### tde_read_src_metadata

macro( tde_read_src_metadata )
  # look for SCM data if present
  if( EXISTS "${CMAKE_SOURCE_DIR}/.tdescminfo" )
    file( READ "${CMAKE_SOURCE_DIR}/.tdescminfo" TDE_SCM_INFO )
    string( REGEX MATCH "(^|\n)Name: ([^\n]*)" TDE_SCM_MODULE_NAME "${TDE_SCM_INFO}" )
    string( REGEX REPLACE "^[^:]*: " "" TDE_SCM_MODULE_NAME "${TDE_SCM_MODULE_NAME}" )
    string( REGEX MATCH "(^|\n)Revision: ([^\n]*)" TDE_SCM_MODULE_REVISION "${TDE_SCM_INFO}" )
    string( REGEX REPLACE "^[^:]*: " "" TDE_SCM_MODULE_REVISION "${TDE_SCM_MODULE_REVISION}" )
    string( REGEX MATCH "(^|\n)DateTime: ([^\n]*)" TDE_SCM_MODULE_DATETIME "${TDE_SCM_INFO}" )
    string( REGEX REPLACE "^[^:]*: " "" TDE_SCM_MODULE_DATETIME "${TDE_SCM_MODULE_DATETIME}" )
  else( )
    if( EXISTS "${CMAKE_SOURCE_DIR}/.tdescmmodule" )
      file( STRINGS "${CMAKE_SOURCE_DIR}/.tdescmmodule" TDE_SCM_MODULE_NAME )
    endif( EXISTS "${CMAKE_SOURCE_DIR}/.tdescmmodule" )
    if( EXISTS "${CMAKE_SOURCE_DIR}/.tdescmrevision" )
      file( STRINGS "${CMAKE_SOURCE_DIR}/.tdescmrevision" TDE_SCM_MODULE_REVISION )
    endif( EXISTS "${CMAKE_SOURCE_DIR}/.tdescmrevision" )
  endif( )

  # look for package data if present
  if( EXISTS "${CMAKE_SOURCE_DIR}/.tdepkginfo" )
    file( READ "${CMAKE_SOURCE_DIR}/.tdepkginfo" TDE_PKG_INFO )
  endif( )
  if( EXISTS "${CMAKE_BINARY_DIR}/.tdepkginfo" )
    file( READ "${CMAKE_BINARY_DIR}/.tdepkginfo" TDE_PKG_INFO )
  endif( )
  if( TDE_PKG_INFO )
    string( REGEX MATCH "(^|\n)Name: ([^\n]*)" TDE_PKG_NAME "${TDE_PKG_INFO}" )
    string( REGEX REPLACE "^[^:]*: " "" TDE_PKG_NAME "${TDE_PKG_NAME}" )
    string( REGEX MATCH "(^|\n)Version: ([^\n]*)" TDE_PKG_VERSION "${TDE_PKG_INFO}" )
    string( REGEX REPLACE "^[^:]*: " "" TDE_PKG_VERSION "${TDE_PKG_VERSION}" )
    string( REGEX MATCH "(^|\n)DateTime: ([^\n]*)" TDE_PKG_DATETIME "${TDE_PKG_INFO}" )
    string( REGEX REPLACE "^[^:]*: " "" TDE_PKG_DATETIME "${TDE_PKG_DATETIME}" )
  endif( )
endmacro( tde_read_src_metadata )


if( DEFINED MASTER_SOURCE_DIR )
  return( )
endif( )
########### slave part ends here ###############


################################################
#####
##### tde_install_icons( <icons...> THEME <svgicons> DESTINATION <destdir> )
##### default theme: hicolor
##### default destination: ${SHARE_INSTALL_DIR}/icons

macro( tde_install_icons )
  tde_get_arg( DESTINATION 1 _dest _args ${ARGV} )
  tde_get_arg( THEME 1 _req_theme _icons ${_args} )

  #defaults
  if( NOT _icons )
    set( _icons "*" )
  endif( NOT _icons )
  if( NOT _dest )
    set( _dest "${ICON_INSTALL_DIR}" )
  endif( NOT _dest )

  foreach( _icon ${_icons} )
    unset( _theme ) # clearing

    file(GLOB _icon_files *-${_icon}.png *-${_icon}.mng _icon_files *-${_icon}.svg* )
    foreach( _icon_file ${_icon_files} )
      # FIXME need a review
      string( REGEX MATCH "^.*/([a-zA-Z][a-zA-Z])([0-9a-zA-Z]+)\\-([a-z]+)\\-([^/]+)$" _dummy  "${_icon_file}" )
      set( _type  "${CMAKE_MATCH_1}" )
      set( _size  "${CMAKE_MATCH_2}" )
      set( _group "${CMAKE_MATCH_3}" )
      set( _name  "${CMAKE_MATCH_4}" )

      # we must ignore invalid icon names
      if( _type AND _size AND _group AND _name )

        # autodetect theme
        if( NOT _req_theme )
          unset( _theme )
          if( "${_type}" STREQUAL "cr" )
            set( _theme crystalsvg )
          elseif( "${_type}" STREQUAL "lo" )
            set( _theme locolor )
          endif( "${_type}" STREQUAL "cr" )
          # defaulting
          if( NOT _theme )
            set( _theme hicolor )
          endif( NOT _theme )
        else( NOT _req_theme )
          set( _theme ${_req_theme} )
        endif( NOT _req_theme )

        # fix "group" name
        if( "${_group}" STREQUAL "mime" )
          set( _group  "mimetypes" )
        endif( "${_group}" STREQUAL "mime" )
        if( "${_group}" STREQUAL "filesys" )
          set( _group  "places" )
        endif( "${_group}" STREQUAL "filesys" )
        if( "${_group}" STREQUAL "category" )
          set( _group  "categories" )
        endif( "${_group}" STREQUAL "category" )
        if( "${_group}" STREQUAL "device" )
          set( _group  "devices" )
        endif( "${_group}" STREQUAL "device" )
        if( "${_group}" STREQUAL "app" )
          set( _group  "apps" )
        endif( "${_group}" STREQUAL "app" )
        if( "${_group}" STREQUAL "action" )
          set( _group  "actions" )
        endif( "${_group}" STREQUAL "action" )

        if( "${_size}" STREQUAL "sc" )
          install( FILES ${_icon_file} DESTINATION ${_dest}/${_theme}/scalable/${_group}/ RENAME ${_name} )
        else( "${_size}" STREQUAL "sc" )
          install( FILES ${_icon_file} DESTINATION ${_dest}/${_theme}/${_size}x${_size}/${_group}/ RENAME ${_name} )
        endif( "${_size}" STREQUAL "sc" )

      endif( _type AND _size AND _group AND _name )

    endforeach( _icon_file )
  endforeach( _icon )

endmacro( tde_install_icons )


#################################################
#####
##### tde_add_lut( <source> <result> [depends] )
##### default depends: source

macro( tde_add_lut _src _lut _dep )
  set( create_hash_table ${CMAKE_SOURCE_DIR}/kjs/create_hash_table )
  if( NOT _dep )
    set( _dep ${_src} )
  endif( NOT _dep )
  add_custom_command( OUTPUT ${_lut}
    COMMAND perl ARGS ${create_hash_table} ${CMAKE_CURRENT_SOURCE_DIR}/${_src} -i > ${_lut}
    DEPENDS ${_src} )
  set_source_files_properties( ${_dep} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_lut} )
  unset( _dep )
endmacro( tde_add_lut )


#################################################
#####
##### tde_add_luts( <sources...> )

macro( tde_add_luts )
  foreach( _src ${ARGV} )
    get_filename_component( _lut ${_src} NAME_WE )
    set( _lut "${_lut}.lut.h" )
    tde_add_lut( ${_src} ${_lut} ${_src} )
  endforeach( _src )
endmacro( tde_add_luts )


#################################################
#####
##### tde_file_to_cpp( <source> <destination> <variable> )

macro( tde_file_to_cpp _src _dst _var )
  if( IS_ABSOLUTE ${_dst} )
    set( dst ${_dst} )
  else( )
    set( dst "${CMAKE_CURRENT_BINARY_DIR}/${_dst}" )
  endif( )
  file( READ ${_src} text )
  string( REGEX REPLACE "\n" "\\\\n\"\n\"" text "${text}" )
  set( text "/* Generated by CMake */\n\nconst char *${_var} = \n\n\"${text}\";\n" )
  string( REGEX REPLACE "\n\"\";\n$" ";\n" text "${text}" )
  file( WRITE ${dst} "${text}" )
endmacro( )


#################################################
#####
##### tde_get_library_filename( <var> <target> )

function( tde_get_library_filename _filename _target )
  get_target_property( _type ${_target} TYPE )
  if( "${_type}" MATCHES "_LIBRARY" )
    get_target_property( _output_prefix ${_target} PREFIX )
    if( "${_output_prefix}" STREQUAL "_output_prefix-NOTFOUND" )
      if( "${_type}" MATCHES "STATIC_" )
        set( _output_prefix "${CMAKE_STATIC_LIBRARY_PREFIX}" )
      elseif( "${_type}" MATCHES "SHARED_" )
        set( _output_prefix "${CMAKE_SHARED_LIBRARY_PREFIX}" )
      elseif( "${_type}" MATCHES "MODULE_" )
        set( _output_prefix "${CMAKE_SHARED_MODULE_PREFIX}" )
      else( )
        set( _output_prefix "" )
      endif( )
    endif( )
    get_target_property( _output_suffix ${_target} SUFFIX )
    if( "${_output_suffix}" STREQUAL "_output_suffix-NOTFOUND" )
      if( "${_type}" MATCHES "STATIC_" )
        set( _output_suffix "${CMAKE_STATIC_LIBRARY_SUFFIX}" )
      elseif( "${_type}" MATCHES "SHARED_" )
        set( _output_suffix "${CMAKE_SHARED_LIBRARY_SUFFIX}" )
      elseif( "${_type}" MATCHES "MODULE_" )
        set( _output_suffix "${CMAKE_SHARED_MODULE_SUFFIX}" )
      else( )
        set( _output_suffix "" )
      endif( )
    endif( )
    get_target_property( _output ${_target} OUTPUT_NAME )
    set( ${_filename} "${_output_prefix}${_output}${_output_suffix}" PARENT_SCOPE )
  else( )
    set( ${_filename} "" PARENT_SCOPE )
  endif( )
endfunction( )


#################################################
#####
##### tde_install_la_file( <target> <destination> )

macro( tde_install_la_file _target _destination )

  tde_get_library_filename( _soname ${_target} )
  get_target_property( _target_release ${_target} RELEASE )
  if( _target_release )
    string( REPLACE "-${_target_release}" "" _soname_base "${_soname}" )
  else( )
    set( _soname_base ${_soname} )
  endif( )
  string( REGEX REPLACE "\\.so(\\.[0-9]+)*$" "" _laname "${_soname_base}" )
  set( _laname ${CMAKE_CURRENT_BINARY_DIR}/${_laname}.la )

  file( WRITE ${_laname}
"# ${_laname} - a libtool library file, generated by cmake
# The name that we can dlopen(3).
dlname='${_soname}'
# Names of this library
library_names='${_soname} ${_soname} ${_soname_base}'
# The name of the static archive
old_library=''
# Libraries that this one depends upon.
dependency_libs=''
# Version information.\ncurrent=0\nage=0\nrevision=0
# Is this an already installed library?\ninstalled=yes
# Should we warn about portability when linking against -modules?\nshouldnotlink=yes
# Files to dlopen/dlpreopen\ndlopen=''\ndlpreopen=''
# Directory that this library needs to be installed in:
libdir='${_destination}'
" )

  install( FILES ${_laname} DESTINATION ${_destination} )

endmacro( tde_install_la_file )


#################################################
#####
##### tde_add_ui_files

macro( tde_add_ui_files _sources )
  if( TDE_FOUND OR "${CMAKE_PROJECT_NAME}" STREQUAL "tdelibs" )
    set( HAVE_TDE 1 )
  endif()

  foreach( _ui_file ${ARGN} )

    get_filename_component( _ui_basename ${_ui_file} NAME_WE )
    get_filename_component( _ui_absolute_path ${_ui_file} ABSOLUTE )

    list( APPEND ${_sources} ${_ui_basename}.cpp )

    add_custom_command( OUTPUT ${_ui_basename}.h ${_ui_basename}.cpp
      COMMAND ${CMAKE_COMMAND}
        -DUIC_EXECUTABLE:FILEPATH=${UIC_EXECUTABLE}
        -DTQT_REPLACE_SCRIPT:FILEPATH=${TQT_REPLACE_SCRIPT}
        -DTDE_TQTPLUGINS_DIR:FILEPATH=${TDE_TQTPLUGINS_DIR}
        -DMOC_EXECUTABLE:FILEPATH=${MOC_EXECUTABLE}
        -DUI_FILE:FILEPATH=${_ui_absolute_path}
        -DMASTER_SOURCE_DIR:FILEPATH=${CMAKE_SOURCE_DIR}
        -DMASTER_BINARY_DIR:FILEPATH=${CMAKE_BINARY_DIR}
        -DTDE_FOUND=${HAVE_TDE}
        -DTQT_ONLY=${TQT_ONLY}
        -P ${TDE_CMAKE_MODULES}/tde_uic.cmake
      DEPENDS ${_ui_absolute_path} )

  endforeach( _ui_file )
endmacro( tde_add_ui_files )


#################################################
#####
##### tde_moc

macro( tde_moc _sources )
  foreach( _input_file ${ARGN} )

    get_filename_component( _input_file "${_input_file}" ABSOLUTE )
    get_filename_component( _basename ${_input_file} NAME_WE )
    set( _output_file "${_basename}.moc.cpp" )
    add_custom_command( OUTPUT ${_output_file}
      COMMAND
        ${TMOC_EXECUTABLE} ${_input_file} -o ${_output_file}
      DEPENDS
        ${_input_file} )
    list( APPEND ${_sources} ${_output_file} )

  endforeach( )
endmacro( )


#################################################
#####
##### tde_automoc

macro( tde_automoc )
  foreach( _src_file ${ARGN} )

    get_filename_component( _src_file "${_src_file}" ABSOLUTE )

    if( EXISTS "${_src_file}" )

      # read source file and check if have moc include
      file( READ "${_src_file}" _src_content )
      string( REGEX MATCHALL "#include +[^ ]+\\.moc[\">]" _moc_includes "${_src_content}" )

      # found included moc(s)?
      if( _moc_includes )
        foreach( _moc_file ${_moc_includes} )

          # extracting moc filename
          string( REGEX MATCH "[^ <\"]+\\.moc" _moc_file "${_moc_file}" )
          set( _moc_file "${CMAKE_CURRENT_BINARY_DIR}/${_moc_file}" )

          # create header filename
          get_filename_component( _src_path "${_src_file}" ABSOLUTE )
          get_filename_component( _src_path "${_src_path}" PATH )
          get_filename_component( _src_header "${_moc_file}" NAME_WE )
          set( _header_file "${_src_path}/${_src_header}.h" )

          # if header doesn't exists, check in META_INCLUDES
          if( NOT EXISTS "${_header_file}" )
            unset( _found )
            foreach( _src_path ${_meta_includes} )
              set( _header_file "${_src_path}/${_src_header}.h" )
              if( EXISTS "${_header_file}" )
                set( _found 1 )
                break( )
              endif( )
            endforeach( )
            if( NOT _found )
              get_filename_component( _moc_file "${_moc_file}" NAME )
              tde_message_fatal( "AUTOMOC error: '${_moc_file}' cannot be generated.\n Reason: '${_src_header}.h' not found." )
            endif( )
          endif( )

          # moc-ing header
          file( RELATIVE_PATH _moc_file_relative "${CMAKE_BINARY_DIR}" "${_moc_file}" )
          add_custom_command( OUTPUT ${_moc_file}
            COMMAND ${TMOC_EXECUTABLE} ${_header_file} -o ${_moc_file}
            COMMENT "Generating ${_moc_file_relative}"
            DEPENDS ${_header_file} )

          # create dependency between source file and moc file
          set_property( SOURCE ${_src_file} APPEND PROPERTY OBJECT_DEPENDS ${_moc_file} )

        endforeach( _moc_file )

      endif( _moc_includes )

    else( EXISTS "${_src_file}" )

      # for generated file, the path must match the binary directory
      string( LENGTH "${CMAKE_BINARY_DIR}" CMAKE_BINARY_DIR_LEN )
      string( LENGTH "${_src_file}" _basename_len )
      if( ${_basename_len} LESS ${CMAKE_BINARY_DIR_LEN} )
        set( _basedir_prefix "${CMAKE_SOURCE_DIR}" )
      else( )
        string( SUBSTRING "${_src_file}" 0 ${CMAKE_BINARY_DIR_LEN} _basedir_prefix )
      endif( )
      if( ${_basedir_prefix} STREQUAL "${CMAKE_BINARY_DIR}" )
        file( RELATIVE_PATH _sourcename "${CMAKE_BINARY_DIR}" "${_src_file}" )
        file( RELATIVE_PATH _basename "${CMAKE_CURRENT_BINARY_DIR}" "${_src_file}" )
      else( )
        file( RELATIVE_PATH _sourcename "${CMAKE_SOURCE_DIR}" "${_src_file}" )
        file( RELATIVE_PATH _basename "${CMAKE_CURRENT_SOURCE_DIR}" "${_src_file}" )
      endif( )

      # automocing generated file
      get_filename_component( _automoc_file "${_src_file}+automoc" NAME )
      set( _automoc_file "${CMAKE_CURRENT_BINARY_DIR}/${_automoc_file}" )

      add_custom_command( OUTPUT ${_automoc_file}
        COMMAND ${CMAKE_COMMAND}
          -DTMOC_EXECUTABLE:FILEPATH=${TMOC_EXECUTABLE}
          -DSRC_FILE:FILEPATH=${CMAKE_CURRENT_BINARY_DIR}/${_basename}
          -DMETA_INCLUDES:STRING="${_meta_includes}"
          -DMASTER_SOURCE_DIR:FILEPATH=${CMAKE_SOURCE_DIR}
          -DMASTER_BINARY_DIR:FILEPATH=${CMAKE_BINARY_DIR}
          -P ${TDE_CMAKE_MODULES}/tde_automoc.cmake
        COMMENT "Automocing ${_sourcename}"
        DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_basename}"
      )

      # create dependency between source file and moc file
      set_property( SOURCE "${CMAKE_CURRENT_BINARY_DIR}/${_basename}"
                    APPEND PROPERTY OBJECT_DEPENDS ${_automoc_file} )

    endif( EXISTS "${_src_file}" )

  endforeach( _src_file )
endmacro( tde_automoc )


#################################################
#####
##### tde_create_dcop_kidl

macro( tde_create_dcop_kidl _kidl _kidl_source )

  get_filename_component( _kidl_source ${_kidl_source} ABSOLUTE )
  get_filename_component( _kidl_basename ${_kidl_source} NAME_WE )
  set( _kidl_output ${CMAKE_CURRENT_BINARY_DIR}/${_kidl_basename}.kidl )
  file( RELATIVE_PATH _kidl_target "${CMAKE_BINARY_DIR}" "${_kidl_output}" )
  string( REPLACE "/" "+" _kidl_target "${_kidl_target}" )

  if( NOT TARGET ${_kidl_target} )
    add_custom_command(
      OUTPUT ${_kidl_output}
      COMMAND ${KDE3_DCOPIDLNG_EXECUTABLE}
      ARGS ${_kidl_source} > ${_kidl_output}
      DEPENDS ${_kidl_source}
    )
    add_custom_target( ${_kidl_target} DEPENDS ${_kidl_output} )
  endif( )

  set( ${_kidl} ${_kidl_output} )

endmacro( tde_create_dcop_kidl )


#################################################
#####
##### tde_add_dcop_skels

macro( tde_add_dcop_skels _sources )
  foreach( _current_FILE ${ARGN} )

    get_filename_component( _tmp_FILE ${_current_FILE} ABSOLUTE )
    get_filename_component( _basename ${_tmp_FILE} NAME_WE )

    set( _skel ${CMAKE_CURRENT_BINARY_DIR}/${_basename}_skel.cpp )
    file( RELATIVE_PATH _skel_target "${CMAKE_BINARY_DIR}" "${_skel}" )
    string( REPLACE "/" "+" _skel_target "${_skel_target}" )

    tde_create_dcop_kidl( _kidl ${_tmp_FILE} )

    if( NOT TARGET ${_skel_target} )
      add_custom_command(
        OUTPUT ${_skel}
        COMMAND ${KDE3_DCOPIDL2CPP_EXECUTABLE}
        ARGS --c++-suffix cpp --no-signals --no-stub ${_kidl}
        DEPENDS ${_kidl_target}
      )
      add_custom_target( ${_skel_target} DEPENDS ${_skel} )
    endif( )

    list( APPEND ${_sources} ${_skel} )

  endforeach( _current_FILE )
endmacro( tde_add_dcop_skels )


#################################################
#####
##### tde_add_dcop_stubs

macro( tde_add_dcop_stubs _sources )
  foreach( _current_FILE ${ARGN} )

    get_filename_component( _tmp_FILE ${_current_FILE} ABSOLUTE )
    get_filename_component( _basename ${_tmp_FILE} NAME_WE )

    set( _stub_CPP ${CMAKE_CURRENT_BINARY_DIR}/${_basename}_stub.cpp )
    set( _stub_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${_basename}_stub.h )
    file( RELATIVE_PATH _stub_target "${CMAKE_BINARY_DIR}" "${_stub_CPP}" )
    string( REPLACE "/" "+" _stub_target "${_stub_target}" )

    tde_create_dcop_kidl( _kidl ${_tmp_FILE} )

    if( NOT TARGET ${_stub_target} )
      add_custom_command(
        OUTPUT ${_stub_CPP} ${_stub_HEADER}
        COMMAND ${KDE3_DCOPIDL2CPP_EXECUTABLE}
        ARGS --c++-suffix cpp --no-signals --no-skel ${_kidl}
        DEPENDS ${_kidl_target}
      )
      add_custom_target( ${_stub_target} DEPENDS ${_stub_CPP} ${_stub_HEADER} )
    endif( )

    list( APPEND ${_sources} ${_stub_CPP} )

  endforeach( _current_FILE )
endmacro( tde_add_dcop_stubs )


#################################################
#####
##### tde_add_kcfg_files

macro( tde_add_kcfg_files _sources )
  foreach( _current_FILE ${ARGN} )

    get_filename_component( _tmp_FILE ${_current_FILE} ABSOLUTE )
    get_filename_component( _basename ${_tmp_FILE} NAME_WE )

    file( READ ${_tmp_FILE} _contents )
    string( REGEX REPLACE "^(.*\n)?File=([^\n]+)\n.*$" "\\2"  _kcfg_FILE "${_contents}" )

    set( _src_FILE    ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp )
    set( _header_FILE ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h )
    file( RELATIVE_PATH _kcfg_target "${CMAKE_BINARY_DIR}" "${_src_FILE}" )
    string( REPLACE "/" "+" _kcfg_target "${_kcfg_target}" )

    if( NOT TARGET ${_kcfg_target} )
      add_custom_command(
        OUTPUT ${_src_FILE} ${_header_FILE}
        COMMAND ${KDE3_KCFGC_EXECUTABLE}
        ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${_kcfg_FILE} ${_tmp_FILE}
        DEPENDS ${_tmp_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/${_kcfg_FILE}
      )
      add_custom_target( ${_kcfg_target} DEPENDS ${_src_FILE} ${_header_FILE} )
    endif( )

    list( APPEND ${_sources} ${_src_FILE} )

  endforeach( _current_FILE )
endmacro( tde_add_kcfg_files )


#################################################
#####
##### __tde_internal_process_sources

macro( __tde_internal_process_sources _sources )

  unset( ${_sources} )

  foreach( _arg ${ARGN} )
    get_filename_component( _ext ${_arg} EXT )
    get_filename_component( _name ${_arg} NAME_WE )
    get_filename_component( _path ${_arg} PATH )

    # if not path, set it to "."
    if( NOT _path )
      set( _path "./" )
    endif( NOT _path )

    # handle .ui files
    if( ${_ext} STREQUAL ".ui" )
      tde_add_ui_files( ${_sources} ${_arg} )

    # handle .skel files
    elseif( ${_ext} STREQUAL ".skel" )
      tde_add_dcop_skels( ${_sources} ${_path}/${_name}.h )

    # handle .stub files
    elseif( ${_ext} STREQUAL ".stub" )
      tde_add_dcop_stubs( ${_sources} ${_path}/${_name}.h )

    # handle .kcfgc files
    elseif( ${_ext} STREQUAL ".kcfgc" )
      tde_add_kcfg_files( ${_sources} ${_arg} )

    # handle any other files
    else( ${_ext} STREQUAL ".ui" )
      list(APPEND ${_sources} ${_arg} )
    endif( ${_ext} STREQUAL ".ui" )
  endforeach( _arg )

endmacro( __tde_internal_process_sources )


#################################################
#####
##### tde_install_libtool_file

macro( tde_install_libtool_file _target _destination )

  # get .so name
  tde_get_library_filename( _soname ${_target} )
  get_target_property( _target_release ${_target} RELEASE )
  if( _target_release )
    string( REPLACE "-${_target_release}" "" _soname_base "${_soname}" )
  else( )
    set( _soname_base ${_soname} )
  endif( )

  # get .la name
  string( REGEX REPLACE "\\.so(\\.[0-9]+)*$" "" _laname "${_soname_base}" )
  set( _laname ${_laname}.la )

  # get version of target
  get_target_property( _target_version ${_target} VERSION )
  get_target_property( _target_soversion ${_target} SOVERSION )

  # we have so version
  if( _target_version )
    set( _library_name_1 "${_soname}.${_target_version}" )
    set( _library_name_2 "${_soname}.${_target_soversion}" )
    set( _library_name_3 "${_soname_base}" )

    string( REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" _dummy  "${_target_version}" )
    set( _version_current  "${CMAKE_MATCH_1}" )
    set( _version_age  "${CMAKE_MATCH_2}" )
    set( _version_revision "${CMAKE_MATCH_3}" )

  # we have no so version (module?)
  else( _target_version )
    set( _library_name_1 "${_soname}" )
    set( _library_name_2 "${_soname}" )
    set( _library_name_3 "${_soname_base}" )
    set( _version_current  "0" )
    set( _version_age  "0" )
    set( _version_revision "0" )
  endif( _target_version )

  if( IS_ABSOLUTE ${_destination} )
    set( _libdir "${_destination}" )
  else( IS_ABSOLUTE ${_destination} )
    set( _libdir "${CMAKE_INSTALL_PREFIX}/${_destination}" )
  endif( IS_ABSOLUTE ${_destination} )

  configure_file( ${TDE_CMAKE_TEMPLATES}/tde_libtool_file.cmake "${_laname}" @ONLY )

  install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${_laname}" DESTINATION ${_destination} )

endmacro( tde_install_libtool_file )


#################################################
#####
##### tde_install_export / tde_import

function( tde_install_export )
  file( GLOB export_files ${CMAKE_CURRENT_BINARY_DIR}/export-*.cmake )
  list( SORT export_files )

  set( mode "WRITE" )
  foreach( filename ${export_files} )
    file( READ ${filename} content )
    file( ${mode} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.cmake" "${content}" )
    set( mode "APPEND" )
  endforeach( )

  install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.cmake" DESTINATION ${CMAKE_INSTALL_DIR} )
endfunction( )


macro( tde_import _library )
  message( STATUS "checking for '${_library}'" )
  string( TOUPPER "BUILD_${_library}" _build )
  if( ${_build} )
    message( STATUS "  ok, activated for build" )
  else()
    if( EXISTS "${TDE_CMAKE_DIR}/${_library}.cmake" )
      include( "${TDE_CMAKE_DIR}/${_library}.cmake" )
    elseif( EXISTS "${TQT_CMAKE_DIR}/${_library}.cmake" )
      include( "${TQT_CMAKE_DIR}/${_library}.cmake" )
    else()
      tde_message_fatal( "'${_library}' is required, but is not installed nor selected for build" )
    endif()
    message( STATUS "  ok, found import file" )
  endif()
endmacro()


#################################################
#####
##### tde_add_library

macro( tde_add_library _arg_target )

  unset( _target )
  unset( _type )
  unset( _static_pic )
  unset( _automoc )
  unset( _meta_includes )
  unset( _no_libtool_file )
  unset( _no_export )
  unset( _version )
  unset( _release )
  unset( _sources )
  unset( _cxx_features )
  unset( _cxx_features_private )
  unset( _destination )
  unset( _embed )
  unset( _link )
  unset( _link_private )
  unset( _dependencies )
  unset( _storage )
  unset( _exclude_from_all )

  set( _shouldnotlink no )

  # metadata
  unset( _description )
  unset( _license )
  unset( _copyright )
  unset( _authors )
  unset( _product )
  unset( _organization )
  unset( _version )
  unset( _datetime )
  unset( _notes )

  # default metadata
  set( _product "Trinity Desktop Environment" )
  tde_curdatetime( _datetime )

  foreach( _arg ${ARGV} )

    # this variable help us to skip
    # storing unapropriate values (i.e. directives)
    unset( _skip_store )

    # found one of directives: "SHARED", "STATIC", "MODULE"
    if( "+${_arg}" STREQUAL "+SHARED" OR "+${_arg}" STREQUAL "+STATIC" OR "+${_arg}" STREQUAL "+MODULE" )
      set( _skip_store 1 )
      set( _type "${_arg}" )
    endif( "+${_arg}" STREQUAL "+SHARED" OR "+${_arg}" STREQUAL "+STATIC" OR "+${_arg}" STREQUAL "+MODULE" )

    # found directive "STATIC_PIC"
    if( "+${_arg}" STREQUAL "+STATIC_PIC" )
      set( _skip_store 1 )
      set( _type "STATIC" )
      set( _static_pic 1 )
    endif( "+${_arg}" STREQUAL "+STATIC_PIC" )

    # found directive "AUTOMOC"
    if( "+${_arg}" STREQUAL "+AUTOMOC" )
      set( _skip_store 1 )
      set( _automoc 1 )
    endif( "+${_arg}" STREQUAL "+AUTOMOC" )

    # found directive "META_INCLUDES"
    if( "+${_arg}" STREQUAL "+META_INCLUDES" )
      set( _skip_store 1 )
      set( _storage "_meta_includes" )
    endif( )

    # found directive "NO_LIBTOOL_FILE"
    if( "+${_arg}" STREQUAL "+NO_LIBTOOL_FILE" )
      set( _skip_store 1 )
      set( _no_libtool_file 1 )
    endif( "+${_arg}" STREQUAL "+NO_LIBTOOL_FILE" )

    # found directive "NO_EXPORT"
    if( "+${_arg}" STREQUAL "+NO_EXPORT" )
      set( _skip_store 1 )
      set( _no_export 1 )
    endif( "+${_arg}" STREQUAL "+NO_EXPORT" )

    # found directive "VERSION"
    if( "+${_arg}" STREQUAL "+VERSION" )
      set( _skip_store 1 )
      set( _storage "_version" )
    endif( "+${_arg}" STREQUAL "+VERSION" )

    # found directive "RELEASE"
    if( "+${_arg}" STREQUAL "+RELEASE" )
      set( _skip_store 1 )
      set( _storage "_release" )
    endif( "+${_arg}" STREQUAL "+RELEASE" )

    # found directive "SOURCES"
    if( "+${_arg}" STREQUAL "+SOURCES" )
      set( _skip_store 1 )
      set( _storage "_sources" )
    endif( "+${_arg}" STREQUAL "+SOURCES" )

    # found directive "CXX_FEATURES"
    if( "+${_arg}" STREQUAL "+CXX_FEATURES" )
      set( _skip_store 1 )
      set( _storage "_cxx_features" )
    endif( "+${_arg}" STREQUAL "+CXX_FEATURES" )

    # found directive "CXX_FEATURES_PRIVATE"
    if( "+${_arg}" STREQUAL "+CXX_FEATURES_PRIVATE" )
      set( _skip_store 1 )
      set( _storage "_cxx_features_private" )
    endif( "+${_arg}" STREQUAL "+CXX_FEATURES_PRIVATE" )

    # found directive "EMBED"
    if( "+${_arg}" STREQUAL "+EMBED" )
      set( _skip_store 1 )
      set( _storage "_embed" )
    endif( "+${_arg}" STREQUAL "+EMBED" )

    # found directive "LINK"
    if( "+${_arg}" STREQUAL "+LINK" )
      set( _skip_store 1 )
      set( _storage "_link" )
    endif( "+${_arg}" STREQUAL "+LINK" )

    # found directive "LINK_PRIVATE"
    if( "+${_arg}" STREQUAL "+LINK_PRIVATE" )
      set( _skip_store 1 )
      set( _storage "_link_private" )
    endif( "+${_arg}" STREQUAL "+LINK_PRIVATE" )

    # found directive "DEPENDENCIES"
    if( "+${_arg}" STREQUAL "+DEPENDENCIES" )
      set( _skip_store 1 )
      set( _storage "_dependencies" )
    endif( "+${_arg}" STREQUAL "+DEPENDENCIES" )

    # found directive "DESTINATION"
    if( "+${_arg}" STREQUAL "+DESTINATION" )
      set( _skip_store 1 )
      set( _storage "_destination" )
      unset( ${_storage} )
    endif( "+${_arg}" STREQUAL "+DESTINATION" )

    # found directive "EXCLUDE_FROM_ALL"
    if( "+${_arg}" STREQUAL "+EXCLUDE_FROM_ALL" )
      set( _skip_store 1 )
      set( _exclude_from_all "EXCLUDE_FROM_ALL" )
    endif( "+${_arg}" STREQUAL "+EXCLUDE_FROM_ALL" )

   # metadata
    if( "+${_arg}" STREQUAL "+DESCRIPTION" )
      set( _skip_store 1 )
      set( _storage "_description" )
    endif( )
    if( "+${_arg}" STREQUAL "+LICENSE" )
      set( _skip_store 1 )
      set( _storage "_license" )
    endif( )
    if( "+${_arg}" STREQUAL "+COPYRIGHT" )
      set( _skip_store 1 )
      set( _storage "_copyright" )
    endif( )
    if( "+${_arg}" STREQUAL "+AUTHORS" )
      set( _skip_store 1 )
      set( _storage "_authors" )
    endif( )
    if( "+${_arg}" STREQUAL "+PRODUCT" )
      set( _skip_store 1 )
      set( _storage "_product" )
    endif( )
    if( "+${_arg}" STREQUAL "+ORGANIZATION" )
      set( _skip_store 1 )
      set( _storage "_organization" )
    endif( )
    if( "+${_arg}" STREQUAL "+VERSION" )
      set( _skip_store 1 )
      set( _storage "_version" )
    endif( )
    if( "+${_arg}" STREQUAL "+DATETIME" )
      set( _skip_store 1 )
      set( _storage "_datetime" )
    endif( )
    if( "+${_arg}" STREQUAL "+NOTES" )
      set( _skip_store 1 )
      set( _storage "_notes" )
    endif( )

    # storing value
    if( _storage AND NOT _skip_store )
      list( APPEND ${_storage} ${_arg} )
      list( REMOVE_DUPLICATES ${_storage} )
    endif( _storage AND NOT _skip_store )

  endforeach( _arg )

  # if no type is set, we choose one
  # based on BUILD_SHARED_LIBS
  if( NOT _type )
    if( BUILD_SHARED_LIBS )
      set( _type "SHARED" )
    else( BUILD_SHARED_LIBS )
      set( _type "STATIC" )
    endif( BUILD_SHARED_LIBS )
  endif( NOT _type )

  # change target name, based on type
  string( TOLOWER "${_type}" _type_lower )
  set( _target "${_arg_target}-${_type_lower}" )

  # create variables like "LIB_xxx" for convenience
  if( ${_type} STREQUAL "SHARED" )
    string( TOUPPER "${_arg_target}" _tmp )
    set( LIB_${_tmp} ${_target} CACHE INTERNAL LIB_${tmp} FORCE )
  endif( ${_type} STREQUAL "SHARED" )

  # disallow target without sources
  if( NOT _sources )
    message( FATAL_ERROR "\nTarget [$_target] have no sources." )
  endif( NOT _sources )

  # processing different types of sources
  __tde_internal_process_sources( _sources ${_sources} )

  # set automoc
  if( _automoc )
    tde_automoc( ${_sources} )
  endif( _automoc )

  # add target
  add_library( ${_target} ${_type} ${_exclude_from_all} ${_sources} )

  # set cxx features
  if( _cxx_features )
    target_compile_features( ${_target} PUBLIC ${_cxx_features} )
  endif( )
  if( TDE_CXX_FEATURES OR PROJECT_CXX_FEATURES OR _cxx_features_private )
    target_compile_features( ${_target} PRIVATE
      ${TDE_CXX_FEATURES} ${PROJECT_CXX_FEATURES} ${_cxx_features_private} )
  endif( )

  # we assume that modules have no prefix and no version
  # also, should not link
  if( ${_type} STREQUAL "MODULE" )
    set_target_properties( ${_target} PROPERTIES PREFIX "" )
    unset( _version )
    set( _shouldnotlink yes )
  endif( ${_type} STREQUAL "MODULE" )

  # set real name of target
  if( _release )
    # add release number to output name
    set_target_properties( ${_target} PROPERTIES RELEASE ${_release} )
    set_target_properties( ${_target} PROPERTIES OUTPUT_NAME "${_arg_target}-${_release}" )
  else( _release )
    set_target_properties( ${_target} PROPERTIES OUTPUT_NAME ${_arg_target} )
  endif( _release )

  # set -fPIC flag for static libraries
  if( _static_pic )
    if( "${CMAKE_VERSION}" VERSION_LESS "2.8.9" )
      set_target_properties( ${_target} PROPERTIES COMPILE_FLAGS -fPIC )
    else( )
      set_target_properties( ${_target} PROPERTIES POSITION_INDEPENDENT_CODE ON )
    endif( )
  endif( _static_pic )

  # set version
  if( _version )
    if( ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" )
      # OpenBSD: _soversion and _version both contains only major and minor
      string( REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" _dummy  "${_version}" )
      set( _version  "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}" )
      set( _soversion  "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}" )
    else( ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" )
      # General (Linux) case: _soversion contains only the major number of version
      string( REGEX MATCH "^[0-9]+" _soversion ${_version} )
    endif( ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" )
    set_target_properties( ${_target} PROPERTIES VERSION ${_version} SOVERSION ${_soversion} )
  endif( _version )

  # set interface libraries (only for shared)
  unset( _shared_libs )
  if( NOT ${_type} STREQUAL "STATIC" )
    foreach( _lib ${_link} )
      #get_target_property( _lib_type ${_lib} TYPE )
      #if( NOT "STATIC_LIBRARY" STREQUAL "${_lib_type}" )
      if( NOT ${_lib} MATCHES ".+-static" )
        list( APPEND _shared_libs ${_lib} )
      endif( NOT ${_lib} MATCHES ".+-static" )
      #endif( NOT "STATIC_LIBRARY" STREQUAL "${_lib_type}" )
    endforeach( _lib )
  endif( NOT ${_type} STREQUAL "STATIC" )

  # set embedded archives
  if( _embed )
    if( ${CMAKE_SYSTEM_NAME} MATCHES "SunOS" )
      list( INSERT _link_private 0 -Wl,-zallextract ${_embed} -Wl,-zdefaultextract )
    else( )
      list( INSERT _link_private 0 -Wl,-whole-archive ${_embed} -Wl,-no-whole-archive )
    endif( )
  endif( _embed )

  # set private linked libraries
  if( _link_private )
    if( NOT ${CMAKE_VERSION} VERSION_LESS "2.8.12" )
      if( _link )
        list( INSERT _link 0 "PUBLIC" )
      endif()
      list( INSERT _link_private 0 "PRIVATE" )
    endif()
    list( INSERT _link 0 ${_link_private} )
  endif( _link_private )

  # set link libraries
  if( _link )
    if( _embed AND ${CMAKE_VERSION} VERSION_EQUAL "2.8.12.0" )
      # hack for broken CMake 2.8.12.0
      set_target_properties( ${_target} PROPERTIES LINK_LIBRARIES "${_link}" )
    else( _embed AND  ${CMAKE_VERSION} VERSION_EQUAL "2.8.12.0" )
      target_link_libraries( ${_target} ${_link} )
    endif( _embed AND  ${CMAKE_VERSION} VERSION_EQUAL "2.8.12.0" )
  endif( )
  if( _shared_libs )
    string( TOUPPER "${CMAKE_BUILD_TYPE}" _build_type )
    set_target_properties( ${_target} PROPERTIES
                           LINK_INTERFACE_LIBRARIES "${_shared_libs}"
                           LINK_INTERFACE_LIBRARIES_${_build_type} "${_shared_libs}"
                           INTERFACE_LINK_LIBRARIES "${_shared_libs}"
                           INTERFACE_LINK_LIBRARIES_${_build_type} "${_shared_libs}" )
  endif( _shared_libs )

  # set dependencies
  if( _dependencies )
    add_dependencies( ${_target} ${_dependencies} )
  endif( _dependencies )

  # if destination directory is set
  if( _destination )

    # we export only shared libs (no static, no modules);
    # also, do not export targets marked as "NO_EXPORT" (usually for tdeinit)
    if( "SHARED" STREQUAL ${_type} AND NOT _no_export )

      # get target properties: output name, version, soversion
      tde_get_library_filename( _output ${_target} )
      get_target_property( _version ${_target} VERSION )
      get_target_property( _soversion ${_target} SOVERSION )

      if( _version )
        set( _location "${_destination}/${_output}.${_version}" )
        set( _soname "${_output}.${_soversion}" )
      else( )
        set( _location "${_destination}/${_output}" )
        set( _soname "${_output}" )
        unset( _version )
      endif( )

      configure_file( ${TDE_CMAKE_TEMPLATES}/tde_export_library.cmake "${PROJECT_BINARY_DIR}/export-${_target}.cmake" @ONLY )
    endif( )

    # install target
    install( TARGETS ${_target} DESTINATION ${_destination} )

    # install base soname
    if( _release AND NOT "STATIC" STREQUAL ${_type} )
      tde_get_library_filename( _soname ${_target} )
      string( REPLACE "-${_release}" "" _soname_base "${_soname}" )
      if( _version )
        get_target_property( _soversion ${_target} SOVERSION )
        set( _soname "${_soname}.${_soversion}" )
      endif( )
      if( NOT _exclude_from_all )
        add_custom_command(
          OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_soname_base}"
          COMMAND ln -s ${_soname} "${CMAKE_CURRENT_BINARY_DIR}/${_soname_base}"
          DEPENDS ${_target}
        )
        add_custom_target(
          ${_target}+base-so ALL
          DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_soname_base}"
        )
      else( )
        add_custom_command(
          TARGET ${_target} POST_BUILD
          COMMAND ln -s ${_soname} "${CMAKE_CURRENT_BINARY_DIR}/${_soname_base}"
        )
      endif( )
      install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${_soname_base}" DESTINATION ${_destination} )
    endif( )

    # install .la files for dynamic libraries
    if( NOT "STATIC" STREQUAL ${_type} AND NOT _no_libtool_file )
      tde_install_libtool_file( ${_target} ${_destination} )
    endif( )

  endif( _destination )

  # embed name and metadata
  set( ELF_EMBEDDING_METADATA "\"${_target}\" \"${_description}\" \"${_license}\" \"${_copyright}\" \"${_authors}\" \"${_product}\" \"${_organization}\" \"${_version}\" \"${_datetime}\" \"x-sharedlib\" \"${TDE_SCM_MODULE_NAME}\" \"${TDE_SCM_MODULE_REVISION}\" \"${_notes}\"" )
  separate_arguments( ELF_EMBEDDING_METADATA )
  if( TDELFEDITOR_EXECUTABLE AND _soname )
    if( _version )
      get_filename_component( _target_lib ${CMAKE_CURRENT_BINARY_DIR}/${_soname}.${_version} ABSOLUTE )
    else( )
      get_filename_component( _target_lib ${CMAKE_CURRENT_BINARY_DIR}/${_soname} ABSOLUTE )
    endif( )
    file( RELATIVE_PATH _target_path "${CMAKE_BINARY_DIR}" "${_target_lib}" )

    if( TARGET ${TDELFEDITOR_EXECUTABLE} AND NOT _exclude_from_all )
      # create target for all metadata writes
      if( NOT TARGET tdelfeditor-write )
        add_custom_target( tdelfeditor-write
          WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
          DEPENDS ${TDELFEDITOR_EXECUTABLE}
          COMMENT "Write metadata to binaries..."
        )
      endif( )
      add_custom_target(
        ${_target}+metadata ALL
        COMMAND ${TDELFEDITOR_EXECUTABLE} -m ${_target_lib} ${ELF_EMBEDDING_METADATA} || true
        COMMAND ${TDELFEDITOR_EXECUTABLE} -e ${_target_lib} || true
        COMMENT "Storing SCM metadata in ${_target_path}"
        DEPENDS tdelfeditor-write
      )
      add_dependencies( tdelfeditor-write ${_target} )
    else( )
      add_custom_command(
        TARGET ${_target}
        POST_BUILD
        COMMAND ${TDELFEDITOR_EXECUTABLE} -m ${_target_lib} ${ELF_EMBEDDING_METADATA} || true
        COMMAND ${TDELFEDITOR_EXECUTABLE} -e ${_target_lib} || true
        COMMENT "Storing SCM metadata in ${_target_path}"
      )
      if( TARGET ${TDELFEDITOR_EXECUTABLE} )
        add_dependencies( ${_target} ${TDELFEDITOR_EXECUTABLE} )
      endif()
    endif( )
  endif( TDELFEDITOR_EXECUTABLE AND _soname )

endmacro( tde_add_library )


#################################################
#####
##### tde_add_kpart

macro( tde_add_kpart _target )
  tde_add_library( ${_target} ${ARGN} MODULE )
endmacro( tde_add_kpart )


#################################################
#####
##### tde_curdatetime

macro( tde_curdatetime result )
  if( TDE_PKG_DATETIME )
    set( ${result} ${TDE_PKG_DATETIME} )
  elseif( TDE_SCM_MODULE_DATETIME )
    set( ${result} ${TDE_SCM_MODULE_DATETIME} )
  else( )
    tde_execute_process( COMMAND "date" "-u" "+%m/%d/%Y %H:%M:%S" OUTPUT_VARIABLE ${result} )
    string( REGEX REPLACE "(..)/(..)/(....) (........).*" "\\1/\\2/\\3 \\4" ${result} ${${result}} )
  endif( )
endmacro( tde_curdatetime )


#################################################
#####
##### tde_add_executable

macro( tde_add_executable _arg_target )

  unset( _target )
  unset( _automoc )
  unset( _meta_includes )
  unset( _setuid )
  unset( _sources )
  unset( _cxx_features )
  unset( _destination )
  unset( _link )
  unset( _dependencies )
  unset( _storage )

  # metadata
  unset( _description )
  unset( _license )
  unset( _copyright )
  unset( _authors )
  unset( _product )
  unset( _organization )
  unset( _version )
  unset( _datetime )
  unset( _notes )

  # default metadata
  set( _product "Trinity Desktop Environment" )
  set( _version "${TDE_VERSION}" )
  if( TDE_PKG_VERSION )
    set( _version "${_version} (${TDE_PKG_VERSION})" )
  endif( )
  tde_curdatetime( _datetime )

  foreach( _arg ${ARGV} )

    # this variable help us to skip
    # storing unapropriate values (i.e. directives)
    unset( _skip_store )

    # found directive "AUTOMOC"
    if( "+${_arg}" STREQUAL "+AUTOMOC" )
      set( _skip_store 1 )
      set( _automoc 1 )
    endif( "+${_arg}" STREQUAL "+AUTOMOC" )

    # found directive "META_INCLUDES"
    if( "+${_arg}" STREQUAL "+META_INCLUDES" )
      set( _skip_store 1 )
      set( _storage "_meta_includes" )
    endif( )

    # found directive "SETUID"
    if( "+${_arg}" STREQUAL "+SETUID" )
      set( _skip_store 1 )
      set( _setuid 1 )
    endif( "+${_arg}" STREQUAL "+SETUID" )

    # found directive "SOURCES"
    if( "+${_arg}" STREQUAL "+SOURCES" )
      set( _skip_store 1 )
      set( _storage "_sources" )
    endif( "+${_arg}" STREQUAL "+SOURCES" )

    # found directive "CXX_FEATURES"
    if( "+${_arg}" STREQUAL "+CXX_FEATURES" )
      set( _skip_store 1 )
      set( _storage "_cxx_features" )
    endif( "+${_arg}" STREQUAL "+CXX_FEATURES" )

    # found directive "LINK"
    if( "+${_arg}" STREQUAL "+LINK" )
      set( _skip_store 1 )
      set( _storage "_link" )
    endif( "+${_arg}" STREQUAL "+LINK" )

    # found directive "DEPENDENCIES"
    if( "+${_arg}" STREQUAL "+DEPENDENCIES" )
      set( _skip_store 1 )
      set( _storage "_dependencies" )
    endif( "+${_arg}" STREQUAL "+DEPENDENCIES" )

    # found directive "DESTINATION"
    if( "+${_arg}" STREQUAL "+DESTINATION" )
      set( _skip_store 1 )
      set( _storage "_destination" )
      unset( ${_storage} )
    endif( "+${_arg}" STREQUAL "+DESTINATION" )

    # metadata
    if( "+${_arg}" STREQUAL "+DESCRIPTION" )
      set( _skip_store 1 )
      set( _storage "_description" )
    endif( )
    if( "+${_arg}" STREQUAL "+LICENSE" )
      set( _skip_store 1 )
      set( _storage "_license" )
    endif( )
    if( "+${_arg}" STREQUAL "+COPYRIGHT" )
      set( _skip_store 1 )
      set( _storage "_copyright" )
    endif( )
    if( "+${_arg}" STREQUAL "+AUTHORS" )
      set( _skip_store 1 )
      set( _storage "_authors" )
    endif( )
    if( "+${_arg}" STREQUAL "+PRODUCT" )
      set( _skip_store 1 )
      set( _storage "_product" )
    endif( )
    if( "+${_arg}" STREQUAL "+ORGANIZATION" )
      set( _skip_store 1 )
      set( _storage "_organization" )
    endif( )
    if( "+${_arg}" STREQUAL "+VERSION" )
      set( _skip_store 1 )
      set( _storage "_version" )
    endif( )
    if( "+${_arg}" STREQUAL "+DATETIME" )
      set( _skip_store 1 )
      set( _storage "_datetime" )
    endif( )
    if( "+${_arg}" STREQUAL "+NOTES" )
      set( _skip_store 1 )
      set( _storage "_notes" )
    endif( )

    # storing value
    if( _storage AND NOT _skip_store )
      #set( ${_storage} "${${_storage}} ${_arg}" )
      list( APPEND ${_storage} ${_arg} )
    endif( _storage AND NOT _skip_store )

  endforeach( _arg )

  set( _target "${_arg_target}" )

  # disallow target without sources
  if( NOT _sources )
    message( FATAL_ERROR "\nTarget [$_target] have no sources." )
  endif( NOT _sources )

  # processing different types of sources
  __tde_internal_process_sources( _sources ${_sources} )

  # set automoc
  if( _automoc )
    tde_automoc( ${_sources} )
  endif( _automoc )

  # add target
  add_executable( ${_target} ${_sources} )

  # set cxx features
  if( TDE_CXX_FEATURES OR PROJECT_CXX_FEATURES OR _cxx_features )
    target_compile_features( ${_target} PRIVATE
      ${TDE_CXX_FEATURES} ${PROJECT_CXX_FEATURES} ${_cxx_features} )
  endif( )

  # set link libraries
  if( _link )
    target_link_libraries( ${_target} ${_link} )
  endif( _link )

  # set dependencies
  if( _dependencies )
    add_dependencies( ${_target} ${_dependencies} )
  endif( _dependencies )

  # set PIE flags for setuid binaries
  if( _setuid )
    set_target_properties( ${_target} PROPERTIES COMPILE_FLAGS "${TDE_PIE_CFLAGS}" )
    set_target_properties( ${_target} PROPERTIES LINK_FLAGS "${TDE_PIE_LDFLAGS}" )
  endif( _setuid )

  # set destination directory
  if( _destination )
    if( _setuid )
      install( TARGETS ${_target} DESTINATION ${_destination} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE SETUID )
    else( _setuid )
      install( TARGETS ${_target} DESTINATION ${_destination} )
    endif( _setuid )
  endif( _destination )

  # embed icon, name, and metadata
  set( ELF_EMBEDDING_METADATA "\"${_target}\" \"${_description}\" \"${_license}\" \"${_copyright}\" \"${_authors}\" \"${_product}\" \"${_organization}\" \"${_version}\" \"${_datetime}\" \"${_target}\" \"${TDE_SCM_MODULE_NAME}\" \"${TDE_SCM_MODULE_REVISION}\" \"${_notes}\"" )
  separate_arguments( ELF_EMBEDDING_METADATA )
  if( TDELFEDITOR_EXECUTABLE )
    get_filename_component( _target_path ${CMAKE_CURRENT_BINARY_DIR}/${_target} ABSOLUTE )
    file( RELATIVE_PATH _target_path "${CMAKE_BINARY_DIR}" "${_target_path}" )
    if( TARGET ${TDELFEDITOR_EXECUTABLE} )
      # create target for all metadata writes
      if( NOT TARGET tdelfeditor-write )
        add_custom_target( tdelfeditor-write
          WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
          DEPENDS ${TDELFEDITOR_EXECUTABLE}
          COMMENT "Write metadata to binaries..."
        )
      endif( )
      add_custom_target(
        ${_target}+metadata ALL
        COMMAND ${TDELFEDITOR_EXECUTABLE} -m ${CMAKE_CURRENT_BINARY_DIR}/${_target} ${ELF_EMBEDDING_METADATA} || true
        COMMAND ${TDELFEDITOR_EXECUTABLE} -e ${CMAKE_CURRENT_BINARY_DIR}/${_target} || true
        COMMENT "Storing SCM metadata in ${_target_path}"
        DEPENDS tdelfeditor-write
      )
      add_dependencies( tdelfeditor-write ${_target} )
    else()
      add_custom_command(
        TARGET ${_target}
        POST_BUILD
        COMMAND ${TDELFEDITOR_EXECUTABLE} -m ${CMAKE_CURRENT_BINARY_DIR}/${_target} ${ELF_EMBEDDING_METADATA} || true
        COMMAND ${TDELFEDITOR_EXECUTABLE} -e ${CMAKE_CURRENT_BINARY_DIR}/${_target} || true
        COMMAND ${TDELFEDITOR_EXECUTABLE} -t ${CMAKE_CURRENT_BINARY_DIR}/${_target} ${_target} || true
        COMMENT "Storing SCM metadata in ${_target_path}"
      )
    endif()
  endif( TDELFEDITOR_EXECUTABLE )

endmacro( tde_add_executable )


#################################################
#####
##### tde_add_check_executable

macro( tde_add_check_executable _arg_target )

  unset( _target )
  unset( _automoc )
  unset( _test )
  unset( _test_args )
  unset( _meta_includes )
  unset( _sources )
  unset( _cxx_features )
  unset( _destination )
  unset( _link )
  unset( _dependencies )
  unset( _storage )

  foreach( _arg ${ARGV} )

    # this variable help us to skip
    # storing unapropriate values (i.e. directives)
    unset( _skip_store )

    # found directive "AUTOMOC"
    if( "+${_arg}" STREQUAL "+AUTOMOC" )
      set( _skip_store 1 )
      set( _automoc 1 )
    endif( "+${_arg}" STREQUAL "+AUTOMOC" )

    # found directive "TEST"
    if( "+${_arg}" STREQUAL "+TEST" )
      set( _skip_store 1 )
      set( _test 1 )
      set( _storage "_test_args" )
    endif( "+${_arg}" STREQUAL "+TEST" )

    # found directive "META_INCLUDES"
    if( "+${_arg}" STREQUAL "+META_INCLUDES" )
      set( _skip_store 1 )
      set( _storage "_meta_includes" )
    endif( )

    # found directive "SOURCES"
    if( "+${_arg}" STREQUAL "+SOURCES" )
      set( _skip_store 1 )
      set( _storage "_sources" )
    endif( "+${_arg}" STREQUAL "+SOURCES" )

    # found directive "CXX_FEATURES"
    if( "+${_arg}" STREQUAL "+CXX_FEATURES" )
      set( _skip_store 1 )
      set( _storage "_cxx_features" )
    endif( "+${_arg}" STREQUAL "+CXX_FEATURES" )

    # found directive "LINK"
    if( "+${_arg}" STREQUAL "+LINK" )
      set( _skip_store 1 )
      set( _storage "_link" )
    endif( "+${_arg}" STREQUAL "+LINK" )

    # found directive "DEPENDENCIES"
    if( "+${_arg}" STREQUAL "+DEPENDENCIES" )
      set( _skip_store 1 )
      set( _storage "_dependencies" )
    endif( "+${_arg}" STREQUAL "+DEPENDENCIES" )

    # storing value
    if( _storage AND NOT _skip_store )
      #set( ${_storage} "${${_storage}} ${_arg}" )
      list( APPEND ${_storage} ${_arg} )
    endif( _storage AND NOT _skip_store )

  endforeach( _arg )

  set( _target "${_arg_target}" )

  # try to autodetect sources
  if( NOT _sources )
    file( GLOB _sources "${_target}.cpp" "${_target}.cxx" "${_target}.c" )
    if( NOT _sources )
      message( FATAL_ERROR "\nNo sources found for test executable \"${_target}\"." )
    endif( )
  endif( NOT _sources )

  # processing different types of sources
  __tde_internal_process_sources( _sources ${_sources} )

  # set automoc
  if( _automoc )
    tde_automoc( ${_sources} )
  endif( _automoc )

  # add target
  add_executable( ${_target} EXCLUDE_FROM_ALL ${_sources} )

  # set cxx features
  if( TDE_CXX_FEATURES OR PROJECT_CXX_FEATURES OR _cxx_features )
    target_compile_features( ${_target} PRIVATE
      ${TDE_CXX_FEATURES} ${PROJECT_CXX_FEATURES} ${_cxx_features} )
  endif( )

  # set link libraries
  if( _link )
    target_link_libraries( ${_target} ${_link} )
  endif( _link )

  # set dependencies
  if( _dependencies )
    add_dependencies( ${_target} ${_dependencies} )
  endif( _dependencies )

  # create make check target
  if(NOT TARGET check)
      add_custom_target( check
        COMMAND ${CMAKE_CTEST_COMMAND}
        WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
        COMMENT "Running tests..." )
  endif(NOT TARGET check)

  add_dependencies( check ${_target} )

  # add test target
  if( _test )
    # get relative path to current directory and strip end tests dir
    file( RELATIVE_PATH _test_prefix ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
    string( REGEX REPLACE "(^\\.+/?|(^|/)tests?$|/$)" "" _test_prefix "${_test_prefix}" )
    if( _test_prefix )
        set( _test_prefix "${_test_prefix}/" )
    endif( _test_prefix )
    add_test( NAME "${_test_prefix}${_target}" COMMAND "${_target}" ${_test_args} )
  endif( _test )

endmacro( tde_add_check_executable )


#################################################
#####
##### tde_add_tdeinit_executable

macro( tde_add_tdeinit_executable _target )

  configure_file( ${TDE_CMAKE_TEMPLATES}/tde_tdeinit_executable.cmake ${_target}_tdeinit_executable.cpp COPYONLY )
  configure_file( ${TDE_CMAKE_TEMPLATES}/tde_tdeinit_module.cmake ${_target}_tdeinit_module.cpp COPYONLY )

  unset( _sources )
  unset( _runtime_destination )
  unset( _library_destination )
  unset( _plugin_destination )

  # default storage is _sources
  set( _storage _sources )

  # set default export to NO_EXPORT
  set( _export "NO_EXPORT" )

  foreach( _arg ${ARGN} )

    # this variable help us to skip
    # storing unapropriate values (i.e. directives)
    unset( _skip_store )

    # found directive "EXPORT"
    if( "+${_arg}" STREQUAL "+EXPORT" )
      set( _skip_store 1 )
      unset( _export )
    endif( "+${_arg}" STREQUAL "+EXPORT" )

    # found directive "RUNTIME_DESTINATION"
    if( "+${_arg}" STREQUAL "+RUNTIME_DESTINATION" )
      set( _skip_store 1 )
      set( _storage "_runtime_destination" )
      unset( ${_storage} )
    endif( "+${_arg}" STREQUAL "+RUNTIME_DESTINATION" )

    # found directive "LIBRARY_DESTINATION"
    if( "+${_arg}" STREQUAL "+LIBRARY_DESTINATION" )
      set( _skip_store 1 )
      set( _storage "_library_destination" )
      unset( ${_storage} )
    endif( "+${_arg}" STREQUAL "+LIBRARY_DESTINATION" )

    # found directive "PLUGIN_DESTINATION"
    if( "+${_arg}" STREQUAL "+PLUGIN_DESTINATION" )
      set( _skip_store 1 )
      set( _storage "_plugin_destination" )
      unset( ${_storage} )
    endif( "+${_arg}" STREQUAL "+PLUGIN_DESTINATION" )

    # storing value
    if( _storage AND NOT _skip_store )
      list( APPEND ${_storage} ${_arg} )
      set( _storage "_sources" )
    endif( _storage AND NOT _skip_store )

  endforeach( _arg )

  # if destinations are not set, we using some defaults
  # we assume that tdeinit executable MUST be installed
  # (otherwise why we build it?)
  if( NOT _runtime_destination )
    set( _runtime_destination ${BIN_INSTALL_DIR} )
  endif( NOT _runtime_destination )
  if( NOT _library_destination )
    set( _library_destination ${LIB_INSTALL_DIR} )
  endif( NOT _library_destination )
  if( NOT _plugin_destination )
    set( _plugin_destination ${PLUGIN_INSTALL_DIR} )
  endif( NOT _plugin_destination )

  # create the library
  tde_add_library( tdeinit_${_target} ${_sources} SHARED ${_export}
    DESTINATION ${_library_destination}
  )

  # create the executable
  tde_add_executable( ${_target}
    SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${_target}_tdeinit_executable.cpp
    LINK tdeinit_${_target}-shared
    DESTINATION ${_runtime_destination}
  )

  # create the plugin
  tde_add_kpart( ${_target}
    SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${_target}_tdeinit_module.cpp
    LINK tdeinit_${_target}-shared
    DESTINATION ${_plugin_destination}
  )

endmacro( tde_add_tdeinit_executable )


#################################################
#####
##### tde_conditional_add_project_translations
##### tde_add_project_translations
#####
##### Macro for standard processing and installation of translations.
##### This is designed for ordinary modules - as an applications, not for core modules.

function( tde_conditional_add_project_translations _cond )

  if( ${_cond} )
    tde_add_project_translations()
  endif()

endfunction()

function( tde_add_project_translations )

  if( ${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR} )
    set( TRANSLATIONS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/translations/messages )
  else()
    set( TRANSLATIONS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
  endif()

  file( GLOB_RECURSE po_files RELATIVE ${TRANSLATIONS_SOURCE_DIR} ${TRANSLATIONS_SOURCE_DIR}/*.po )
  string( REGEX REPLACE "[ \r\n\t]+" ";" _linguas "$ENV{LINGUAS}" )

  foreach( _po ${po_files} )
    get_filename_component( _lang ${_po} NAME_WE )
    if( "${_linguas}" MATCHES "^;*$" OR ";${_linguas};" MATCHES ";${_lang};" )
      if( "${_po}" MATCHES "^([^/]*)/.*" )
        string( REGEX REPLACE "^([^/]*)/.*" "\\1" _component "${_po}" )
      else( )
        set( _component "${PROJECT_NAME}" )
      endif( )
      tde_create_translation( FILES ${TRANSLATIONS_SOURCE_DIR}/${_po} LANG ${_lang} OUTPUT_NAME ${_component} )
    endif( )
  endforeach( )

endfunction()


#################################################
#####
##### tde_create_translation

macro( tde_create_translation )

  unset( _srcs )
  unset( _lang )
  unset( _dest )
  unset( _out_name )
  unset( _directive )
  unset( _var )

  foreach( _arg ${ARGN} )

    # found directive "FILES"
    if( "+${_arg}" STREQUAL "+FILES" )
      unset( _srcs )
      set( _var _srcs )
      set( _directive 1 )
    endif( )

    # found directive "LANG"
    if( "+${_arg}" STREQUAL "+LANG" )
      unset( _lang )
      set( _var _lang )
      set( _directive 1 )
    endif( )

    # found directive "DESTINATION"
    if( "+${_arg}" STREQUAL "+DESTINATION" )
      unset( _dest )
      set( _var _dest )
      set( _directive 1 )
    endif( )

    # found directive "OUTPUT_NAME"
    if( "+${_arg}" STREQUAL "+OUTPUT_NAME" )
      unset( _out_name )
      set( _var _out_name )
      set( _directive 1 )
    endif( )

    # collect data
    if( _directive )
      unset( _directive )
    elseif( _var )
      list( APPEND ${_var} ${_arg} )
    endif()

  endforeach( )

  if( NOT MSGFMT_EXECUTABLE )
    tde_setup_msgfmt( )
  endif( )
  if( NOT _lang )
    tde_message_fatal( "missing LANG directive" )
  endif( )

  # if no file specified, include all *.po files
  if( NOT _srcs )
    file( GLOB _srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.po  )
  endif( )
  if( NOT _srcs )
    tde_message_fatal( "no source files" )
  endif( )

  if( NOT _lang STREQUAL "auto")
    set( _real_lang ${_lang} )

    if( NOT _dest )
      set( _dest "${LOCALE_INSTALL_DIR}/${_lang}/LC_MESSAGES" )
    endif( )

    # OUTPUT_NAME can only be used if we have only one file
    list( LENGTH _srcs _srcs_num)
    if( _out_name AND _srcs_num GREATER 1 )
      tde_message_fatal( "OUTPUT_NAME can be supplied only with single file or LANG=auto" )
    endif( )

  elseif( NOT _out_name )
    tde_message_fatal( "LANG=auto reqires OUTPUT_NAME directive to be set" )
  elseif( _dest )
    tde_message_fatal( "DESTINATION cannot be used with LANG=auto" )
  endif( )

  # generate *.mo files
  foreach( _src ${_srcs} )

    get_filename_component( _src ${_src} ABSOLUTE )

    if( _out_name )
      set( _out ${_out_name} )
      if( _lang STREQUAL "auto" )
        get_filename_component( _real_lang ${_src} NAME_WE )
        set( _dest "${LOCALE_INSTALL_DIR}/${_real_lang}/LC_MESSAGES" )
      endif( )
    else( )
      get_filename_component( _out ${_src} NAME_WE )
    endif( )

    string( REPLACE "@" "_" _target ${_real_lang} )
    set( _out_filename "${_out}-${_real_lang}.mo" )
    set( _install_filename "${_out}.mo" )

    add_custom_command(
      OUTPUT ${_out_filename}
      COMMAND ${MSGFMT_EXECUTABLE} ${_src} -o ${_out_filename}
      DEPENDS ${_src} )
    add_custom_target( "${_out}-${_target}-translation" ALL DEPENDS ${_out_filename} )
    install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${_out_filename} RENAME ${_install_filename} DESTINATION ${_dest} )

  endforeach( )

endmacro( )


#################################################
#####
##### tde_create_translated_desktop
#####
##### Macro is used to merge translations into desktop file
#####
##### Syntax:
#####   tde_create_translated_desktop(
#####     [SOURCE] file_name
#####     [KEYWORDS keyword [keyword]]
#####     [FORMAT (desktop|xml)]
#####     [PO_DIR po_directory]
#####     [DESTINATION directory]
#####     [OUTPUT_NAME file_name]
#####   )

macro( tde_create_translated_desktop )

  unset( _srcs )
  unset( _arg_out_name )
  unset( _arg_po_dir )
  unset( _keywords_add )
  unset( _dest )
  unset( _directive )
  set( _var _srcs )
  set( _keywords_desktop_default
       "Name" "GenericName" "Comment" "Keywords"
       "Description" "ExtraNames" "X-TDE-Submenu" )
  set( _format "desktop" )

  foreach( _arg ${ARGN} )

    # found directive "SOURCE"
    if( "+${_arg}" STREQUAL "+SOURCE" )
      unset( _srcs )
      set( _var _srcs )
      set( _directive 1 )
    endif( )

    # found directive "KEYWORDS"
    if( "+${_arg}" STREQUAL "+KEYWORDS" )
      unset( _keywords_add )
      set( _var _keywords_add )
      set( _directive 1 )
    endif( )

    # found directive "FORMAT"
    if( "+${_arg}" STREQUAL "+FORMAT" )
      unset( _format )
      set( _var _format )
      set( _directive 1 )
    endif( )

    # found directive "PO_DIR"
    if( "+${_arg}" STREQUAL "+PO_DIR" )
      unset( _arg_po_dir )
      set( _var _arg_po_dir )
      set( _directive 1 )
    endif( )

    # found directive "DESTINATION"
    if( "+${_arg}" STREQUAL "+DESTINATION" )
      unset( _dest )
      set( _var _dest )
      set( _directive 1 )
    endif( )

    # found directive "OUTPUT_NAME"
    if( "+${_arg}" STREQUAL "+OUTPUT_NAME" )
      unset( _arg_out_name )
      set( _var _arg_out_name )
      set( _directive 1 )
    endif( )

    # collect data
    if( _directive )
      unset( _directive )
    elseif( _var )
      list( APPEND ${_var} ${_arg} )
    endif()

  endforeach( )

  # no source file specified!
  if( NOT _srcs )
    tde_message_fatal( "no source desktop file specified" )
  endif( )

  # OUTPUT_NAME can only be used if we have only one file
  list( LENGTH _srcs _srcs_num )
  if( _arg_out_name AND _srcs_num GREATER 1 )
    tde_message_fatal( "OUTPUT_NAME can be supplied only with single file" )
  endif( )

  # if no destination directory specified, install as application link
  if( NOT _dest )
    set( _dest ${XDG_APPS_INSTALL_DIR} )
  endif( )

  # select a tool for merging desktop file translations
  #
  # Because some of our desktop files contain underscores in variable
  # names (for example eventsrc), which is not an allowed character
  # for names of entries in desktop style files, we can't use msgfmt,
  # so we need intltool-merge.
  #
  #if( NOT MSGFMT_EXECUTABLE OR NOT MSGFMT_VERSION )
  #  tde_setup_msgfmt( )
  #endif( )
  #if( "${MSGFMT_VERSION}" VERSION_LESS "0.19" )
  if( TRUE )
    if( NOT PERL_EXECUTABLE )
      include( FindPerl )
    endif( )
    if( NOT INTLTOOL_MERGE_EXECUTABLE )
      find_file( INTLTOOL_MERGE_EXECUTABLE
        NAMES tde_l10n_merge.pl
        HINTS ${TDE_CMAKE_MODULES}
      )
      if( "${INTLTOOL_MERGE_EXECUTABLE}" STREQUAL "INTLTOOL_MERGE_EXECUTABLE-NOTFOUND" )
        #tde_message_fatal( "xgettext >= 0.19 or tde_l10n_merge.pl is required but not found" )
        tde_message_fatal( "tde_l10n_merge.pl is required but not found" )
      endif( )
      message( STATUS "Found intltool: ${INTLTOOL_MERGE_EXECUTABLE}" )
    endif( )
    set( DESKTOP_MERGE_INTLTOOL 1 )
  else( )
    set( DESKTOP_MERGE_MSGFMT 1 )
  endif( )

  # pick keywords
  unset( _keywords_desktop )
  foreach( _keyword ${_keywords_desktop_default} ${_keywords_add} )
    if( "${_keyword}" STREQUAL "-" )
      unset( _keywords_desktop )
      unset( _keyword )
    endif( )
    if( _keyword )
      list( APPEND _keywords_desktop "${_keyword}" )
    endif( )
  endforeach( )

  # prepare the length of the binary path prefix
  string( LENGTH "${CMAKE_BINARY_DIR}" CMAKE_BINARY_DIR_LEN )

  # process source files
  foreach( _src IN LISTS _srcs )

    # get a base name and a directory
    get_filename_component( _basename ${_src} ABSOLUTE )
    get_filename_component( _basedir ${_basename} PATH )
    file( RELATIVE_PATH _sourcename "${CMAKE_SOURCE_DIR}" "${_basename}" )
    string( LENGTH "${_basename}" _basename_len )
    if( ${_basename_len} LESS ${CMAKE_BINARY_DIR_LEN} )
      set( _basedir_prefix "${CMAKE_SOURCE_DIR}" )
    else( )
      string( SUBSTRING "${_basename}" 0 ${CMAKE_BINARY_DIR_LEN} _basedir_prefix )
    endif( )
    if( ${_basedir_prefix} STREQUAL "${CMAKE_BINARY_DIR}" )
      file( RELATIVE_PATH _basename "${CMAKE_CURRENT_BINARY_DIR}" "${_basename}" )
      set( _binsuffix ".out" )
    else( )
      file( RELATIVE_PATH _basename "${CMAKE_CURRENT_SOURCE_DIR}" "${_basename}" )
      set( _binsuffix "" )
    endif( )

    # prepare the binary directory according to source directory
    if( ${_basedir_prefix} STREQUAL "${CMAKE_BINARY_DIR}" )
      file( RELATIVE_PATH _binary_basedir "${CMAKE_CURRENT_BINARY_DIR}" "${_basedir}" )
    else( )
      file( RELATIVE_PATH _binary_basedir "${CMAKE_CURRENT_SOURCE_DIR}" "${_basedir}" )
    endif( )
    set( _binary_basedir "${CMAKE_CURRENT_BINARY_DIR}/${_binary_basedir}" )
    file( MAKE_DIRECTORY "${_binary_basedir}" )

    # process source file as a configuration file if necessary
    if( "+${_src}" MATCHES "\\.cmake$" )
      configure_file( ${_src} ${_basename} @ONLY )
      set( _src "${CMAKE_CURRENT_BINARY_DIR}/${_basename}" )
      string( REGEX REPLACE "\\.cmake$" "" _basename "${_basename}" )
    endif()

    # determine output name
    if( _arg_out_name )
      set( _out_name ${_arg_out_name} )
    else()
      get_filename_component( _out_name ${_basename} NAME )
    endif( )

    # determine po directory
    if( _arg_po_dir )
      set( _po_base ${_arg_po_dir} )
    else()
      get_filename_component( _po_base ${_basename} NAME )
    endif()
    if( IS_ABSOLUTE ${_po_base} )
      set( _po_dir ${_po_base} )
    else()
      if( EXISTS ${CMAKE_SOURCE_DIR}/translations/desktop_files/${_po_base} AND
          IS_DIRECTORY ${CMAKE_SOURCE_DIR}/translations/desktop_files/${_po_base} )
        set( _po_dir ${CMAKE_SOURCE_DIR}/translations/desktop_files/${_po_base} )

      elseif( EXISTS ${CMAKE_SOURCE_DIR}/po/desktop_files/${_po_base} AND
              IS_DIRECTORY ${CMAKE_SOURCE_DIR}/po/desktop_files/${_po_base} )
        set( _po_dir ${CMAKE_SOURCE_DIR}/po/desktop_files/${_po_base} )

      else()
        set( _po_dir ${CMAKE_SOURCE_DIR}/translations/desktop_files )
      endif( )
    endif( )

    # if the translated desktop file is not installed, generate to the specified output name
    if( "${_dest}" STREQUAL "-" )
      set( _basename "${_out_name}" )
      set( _binsuffix "" )
      get_filename_component( _out_dir "${CMAKE_CURRENT_BINARY_DIR}/${_out_name}" PATH )
      file( MAKE_DIRECTORY "${_out_dir}" )
    endif( )

    # are there any translations available?
    unset( _translations )
    if( EXISTS "${_po_dir}" AND IS_DIRECTORY "${_po_dir}" )
      file( GLOB _translations RELATIVE "${_po_dir}" "${_po_dir}/*.po" )
    endif( )

    # prepare a full name for the target
    get_filename_component( _target ${_basename} ABSOLUTE )
    file( RELATIVE_PATH _target "${CMAKE_SOURCE_DIR}" "${_target}-translated" )
    string( REPLACE "/" "+" _target "${_target}" )
    string( REPLACE "@" "_" _target "${_target}" )

    if( NOT TARGET ${_target} )

      # use absolute path for src
      get_filename_component( _src ${_src} ABSOLUTE )

      if( _translations )

        if( DESKTOP_MERGE_MSGFMT )

          # Decide which translations to build; the ones selected in the
          # LINGUAS environment variable, or all that are available.
          if( DEFINED ENV{LINGUAS} )
            set( _linguas "$ENV{LINGUAS}" )
          else( )
            string( REPLACE ".po;" " " _linguas "${_translations};" )
          endif( )

          # prepare keywords for msgfmt
          set( _keywords_arg "--keyword=" )
          foreach( _keyword ${_keywords_desktop} )
            list( APPEND _keywords_arg "--keyword=\"${_keyword}\"" )
          endforeach( )

          # merge translations command
          add_custom_command(
            OUTPUT ${_basename}${_binsuffix}
            COMMAND ${CMAKE_COMMAND} -E env "LINGUAS=${_linguas}" ${MSGFMT_EXECUTABLE} --desktop --template ${_src} -d ${_po_dir} -o ${_basename}${_binsuffix} ${_keywords_arg}
            DEPENDS ${_src}
            COMMENT "Merging translations into ${_sourcename}"
          )

        else( )

          # prepare keywords for intltool
          string( REPLACE ";" "|" _keywords_match "(${_keywords_desktop})" )

          # merge translations command
          if( _format STREQUAL "desktop" )
            add_custom_command(
              OUTPUT ${_basename}${_binsuffix}
              COMMAND ${PERL_EXECUTABLE} -p -e  "'s/^${_keywords_match}[ ]*=[ ]*/_\\1=/'" < ${_src} > ${_basename}.in
              COMMAND ${PERL_EXECUTABLE} ${INTLTOOL_MERGE_EXECUTABLE} -q -d ${_po_dir} ${_basename}.in ${_basename}${_binsuffix}
              DEPENDS ${_src}
              COMMENT "Merging translations into ${_sourcename}"
            )
          elseif( _format STREQUAL "xml" )
            add_custom_command(
              OUTPUT ${_basename}${_binsuffix}
              COMMAND ${PERL_EXECUTABLE} -p -e  "'s/(<\\/?)${_keywords_match}([ >])/\\1_\\2\\3/g'" < ${_src} > ${_basename}.in
              COMMAND ${PERL_EXECUTABLE} ${INTLTOOL_MERGE_EXECUTABLE} -q -x ${_po_dir} ${_basename}.in ${_basename}${_binsuffix}
              DEPENDS ${_src}
              COMMENT "Merging translations into ${_sourcename}"
            )
          else()
            tde_message_fatal( "Unknown file format for merging translations." )
          endif()

        endif( )


      else( )

        # just copy the original file without translations
        add_custom_command(
          OUTPUT ${_basename}${_binsuffix}
          COMMAND ${CMAKE_COMMAND} -E copy ${_src} ${_basename}${_binsuffix}
          DEPENDS ${_src}
          COMMENT "Skiping translation and copying source file to ${_sourcename}"
        )

      endif( )

      # merge translations target
      add_custom_target( "${_target}" ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_basename}${_binsuffix} )

    endif( )

    # install traslated desktop file
    if( NOT "${_dest}" STREQUAL "-" )
      install(
        FILES ${CMAKE_CURRENT_BINARY_DIR}/${_basename}${_binsuffix}
        RENAME ${_out_name}
        DESTINATION ${_dest}
      )
    endif()

  endforeach()

endmacro( )


#################################################
#####
##### tde_conditional_add_project_docs
##### tde_add_project_docs
#####
##### Macro for standard processing and installation of documentation and man pages.
##### This is designed for ordinary modules - as an applications, not for core modules.

function( tde_conditional_add_project_docs _cond )

  if( ${_cond} )
    tde_add_project_docs()
  endif()

endfunction()

function( tde_add_project_docs )

  if( ${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR} )
    set( DOCS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/doc )
  else()
    set( DOCS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
  endif()

  file( GLOB_RECURSE _doc_files RELATIVE ${DOCS_SOURCE_DIR} ${DOCS_SOURCE_DIR}/* )
  foreach( _doc_file IN LISTS _doc_files )
    get_filename_component( _dir ${_doc_file} PATH )
    list( APPEND _dirs ${_dir} )
  endforeach()
  if( _dirs )
    list( SORT _dirs )
    list( REMOVE_DUPLICATES _dirs )
  endif()

  string( REGEX REPLACE "[ \r\n\t]+" ";" _linguas "$ENV{LINGUAS}" )

  unset( _skip_subdir )
  foreach( _dir IN LISTS _dirs )
    string( REGEX REPLACE "/.*" "" _lang ${_dir} )
    if( NOT ${_lang} MATCHES "^(html|man|misc|other)$"
        AND ( NOT DEFINED _skip_subdir OR
              NOT ${_dir} MATCHES "^${_skip_subdir}/" )
        AND ( ${_lang} STREQUAL "en" OR
              "${_linguas}" MATCHES "^;*$" OR
              ";${_linguas};" MATCHES ";${_lang};" ))
      if( EXISTS ${DOCS_SOURCE_DIR}/${_dir}/CMakeLists.txt )
        set( _skip_subdir ${_dir} )
        add_subdirectory( ${DOCS_SOURCE_DIR}/${_dir} )
      else()
        unset( _skip_subdir )
        if( ${_dir} MATCHES "/[^/]*/" )
          string( REGEX REPLACE "^[^/]*/(.*)" "\\1" _doc_dest "${_dir}" )
        else()
          string( REGEX REPLACE "^[^/]*/(.*)" "\\1" _doc_dest "${_dir}/${PROJECT_NAME}" )
        endif()
        file( GLOB _doc_files RELATIVE ${DOCS_SOURCE_DIR}/${_dir} ${DOCS_SOURCE_DIR}/${_dir}/*.docbook )
        if( _doc_files )
          list( FIND _doc_files "index.docbook" _find_index )
          if( -1 EQUAL _find_index )
            set( _noindex "NOINDEX" )
          else()
            unset( _noindex )
          endif()
          tde_create_handbook(
            SOURCE_BASEDIR ${DOCS_SOURCE_DIR}/${_dir}
            ${_noindex}
            LANG ${_lang}
            DESTINATION ${_doc_dest}
          )
        else()
          file( GLOB _html_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${DOCS_SOURCE_DIR}/${_dir}/*.html )
          if( _html_files )
            file( GLOB _htmldoc_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
                  ${DOCS_SOURCE_DIR}/${_dir}/*.css
                  ${DOCS_SOURCE_DIR}/${_dir}/*.gif
                  ${DOCS_SOURCE_DIR}/${_dir}/*.jpg
                  ${DOCS_SOURCE_DIR}/${_dir}/*.png
            )
            install(
              FILES ${_html_files} ${_htmldoc_files}
              DESTINATION ${HTML_INSTALL_DIR}/${_lang}/${_doc_dest}
            )
          endif()
        endif()
      endif()
    endif()
  endforeach()

  if( EXISTS ${DOCS_SOURCE_DIR}/man AND
      NOT EXISTS ${DOCS_SOURCE_DIR}/man/CMakeLists.txt )
    file( GLOB_RECURSE _man_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${DOCS_SOURCE_DIR}/man/* )
    foreach( _man_file IN LISTS _man_files )
      if( ${_man_file} MATCHES "\\.[0-9]$" )
        string( REGEX REPLACE ".*\\.([0-9])$" "\\1" _man_section "${_man_file}" )
        list( APPEND _man_files_${_man_section} "${_man_file}" )
        list( APPEND _man_sections "${_man_section}" )
      endif()
    endforeach()
    foreach( _man_section IN LISTS _man_sections )
      INSTALL(
          FILES ${_man_files_${_man_section}}
          DESTINATION ${MAN_INSTALL_DIR}/man${_man_section}
          COMPONENT doc
      )
    endforeach()
  endif()

  if( EXISTS ${DOCS_SOURCE_DIR}/misc AND
      NOT EXISTS ${DOCS_SOURCE_DIR}/misc/CMakeLists.txt )
    install(
        DIRECTORY ${DOCS_SOURCE_DIR}/misc/
        DESTINATION ${SHARE_INSTALL_PREFIX}/doc/${PROJECT_NAME}
        COMPONENT doc
        PATTERN Makefile.am EXCLUDE
    )
  endif()

  foreach( _dir html man misc other )
    if( EXISTS ${DOCS_SOURCE_DIR}/${_dir}/CMakeLists.txt )
      add_subdirectory( ${DOCS_SOURCE_DIR}/${_dir} )
    endif()
  endforeach()

endfunction( )


#################################################
#####
##### tde_create_handbook

macro( tde_create_handbook )

  unset( _target )
  unset( _dest )
  unset( _noindex )
  unset( _srcs )
  unset( _extra )
  unset( _srcdir )

  get_filename_component( _source_basedir "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE )
  set( _lang en )
  set( _first_arg 1 )
  set( _var _target )

  foreach( _arg ${ARGN} )

    # found directive "SOURCE_BASEDIR"
    if( "+${_arg}" STREQUAL "+SOURCE_BASEDIR" )
      unset( _source_basedir )
      set( _var _source_basedir )
      set( _directive 1 )
    endif()

    # found directive "NOINDEX"
    if( "+${_arg}" STREQUAL "+NOINDEX" )
      set( _noindex 1 )
      set( _directive 1 )
    endif()

    # found directive "FILES"
    if( "+${_arg}" STREQUAL "+FILES" )
      unset( _srcs )
      set( _var _srcs )
      set( _directive 1 )
    endif()

    # found directive "EXTRA"
    if( "+${_arg}" STREQUAL "+EXTRA" )
      unset( _extra )
      set( _var _extra )
      set( _directive 1 )
    endif()

    # found directive "SRCDIR"
    if( "+${_arg}" STREQUAL "+SRCDIR" )
      unset( _srcdir )
      set( _var _srcdir )
      set( _directive 1 )
    endif()

    # found directive DESTINATION
    if( "+${_arg}" STREQUAL "+DESTINATION" )
      unset( _dest )
      set( _var _dest )
      set( _directive 1 )
    endif()

    # found directive "LANG"
    if( "+${_arg}" STREQUAL "+LANG" )
      unset( _lang )
      set( _var _lang )
      set( _directive 1 )
    endif()

    # collect data
    if( _directive )
      unset( _directive )
    elseif( _var )
      if( _first_arg )
        set( _target "${_arg}" )
      else()
        list( APPEND ${_var} ${_arg} )
      endif()
    endif()

    unset( _first_arg )

  endforeach()

  # if source_basedir is relative, complete the path to absolute
  if( NOT IS_ABSOLUTE ${_source_basedir} )
    get_filename_component( _source_basedir "${_source_basedir}" ABSOLUTE )
  endif()

  # prepare the binary directory according to source_basedir
  file( RELATIVE_PATH _binary_basedir "${CMAKE_CURRENT_SOURCE_DIR}" "${_source_basedir}" )
  set( _binary_basedir "${CMAKE_CURRENT_BINARY_DIR}/${_binary_basedir}" )
  file( MAKE_DIRECTORY "${_binary_basedir}" )

  # if no target specified, try to guess it from DESTINATION
  if( NOT _target )
    if( NOT _dest )
      tde_message_fatal( "target name cannot be determined because DESTINATION is not set" )
    endif()
    string( REPLACE "/" "-" _target "${_dest}" )
  endif()

  set( _target "${_target}-${_lang}-handbook" )

  # if sources are listed, complete the path to absolute
  if( _srcs )
    foreach( _src ${_srcs} )
      if( NOT IS_ABSOLUTE ${_src} )
        list( REMOVE_ITEM _srcs ${_src} )
        get_filename_component( _src "${_source_basedir}/${_src}" ABSOLUTE )
        list( APPEND _srcs ${_src} )
      endif()
    endforeach()
  endif()

  # if no file specified, include all docbooks, stylesheets and images
  if( NOT _srcs )
    file( GLOB _srcs
      ${_source_basedir}/*.docbook
      ${_source_basedir}/*.css
      ${_source_basedir}/*.gif
      ${_source_basedir}/*.jpg
      ${_source_basedir}/*.png
    )
  endif()

  # if no destination specified, defaulting to HTML_INSTALL_DIR
  if( NOT _dest )
    set( _dest "${HTML_INSTALL_DIR}/${_lang}" )
  # if destination is NOT absolute path,
  # we assume that is relative to HTML_INSTALL_DIR
  elseif( NOT IS_ABSOLUTE ${_dest} )
    set( _dest "${HTML_INSTALL_DIR}/${_lang}/${_dest}" )
  endif()

  if( NOT _srcs )
    tde_message_fatal( "no source files" )
  endif()

  if( NOT _noindex )

    # check for index.docbook
    list( FIND _srcs "${_source_basedir}/index.docbook" _find_index )
    if( -1 EQUAL _find_index )
      tde_message_fatal( "missing index.docbook file" )
    endif()

    # check for srcdir
    if( _srcdir )
      set( _srcdir "--srcdir=${_srcdir}" )
    endif()

    add_custom_command(
      OUTPUT ${_binary_basedir}/index.cache.bz2
      COMMAND ${KDE3_MEINPROC_EXECUTABLE} ${_srcdir} --check --cache index.cache.bz2 ${_source_basedir}/index.docbook
      COMMENT "Generating ${_target}"
      DEPENDS ${_srcs}
      WORKING_DIRECTORY "${_binary_basedir}"
    )

    string( REPLACE "@" "_" _target "${_target}" )
    add_custom_target( ${_target} ALL DEPENDS ${_binary_basedir}/index.cache.bz2 )

    list( APPEND _srcs ${_binary_basedir}/index.cache.bz2 )

    if( NOT TDE_HTML_DIR )
      set( TDE_HTML_DIR ${HTML_INSTALL_DIR} )
    endif( )

    tde_install_empty_directory( ${_dest} )
    file( RELATIVE_PATH _common_link_target "${_dest}" "${TDE_HTML_DIR}/${_lang}/common" )
    tde_install_symlink( ${_common_link_target} ${_dest} )

  endif()

  install( FILES ${_srcs} ${_extra} DESTINATION ${_dest} )

endmacro( )


#################################################
#####
##### tde_create_tarball
#####
##### Macro is used to create tarball.
#####

macro( tde_create_tarball )

  unset( _target )
  unset( _files )
  unset( _destination )
  set( _sourcedir "${CMAKE_CURRENT_SOURCE_DIR}" )
  set( _compression "gzip" )
  set( _var _target )

  foreach( _arg ${ARGN} )

    # found directive "TARGET"
    if( "+${_arg}" STREQUAL "+TARGET" )
      unset( _target )
      set( _var _target )
      set( _directive 1 )
    endif( )

    # found directive "SOURCEDIR"
    if( "+${_arg}" STREQUAL "+SOURCEDIR" )
      unset( _sourcedir )
      set( _var _sourcedir )
      set( _directive 1 )
    endif( )

    # found directive "FILES"
    if( "+${_arg}" STREQUAL "+FILES" )
      unset( _files )
      set( _var _files )
      set( _directive 1 )
    endif( )

    # found directive "DESTINATION"
    if( "+${_arg}" STREQUAL "+DESTINATION" )
      unset( _destination )
      set( _var _destination )
      set( _directive 1 )
    endif( )

    # found directive "COMPRESSION"
    if( "+${_arg}" STREQUAL "+COMPRESSION" )
      unset( _compression )
      set( _var _compression )
      set( _directive 1 )
    endif( )

    # collect data
    if( _directive )
      unset( _directive )
    elseif( _var )
      list( APPEND ${_var} ${_arg} )
    endif( )

  endforeach( )

  if( NOT _target )
    tde_message_fatal( "Target tarball name not specified." )
  endif( )

  if( NOT IS_ABSOLUTE ${_sourcedir} )
    set( _sourcedir "${CMAKE_CURRENT_SOURCE_DIR}/${_sourcedir}" )
  endif( )

  if( NOT _files )
    file( GLOB_RECURSE _files RELATIVE ${_sourcedir} "${_sourcedir}/*" )
    list( SORT _files )
  endif( )

  unset( _files_deps )
  foreach( _file ${_files} )
    list( APPEND _files_deps "${_sourcedir}/${_file}" )
  endforeach( )

  if( NOT DEFINED TAR_EXECUTABLE )
    find_program( TAR_EXECUTABLE NAMES tar )
    if( "${TAR_EXECUTABLE}" STREQUAL "TAR_EXECUTABLE-NOTFOUND" )
      tde_message_fatal( "tar executable is required but not found on your system" )
    endif( )
  endif( )

  if( NOT DEFINED TAR_SETOWNER )
    execute_process(
        COMMAND ${TAR_EXECUTABLE} --version
        OUTPUT_VARIABLE TAR_VERSION
    )
    string( REGEX REPLACE "^([^\n]*)\n.*" "\\1" TAR_VERSION "${TAR_VERSION}" )
    if( "${TAR_VERSION}" MATCHES "GNU *tar" )
      set( TAR_SETOWNER "--owner=root;--group=root" )
      set( TAR_REPRODUCIBLE "--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime" )
      list( APPEND TAR_REPRODUCIBLE "--mode=u+rw,go=rX,a-s" )
      tde_read_src_metadata()
      if( TDE_PKG_DATETIME )
        list( APPEND TAR_REPRODUCIBLE --mtime "${TDE_PKG_DATETIME} UTC" )
      elseif( TDE_SCM_MODULE_DATETIME )
        list( APPEND TAR_REPRODUCIBLE --mtime "${TDE_SCM_MODULE_DATETIME} UTC" )
      endif( )
    elseif( "${TAR_VERSION}" MATCHES "bsd *tar" )
      set( TAR_SETOWNER "--uname=root;--gname=root" )
    else( )
      set( TAR_SETOWNER "" )
    endif( )
  endif( )

  if( "${_compression}" STREQUAL "-" )
    unset( _compression )
  endif( )
  if( _compression )
    if( "${_compression}" STREQUAL "gzip" )
      set( TAR_COMPRESSION "|" ${_compression} "-n" )
    else( )
      set( TAR_COMPRESSION "|" ${_compression} )
    endif( )
  endif( )

  get_filename_component( _target_path "${CMAKE_CURRENT_BINARY_DIR}/${_target}" ABSOLUTE )
  file( RELATIVE_PATH _target_path "${CMAKE_BINARY_DIR}" "${_target_path}" )
  string( REPLACE "/" "+" _target_name "${_target_path}" )
  add_custom_target( "${_target_name}-tarball" ALL
    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_target}" )

  add_custom_command(
    COMMAND ${TAR_EXECUTABLE} cf -
        ${TAR_SETOWNER} ${TAR_REPRODUCIBLE} -- ${_files}
        ${TAR_COMPRESSION} > ${CMAKE_CURRENT_BINARY_DIR}/${_target}
    WORKING_DIRECTORY "${_sourcedir}"
    OUTPUT  "${CMAKE_CURRENT_BINARY_DIR}/${_target}"
    DEPENDS ${_files_deps}
    COMMENT "Create tarball ${_target_path}"
  )

  if( _destination )
    install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${_target} DESTINATION ${_destination} )
  endif( )

endmacro()


#################################################
#####
##### tde_include_tqt

macro( tde_include_tqt )
  foreach( _cpp ${ARGN} )
    set_source_files_properties( ${_cpp} PROPERTIES COMPILE_FLAGS "-include tqt.h" )
  endforeach()
endmacro( )


#################################################
#####
##### tde_install_symlink

macro( tde_install_symlink _target _link )

  # if path is relative, we must to prefix it with CMAKE_INSTALL_PREFIX
  if( IS_ABSOLUTE "${_link}" )
    set( _destination "${_link}" )
  else( IS_ABSOLUTE "${_link}" )
    set( _destination "${CMAKE_INSTALL_PREFIX}/${_link}" )
  endif( IS_ABSOLUTE "${_link}" )

  get_filename_component( _path "${_destination}" PATH )
  if( NOT IS_DIRECTORY "\$ENV{DESTDIR}${_path}" )
    install( CODE "file( MAKE_DIRECTORY \"\$ENV{DESTDIR}${_path}\" )" )
  endif( NOT IS_DIRECTORY "\$ENV{DESTDIR}${_path}" )

  install( CODE "execute_process( COMMAND ln -s ${_target} \$ENV{DESTDIR}${_destination} )" )

endmacro( tde_install_symlink )


#################################################
#####
##### tde_install_empty_directory

macro( tde_install_empty_directory _path )

  # if path is relative, we must to prefix it with CMAKE_INSTALL_PREFIX
  if( IS_ABSOLUTE "${_path}" )
    set( _destination "${_path}" )
  else( IS_ABSOLUTE "${_path}" )
    set( _destination "${CMAKE_INSTALL_PREFIX}/${_path}" )
  endif( IS_ABSOLUTE "${_path}" )

  install( CODE "file( MAKE_DIRECTORY \"\$ENV{DESTDIR}${_destination}\" )" )

endmacro( tde_install_empty_directory )


#################################################
#####
##### tde_conditional_add_subdirectory

macro( tde_conditional_add_subdirectory _cond _path )

  if( ${_cond} )
    add_subdirectory( "${_path}" )
  endif( ${_cond} )

endmacro( tde_conditional_add_subdirectory )


#################################################
#####
##### tde_auto_add_subdirectories

macro( tde_auto_add_subdirectories )
  file( GLOB _dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} * )
  foreach( _dir ${_dirs} )
    if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${_dir} )
      if( NOT ${_dir} STREQUAL ".svn" AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_dir}/CMakeLists.txt )
        add_subdirectory( ${_dir} )
      endif()
    endif()
  endforeach()
endmacro()


#################################################
#####
##### tde_save / tde_restore

macro( tde_save )
  foreach( _var ${ARGN} )
    set( __bak_${_var} ${${_var}} )
  endforeach()
endmacro()

macro( tde_save_and_set _var )
  set( __bak_${_var} ${${_var}} )
  set( ${_var} ${ARGN} )
endmacro( )

macro( tde_restore )
  foreach( _var ${ARGN} )
    set( ${_var} ${__bak_${_var}} )
    unset( __bak_${_var} )
  endforeach()
endmacro()


#################################################
#####
##### tde_setup_install_path

macro( tde_setup_install_path _path _default )
  if( DEFINED ${_path} )
    set( ${_path} "${${_path}}" CACHE INTERNAL "" FORCE )
  else( )
    set( ${_path} "${_default}" )
  endif( )
endmacro( )


##################################################

if( ${CMAKE_SOURCE_DIR} MATCHES ${CMAKE_BINARY_DIR} )
    tde_message_fatal( "Please use out-of-source building, like this:
 \n   rm ${CMAKE_SOURCE_DIR}/CMakeCache.txt
   mkdir /tmp/${PROJECT_NAME}.build
   cd /tmp/${PROJECT_NAME}.build
   cmake ${CMAKE_SOURCE_DIR} [arguments...]" )
endif( )

#################################################
#####
##### tde_setup_architecture_flags

macro( tde_setup_architecture_flags )
  if( NOT DEFINED LINKER_IMMEDIATE_BINDING_FLAGS )
    message( STATUS "Detected ${CMAKE_SYSTEM_PROCESSOR} CPU architecture" )
    ## Immediate symbol binding is available only for gcc but not on ARM architectures
    if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" AND NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES arm* AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" )
      set( LINKER_IMMEDIATE_BINDING_FLAGS "-z\ now" CACHE INTERNAL "" FORCE )
    else( )
      set( LINKER_IMMEDIATE_BINDING_FLAGS "" CACHE INTERNAL "" FORCE )
    endif( )

    check_cxx_compiler_flag( -fPIE HAVE_PIE_SUPPORT )
    if( HAVE_PIE_SUPPORT )
      set( TDE_PIE_CFLAGS -fPIE )
      set( TDE_PIE_LDFLAGS -pie )
    endif( HAVE_PIE_SUPPORT )

    set( _reproducible_cxxflags
         "-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=."
         "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=."
    )
    foreach( _flag ${_reproducible_cxxflags} )
      string( REGEX REPLACE "=.*" "" _flag_name "${_flag}" )
      string( REGEX REPLACE "[^a-zA-Z0-9]+" "_" _flag_var "CXXFLAG_${_flag_name}" )
      if( NOT "${CMAKE_CXX_FLAGS}" MATCHES "(^| )${_flag_name}" )
        check_cxx_compiler_flag( "${_flag}" ${_flag_var} )
        if( ${_flag_var} )
          set( CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" )
          set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" )
        endif()
      endif()
    endforeach()
  endif( )
endmacro( )


#################################################
#####
##### tde_setup_gcc_visibility

macro( tde_setup_gcc_visibility )
  if( NOT DEFINED __KDE_HAVE_GCC_VISIBILITY )
    if( NOT UNIX )
      tde_message_fatal( "gcc visibility support was requested, but your system is not *NIX" )
    endif( NOT UNIX )

    if( TQT_FOUND AND NOT DEFINED HAVE_TQT_VISIBILITY )
      find_library( TQT_LIBFILE tqt-mt HINTS "${TQT_LIBRARY_DIRS}" )
      if( NOT "${TQT_LIBFILE}" STREQUAL "TQT_LIBFILE-NOTFOUND" )
        message( STATUS "Performing Test HAVE_TQT_VISIBILITY" )
        execute_process(
          COMMAND readelf --syms "${TQT_LIBFILE}"
          OUTPUT_VARIABLE HAVE_TQT_VISIBILITY
        )
        if( "${HAVE_TQT_VISIBILITY}" STREQUAL "" OR
            "${HAVE_TQT_VISIBILITY}" MATCHES "GLOBAL[\t ]+DEFAULT[^\n]+QSettingsPrivate" )
          message( STATUS "Performing Test HAVE_TQT_VISIBILITY - Failed" )
          tde_message_fatal( "gcc visibility support was requested, but not supported in tqt library" )
        endif( )
        set( HAVE_TQT_VISIBILITY 1 CACHE INTERNAL "" )
        message( STATUS "Performing Test HAVE_TQT_VISIBILITY - Success" )
      endif( )
    endif( TQT_FOUND AND NOT DEFINED HAVE_TQT_VISIBILITY )

    if( TDE_FOUND AND NOT DEFINED HAVE_TDE_VISIBILITY )
      find_file( TDEMACROS_H kdemacros.h HINTS "${TDE_INCLUDE_DIR}" )
      if( NOT "${TDEMACROS_H}" STREQUAL "TDEMACROS_H-NOTFOUND" )
        tde_save_and_set( CMAKE_REQUIRED_INCLUDES "${TDE_INCLUDE_DIR}" )
        check_cxx_source_compiles( "
          #include <${TDEMACROS_H}>
            #ifndef __KDE_HAVE_GCC_VISIBILITY
            #error gcc visibility is not enabled in tdelibs
            #endif
            int main() { return 0; } "
          HAVE_TDE_VISIBILITY
        )
        tde_restore( CMAKE_REQUIRED_INCLUDES )
        if( NOT HAVE_TDE_VISIBILITY )
          tde_message_fatal( "gcc visibility support was requested, but not supported in tdelibs" )
        endif( NOT HAVE_TDE_VISIBILITY )
      endif( )
    endif( TDE_FOUND AND NOT DEFINED HAVE_TDE_VISIBILITY )

    set( __KDE_HAVE_GCC_VISIBILITY 1 CACHE INTERNAL "" )
    set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden")
  endif( )
endmacro( )


#################################################
#####
##### tde_setup_msgfmt

macro( tde_setup_msgfmt )
  if( NOT DEFINED MSGFMT_EXECUTABLE )
    include( FindGettext )
    if( GETTEXT_FOUND )
      set( MSGFMT_EXECUTABLE ${GETTEXT_MSGFMT_EXECUTABLE}
           CACHE FILEPATH "path to msgfmt executable" )
      if( GETTEXT_VERSION_STRING )
        set( MSGFMT_VERSION ${GETTEXT_VERSION_STRING}
             CACHE STRING "version of msgfmt executable" )
      endif( )
    endif( GETTEXT_FOUND )

    if( NOT MSGFMT_EXECUTABLE )
      tde_message_fatal( "msgfmt is required but was not found on your system." )
    endif( NOT MSGFMT_EXECUTABLE )
  endif( )

  if( NOT MSGFMT_VERSION )
    execute_process(
      COMMAND ${MSGFMT_EXECUTABLE} --version
      OUTPUT_VARIABLE _msgfmt_version
      ERROR_VARIABLE _msgfmt_version
    )
    string( REGEX REPLACE "^msgfmt[^\n]* ([^ ]*)\n.*" "\\1" _msgfmt_version ${_msgfmt_version} )
    set( MSGFMT_VERSION ${_msgfmt_version}
         CACHE STRING "version of msgfmt executable" )
  endif( )
endmacro( )


################################################
#####
##### tde_setup_largefiles

macro( tde_setup_largefiles )
  if( NOT DEFINED HAVE_LARGEFILES )
    message( STATUS "Check support for large files" )
    unset( LARGEFILES_DEFINITIONS )

    # check without special definitions
    unset( HAVE_SIZEOF_T CACHE )
    check_type_size( off_t SIZEOF_OFF_T )
    if( SIZEOF_OFF_T GREATER 7 )
      set( HAVE_LARGEFILES 1 )
    endif( )

    # check with definition _FILE_OFFSET_BITS=64
    if( NOT HAVE_LARGEFILES )
      unset( HAVE_SIZEOF_OFF_T CACHE )
      tde_save_and_set( CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_FILE_OFFSET_BITS=64" )
      check_type_size( off_t SIZEOF_OFF_T )
      tde_restore( CMAKE_REQUIRED_DEFINITIONS )
      if( SIZEOF_OFF_T GREATER 7 )
        set( LARGEFILES_DEFINITIONS "-D_FILE_OFFSET_BITS=64" )
        set( HAVE_LARGEFILES 1 )
      endif( )
    endif( )

    # check with definition _LARGE_FILES
    if( NOT HAVE_LARGEFILES )
      unset( HAVE_SIZEOF_OFF_T CACHE )
      tde_save_and_set( CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_LARGE_FILES" )
      check_type_size( off_t SIZEOF_OFF_T )
      tde_restore( CMAKE_REQUIRED_DEFINITIONS )
      if( SIZEOF_OFF_T GREATER 7 )
        set( LARGEFILES_DEFINITIONS "-D_LARGE_FILES" )
        set( HAVE_LARGEFILES 1 )
      endif( )
    endif( )

    # check with definition _LARGEFILE_SOURCE
    if( NOT HAVE_LARGEFILES )
      unset( HAVE_SIZEOF_OFF_T CACHE )
      tde_save_and_set( CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_LARGEFILE_SOURCE" )
      check_type_size( off_t SIZEOF_OFF_T )
      tde_restore( CMAKE_REQUIRED_DEFINITIONS )
      if( SIZEOF_OFF_T GREATER 7 )
        set( LARGEFILES_DEFINITIONS "-D_LARGEFILE_SOURCE" )
        set( HAVE_LARGEFILES 1 )
      endif( )
    endif( )

    # check for fseeko/ftello
    if( HAVE_LARGEFILES )
      tde_save_and_set( CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} ${LARGEFILES_DEFINITIONS}" )
      check_symbol_exists( "fseeko" "stdio.h" HAVE_FSEEKO )
      tde_restore( CMAKE_REQUIRED_DEFINITIONS )
      if( NOT HAVE_FSEEKO )
        tde_save_and_set( CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} ${LARGEFILES_DEFINITIONS} -D_LARGEFILE_SOURCE" )
        check_symbol_exists( "fseeko" "stdio.h" HAVE_FSEEKO )
        tde_restore( CMAKE_REQUIRED_DEFINITIONS )
        if( HAVE_FSEEKO )
          set( LARGEFILES_DEFINITIONS "${LARGEFILES_DEFINITIONS} -D_LARGEFILE_SOURCE" )
        else( )
          unset( HAVE_LARGEFILES )
        endif( )
      endif( )
    endif( )

    # check results
    if( HAVE_LARGEFILES )
      if( "${LARGEFILES_DEFINITIONS}" STREQUAL "" )
        message( STATUS "Check support for large files - Success" )
      else( )
        add_definitions( ${LARGEFILES_DEFINITIONS} )
        message( STATUS "Check support for large files - Success with ${LARGEFILES_DEFINITIONS}" )
      endif( )
      set( HAVE_LARGEFILES 1 CACHE INTERNAL "Support for large files enabled" )
    else( )
      message( STATUS "Check support for large files - Failed" )
      tde_message_fatal( "Cannot find a way to enable support for large files." )
    endif( )
  endif( )
endmacro( )


#################################################
#####
##### tde_setup_dbus

macro( tde_setup_dbus )
  if( NOT DBUS_FOUND )
    pkg_search_module( DBUS dbus-1 )
    if( NOT DBUS_FOUND )
      tde_message_fatal( "dbus-1 is required, but not found on your system" )
    endif( )
  endif( )

  if( NOT DEFINED DBUS_SYSTEM_CONF_DIRECTORY )
    execute_process(
      COMMAND ${PKG_CONFIG_EXECUTABLE}
        dbus-1 --variable=sysconfdir
      OUTPUT_VARIABLE DBUS_SYSTEM_CONF_BASE
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if( DBUS_SYSTEM_CONF_BASE )
      set( DBUS_SYSTEM_CONF_DIRECTORY "${DBUS_SYSTEM_CONF_BASE}/dbus-1/system.d"
           CACHE PATH "Path for DBUS system configuration files" )
      message( STATUS "Using " ${DBUS_SYSTEM_CONF_DIRECTORY} " for DBUS system configuration files" )
    else( )
      tde_message_fatal( "Can not find the base directory for the dbus-1 configuration" )
    endif( )
  endif( )

  if( NOT DEFINED DBUS_SESSION_CONF_DIRECTORY )
    execute_process(
      COMMAND ${PKG_CONFIG_EXECUTABLE}
        dbus-1 --variable=sysconfdir
      OUTPUT_VARIABLE DBUS_SYSTEM_CONF_BASE
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if( DBUS_SYSTEM_CONF_BASE )
      set( DBUS_SESSION_CONF_DIRECTORY "${DBUS_SYSTEM_CONF_BASE}/dbus-1/session.d"
           CACHE PATH "Path for DBUS session configuration files" )
      message( STATUS "Using " ${DBUS_SESSION_CONF_DIRECTORY} " for DBUS session configuration files" )
    else( )
      tde_message_fatal( "Can not find the base directory for the dbus-1 configuration" )
    endif( )
  endif( )

  if( NOT DEFINED DBUS_SESSION_DIRECTORY )
    execute_process(
      COMMAND ${PKG_CONFIG_EXECUTABLE}
        dbus-1 --variable=session_bus_services_dir
      OUTPUT_VARIABLE DBUS_SESSION_DIRECTORY
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    set( DBUS_SESSION_DIRECTORY "${DBUS_SESSION_DIRECTORY}"
         CACHE PATH "Path for DBUS session service files" )
    message( STATUS "Using " ${DBUS_SESSION_DIRECTORY} " for DBUS session service files" )
  endif( )

  if( NOT DEFINED DBUS_SERVICE_DIRECTORY )
    execute_process(
      COMMAND ${PKG_CONFIG_EXECUTABLE}
        dbus-1 --variable=system_bus_services_dir
      OUTPUT_VARIABLE DBUS_SERVICE_DIRECTORY
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if( "${DBUS_SERVICE_DIRECTORY}" STREQUAL "" )
      if( "${DBUS_SESSION_DIRECTORY}" MATCHES "/services$" )
        string( REGEX REPLACE "/services$" "/system-services"
                DBUS_SERVICE_DIRECTORY "${DBUS_SESSION_DIRECTORY}" )
      else( )
        tde_message_fatal( "Directory for DBUS system service files can not be determined." )
      endif( )
    endif( )
    set( DBUS_SERVICE_DIRECTORY "${DBUS_SERVICE_DIRECTORY}"
         CACHE PATH "Path for DBUS system service files" )
    message( STATUS "Using " ${DBUS_SERVICE_DIRECTORY} " for DBUS system service files" )
  endif( )

  if( NOT "${ARGV}" STREQUAL "" AND NOT DBUS_TQT_FOUND )
    pkg_search_module( DBUS_TQT ${ARGV} )
    if( NOT DBUS_TQT_FOUND )
      tde_message_fatal( "${ARGV} is required, but not found on your system" )
    endif( )
  endif( )
  if( "${ARGV}" STREQUAL "dbus-1-tqt" AND NOT DEFINED DBUSXML2QT3_EXECUTABLE )
    find_program( DBUSXML2QT3_EXECUTABLE
      NAMES dbusxml2qt3
      HINTS "${TDE_PREFIX}/bin" ${BIN_INSTALL_DIR}
    )
  endif( )

endmacro( )


#################################################
#####
##### tde_setup_polkit

macro( tde_setup_polkit )
  if( NOT POLKIT_GOBJECT_FOUND )
    pkg_search_module( POLKIT_GOBJECT polkit-gobject-1 )
    if( NOT POLKIT_GOBJECT_FOUND )
      tde_message_fatal( "polkit-gobject-1 is required, but not found on your system" )
    endif( )
  endif( )

  foreach( _arg ${ARGV} )
    if( NOT "${_arg}" MATCHES "^polkit-" )
      set( _arg "polkit-${_arg}" )
    endif( )
    string( TOUPPER "${_arg}" _polkit_module )
    if( "${_polkit_module}" MATCHES "-[0-9]+$" )
      string( REGEX REPLACE "-[0-9]+$" "" _polkit_module "${_polkit_module}" )
    endif( )
    string( REPLACE "-" "_" _polkit_module "${_polkit_module}" )
    if( NOT "${_arg}" MATCHES "-[0-9]+$" )
      set( _arg "${_arg}-1" )
    endif( )
    if( NOT ${_polkit_module}_FOUND )
      pkg_search_module( ${_polkit_module} ${_arg} )
      if( NOT ${_polkit_module}_FOUND )
        tde_message_fatal( "${_arg} is required, but not found on your system" )
      endif( )
    endif( )
  endforeach( )

  if( NOT DEFINED POLKIT_ACTIONS_DIRECTORY )
    execute_process(
      COMMAND ${PKG_CONFIG_EXECUTABLE}
        polkit-gobject-1 --variable=actiondir
      OUTPUT_VARIABLE POLKIT_ACTIONS_DIRECTORY
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    set( POLKIT_ACTIONS_DIRECTORY "${POLKIT_ACTIONS_DIRECTORY}"
         CACHE PATH "Path for PolicyKit action files" )
    message( STATUS "Using " ${POLKIT_ACTIONS_DIRECTORY} " for PolicyKit action files" )
  endif( )

endmacro( )