forked from bilke/cmake-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cotire.cmake
executable file
·4200 lines (4013 loc) · 171 KB
/
cotire.cmake
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
# - cotire (compile time reducer)
#
# See the cotire manual for usage hints.
#
#=============================================================================
# Copyright 2012-2018 Sascha Kratky
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#=============================================================================
if(__COTIRE_INCLUDED)
return()
endif()
set(__COTIRE_INCLUDED TRUE)
# call cmake_minimum_required, but prevent modification of the CMake policy stack in include mode
# cmake_minimum_required also sets the policy version as a side effect, which we have to avoid
if (NOT CMAKE_SCRIPT_MODE_FILE)
cmake_policy(PUSH)
endif()
cmake_minimum_required(VERSION 2.8.12)
if (NOT CMAKE_SCRIPT_MODE_FILE)
cmake_policy(POP)
endif()
set (COTIRE_CMAKE_MODULE_FILE "${CMAKE_CURRENT_LIST_FILE}")
set (COTIRE_CMAKE_MODULE_VERSION "1.8.0")
# activate select policies
if (POLICY CMP0025)
# Compiler id for Apple Clang is now AppleClang
cmake_policy(SET CMP0025 NEW)
endif()
if (POLICY CMP0026)
# disallow use of the LOCATION target property
cmake_policy(SET CMP0026 NEW)
endif()
if (POLICY CMP0038)
# targets may not link directly to themselves
cmake_policy(SET CMP0038 NEW)
endif()
if (POLICY CMP0039)
# utility targets may not have link dependencies
cmake_policy(SET CMP0039 NEW)
endif()
if (POLICY CMP0040)
# target in the TARGET signature of add_custom_command() must exist
cmake_policy(SET CMP0040 NEW)
endif()
if (POLICY CMP0045)
# error on non-existent target in get_target_property
cmake_policy(SET CMP0045 NEW)
endif()
if (POLICY CMP0046)
# error on non-existent dependency in add_dependencies
cmake_policy(SET CMP0046 NEW)
endif()
if (POLICY CMP0049)
# do not expand variables in target source entries
cmake_policy(SET CMP0049 NEW)
endif()
if (POLICY CMP0050)
# disallow add_custom_command SOURCE signatures
cmake_policy(SET CMP0050 NEW)
endif()
if (POLICY CMP0051)
# include TARGET_OBJECTS expressions in a target's SOURCES property
cmake_policy(SET CMP0051 NEW)
endif()
if (POLICY CMP0053)
# simplify variable reference and escape sequence evaluation
cmake_policy(SET CMP0053 NEW)
endif()
if (POLICY CMP0054)
# only interpret if() arguments as variables or keywords when unquoted
cmake_policy(SET CMP0054 NEW)
endif()
if (POLICY CMP0055)
# strict checking for break() command
cmake_policy(SET CMP0055 NEW)
endif()
include(CMakeParseArguments)
include(ProcessorCount)
function (cotire_get_configuration_types _configsVar)
set (_configs "")
if (CMAKE_CONFIGURATION_TYPES)
list (APPEND _configs ${CMAKE_CONFIGURATION_TYPES})
endif()
if (CMAKE_BUILD_TYPE)
list (APPEND _configs "${CMAKE_BUILD_TYPE}")
endif()
if (_configs)
list (REMOVE_DUPLICATES _configs)
set (${_configsVar} ${_configs} PARENT_SCOPE)
else()
set (${_configsVar} "None" PARENT_SCOPE)
endif()
endfunction()
function (cotire_get_source_file_extension _sourceFile _extVar)
# get_filename_component returns extension from first occurrence of . in file name
# this function computes the extension from last occurrence of . in file name
string (FIND "${_sourceFile}" "." _index REVERSE)
if (_index GREATER -1)
math (EXPR _index "${_index} + 1")
string (SUBSTRING "${_sourceFile}" ${_index} -1 _sourceExt)
else()
set (_sourceExt "")
endif()
set (${_extVar} "${_sourceExt}" PARENT_SCOPE)
endfunction()
macro (cotire_check_is_path_relative_to _path _isRelativeVar)
set (${_isRelativeVar} FALSE)
if (IS_ABSOLUTE "${_path}")
foreach (_dir ${ARGN})
file (RELATIVE_PATH _relPath "${_dir}" "${_path}")
if (NOT _relPath OR (NOT IS_ABSOLUTE "${_relPath}" AND NOT "${_relPath}" MATCHES "^\\.\\."))
set (${_isRelativeVar} TRUE)
break()
endif()
endforeach()
endif()
endmacro()
function (cotire_filter_language_source_files _language _target _sourceFilesVar _excludedSourceFilesVar _cotiredSourceFilesVar)
if (CMAKE_${_language}_SOURCE_FILE_EXTENSIONS)
set (_languageExtensions "${CMAKE_${_language}_SOURCE_FILE_EXTENSIONS}")
else()
set (_languageExtensions "")
endif()
if (CMAKE_${_language}_IGNORE_EXTENSIONS)
set (_ignoreExtensions "${CMAKE_${_language}_IGNORE_EXTENSIONS}")
else()
set (_ignoreExtensions "")
endif()
if (COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS)
set (_excludeExtensions "${COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS}")
else()
set (_excludeExtensions "")
endif()
if (COTIRE_DEBUG AND _languageExtensions)
message (STATUS "${_language} source file extensions: ${_languageExtensions}")
endif()
if (COTIRE_DEBUG AND _ignoreExtensions)
message (STATUS "${_language} ignore extensions: ${_ignoreExtensions}")
endif()
if (COTIRE_DEBUG AND _excludeExtensions)
message (STATUS "${_language} exclude extensions: ${_excludeExtensions}")
endif()
if (CMAKE_VERSION VERSION_LESS "3.1.0")
set (_allSourceFiles ${ARGN})
else()
# as of CMake 3.1 target sources may contain generator expressions
# since we cannot obtain required property information about source files added
# through generator expressions at configure time, we filter them out
string (GENEX_STRIP "${ARGN}" _allSourceFiles)
endif()
set (_filteredSourceFiles "")
set (_excludedSourceFiles "")
foreach (_sourceFile ${_allSourceFiles})
get_source_file_property(_sourceIsHeaderOnly "${_sourceFile}" HEADER_FILE_ONLY)
get_source_file_property(_sourceIsExternal "${_sourceFile}" EXTERNAL_OBJECT)
get_source_file_property(_sourceIsSymbolic "${_sourceFile}" SYMBOLIC)
if (NOT _sourceIsHeaderOnly AND NOT _sourceIsExternal AND NOT _sourceIsSymbolic)
cotire_get_source_file_extension("${_sourceFile}" _sourceExt)
if (_sourceExt)
list (FIND _ignoreExtensions "${_sourceExt}" _ignoreIndex)
if (_ignoreIndex LESS 0)
list (FIND _excludeExtensions "${_sourceExt}" _excludeIndex)
if (_excludeIndex GREATER -1)
list (APPEND _excludedSourceFiles "${_sourceFile}")
else()
list (FIND _languageExtensions "${_sourceExt}" _sourceIndex)
if (_sourceIndex GREATER -1)
# consider source file unless it is excluded explicitly
get_source_file_property(_sourceIsExcluded "${_sourceFile}" COTIRE_EXCLUDED)
if (_sourceIsExcluded)
list (APPEND _excludedSourceFiles "${_sourceFile}")
else()
list (APPEND _filteredSourceFiles "${_sourceFile}")
endif()
else()
get_source_file_property(_sourceLanguage "${_sourceFile}" LANGUAGE)
if ("${_sourceLanguage}" STREQUAL "${_language}")
# add to excluded sources, if file is not ignored and has correct language without having the correct extension
list (APPEND _excludedSourceFiles "${_sourceFile}")
endif()
endif()
endif()
endif()
endif()
endif()
endforeach()
# separate filtered source files from already cotired ones
# the COTIRE_TARGET property of a source file may be set while a target is being processed by cotire
set (_sourceFiles "")
set (_cotiredSourceFiles "")
foreach (_sourceFile ${_filteredSourceFiles})
get_source_file_property(_sourceIsCotired "${_sourceFile}" COTIRE_TARGET)
if (_sourceIsCotired)
list (APPEND _cotiredSourceFiles "${_sourceFile}")
else()
get_source_file_property(_sourceCompileFlags "${_sourceFile}" COMPILE_FLAGS)
if (_sourceCompileFlags)
# add to excluded sources, if file has custom compile flags
list (APPEND _excludedSourceFiles "${_sourceFile}")
else()
get_source_file_property(_sourceCompileOptions "${_sourceFile}" COMPILE_OPTIONS)
if (_sourceCompileOptions)
# add to excluded sources, if file has list of custom compile options
list (APPEND _excludedSourceFiles "${_sourceFile}")
else()
list (APPEND _sourceFiles "${_sourceFile}")
endif()
endif()
endif()
endforeach()
if (COTIRE_DEBUG)
if (_sourceFiles)
message (STATUS "Filtered ${_target} ${_language} sources: ${_sourceFiles}")
endif()
if (_excludedSourceFiles)
message (STATUS "Excluded ${_target} ${_language} sources: ${_excludedSourceFiles}")
endif()
if (_cotiredSourceFiles)
message (STATUS "Cotired ${_target} ${_language} sources: ${_cotiredSourceFiles}")
endif()
endif()
set (${_sourceFilesVar} ${_sourceFiles} PARENT_SCOPE)
set (${_excludedSourceFilesVar} ${_excludedSourceFiles} PARENT_SCOPE)
set (${_cotiredSourceFilesVar} ${_cotiredSourceFiles} PARENT_SCOPE)
endfunction()
function (cotire_get_objects_with_property_on _filteredObjectsVar _property _type)
set (_filteredObjects "")
foreach (_object ${ARGN})
get_property(_isSet ${_type} "${_object}" PROPERTY ${_property} SET)
if (_isSet)
get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
if (_propertyValue)
list (APPEND _filteredObjects "${_object}")
endif()
endif()
endforeach()
set (${_filteredObjectsVar} ${_filteredObjects} PARENT_SCOPE)
endfunction()
function (cotire_get_objects_with_property_off _filteredObjectsVar _property _type)
set (_filteredObjects "")
foreach (_object ${ARGN})
get_property(_isSet ${_type} "${_object}" PROPERTY ${_property} SET)
if (_isSet)
get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
if (NOT _propertyValue)
list (APPEND _filteredObjects "${_object}")
endif()
endif()
endforeach()
set (${_filteredObjectsVar} ${_filteredObjects} PARENT_SCOPE)
endfunction()
function (cotire_get_source_file_property_values _valuesVar _property)
set (_values "")
foreach (_sourceFile ${ARGN})
get_source_file_property(_propertyValue "${_sourceFile}" ${_property})
if (_propertyValue)
list (APPEND _values "${_propertyValue}")
endif()
endforeach()
set (${_valuesVar} ${_values} PARENT_SCOPE)
endfunction()
function (cotire_resolve_config_properties _configurations _propertiesVar)
set (_properties "")
foreach (_property ${ARGN})
if ("${_property}" MATCHES "<CONFIG>")
foreach (_config ${_configurations})
string (TOUPPER "${_config}" _upperConfig)
string (REPLACE "<CONFIG>" "${_upperConfig}" _configProperty "${_property}")
list (APPEND _properties ${_configProperty})
endforeach()
else()
list (APPEND _properties ${_property})
endif()
endforeach()
set (${_propertiesVar} ${_properties} PARENT_SCOPE)
endfunction()
function (cotire_copy_set_properties _configurations _type _source _target)
cotire_resolve_config_properties("${_configurations}" _properties ${ARGN})
foreach (_property ${_properties})
get_property(_isSet ${_type} ${_source} PROPERTY ${_property} SET)
if (_isSet)
get_property(_propertyValue ${_type} ${_source} PROPERTY ${_property})
set_property(${_type} ${_target} PROPERTY ${_property} "${_propertyValue}")
endif()
endforeach()
endfunction()
function (cotire_get_target_usage_requirements _target _config _targetRequirementsVar)
set (_targetRequirements "")
get_target_property(_librariesToProcess ${_target} LINK_LIBRARIES)
while (_librariesToProcess)
# remove from head
list (GET _librariesToProcess 0 _library)
list (REMOVE_AT _librariesToProcess 0)
if (_library MATCHES "^\\$<\\$<CONFIG:${_config}>:([A-Za-z0-9_:-]+)>$")
set (_library "${CMAKE_MATCH_1}")
elseif (_config STREQUAL "None" AND _library MATCHES "^\\$<\\$<CONFIG:>:([A-Za-z0-9_:-]+)>$")
set (_library "${CMAKE_MATCH_1}")
endif()
if (TARGET ${_library})
list (FIND _targetRequirements ${_library} _index)
if (_index LESS 0)
list (APPEND _targetRequirements ${_library})
# BFS traversal of transitive libraries
get_target_property(_libraries ${_library} INTERFACE_LINK_LIBRARIES)
if (_libraries)
list (APPEND _librariesToProcess ${_libraries})
list (REMOVE_DUPLICATES _librariesToProcess)
endif()
endif()
endif()
endwhile()
set (${_targetRequirementsVar} ${_targetRequirements} PARENT_SCOPE)
endfunction()
function (cotire_filter_compile_flags _language _flagFilter _matchedOptionsVar _unmatchedOptionsVar)
if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
set (_flagPrefix "[/-]")
else()
set (_flagPrefix "--?")
endif()
set (_optionFlag "")
set (_matchedOptions "")
set (_unmatchedOptions "")
foreach (_compileFlag ${ARGN})
if (_compileFlag)
if (_optionFlag AND NOT "${_compileFlag}" MATCHES "^${_flagPrefix}")
# option with separate argument
list (APPEND _matchedOptions "${_compileFlag}")
set (_optionFlag "")
elseif ("${_compileFlag}" MATCHES "^(${_flagPrefix})(${_flagFilter})$")
# remember option
set (_optionFlag "${CMAKE_MATCH_2}")
elseif ("${_compileFlag}" MATCHES "^(${_flagPrefix})(${_flagFilter})(.+)$")
# option with joined argument
list (APPEND _matchedOptions "${CMAKE_MATCH_3}")
set (_optionFlag "")
else()
# flush remembered option
if (_optionFlag)
list (APPEND _matchedOptions "${_optionFlag}")
set (_optionFlag "")
endif()
# add to unfiltered options
list (APPEND _unmatchedOptions "${_compileFlag}")
endif()
endif()
endforeach()
if (_optionFlag)
list (APPEND _matchedOptions "${_optionFlag}")
endif()
if (COTIRE_DEBUG AND _matchedOptions)
message (STATUS "Filter ${_flagFilter} matched: ${_matchedOptions}")
endif()
if (COTIRE_DEBUG AND _unmatchedOptions)
message (STATUS "Filter ${_flagFilter} unmatched: ${_unmatchedOptions}")
endif()
set (${_matchedOptionsVar} ${_matchedOptions} PARENT_SCOPE)
set (${_unmatchedOptionsVar} ${_unmatchedOptions} PARENT_SCOPE)
endfunction()
function (cotire_is_target_supported _target _isSupportedVar)
if (NOT TARGET "${_target}")
set (${_isSupportedVar} FALSE PARENT_SCOPE)
return()
endif()
get_target_property(_imported ${_target} IMPORTED)
if (_imported)
set (${_isSupportedVar} FALSE PARENT_SCOPE)
return()
endif()
get_target_property(_targetType ${_target} TYPE)
if (NOT _targetType MATCHES "EXECUTABLE|(STATIC|SHARED|MODULE|OBJECT)_LIBRARY")
set (${_isSupportedVar} FALSE PARENT_SCOPE)
return()
endif()
set (${_isSupportedVar} TRUE PARENT_SCOPE)
endfunction()
function (cotire_get_target_compile_flags _config _language _target _flagsVar)
string (TOUPPER "${_config}" _upperConfig)
# collect options from CMake language variables
set (_compileFlags "")
if (CMAKE_${_language}_FLAGS)
set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_FLAGS}")
endif()
if (CMAKE_${_language}_FLAGS_${_upperConfig})
set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_FLAGS_${_upperConfig}}")
endif()
if (_target)
# add target compile flags
get_target_property(_targetflags ${_target} COMPILE_FLAGS)
if (_targetflags)
set (_compileFlags "${_compileFlags} ${_targetflags}")
endif()
endif()
if (UNIX)
separate_arguments(_compileFlags UNIX_COMMAND "${_compileFlags}")
elseif(WIN32)
separate_arguments(_compileFlags WINDOWS_COMMAND "${_compileFlags}")
else()
separate_arguments(_compileFlags)
endif()
# target compile options
if (_target)
get_target_property(_targetOptions ${_target} COMPILE_OPTIONS)
if (_targetOptions)
list (APPEND _compileFlags ${_targetOptions})
endif()
endif()
# interface compile options from linked library targets
if (_target)
set (_linkedTargets "")
cotire_get_target_usage_requirements(${_target} ${_config} _linkedTargets)
foreach (_linkedTarget ${_linkedTargets})
get_target_property(_targetOptions ${_linkedTarget} INTERFACE_COMPILE_OPTIONS)
if (_targetOptions)
list (APPEND _compileFlags ${_targetOptions})
endif()
endforeach()
endif()
# handle language standard properties
if (CMAKE_${_language}_STANDARD_DEFAULT)
# used compiler supports language standard levels
if (_target)
get_target_property(_targetLanguageStandard ${_target} ${_language}_STANDARD)
if (_targetLanguageStandard)
set (_type "EXTENSION")
get_property(_isSet TARGET ${_target} PROPERTY ${_language}_EXTENSIONS SET)
if (_isSet)
get_target_property(_targetUseLanguageExtensions ${_target} ${_language}_EXTENSIONS)
if (NOT _targetUseLanguageExtensions)
set (_type "STANDARD")
endif()
endif()
if (CMAKE_${_language}${_targetLanguageStandard}_${_type}_COMPILE_OPTION)
list (APPEND _compileFlags "${CMAKE_${_language}${_targetLanguageStandard}_${_type}_COMPILE_OPTION}")
endif()
endif()
endif()
endif()
# handle the POSITION_INDEPENDENT_CODE target property
if (_target)
get_target_property(_targetPIC ${_target} POSITION_INDEPENDENT_CODE)
if (_targetPIC)
get_target_property(_targetType ${_target} TYPE)
if (_targetType STREQUAL "EXECUTABLE" AND CMAKE_${_language}_COMPILE_OPTIONS_PIE)
list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_PIE}")
elseif (CMAKE_${_language}_COMPILE_OPTIONS_PIC)
list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_PIC}")
endif()
endif()
endif()
# handle visibility target properties
if (_target)
get_target_property(_targetVisibility ${_target} ${_language}_VISIBILITY_PRESET)
if (_targetVisibility AND CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY)
list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY}${_targetVisibility}")
endif()
get_target_property(_targetVisibilityInlines ${_target} VISIBILITY_INLINES_HIDDEN)
if (_targetVisibilityInlines AND CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN)
list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN}")
endif()
endif()
# platform specific flags
if (APPLE)
get_target_property(_architectures ${_target} OSX_ARCHITECTURES_${_upperConfig})
if (NOT _architectures)
get_target_property(_architectures ${_target} OSX_ARCHITECTURES)
endif()
if (_architectures)
foreach (_arch ${_architectures})
list (APPEND _compileFlags "-arch" "${_arch}")
endforeach()
endif()
if (CMAKE_OSX_SYSROOT)
if (CMAKE_${_language}_SYSROOT_FLAG)
list (APPEND _compileFlags "${CMAKE_${_language}_SYSROOT_FLAG}" "${CMAKE_OSX_SYSROOT}")
else()
list (APPEND _compileFlags "-isysroot" "${CMAKE_OSX_SYSROOT}")
endif()
endif()
if (CMAKE_OSX_DEPLOYMENT_TARGET)
if (CMAKE_${_language}_OSX_DEPLOYMENT_TARGET_FLAG)
list (APPEND _compileFlags "${CMAKE_${_language}_OSX_DEPLOYMENT_TARGET_FLAG}${CMAKE_OSX_DEPLOYMENT_TARGET}")
else()
list (APPEND _compileFlags "-mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif()
endif()
endif()
if (COTIRE_DEBUG AND _compileFlags)
message (STATUS "Target ${_target} compile flags: ${_compileFlags}")
endif()
set (${_flagsVar} ${_compileFlags} PARENT_SCOPE)
endfunction()
function (cotire_get_target_include_directories _config _language _target _includeDirsVar _systemIncludeDirsVar)
set (_includeDirs "")
set (_systemIncludeDirs "")
# default include dirs
if (CMAKE_INCLUDE_CURRENT_DIR)
list (APPEND _includeDirs "${CMAKE_CURRENT_BINARY_DIR}")
list (APPEND _includeDirs "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
set (_targetFlags "")
cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
# parse additional include directories from target compile flags
if (CMAKE_INCLUDE_FLAG_${_language})
string (STRIP "${CMAKE_INCLUDE_FLAG_${_language}}" _includeFlag)
string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
if (_includeFlag)
set (_dirs "")
cotire_filter_compile_flags("${_language}" "${_includeFlag}" _dirs _ignore ${_targetFlags})
if (_dirs)
list (APPEND _includeDirs ${_dirs})
endif()
endif()
endif()
# parse additional system include directories from target compile flags
if (CMAKE_INCLUDE_SYSTEM_FLAG_${_language})
string (STRIP "${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}" _includeFlag)
string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
if (_includeFlag)
set (_dirs "")
cotire_filter_compile_flags("${_language}" "${_includeFlag}" _dirs _ignore ${_targetFlags})
if (_dirs)
list (APPEND _systemIncludeDirs ${_dirs})
endif()
endif()
endif()
# target include directories
get_directory_property(_dirs DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" INCLUDE_DIRECTORIES)
if (_target)
get_target_property(_targetDirs ${_target} INCLUDE_DIRECTORIES)
if (_targetDirs)
list (APPEND _dirs ${_targetDirs})
endif()
get_target_property(_targetDirs ${_target} INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
if (_targetDirs)
list (APPEND _systemIncludeDirs ${_targetDirs})
endif()
endif()
# interface include directories from linked library targets
if (_target)
set (_linkedTargets "")
cotire_get_target_usage_requirements(${_target} ${_config} _linkedTargets)
foreach (_linkedTarget ${_linkedTargets})
get_target_property(_linkedTargetType ${_linkedTarget} TYPE)
if (CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE AND NOT CMAKE_VERSION VERSION_LESS "3.4.0" AND
_linkedTargetType MATCHES "(STATIC|SHARED|MODULE|OBJECT)_LIBRARY")
# CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE refers to CMAKE_CURRENT_BINARY_DIR and CMAKE_CURRENT_SOURCE_DIR
# at the time, when the target was created. These correspond to the target properties BINARY_DIR and SOURCE_DIR
# which are only available with CMake 3.4 or later.
get_target_property(_targetDirs ${_linkedTarget} BINARY_DIR)
if (_targetDirs)
list (APPEND _dirs ${_targetDirs})
endif()
get_target_property(_targetDirs ${_linkedTarget} SOURCE_DIR)
if (_targetDirs)
list (APPEND _dirs ${_targetDirs})
endif()
endif()
get_target_property(_targetDirs ${_linkedTarget} INTERFACE_INCLUDE_DIRECTORIES)
if (_targetDirs)
list (APPEND _dirs ${_targetDirs})
endif()
get_target_property(_targetDirs ${_linkedTarget} INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
if (_targetDirs)
list (APPEND _systemIncludeDirs ${_targetDirs})
endif()
endforeach()
endif()
if (dirs)
list (REMOVE_DUPLICATES _dirs)
endif()
list (LENGTH _includeDirs _projectInsertIndex)
foreach (_dir ${_dirs})
if (CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE)
cotire_check_is_path_relative_to("${_dir}" _isRelative "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}")
if (_isRelative)
list (LENGTH _includeDirs _len)
if (_len EQUAL _projectInsertIndex)
list (APPEND _includeDirs "${_dir}")
else()
list (INSERT _includeDirs _projectInsertIndex "${_dir}")
endif()
math (EXPR _projectInsertIndex "${_projectInsertIndex} + 1")
else()
list (APPEND _includeDirs "${_dir}")
endif()
else()
list (APPEND _includeDirs "${_dir}")
endif()
endforeach()
list (REMOVE_DUPLICATES _includeDirs)
list (REMOVE_DUPLICATES _systemIncludeDirs)
if (CMAKE_${_language}_IMPLICIT_INCLUDE_DIRECTORIES)
list (REMOVE_ITEM _includeDirs ${CMAKE_${_language}_IMPLICIT_INCLUDE_DIRECTORIES})
endif()
if (WIN32 AND NOT MINGW)
# convert Windows paths in include directories to CMake paths
if (_includeDirs)
set (_paths "")
foreach (_dir ${_includeDirs})
file (TO_CMAKE_PATH "${_dir}" _path)
list (APPEND _paths "${_path}")
endforeach()
set (_includeDirs ${_paths})
endif()
if (_systemIncludeDirs)
set (_paths "")
foreach (_dir ${_systemIncludeDirs})
file (TO_CMAKE_PATH "${_dir}" _path)
list (APPEND _paths "${_path}")
endforeach()
set (_systemIncludeDirs ${_paths})
endif()
endif()
if (COTIRE_DEBUG AND _includeDirs)
message (STATUS "Target ${_target} include dirs: ${_includeDirs}")
endif()
set (${_includeDirsVar} ${_includeDirs} PARENT_SCOPE)
if (COTIRE_DEBUG AND _systemIncludeDirs)
message (STATUS "Target ${_target} system include dirs: ${_systemIncludeDirs}")
endif()
set (${_systemIncludeDirsVar} ${_systemIncludeDirs} PARENT_SCOPE)
endfunction()
function (cotire_get_target_export_symbol _target _exportSymbolVar)
set (_exportSymbol "")
get_target_property(_targetType ${_target} TYPE)
get_target_property(_enableExports ${_target} ENABLE_EXPORTS)
if (_targetType MATCHES "(SHARED|MODULE)_LIBRARY" OR
(_targetType STREQUAL "EXECUTABLE" AND _enableExports))
get_target_property(_exportSymbol ${_target} DEFINE_SYMBOL)
if (NOT _exportSymbol)
set (_exportSymbol "${_target}_EXPORTS")
endif()
string (MAKE_C_IDENTIFIER "${_exportSymbol}" _exportSymbol)
endif()
set (${_exportSymbolVar} ${_exportSymbol} PARENT_SCOPE)
endfunction()
function (cotire_get_target_compile_definitions _config _language _target _definitionsVar)
string (TOUPPER "${_config}" _upperConfig)
set (_configDefinitions "")
# CMAKE_INTDIR for multi-configuration build systems
if (NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
list (APPEND _configDefinitions "CMAKE_INTDIR=\"${_config}\"")
endif()
# target export define symbol
cotire_get_target_export_symbol("${_target}" _defineSymbol)
if (_defineSymbol)
list (APPEND _configDefinitions "${_defineSymbol}")
endif()
# directory compile definitions
get_directory_property(_definitions DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" COMPILE_DEFINITIONS)
if (_definitions)
list (APPEND _configDefinitions ${_definitions})
endif()
get_directory_property(_definitions DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" COMPILE_DEFINITIONS_${_upperConfig})
if (_definitions)
list (APPEND _configDefinitions ${_definitions})
endif()
# target compile definitions
get_target_property(_definitions ${_target} COMPILE_DEFINITIONS)
if (_definitions)
list (APPEND _configDefinitions ${_definitions})
endif()
get_target_property(_definitions ${_target} COMPILE_DEFINITIONS_${_upperConfig})
if (_definitions)
list (APPEND _configDefinitions ${_definitions})
endif()
# interface compile definitions from linked library targets
set (_linkedTargets "")
cotire_get_target_usage_requirements(${_target} ${_config} _linkedTargets)
foreach (_linkedTarget ${_linkedTargets})
get_target_property(_definitions ${_linkedTarget} INTERFACE_COMPILE_DEFINITIONS)
if (_definitions)
list (APPEND _configDefinitions ${_definitions})
endif()
endforeach()
# parse additional compile definitions from target compile flags
# and do not look at directory compile definitions, which we already handled
set (_targetFlags "")
cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
cotire_filter_compile_flags("${_language}" "D" _definitions _ignore ${_targetFlags})
if (_definitions)
list (APPEND _configDefinitions ${_definitions})
endif()
list (REMOVE_DUPLICATES _configDefinitions)
if (COTIRE_DEBUG AND _configDefinitions)
message (STATUS "Target ${_target} compile definitions: ${_configDefinitions}")
endif()
set (${_definitionsVar} ${_configDefinitions} PARENT_SCOPE)
endfunction()
function (cotire_get_target_compiler_flags _config _language _target _compilerFlagsVar)
# parse target compile flags omitting compile definitions and include directives
set (_targetFlags "")
cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
set (_flagFilter "D")
if (CMAKE_INCLUDE_FLAG_${_language})
string (STRIP "${CMAKE_INCLUDE_FLAG_${_language}}" _includeFlag)
string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
if (_includeFlag)
set (_flagFilter "${_flagFilter}|${_includeFlag}")
endif()
endif()
if (CMAKE_INCLUDE_SYSTEM_FLAG_${_language})
string (STRIP "${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}" _includeFlag)
string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
if (_includeFlag)
set (_flagFilter "${_flagFilter}|${_includeFlag}")
endif()
endif()
set (_compilerFlags "")
cotire_filter_compile_flags("${_language}" "${_flagFilter}" _ignore _compilerFlags ${_targetFlags})
if (COTIRE_DEBUG AND _compilerFlags)
message (STATUS "Target ${_target} compiler flags: ${_compilerFlags}")
endif()
set (${_compilerFlagsVar} ${_compilerFlags} PARENT_SCOPE)
endfunction()
function (cotire_add_sys_root_paths _pathsVar)
if (APPLE)
if (CMAKE_OSX_SYSROOT AND CMAKE_${_language}_HAS_ISYSROOT)
foreach (_path IN LISTS ${_pathsVar})
if (IS_ABSOLUTE "${_path}")
get_filename_component(_path "${CMAKE_OSX_SYSROOT}/${_path}" ABSOLUTE)
if (EXISTS "${_path}")
list (APPEND ${_pathsVar} "${_path}")
endif()
endif()
endforeach()
endif()
endif()
set (${_pathsVar} ${${_pathsVar}} PARENT_SCOPE)
endfunction()
function (cotire_get_source_extra_properties _sourceFile _pattern _resultVar)
set (_extraProperties ${ARGN})
set (_result "")
if (_extraProperties)
list (FIND _extraProperties "${_sourceFile}" _index)
if (_index GREATER -1)
math (EXPR _index "${_index} + 1")
list (LENGTH _extraProperties _len)
math (EXPR _len "${_len} - 1")
foreach (_index RANGE ${_index} ${_len})
list (GET _extraProperties ${_index} _value)
if (_value MATCHES "${_pattern}")
list (APPEND _result "${_value}")
else()
break()
endif()
endforeach()
endif()
endif()
set (${_resultVar} ${_result} PARENT_SCOPE)
endfunction()
function (cotire_get_source_compile_definitions _config _language _sourceFile _definitionsVar)
set (_compileDefinitions "")
if (NOT CMAKE_SCRIPT_MODE_FILE)
string (TOUPPER "${_config}" _upperConfig)
get_source_file_property(_definitions "${_sourceFile}" COMPILE_DEFINITIONS)
if (_definitions)
list (APPEND _compileDefinitions ${_definitions})
endif()
get_source_file_property(_definitions "${_sourceFile}" COMPILE_DEFINITIONS_${_upperConfig})
if (_definitions)
list (APPEND _compileDefinitions ${_definitions})
endif()
endif()
cotire_get_source_extra_properties("${_sourceFile}" "^[a-zA-Z0-9_]+(=.*)?$" _definitions ${ARGN})
if (_definitions)
list (APPEND _compileDefinitions ${_definitions})
endif()
if (COTIRE_DEBUG AND _compileDefinitions)
message (STATUS "Source ${_sourceFile} compile definitions: ${_compileDefinitions}")
endif()
set (${_definitionsVar} ${_compileDefinitions} PARENT_SCOPE)
endfunction()
function (cotire_get_source_files_compile_definitions _config _language _definitionsVar)
set (_configDefinitions "")
foreach (_sourceFile ${ARGN})
cotire_get_source_compile_definitions("${_config}" "${_language}" "${_sourceFile}" _sourceDefinitions)
if (_sourceDefinitions)
list (APPEND _configDefinitions "${_sourceFile}" ${_sourceDefinitions} "-")
endif()
endforeach()
set (${_definitionsVar} ${_configDefinitions} PARENT_SCOPE)
endfunction()
function (cotire_get_source_undefs _sourceFile _property _sourceUndefsVar)
set (_sourceUndefs "")
if (NOT CMAKE_SCRIPT_MODE_FILE)
get_source_file_property(_undefs "${_sourceFile}" ${_property})
if (_undefs)
list (APPEND _sourceUndefs ${_undefs})
endif()
endif()
cotire_get_source_extra_properties("${_sourceFile}" "^[a-zA-Z0-9_]+$" _undefs ${ARGN})
if (_undefs)
list (APPEND _sourceUndefs ${_undefs})
endif()
if (COTIRE_DEBUG AND _sourceUndefs)
message (STATUS "Source ${_sourceFile} ${_property} undefs: ${_sourceUndefs}")
endif()
set (${_sourceUndefsVar} ${_sourceUndefs} PARENT_SCOPE)
endfunction()
function (cotire_get_source_files_undefs _property _sourceUndefsVar)
set (_sourceUndefs "")
foreach (_sourceFile ${ARGN})
cotire_get_source_undefs("${_sourceFile}" ${_property} _undefs)
if (_undefs)
list (APPEND _sourceUndefs "${_sourceFile}" ${_undefs} "-")
endif()
endforeach()
set (${_sourceUndefsVar} ${_sourceUndefs} PARENT_SCOPE)
endfunction()
macro (cotire_set_cmd_to_prologue _cmdVar)
set (${_cmdVar} "${CMAKE_COMMAND}")
if (COTIRE_DEBUG)
list (APPEND ${_cmdVar} "--warn-uninitialized")
endif()
list (APPEND ${_cmdVar} "-DCOTIRE_BUILD_TYPE:STRING=$<CONFIGURATION>")
if (XCODE)
list (APPEND ${_cmdVar} "-DXCODE:BOOL=TRUE")
endif()
if (COTIRE_VERBOSE)
list (APPEND ${_cmdVar} "-DCOTIRE_VERBOSE:BOOL=ON")
elseif("${CMAKE_GENERATOR}" MATCHES "Makefiles")
list (APPEND ${_cmdVar} "-DCOTIRE_VERBOSE:BOOL=$(VERBOSE)")
endif()
endmacro()
function (cotire_init_compile_cmd _cmdVar _language _compilerLauncher _compilerExe _compilerArg1)
if (NOT _compilerLauncher)
set (_compilerLauncher ${CMAKE_${_language}_COMPILER_LAUNCHER})
endif()
if (NOT _compilerExe)
set (_compilerExe "${CMAKE_${_language}_COMPILER}")
endif()
if (NOT _compilerArg1)
set (_compilerArg1 ${CMAKE_${_language}_COMPILER_ARG1})
endif()
if (WIN32)
file (TO_NATIVE_PATH "${_compilerExe}" _compilerExe)
endif()
string (STRIP "${_compilerArg1}" _compilerArg1)
if ("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
# compiler launcher is only supported for Makefile and Ninja
set (${_cmdVar} ${_compilerLauncher} "${_compilerExe}" ${_compilerArg1} PARENT_SCOPE)
else()
set (${_cmdVar} "${_compilerExe}" ${_compilerArg1} PARENT_SCOPE)
endif()
endfunction()
macro (cotire_add_definitions_to_cmd _cmdVar _language)
foreach (_definition ${ARGN})
if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
list (APPEND ${_cmdVar} "/D${_definition}")
else()
list (APPEND ${_cmdVar} "-D${_definition}")
endif()
endforeach()
endmacro()
function (cotire_add_includes_to_cmd _cmdVar _language _includesVar _systemIncludesVar)
set (_includeDirs ${${_includesVar}} ${${_systemIncludesVar}})
if (_includeDirs)
list (REMOVE_DUPLICATES _includeDirs)
foreach (_include ${_includeDirs})
if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
file (TO_NATIVE_PATH "${_include}" _include)
list (APPEND ${_cmdVar} "${CMAKE_INCLUDE_FLAG_${_language}}${CMAKE_INCLUDE_FLAG_SEP_${_language}}${_include}")
else()
set (_index -1)
if ("${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}" MATCHES ".+")
list (FIND ${_systemIncludesVar} "${_include}" _index)
endif()
if (_index GREATER -1)
list (APPEND ${_cmdVar} "${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}${CMAKE_INCLUDE_FLAG_SEP_${_language}}${_include}")
else()
list (APPEND ${_cmdVar} "${CMAKE_INCLUDE_FLAG_${_language}}${CMAKE_INCLUDE_FLAG_SEP_${_language}}${_include}")
endif()
endif()
endforeach()
endif()
set (${_cmdVar} ${${_cmdVar}} PARENT_SCOPE)
endfunction()
function (cotire_add_frameworks_to_cmd _cmdVar _language _includesVar _systemIncludesVar)
if (APPLE)
set (_frameworkDirs "")
foreach (_include ${${_includesVar}})
if (IS_ABSOLUTE "${_include}" AND _include MATCHES "\\.framework$")
get_filename_component(_frameworkDir "${_include}" DIRECTORY)
list (APPEND _frameworkDirs "${_frameworkDir}")
endif()
endforeach()
set (_systemFrameworkDirs "")
foreach (_include ${${_systemIncludesVar}})
if (IS_ABSOLUTE "${_include}" AND _include MATCHES "\\.framework$")
get_filename_component(_frameworkDir "${_include}" DIRECTORY)
list (APPEND _systemFrameworkDirs "${_frameworkDir}")
endif()
endforeach()
if (_systemFrameworkDirs)
list (APPEND _frameworkDirs ${_systemFrameworkDirs})
endif()
if (_frameworkDirs)
list (REMOVE_DUPLICATES _frameworkDirs)
foreach (_frameworkDir ${_frameworkDirs})
set (_index -1)
if ("${CMAKE_${_language}_SYSTEM_FRAMEWORK_SEARCH_FLAG}" MATCHES ".+")
list (FIND _systemFrameworkDirs "${_frameworkDir}" _index)
endif()
if (_index GREATER -1)
list (APPEND ${_cmdVar} "${CMAKE_${_language}_SYSTEM_FRAMEWORK_SEARCH_FLAG}${_frameworkDir}")
else()
list (APPEND ${_cmdVar} "${CMAKE_${_language}_FRAMEWORK_SEARCH_FLAG}${_frameworkDir}")
endif()
endforeach()
endif()
endif()
set (${_cmdVar} ${${_cmdVar}} PARENT_SCOPE)
endfunction()
macro (cotire_add_compile_flags_to_cmd _cmdVar)
foreach (_flag ${ARGN})
list (APPEND ${_cmdVar} "${_flag}")
endforeach()
endmacro()
function (cotire_check_file_up_to_date _fileIsUpToDateVar _file)
if (EXISTS "${_file}")
set (_triggerFile "")
foreach (_dependencyFile ${ARGN})
if (EXISTS "${_dependencyFile}")
# IS_NEWER_THAN returns TRUE if both files have the same timestamp
# thus we do the comparison in both directions to exclude ties
if ("${_dependencyFile}" IS_NEWER_THAN "${_file}" AND
NOT "${_file}" IS_NEWER_THAN "${_dependencyFile}")
set (_triggerFile "${_dependencyFile}")
break()
endif()
endif()
endforeach()