-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathBLTMacros.cmake
More file actions
1427 lines (1188 loc) · 54.2 KB
/
Copy pathBLTMacros.cmake
File metadata and controls
1427 lines (1188 loc) · 54.2 KB
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
# Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and
# other BLT Project Developers. See the top-level LICENSE file for details
#
# SPDX-License-Identifier: (BSD-3-Clause)
include(${BLT_ROOT_DIR}/cmake/BLTPrivateMacros.cmake)
##------------------------------------------------------------------------------
## blt_assert_exists( [DIRECTORIES [<dir1> <dir2> ...] ]
## [TARGETS [<target1> <target2> ...] ]
## [FILES <file1> <file2> ...] )
##
## Throws a FATAL_ERROR message if any of the specified directories, files, or
## targets do not exist.
##------------------------------------------------------------------------------
macro(blt_assert_exists)
set(options)
set(singleValueArgs)
set(multiValueArgs DIRECTORIES TARGETS FILES)
# parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
if (DEFINED arg_DIRECTORIES)
foreach (_dir ${arg_DIRECTORIES})
if (NOT IS_DIRECTORY ${_dir})
message(FATAL_ERROR "directory [${_dir}] does not exist!")
endif()
endforeach()
endif()
if (DEFINED arg_FILES)
foreach (_file ${arg_FILES})
if (NOT EXISTS ${_file})
message(FATAL_ERROR "file [${_file}] does not exist!")
endif()
endforeach()
endif()
if (DEFINED arg_TARGETS)
foreach (_target ${arg_TARGETS})
if (NOT TARGET ${_target})
message(FATAL_ERROR "target [${_target}] does not exist!")
endif()
endforeach()
endif()
endmacro(blt_assert_exists)
##------------------------------------------------------------------------------
## blt_list_append( TO <list> ELEMENTS [ <element>...] IF <bool> )
##
## Appends elements to a list if the specified bool evaluates to true.
##------------------------------------------------------------------------------
macro(blt_list_append)
set(options)
set(singleValueArgs TO IF)
set(multiValueArgs ELEMENTS )
# parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# Sanity checks
if( NOT DEFINED arg_TO )
message(FATAL_ERROR "blt_list_append() requires a TO <list> argument")
endif()
if ( NOT DEFINED arg_ELEMENTS )
message(FATAL_ERROR "blt_list_append() requires ELEMENTS to be specified" )
endif()
# determine if we should add the elements to the list
set(_shouldAdd FALSE )
set(_listVar "${ARGN}") # convert macro arguments to list variable
if("IF" IN_LIST _listVar)
set(_shouldAdd ${arg_IF}) # use IF condition, when present
else()
set(_shouldAdd TRUE) # otherwise, always add the elements
endif()
# append if
if ( ${_shouldAdd} )
list( APPEND ${arg_TO} ${arg_ELEMENTS} )
endif()
unset(_shouldAdd)
unset(_listVar)
endmacro(blt_list_append)
##------------------------------------------------------------------------------
## blt_list_remove_duplicates( TO <list> )
##
## Removes duplicate elements from the given TO list.
##------------------------------------------------------------------------------
macro(blt_list_remove_duplicates)
set(options)
set(singleValueArgs TO )
set(multiValueArgs )
# parse macro arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# Sanity checks
if( NOT DEFINED arg_TO )
message(FATAL_ERROR "blt_list_append() requires a TO <list> argument")
endif()
if ( ${arg_TO} )
list(REMOVE_DUPLICATES ${arg_TO} )
endif()
endmacro(blt_list_remove_duplicates)
##------------------------------------------------------------------------------
## blt_add_target_definitions(TO <target>
## SCOPE <PUBLIC (Default)| INTERFACE | PRIVATE>
## TARGET_DEFINITIONS [FOO [BAR ...]])
##
## Adds pre-processor definitions to the given target.
##------------------------------------------------------------------------------
macro(blt_add_target_definitions)
set(options)
set(singleValueArgs TO SCOPE)
set(multiValueArgs TARGET_DEFINITIONS)
# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
# Sanity checks
if(NOT TARGET ${arg_TO})
message(FATAL_ERROR "Target ${arg_TO} passed to blt_add_target_definitions is not a valid cmake target")
endif()
blt_determine_scope(TARGET ${arg_TO} SCOPE "${arg_SCOPE}" OUT _scope)
# Only add the flag if it is not empty
string(STRIP "${arg_TARGET_DEFINITIONS}" _strippedDefs)
if(NOT "${_strippedDefs}" STREQUAL "")
target_compile_definitions(${arg_TO} ${_scope} ${_strippedDefs})
endif()
unset(_scope)
unset(_strippedDefs)
endmacro(blt_add_target_definitions)
##------------------------------------------------------------------------------
## blt_add_target_compile_flags(TO <target>
## SCOPE <PUBLIC (Default)| INTERFACE | PRIVATE>
## FLAGS [FOO [BAR ...]])
##
## Adds compiler flags to a target (library, executable or interface) by
## appending to the target's existing flags.
##------------------------------------------------------------------------------
macro(blt_add_target_compile_flags)
set(options)
set(singleValuedArgs TO SCOPE)
set(multiValuedArgs FLAGS)
# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValuedArgs}" "${multiValuedArgs}" ${ARGN} )
# Sanity checks
if(NOT TARGET ${arg_TO})
message(FATAL_ERROR "Target ${arg_TO} passed to blt_add_target_compile_flags is not a valid cmake target")
endif()
blt_determine_scope(TARGET ${arg_TO} SCOPE "${arg_SCOPE}" OUT _scope)
# Only add the flag if it is not empty
string(STRIP "${arg_FLAGS}" _strippedFlags)
if(NOT "${_strippedFlags}" STREQUAL "")
get_target_property(_target_type ${arg_TO} TYPE)
if (("${_target_type}" STREQUAL "INTERFACE_LIBRARY") AND (${CMAKE_VERSION} VERSION_LESS "3.11.0"))
set_property(TARGET ${arg_NAME} APPEND PROPERTY
INTERFACE_COMPILE_OPTIONS ${_strippedFlags})
else()
target_compile_options(${arg_TO} ${_scope} ${_strippedFlags})
endif()
endif()
unset(_strippedFlags)
unset(_scope)
endmacro(blt_add_target_compile_flags)
##------------------------------------------------------------------------------
## blt_set_target_folder(TARGET <target> FOLDER <folder>)
##
## Sets the folder property of cmake target <target> to <folder>.
##------------------------------------------------------------------------------
macro(blt_set_target_folder)
set(options)
set(singleValuedArgs TARGET FOLDER)
set(multiValuedArgs)
# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValuedArgs}" "${multiValuedArgs}" ${ARGN} )
# Sanity checks
if(NOT DEFINED arg_TARGET)
message(FATAL_ERROR "TARGET is a required parameter for blt_set_target_folder macro")
endif()
if(NOT TARGET ${arg_TARGET})
message(FATAL_ERROR "Target ${arg_TARGET} passed to blt_set_target_folder is not a valid cmake target")
endif()
if(NOT DEFINED arg_FOLDER)
message(FATAL_ERROR "FOLDER is a required parameter for blt_set_target_folder macro")
endif()
# Set the folder property for this target
if(ENABLE_FOLDERS AND NOT "${arg_FOLDER}" STREQUAL "")
set_property(TARGET ${arg_TARGET} PROPERTY FOLDER "${arg_FOLDER}")
endif()
endmacro(blt_set_target_folder)
##------------------------------------------------------------------------------
## blt_add_target_link_flags (TO <target>
## SCOPE <PUBLIC (Default)| INTERFACE | PRIVATE>
## FLAGS [FOO [BAR ...]])
##
## Adds linker flags to a target by appending to the target's existing flags.
##------------------------------------------------------------------------------
macro(blt_add_target_link_flags)
set(options)
set(singleValuedArgs TO SCOPE)
set(multiValuedArgs FLAGS)
## parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValuedArgs}" "${multiValuedArgs}" ${ARGN} )
set(_flags ${arg_FLAGS})
# Convert rpath flag if linking with CUDA
if (BLT_CUDA_LINK_WITH_NVCC)
string(REPLACE "-Wl,-rpath," "-Xlinker -rpath -Xlinker "
_flags "${_flags}")
endif()
# Only add the flag if it is not empty
if(NOT "${arg_FLAGS}" STREQUAL "")
if( ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0" )
# In CMake 3.13+, LINK_FLAGS was converted to LINK_OPTIONS.
# This now supports generator expressions and scoping but expects a list
# not a string
blt_determine_scope(TARGET ${arg_TO} SCOPE "${arg_SCOPE}" OUT _scope)
# Note: "SHELL:"" causes the flags to be not de-duplicated and parsed with
# separate_arguments
if(NOT "${arg_FLAGS}" MATCHES SHELL:)
target_link_options(${arg_TO} ${_scope} SHELL:${arg_FLAGS})
else()
target_link_options(${arg_TO} ${_scope} ${arg_FLAGS})
endif()
else()
# In CMake <= 3.12, there is no target_link_flags or target_link_options command
get_target_property(_target_type ${arg_TO} TYPE)
if ("${_target_type}" STREQUAL "INTERFACE_LIBRARY")
# If it's an interface library, we add the flag via link_libraries
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.11.0")
target_link_libraries(${arg_TO} INTERFACE ${_flags})
else()
set_property(TARGET ${arg_NAME} APPEND PROPERTY
INTERFACE_LINK_LIBRARIES ${_flags})
endif()
else()
get_target_property(_link_flags ${arg_TO} LINK_FLAGS)
# Append to existing flags
if(NOT _link_flags)
set(_link_flags "")
endif()
set(_link_flags "${_flags} ${_link_flags}")
# Convert from a CMake ;-list to a string
string (REPLACE ";" " " _link_flags_str "${_link_flags}")
set_target_properties(${arg_TO}
PROPERTIES LINK_FLAGS "${_link_flags_str}")
endif()
endif()
endif()
unset(_flags)
unset(_link_flags)
unset(_link_flags_str)
unset(_scope)
endmacro(blt_add_target_link_flags)
##------------------------------------------------------------------------------
## blt_register_library( NAME <libname>
## DEPENDS_ON [dep1 [dep2 ...]]
## INCLUDES [include1 [include2 ...]]
## TREAT_INCLUDES_AS_SYSTEM [ON|OFF]
## FORTRAN_MODULES [ path1 [ path2 ..]]
## LIBRARIES [lib1 [lib2 ...]]
## COMPILE_FLAGS [ flag1 [ flag2 ..]]
## LINK_FLAGS [ flag1 [ flag2 ..]]
## DEFINES [def1 [def2 ...]] )
##
## Registers a library to the project to ease use in other BLT macro calls.
##------------------------------------------------------------------------------
macro(blt_register_library)
set(singleValueArgs NAME OBJECT TREAT_INCLUDES_AS_SYSTEM)
set(multiValueArgs INCLUDES
DEPENDS_ON
FORTRAN_MODULES
LIBRARIES
COMPILE_FLAGS
LINK_FLAGS
DEFINES )
## parse the arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
string(TOUPPER ${arg_NAME} uppercase_name)
set(_BLT_${uppercase_name}_IS_REGISTERED_LIBRARY TRUE CACHE BOOL "" FORCE)
if( arg_DEPENDS_ON )
set(_BLT_${uppercase_name}_DEPENDS_ON ${arg_DEPENDS_ON} CACHE STRING "" FORCE)
mark_as_advanced(_BLT_${uppercase_name}_DEPENDS_ON)
endif()
if( arg_INCLUDES )
set(_BLT_${uppercase_name}_INCLUDES ${arg_INCLUDES} CACHE STRING "" FORCE)
mark_as_advanced(_BLT_${uppercase_name}_INCLUDES)
endif()
if( ${arg_OBJECT} )
set(_BLT_${uppercase_name}_IS_OBJECT_LIBRARY TRUE CACHE BOOL "" FORCE)
else()
set(_BLT_${uppercase_name}_IS_OBJECT_LIBRARY FALSE CACHE BOOL "" FORCE)
endif()
mark_as_advanced(_BLT_${uppercase_name}_IS_OBJECT_LIBRARY)
# PGI does not support -isystem
if( (${arg_TREAT_INCLUDES_AS_SYSTEM}) AND (NOT ${C_COMPILER_FAMILY_IS_PGI}))
set(_BLT_${uppercase_name}_TREAT_INCLUDES_AS_SYSTEM TRUE CACHE BOOL "" FORCE)
else()
set(_BLT_${uppercase_name}_TREAT_INCLUDES_AS_SYSTEM FALSE CACHE BOOL "" FORCE)
endif()
mark_as_advanced(_BLT_${uppercase_name}_TREAT_INCLUDES_AS_SYSTEM)
if( ENABLE_FORTRAN AND arg_FORTRAN_MODULES )
set(_BLT_${uppercase_name}_FORTRAN_MODULES ${arg_INCLUDES} CACHE STRING "" FORCE)
mark_as_advanced(_BLT_${uppercase_name}_FORTRAN_MODULES)
endif()
if( arg_LIBRARIES )
set(_BLT_${uppercase_name}_LIBRARIES ${arg_LIBRARIES} CACHE STRING "" FORCE)
else()
# This prevents cmake from falling back on adding -l<library name>
# to the command line for BLT registered libraries which are not
# actual CMake targets
set(_BLT_${uppercase_name}_LIBRARIES "BLT_NO_LIBRARIES" CACHE STRING "" FORCE)
endif()
mark_as_advanced(_BLT_${uppercase_name}_LIBRARIES)
if( arg_COMPILE_FLAGS )
set(_BLT_${uppercase_name}_COMPILE_FLAGS "${arg_COMPILE_FLAGS}" CACHE STRING "" FORCE)
mark_as_advanced(_BLT_${uppercase_name}_COMPILE_FLAGS)
endif()
if( arg_LINK_FLAGS )
set(_BLT_${uppercase_name}_LINK_FLAGS "${arg_LINK_FLAGS}" CACHE STRING "" FORCE)
mark_as_advanced(_BLT_${uppercase_name}_LINK_FLAGS)
endif()
if( arg_DEFINES )
set(_BLT_${uppercase_name}_DEFINES ${arg_DEFINES} CACHE STRING "" FORCE)
mark_as_advanced(_BLT_${uppercase_name}_DEFINES)
endif()
endmacro(blt_register_library)
##------------------------------------------------------------------------------
## blt_patch_target( NAME <targetname>
## DEPENDS_ON [dep1 [dep2 ...]]
## INCLUDES [include1 [include2 ...]]
## LIBRARIES [lib1 [lib2 ...]]
## TREAT_INCLUDES_AS_SYSTEM [ON|OFF]
## FORTRAN_MODULES [ path1 [ path2 ..]]
## COMPILE_FLAGS [ flag1 [ flag2 ..]]
## LINK_FLAGS [ flag1 [ flag2 ..]]
## DEFINES [def1 [def2 ...]] )
##
## Modifies an existing CMake target - sets PUBLIC visibility except for INTERFACE
## libraries, which use INTERFACE visibility
##------------------------------------------------------------------------------
macro(blt_patch_target)
set(singleValueArgs NAME TREAT_INCLUDES_AS_SYSTEM)
set(multiValueArgs INCLUDES
DEPENDS_ON
LIBRARIES
FORTRAN_MODULES
COMPILE_FLAGS
LINK_FLAGS
DEFINES )
## parse the arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# Input checks
if( "${arg_NAME}" STREQUAL "" )
message(FATAL_ERROR "blt_patch_target() must be called with argument NAME <name>")
endif()
if (NOT TARGET ${arg_NAME})
message(FATAL_ERROR "blt_patch_target() NAME argument must be a native CMake target")
endif()
# Default to public scope, unless it's an interface library
set(_scope PUBLIC)
get_target_property(_target_type ${arg_NAME} TYPE)
if("${_target_type}" STREQUAL "INTERFACE_LIBRARY")
set(_scope INTERFACE)
endif()
# Interface libraries were heavily restricted pre-3.11
set(_standard_lib_interface FALSE)
if((${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.11.0") OR (NOT "${_target_type}" STREQUAL "INTERFACE_LIBRARY"))
set(_standard_lib_interface TRUE)
endif()
# LIBRARIES and DEPENDS_ON are kept separate in case different logic is needed for
# the library itself versus its dependencies
set(_libs_to_link "")
if( arg_LIBRARIES )
list(APPEND _libs_to_link ${arg_LIBRARIES})
endif()
# TODO: This won't expand BLT-registered libraries
if( arg_DEPENDS_ON )
list(APPEND _libs_to_link ${arg_DEPENDS_ON})
endif()
if(_standard_lib_interface)
target_link_libraries(${arg_NAME} ${_scope} ${_libs_to_link})
else()
set_property(TARGET ${arg_NAME} APPEND PROPERTY
INTERFACE_LINK_LIBRARIES ${_libs_to_link})
endif()
if( arg_INCLUDES )
if(_standard_lib_interface)
target_include_directories(${arg_NAME} ${_scope} ${arg_INCLUDES})
else()
# Interface include directories need to be set manually
set_property(TARGET ${arg_NAME} APPEND PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${arg_INCLUDES})
endif()
endif()
# PGI does not support -isystem
if( (${arg_TREAT_INCLUDES_AS_SYSTEM}) AND (NOT ${C_COMPILER_FAMILY_IS_PGI}))
get_target_property(_target_includes ${arg_NAME} INTERFACE_INCLUDE_DIRECTORIES)
# Don't copy if the target had no include directories
if(_target_includes)
if(_standard_lib_interface)
target_include_directories(${arg_NAME} SYSTEM ${_scope} ${_target_includes})
else()
set_property(TARGET ${arg_NAME} PROPERTY
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${_target_includes})
endif()
endif()
endif()
# FIXME: Is this all that's needed?
if( arg_FORTRAN_MODULES )
target_include_directories(${arg_NAME} ${_scope} ${arg_FORTRAN_MODULES})
endif()
if( arg_COMPILE_FLAGS )
blt_add_target_compile_flags(TO ${arg_NAME}
SCOPE ${_scope}
FLAGS ${arg_COMPILE_FLAGS})
endif()
if( arg_LINK_FLAGS )
blt_add_target_link_flags(TO ${arg_NAME}
SCOPE ${_scope}
FLAGS ${arg_LINK_FLAGS})
endif()
if( arg_DEFINES )
blt_add_target_definitions(TO ${arg_NAME}
SCOPE ${_scope}
TARGET_DEFINITIONS ${arg_DEFINES})
endif()
endmacro(blt_patch_target)
##------------------------------------------------------------------------------
## blt_import_library( NAME <libname>
## LIBRARIES [lib1 [lib2 ...]]
## DEPENDS_ON [dep1 [dep2 ...]]
## INCLUDES [include1 [include2 ...]]
## TREAT_INCLUDES_AS_SYSTEM [ON|OFF]
## FORTRAN_MODULES [ path1 [ path2 ..]]
## COMPILE_FLAGS [ flag1 [ flag2 ..]]
## LINK_FLAGS [ flag1 [ flag2 ..]]
## DEFINES [def1 [def2 ...]]
## GLOBAL [ON|OFF]
## EXPORTABLE [ON|OFF])
##
## Imports a library as a CMake target
##------------------------------------------------------------------------------
macro(blt_import_library)
set(singleValueArgs NAME TREAT_INCLUDES_AS_SYSTEM GLOBAL EXPORTABLE)
set(multiValueArgs LIBRARIES
INCLUDES
DEPENDS_ON
FORTRAN_MODULES
COMPILE_FLAGS
LINK_FLAGS
DEFINES )
## parse the arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# Input checks
if( "${arg_NAME}" STREQUAL "" )
message(FATAL_ERROR "blt_import_library() must be called with argument NAME <name>")
endif()
if(${arg_EXPORTABLE})
if(${arg_GLOBAL})
message(FATAL_ERROR "blt_import_library: EXPORTABLE targets cannot be GLOBAL")
endif()
add_library(${arg_NAME} INTERFACE)
elseif(${arg_GLOBAL})
add_library(${arg_NAME} INTERFACE IMPORTED GLOBAL)
else()
add_library(${arg_NAME} INTERFACE IMPORTED)
endif()
blt_patch_target(
NAME ${arg_NAME}
LIBRARIES ${arg_LIBRARIES}
DEPENDS_ON ${arg_DEPENDS_ON}
INCLUDES ${arg_INCLUDES}
DEFINES ${arg_DEFINES}
TREAT_INCLUDES_AS_SYSTEM ${arg_TREAT_INCLUDES_AS_SYSTEM}
FORTRAN_MODULES ${arg_FORTRAN_MODULES}
COMPILE_FLAGS ${arg_COMPILE_FLAGS}
LINK_FLAGS ${arg_LINK_FLAGS}
DEFINES ${arg_DEFINES}
)
endmacro(blt_import_library)
##------------------------------------------------------------------------------
## blt_add_library( NAME <libname>
## SOURCES [source1 [source2 ...]]
## HEADERS [header1 [header2 ...]]
## INCLUDES [dir1 [dir2 ...]]
## DEFINES [define1 [define2 ...]]
## DEPENDS_ON [dep1 ...]
## OUTPUT_NAME [name]
## OUTPUT_DIR [dir]
## SHARED [TRUE | FALSE]
## OBJECT [TRUE | FALSE]
## CLEAR_PREFIX [TRUE | FALSE]
## FOLDER [name])
##
## Adds a library target, called <libname>, to be built from the given sources.
##------------------------------------------------------------------------------
macro(blt_add_library)
set(options)
set(singleValueArgs NAME OUTPUT_NAME OUTPUT_DIR SHARED OBJECT CLEAR_PREFIX FOLDER)
set(multiValueArgs SOURCES HEADERS INCLUDES DEFINES DEPENDS_ON)
# parse the arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# Sanity checks
if( "${arg_NAME}" STREQUAL "" )
message(FATAL_ERROR "blt_add_library() must be called with argument NAME <name>")
endif()
if (NOT arg_SOURCES AND NOT arg_HEADERS )
message(FATAL_ERROR "blt_add_library(NAME ${arg_NAME} ...) called with no given sources or headers")
endif()
if (DEFINED arg_OBJECT AND arg_OBJECT)
if (DEFINED arg_SHARED AND arg_SHARED)
message(FATAL_ERROR "blt_add_library(NAME ${arg_NAME} ...) cannot be called with both OBJECT and SHARED set to TRUE.")
endif()
if (NOT arg_SOURCES)
message(FATAL_ERROR "blt_add_library(NAME ${arg_NAME} ...) cannot create an object library with no sources.")
endif()
endif()
if ( arg_SOURCES )
# Determine type of library to build. STATIC by default and OBJECT takes
# precedence over global BUILD_SHARED_LIBS variable.
set(_build_shared_library ${BUILD_SHARED_LIBS})
if( DEFINED arg_SHARED )
set(_build_shared_library ${arg_SHARED})
endif()
if ( ${arg_OBJECT} )
set(_lib_type "OBJECT")
blt_register_library( NAME ${arg_NAME}
DEPENDS_ON ${arg_DEPENDS_ON}
INCLUDES ${arg_INCLUDES}
OBJECT TRUE
DEFINES ${arg_DEFINES} )
elseif ( ${_build_shared_library} )
set(_lib_type "SHARED")
else()
set(_lib_type "STATIC")
endif()
add_library( ${arg_NAME} ${_lib_type} ${arg_SOURCES} ${arg_HEADERS} )
if (ENABLE_CUDA AND NOT ENABLE_CLANG_CUDA)
blt_setup_cuda_target(
NAME ${arg_NAME}
SOURCES ${arg_SOURCES}
DEPENDS_ON ${arg_DEPENDS_ON}
LIBRARY_TYPE ${_lib_type})
endif()
else()
#
# Header-only library support
#
foreach (_file ${arg_HEADERS})
# Determine build location of headers
get_filename_component(_absolute ${_file} ABSOLUTE)
list(APPEND _build_headers ${_absolute})
endforeach()
#Note: This only works if both libraries are handled in the same directory,
# otherwise just don't include non-header files in your source list.
set_source_files_properties(${_build_headers} PROPERTIES HEADER_FILE_ONLY ON)
add_library( ${arg_NAME} INTERFACE )
target_sources( ${arg_NAME} INTERFACE
$<BUILD_INTERFACE:${_build_headers}>)
endif()
# Clear value of _have_fortran from previous calls
set(_have_fortran False)
# Must tell fortran where to look for modules
# CMAKE_Fortran_MODULE_DIRECTORY is the location of generated modules
foreach (_file ${arg_SOURCES})
get_source_file_property(_lang ${_file} LANGUAGE)
if(_lang STREQUAL Fortran)
set(_have_fortran TRUE)
endif()
endforeach()
if(_have_fortran)
target_include_directories(${arg_NAME} PRIVATE ${CMAKE_Fortran_MODULE_DIRECTORY})
endif()
blt_setup_target( NAME ${arg_NAME}
DEPENDS_ON ${arg_DEPENDS_ON}
OBJECT ${arg_OBJECT})
if ( arg_INCLUDES )
if (NOT arg_SOURCES )
# Header only
target_include_directories(${arg_NAME} INTERFACE ${arg_INCLUDES})
else()
target_include_directories(${arg_NAME} PUBLIC ${arg_INCLUDES})
endif()
endif()
if ( arg_DEFINES )
target_compile_definitions(${arg_NAME} PUBLIC ${arg_DEFINES})
endif()
if ( arg_OUTPUT_DIR )
set_target_properties(${arg_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${arg_OUTPUT_DIR} )
endif()
if ( arg_OUTPUT_NAME )
set_target_properties(${arg_NAME} PROPERTIES
OUTPUT_NAME ${arg_OUTPUT_NAME} )
endif()
if ( arg_CLEAR_PREFIX )
set_target_properties(${arg_NAME} PROPERTIES
PREFIX "" )
endif()
# Handle optional FOLDER keyword for this target
if(ENABLE_FOLDERS AND DEFINED arg_FOLDER)
blt_set_target_folder(TARGET ${arg_NAME} FOLDER "${arg_FOLDER}")
endif()
blt_update_project_sources( TARGET_SOURCES ${arg_SOURCES} ${arg_HEADERS})
if ( arg_SOURCES )
# Don't clean header-only libraries because you would have to handle
# the white-list of properties that are allowed
blt_clean_target(TARGET ${arg_NAME})
endif()
endmacro(blt_add_library)
##------------------------------------------------------------------------------
## blt_add_executable( NAME <name>
## SOURCES [source1 [source2 ...]]
## HEADERS [header1 [header2 ...]]
## INCLUDES [dir1 [dir2 ...]]
## DEFINES [define1 [define2 ...]]
## DEPENDS_ON [dep1 [dep2 ...]]
## OUTPUT_DIR [dir]
## OUTPUT_NAME [name]
## FOLDER [name])
##
## Adds an executable target, called <name>, to be built from the given sources.
##------------------------------------------------------------------------------
macro(blt_add_executable)
set(options )
set(singleValueArgs NAME OUTPUT_DIR OUTPUT_NAME FOLDER)
set(multiValueArgs HEADERS SOURCES INCLUDES DEFINES DEPENDS_ON)
# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
# Sanity checks
if( "${arg_NAME}" STREQUAL "" )
message(FATAL_ERROR "blt_add_executable() must be called with argument NAME <name>")
endif()
if (NOT arg_SOURCES )
message(FATAL_ERROR "blt_add_executable(NAME ${arg_NAME} ...) given with no sources")
endif()
add_executable( ${arg_NAME} ${arg_SOURCES} ${arg_HEADERS})
if (ENABLE_CUDA AND NOT ENABLE_CLANG_CUDA)
blt_setup_cuda_target(
NAME ${arg_NAME}
SOURCES ${arg_SOURCES}
DEPENDS_ON ${arg_DEPENDS_ON})
endif()
# CMake wants to load with C++ if any of the libraries are C++.
# Force to load with Fortran if the first file is Fortran.
list(GET arg_SOURCES 0 _first)
get_source_file_property(_lang ${_first} LANGUAGE)
if(_lang STREQUAL Fortran)
set_target_properties( ${arg_NAME} PROPERTIES LINKER_LANGUAGE Fortran )
target_include_directories(${arg_NAME} PRIVATE ${CMAKE_Fortran_MODULE_DIRECTORY})
endif()
blt_setup_target(NAME ${arg_NAME}
DEPENDS_ON ${arg_DEPENDS_ON}
OBJECT FALSE)
# Override the linker language with INTERFACE_BLT_LINKER_LANGUAGE_OVERRIDE, if applicable
# Will have just been populated by blt_setup_target
get_target_property(_blt_link_lang ${arg_NAME} INTERFACE_BLT_LINKER_LANGUAGE_OVERRIDE)
if(_blt_link_lang)
# This is the final link (b/c executable), so override the actual LINKER_LANGUAGE
# BLT currently uses this to override for HIP and CUDA linkers
set_target_properties(${arg_NAME} PROPERTIES LINKER_LANGUAGE ${_blt_link_lang})
endif()
# fix the openmp flags for fortran if needed
# NOTE: this needs to be called after blt_setup_target()
if (_lang STREQUAL Fortran)
blt_fix_fortran_openmp_flags( ${arg_NAME} )
endif()
if ( arg_INCLUDES )
target_include_directories(${arg_NAME} PUBLIC ${arg_INCLUDES})
endif()
if ( arg_DEFINES )
target_compile_definitions(${arg_NAME} PUBLIC ${arg_DEFINES})
endif()
if ( arg_OUTPUT_NAME )
set_target_properties(${arg_NAME} PROPERTIES
OUTPUT_NAME ${arg_OUTPUT_NAME} )
endif()
# when using shared libs on windows, all runtime targets
# (dlls and exes) must live in the same dir
# so we do not set runtime_output_dir in this case
if ( arg_OUTPUT_DIR AND NOT (WIN32 AND BUILD_SHARED_LIBS) )
set_target_properties(${arg_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${arg_OUTPUT_DIR} )
endif()
# Handle optional FOLDER keyword for this target
if(ENABLE_FOLDERS AND DEFINED arg_FOLDER)
blt_set_target_folder(TARGET ${arg_NAME} FOLDER "${arg_FOLDER}")
endif()
blt_update_project_sources( TARGET_SOURCES ${arg_SOURCES} ${arg_HEADERS} )
blt_clean_target(TARGET ${arg_NAME})
endmacro(blt_add_executable)
##------------------------------------------------------------------------------
## blt_add_test( NAME [name]
## COMMAND [command]
## NUM_MPI_TASKS [n]
## NUM_OMP_THREADS [n]
## CONFIGURATIONS [config1 [config2...]])
##
## Adds a test to the project.
##------------------------------------------------------------------------------
macro(blt_add_test)
set(options )
set(singleValueArgs NAME NUM_MPI_TASKS NUM_OMP_THREADS)
set(multiValueArgs COMMAND CONFIGURATIONS)
# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
if ( NOT DEFINED arg_NAME )
message(FATAL_ERROR "NAME is a required parameter to blt_add_test")
endif()
if ( NOT DEFINED arg_COMMAND )
message(FATAL_ERROR "COMMAND is a required parameter to blt_add_test")
endif()
# Extract test directory and executable from arg_NAME and arg_COMMAND
set(_test_directory)
if(NOT TARGET ${arg_NAME})
# Handle cases where multiple tests are run against one executable
# the NAME will not be the target
list(GET arg_COMMAND 0 _test_executable)
if(TARGET ${_test_executable})
get_target_property(_test_directory ${_test_executable} RUNTIME_OUTPUT_DIRECTORY )
endif()
else()
set(_test_executable ${arg_NAME})
get_target_property(_test_directory ${arg_NAME} RUNTIME_OUTPUT_DIRECTORY )
endif()
# Append the test_directory to the test argument, accounting for multi-config generators
if(NOT CMAKE_CONFIGURATION_TYPES)
if(NOT "${_test_directory}" STREQUAL "")
set(_test_command ${_test_directory}/${arg_COMMAND} )
else()
set(_test_command ${arg_COMMAND})
endif()
else()
if(TARGET ${_test_executable})
list(INSERT arg_COMMAND 0 "$<TARGET_FILE:${_test_executable}>")
list(REMOVE_AT arg_COMMAND 1)
endif()
set(_test_command ${arg_COMMAND})
endif()
# If configuration option ENABLE_WRAP_ALL_TESTS_WITH_MPIEXEC is set,
# ensure NUM_MPI_TASKS is at least one. This invokes the test
# through MPIEXEC.
if ( ENABLE_WRAP_ALL_TESTS_WITH_MPIEXEC AND NOT arg_NUM_MPI_TASKS )
set( arg_NUM_MPI_TASKS 1 )
endif()
# Handle MPI
if( arg_NUM_MPI_TASKS )
# Handle CMake changing MPIEXEC to MPIEXEC_EXECUTABLE
if( ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.10.0" )
set(_mpiexec ${MPIEXEC_EXECUTABLE})
else()
set(_mpiexec ${MPIEXEC})
endif()
set(_test_command ${_mpiexec} ${MPIEXEC_NUMPROC_FLAG} ${arg_NUM_MPI_TASKS} ${BLT_MPI_COMMAND_APPEND} ${_test_command} )
endif()
add_test(NAME ${arg_NAME}
COMMAND ${_test_command}
CONFIGURATIONS ${arg_CONFIGURATIONS})
# Handle OpenMP
if( arg_NUM_OMP_THREADS )
set_property(TEST ${arg_NAME}
APPEND PROPERTY ENVIRONMENT OMP_NUM_THREADS=${arg_NUM_OMP_THREADS})
endif()
endmacro(blt_add_test)
##------------------------------------------------------------------------------
## blt_add_benchmark( NAME [name]
## COMMAND [command]
## NUM_MPI_TASKS [n])
##
## Adds a benchmark to the project.
##------------------------------------------------------------------------------
macro(blt_add_benchmark)
set(options)
set(singleValueArgs NAME NUM_MPI_TASKS)
set(multiValueArgs COMMAND)
## parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# The 'CONFIGURATIONS Benchmark' line excludes benchmarks
# from the general list of tests
blt_add_test( NAME ${arg_NAME}
COMMAND ${arg_COMMAND}
NUM_MPI_TASKS ${arg_NUM_MPI_TASKS}
CONFIGURATIONS Benchmark)
endmacro(blt_add_benchmark)
##------------------------------------------------------------------------------
## blt_append_custom_compiler_flag(
## FLAGS_VAR flagsVar (required)
## DEFAULT defaultFlag (optional)
## GNU gnuFlag (optional)
## CLANG clangFlag (optional)
## INTEL intelFlag (optional)
## INTELLLVM intelLLVMFlag (optional)
## XL xlFlag (optional)
## MSVC msvcFlag (optional)
## MSVC_INTEL msvcIntelFlag (optional)
## PGI pgiFlag (optional)
## CRAY crayFlag (optional))
##
## Appends compiler-specific flags to a given variable of flags
##------------------------------------------------------------------------------
macro(blt_append_custom_compiler_flag)
set(options)
set(singleValueArgs FLAGS_VAR DEFAULT GNU CLANG PGI INTEL INTELLLVM XL MSVC MSVC_INTEL CRAY)
set(multiValueArgs)
# Parse the arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
# Sanity check for required keywords
if(NOT DEFINED arg_FLAGS_VAR)
message( FATAL_ERROR "append_custom_compiler_flag macro requires FLAGS_VAR keyword and argument." )
endif()
# Set the desired flags based on the compiler family
# MSVC COMPILER FAMILY applies to C/C++ and Fortran
string( TOLOWER ${arg_FLAGS_VAR} lower_flag_var )
if( DEFINED arg_MSVC_INTEL AND COMPILER_FAMILY_IS_MSVC_INTEL )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_MSVC_INTEL} " )
elseif( DEFINED arg_MSVC AND (COMPILER_FAMILY_IS_MSVC OR COMPILER_FAMILY_IS_MSVC_INTEL) )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_MSVC} " )
#else, if we are setting a fortran flag, check against the fortran compiler family
elseif ( ENABLE_FORTRAN AND ${lower_flag_var} MATCHES "fortran" )
if( DEFINED arg_CLANG AND Fortran_COMPILER_FAMILY_IS_CLANG )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_CLANG} " )
elseif( DEFINED arg_XL AND Fortran_COMPILER_FAMILY_IS_XL )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_XL} " )
elseif( DEFINED arg_INTEL AND Fortran_COMPILER_FAMILY_IS_INTEL )