-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLTMacros.cmake
1305 lines (1109 loc) · 51 KB
/
BLTMacros.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
# Copyright (c) 2017-2019, Lawrence Livermore National Security, LLC and
# other BLT Project Developers. See the top-level COPYRIGHT file for details
#
# SPDX-License-Identifier: (BSD-3-Clause)
include(${BLT_ROOT_DIR}/cmake/BLTPrivateMacros.cmake)
##------------------------------------------------------------------------------
## blt_list_append( TO <list> ELEMENTS [ <element>...] IF <bool> )
##
## Appends elements to a list if the specified bool evaluates to true.
##
## This macro is essentially a wrapper around CMake's `list(APPEND ...)`
## command which allows inlining a conditional check within the same call
## for clarity and convenience.
##
## This macro requires specifying:
## (1) The target list to append to by passing TO <list>
## (2) A condition to check by passing IF <bool>
## (3) The list of elements to append by passing ELEMENTS [<element>...]
##
## Note, the argument passed to the IF option has to be a single boolean value
## and cannot be a boolean expression since CMake cannot evaluate those inline.
##
## Usage Example:
##
## set(mylist A B)
## blt_list_append( TO mylist ELEMENTS C IF ${ENABLE_C} )
##
##------------------------------------------------------------------------------
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.
##
## This macro is essentially a wrapper around CMake's `list(REMOVE_DUPLICATES ...)`
## command but doesn't throw an error if the list is empty or not defined.
##
## Usage Example:
##
## set(mylist A B A)
## blt_list_remove_duplicates( TO mylist )
##
##------------------------------------------------------------------------------
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> TARGET_DEFINITIONS [FOO [BAR ...]])
##
## Adds pre-processor definitions to the given target.
##
## Adds pre-processor definitions to a particular target. This macro provides very
## similar functionality to cmake's native "add_definitions" command, but,
## it provides more fine-grained scoping for the compile definitions on a
## per target basis. Given a list of definitions, e.g., FOO and BAR, this macro
## adds compiler definitions to the compiler command for the given target, i.e.,
## it will pass -DFOO and -DBAR.
##
## The supplied target must be added via add_executable() or add_library() or
## with the corresponding blt_add_executable() and blt_add_library() macros.
##
## Note, the target definitions can either include or omit the "-D" characters.
## E.g. the following are all valid ways to add two compile definitions
## (A=1 and B) to target 'foo'
##
## blt_add_target_definitions(TO foo TARGET_DEFINITIONS A=1 B)
## blt_add_target_definitions(TO foo TARGET_DEFINITIONS -DA=1 -DB)
## blt_add_target_definitions(TO foo TARGET_DEFINITIONS "A=1;-DB")
## blt_add_target_definitions(TO foo TARGET_DEFINITIONS " " -DA=1;B")
##------------------------------------------------------------------------------
macro(blt_add_target_definitions)
set(options)
set(singleValueArgs TO)
set(multiValueArgs TARGET_DEFINITIONS)
# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
## check that the passed in parameter TO is actually a target
if(NOT TARGET ${arg_TO})
message(FATAL_ERROR "Target ${arg_TO} passed to blt_add_target_definitions is not a valid cmake target")
endif()
## only add the flag if it is not empty
string(STRIP "${arg_TARGET_DEFINITIONS}" _strippedDefs)
if(NOT "${_strippedDefs}" STREQUAL "")
get_property(_targetType TARGET ${arg_TO} PROPERTY TYPE)
if(${_targetType} STREQUAL "INTERFACE_LIBRARY")
target_compile_definitions(${arg_TO} INTERFACE ${_strippedDefs})
else()
target_compile_definitions(${arg_TO} PUBLIC ${_strippedDefs})
endif()
endif()
unset(_targetType)
unset(_strippedDefs)
endmacro(blt_add_target_definitions)
##------------------------------------------------------------------------------
## blt_add_target_compile_flags (TO <target> FLAGS [FOO [BAR ...]])
##
## Adds compiler flags to a target (library, executable or interface) by
## appending to the target's existing flags.
##
## The TO argument (required) specifies a cmake target.
##
## The FLAGS argument contains a list of compiler flags to add to the target.
## This macro will strip away leading and trailing whitespace from each flag.
##------------------------------------------------------------------------------
macro(blt_add_target_compile_flags)
set(options)
set(singleValuedArgs TO)
set(multiValuedArgs FLAGS)
## parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValuedArgs}" "${multiValuedArgs}" ${ARGN} )
## check that the passed in parameter TO is actually a target
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()
## only add the flag if it is not empty
string(STRIP "${arg_FLAGS}" _strippedFlags)
if(NOT "${_strippedFlags}" STREQUAL "")
get_property(_targetType TARGET ${arg_TO} PROPERTY TYPE)
if(${_targetType} STREQUAL "INTERFACE_LIBRARY")
target_compile_options(${arg_TO} INTERFACE ${_strippedFlags})
else()
target_compile_options(${arg_TO} PUBLIC ${_strippedFlags})
endif()
endif()
unset(_targetType)
unset(_strippedFlags)
endmacro(blt_add_target_compile_flags)
##------------------------------------------------------------------------------
## blt_set_target_folder (TARGET <target> FOLDER <folder>)
##
## Sets the folder property of cmake target <target> to <folder>.
##
## This feature is only available when blt's ENABLE_FOLDERS option is ON and
## in cmake generators that support folders (but is safe to call regardless
## of the generator or value of ENABLE_FOLDERS).
##
## Note: Do not use this macro on header-only (INTERFACE) library targets, since
## this will generate a cmake configuration error.
##------------------------------------------------------------------------------
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} )
## check for required arguments
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> FLAGS [FOO [BAR ...]])
##
## Adds linker flags to a target by appending to the target's existing flags.
##
## The FLAGS argument expects a ; delimited list of linker flags to add to the target.
##
## Note: In CMake versions prior to 3.13, this list is converted to a string internally
## and any ; characters will be removed.
##------------------------------------------------------------------------------
macro(blt_add_target_link_flags)
set(options)
set(singleValuedArgs TO)
set(multiValuedArgs FLAGS)
## parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValuedArgs}" "${multiValuedArgs}" ${ARGN} )
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 but expects a list
# not a string
separate_arguments(_flag_list NATIVE_COMMAND "${arg_FLAGS}" )
foreach( _flag ${_flag_list} )
set_property(TARGET ${arg_TO} APPEND PROPERTY LINK_OPTIONS "${_flag}" )
endforeach()
else()
get_target_property(_link_flags ${arg_TO} LINK_FLAGS)
if(NOT _link_flags)
set(_link_flags "")
endif()
set(_link_flags "${arg_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()
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.
##
## Stores information about a library in a specific way that is easily recalled
## in other macros. For example, after registering gtest, you can add gtest to
## the DEPENDS_ON in your blt_add_executable call and it will add the INCLUDES
## and LIBRARIES to that executable.
##
## TREAT_INCLUDES_AS_SYSTEM informs the compiler to treat this library's include paths
## as system headers. Only some compilers support this. This is useful if the headers
## generate warnings you want to not have them reported in your build. This defaults
## to OFF.
##
## This does not actually build the library. This is strictly to ease use after
## discovering it on your system or building it yourself inside your project.
##
## Note: The OBJECT parameter is for internal BLT support for object libraries
## and is not for users. Object libraries are created using blt_add_library().
##
## Output variables (name = "foo"):
## BLT_FOO_IS_REGISTERED_LIBRARY
## BLT_FOO_IS_OBJECT_LIBRARY
## BLT_FOO_DEPENDS_ON
## BLT_FOO_INCLUDES
## BLT_FOO_TREAT_INCLUDES_AS_SYSTEM
## BLT_FOO_FORTRAN_MODULES
## BLT_FOO_LIBRARIES
## BLT_FOO_COMPILE_FLAGS
## BLT_FOO_LINK_FLAGS
## BLT_FOO_DEFINES
##------------------------------------------------------------------------------
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 LIST "" FORCE)
mark_as_advanced(BLT_${uppercase_name}_DEPENDS_ON)
endif()
if( arg_INCLUDES )
set(BLT_${uppercase_name}_INCLUDES ${arg_INCLUDES} CACHE LIST "" 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)
if( ${arg_TREAT_INCLUDES_AS_SYSTEM} )
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 LIST "" FORCE)
mark_as_advanced(BLT_${uppercase_name}_FORTRAN_MODULES)
endif()
if( arg_LIBRARIES )
set(BLT_${uppercase_name}_LIBRARIES ${arg_LIBRARIES} CACHE LIST "" 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 LIST "" FORCE)
mark_as_advanced(BLT_${uppercase_name}_DEFINES)
endif()
endmacro(blt_register_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.
## This macro uses the BUILD_SHARED_LIBS, which is defaulted to OFF, to determine
## whether the library will be build as shared or static. The optional boolean
## SHARED argument can be used to override this choice.
##
## The OBJECT argument creates a CMake object library. Basically it is a collection
## of compiled source files that are not archived or linked. Unlike regular CMake
## object libraries you do not have to use the $<TARGET_OBJECTS:<libname>> syntax,
## you can just use <libname>.
## Note: Object libraries do not follow CMake's transitivity rules until 3.12.
## BLT will add the various information provided in this macro and its
## dependencies in the order you provide them to help.
##
## The INCLUDES argument allows you to define what include directories are
## needed by any target that is dependent on this library. These will
## be inherited by CMake's target dependency rules.
##
## The DEFINES argument allows you to add needed compiler definitions that are
## needed by any target that is dependent on this library. These will
## be inherited by CMake's target dependency rules.
##
## If given a DEPENDS_ON argument, it will add the necessary includes and
## libraries if they are already registered with blt_register_library. If
## not it will add them as a CMake target dependency.
##
## In addition, this macro will add the associated dependencies to the given
## library target. Specifically, it will add the dependency for the CMake target
## and for copying the headers for that target as well.
##
## The OUTPUT_DIR is used to control the build output directory of this
## library. This is used to overwrite the default lib directory.
##
## OUTPUT_NAME is the name of the output file; the default is NAME.
## It's useful when multiple libraries with the same name need to be created
## by different targets. NAME is the target name, OUTPUT_NAME is the library name.
##
## CLEAR_PREFIX allows you to remove the automatically appended "lib" prefix
## from your built library. The created library will be foo.a instead of libfoo.a.
##
## FOLDER is an optional keyword to organize the target into a folder in an IDE.
## This is available when ENABLE_FOLDERS is ON and when the cmake generator
## supports this feature and will otherwise be ignored.
## Note: Do not use with header-only (INTERFACE)libraries, as this will generate
## a cmake configuration error.
##------------------------------------------------------------------------------
macro(blt_add_library)
set(options)
set(singleValueArgs NAME OUTPUT_NAME OUTPUT_DIR HEADERS_OUTPUT_SUBDIR 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()
if (ENABLE_HIP)
blt_add_hip_library(NAME ${arg_NAME}
SOURCES ${arg_SOURCES}
HEADERS ${arg_HEADERS}
DEPENDS_ON ${arg_DEPENDS_ON}
LIBRARY_TYPE ${_lib_type} )
else()
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()
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 ...]]
## INCLUDES [dir1 [dir2 ...]]
## DEFINES [define1 [define2 ...]]
## DEPENDS_ON [dep1 [dep2 ...]]
## OUTPUT_DIR [dir]
## FOLDER [name])
##
## Adds an executable target, called <name>.
##
## The INCLUDES argument allows you to define what include directories are
## needed to compile this executable.
##
## The DEFINES argument allows you to add needed compiler definitions that are
## needed to compile this executable.
##
## If given a DEPENDS_ON argument, it will add the necessary includes and
## libraries if they are already registered with blt_register_library. If
## not it will add them as a cmake target dependency.
##
## The OUTPUT_DIR is used to control the build output directory of this
## executable. This is used to overwrite the default bin directory.
##
## If the first entry in SOURCES is a Fortran source file, the fortran linker
## is used. (via setting the CMake target property LINKER_LANGUAGE to Fortran )
##
## FOLDER is an optional keyword to organize the target into a folder in an IDE.
## This is available when ENABLE_FOLDERS is ON and when using a cmake generator
## that supports this feature and will otherwise be ignored.
##------------------------------------------------------------------------------
macro(blt_add_executable)
set(options )
set(singleValueArgs NAME OUTPUT_DIR FOLDER)
set(multiValueArgs 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()
if (ENABLE_HIP)
blt_add_hip_executable(NAME ${arg_NAME}
SOURCES ${arg_SOURCES}
DEPENDS_ON ${arg_DEPENDS_ON})
else()
add_executable( ${arg_NAME} ${arg_SOURCES} )
if (ENABLE_CUDA AND NOT ENABLE_CLANG_CUDA)
blt_setup_cuda_target(
NAME ${arg_NAME}
SOURCES ${arg_SOURCES}
DEPENDS_ON ${arg_DEPENDS_ON})
endif()
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)
if (NOT CUDA_LINK_WITH_NVCC)
set_target_properties( ${arg_NAME} PROPERTIES LINKER_LANGUAGE Fortran )
endif()
target_include_directories(${arg_NAME} PRIVATE ${CMAKE_Fortran_MODULE_DIRECTORY})
endif()
blt_setup_target(NAME ${arg_NAME}
DEPENDS_ON ${arg_DEPENDS_ON}
OBJECT FALSE)
if ( arg_INCLUDES )
target_include_directories(${arg_NAME} PUBLIC ${arg_INCLUDES})
endif()
if ( arg_DEFINES )
target_compile_definitions(${arg_NAME} PUBLIC ${arg_DEFINES})
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} )
blt_clean_target(TARGET ${arg_NAME})
endmacro(blt_add_executable)
##------------------------------------------------------------------------------
## blt_add_test( NAME [name] COMMAND [command] NUM_MPI_TASKS [n] )
##
## Adds a CMake test to the project.
##
## NAME is used for the name that CTest reports with.
##
## COMMAND is the command line that will be used to run the test. This will
## have the RUNTIME_OUTPUT_DIRECTORY prepended to it to fully qualify the path.
##
## NUM_MPI_TASKS indicates this is an MPI test and how many tasks to use. The
## command line will use MPIEXEC, MPIEXEC_NUMPROC_FLAG, and BLT_MPI_COMMAND_APPEND
## to create the MPI run line.
##
## MPIEXEC and MPIEXEC_NUMPROC_FLAG are filled in by CMake's FindMPI.cmake but can
## be overwritten in your host-config specific to your platform. BLT_MPI_COMMAND_APPEND
## is useful on machines that require extra arguments to MPIEXEC.
##------------------------------------------------------------------------------
macro(blt_add_test)
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} )
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
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)
get_target_property(test_directory ${test_executable} RUNTIME_OUTPUT_DIRECTORY )
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)
set(test_command ${test_directory}/${arg_COMMAND} )
else()
list(INSERT arg_COMMAND 0 "$<TARGET_FILE:${test_executable}>")
list(REMOVE_AT arg_COMMAND 1)
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} )
endmacro(blt_add_test)
##------------------------------------------------------------------------------
## blt_add_benchmark( NAME [name] COMMAND [command] )
##
## Adds a (google) benchmark test to the project.
##
## NAME is used for the name that CTest reports and should include the string 'benchmark'.
##
## COMMAND is the command line that will be used to run the test and can include arguments.
## This will have the RUNTIME_OUTPUT_DIRECTORY prepended to it to fully qualify the path.
##
## The underlying executable (added with blt_add_executable) should include gbenchmark
## as one of its dependencies.
##
## Example
## blt_add_executable(NAME component_benchmark ... DEPENDS gbenchmark)
## blt_add_benchmark(
## NAME component_benchmark
## COMMAND component_benchmark "--benchmark_min_time=0.0 --v=3 --benchmark_format=json"
## )
##------------------------------------------------------------------------------
macro(blt_add_benchmark)
if(ENABLE_BENCHMARKS)
set(options)
set(singleValueArgs NAME)
set(multiValueArgs COMMAND)
## 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_benchmark")
endif()
if ( NOT DEFINED arg_COMMAND )
message(FATAL_ERROR "COMMAND is a required parameter to blt_add_benchmark")
endif()
# Generate command
if ( NOT TARGET ${arg_NAME} )
# Handle case of running multiple tests against one executable,
# the NAME will not be the target
list(GET arg_COMMAND 0 executable)
get_target_property(runtime_output_directory ${executable} RUNTIME_OUTPUT_DIRECTORY )
else()
get_target_property(runtime_output_directory ${arg_NAME} RUNTIME_OUTPUT_DIRECTORY )
endif()
set(test_command ${runtime_output_directory}/${arg_COMMAND} )
# Note: No MPI handling for now. If desired, see how this is handled in blt_add_test macro
# The 'CONFIGURATIONS Benchmark' line excludes benchmarks
# from the general list of tests
add_test( NAME ${arg_NAME}
COMMAND ${test_command}
CONFIGURATIONS Benchmark
)
if(ENABLE_TESTS)
add_dependencies(run_benchmarks ${arg_NAME})
endif()
endif()
endmacro(blt_add_benchmark)
##------------------------------------------------------------------------------
## blt_append_custom_compiler_flag(
## FLAGS_VAR flagsVar (required)
## DEFAULT defaultFlag (optional)
## GNU gnuFlag (optional)
## CLANG clangFlag (optional)
## HCC hccFlag (optional)
## INTEL intelFlag (optional)
## XL xlFlag (optional)
## MSVC msvcFlag (optional)
## MSVC_INTEL msvcIntelFlag (optional)
## PGI pgiFlag (optional)
## )
##
## Appends compiler-specific flags to a given variable of flags
##
## If a custom flag is given for the current compiler, we use that.
## Otherwise, we will use the DEFAULT flag (if present)
## If ENABLE_FORTRAN is On, any flagsVar with "fortran" (any capitalization)
## in its name will pick the compiler family (GNU,CLANG, INTEL, etc) based on
## the fortran compiler family type. This allows mixing C and Fortran compiler
## families, e.g. using Intel fortran compilers with clang C compilers.
## When using the Intel toolchain within visual studio, we use the
## MSVC_INTEL flag, when provided, with a fallback to the MSVC flag.
##------------------------------------------------------------------------------
macro(blt_append_custom_compiler_flag)
set(options)
set(singleValueArgs FLAGS_VAR DEFAULT GNU CLANG HCC PGI INTEL XL MSVC MSVC_INTEL)
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 )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_INTEL} " )
elseif( DEFINED arg_PGI AND Fortran_COMPILER_FAMILY_IS_PGI )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_PGI} " )
elseif( DEFINED arg_GNU AND Fortran_COMPILER_FAMILY_IS_GNU )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_GNU} " )
elseif( DEFINED arg_DEFAULT )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_DEFAULT} ")
endif()
#else, we are setting a non MSVC C/C++ flag, check against the C family.
else()
if( DEFINED arg_CLANG AND C_COMPILER_FAMILY_IS_CLANG )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_CLANG} " )
elseif( DEFINED arg_XL AND C_COMPILER_FAMILY_IS_XL )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_XL} " )
elseif( DEFINED arg_INTEL AND C_COMPILER_FAMILY_IS_INTEL )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_INTEL} " )
elseif( DEFINED arg_PGI AND C_COMPILER_FAMILY_IS_PGI )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_PGI} " )
elseif( DEFINED arg_GNU AND C_COMPILER_FAMILY_IS_GNU )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_GNU} " )
elseif( DEFINED arg_DEFAULT )
set (${arg_FLAGS_VAR} "${${arg_FLAGS_VAR}} ${arg_DEFAULT} ")
endif()
endif()
unset(lower_flag_var)
endmacro(blt_append_custom_compiler_flag)
##------------------------------------------------------------------------------
## blt_find_libraries( FOUND_LIBS <FOUND_LIBS variable name>
## NAMES [libname1 [libname2 ...]]
## REQUIRED [TRUE (default) | FALSE ]
## PATHS [path1 [path2 ...]] )
##
## This command is used to find a list of libraries.
##
## If the libraries are found the results are appended to the given FOUND_LIBS variable name.
##
## NAMES lists the names of the libraries that will be searched for in the given PATHS.
##
## If REQUIRED is set to TRUE, BLT will produce an error message if any of the
## given libraries are not found. The default value is TRUE.
##
## PATH lists the paths in which to search for NAMES. No system paths will be searched.
##
##------------------------------------------------------------------------------
macro(blt_find_libraries)
set(options )
set(singleValueArgs FOUND_LIBS REQUIRED )
set(multiValueArgs NAMES PATHS )
## parse the arguments
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN} )
if ( NOT DEFINED arg_FOUND_LIBS )
message(FATAL_ERROR "The blt_find_libraries required parameter FOUND_LIBS specifies the list that found libraries will be appended to.")
endif()
if ( NOT DEFINED arg_NAMES )
message(FATAL_ERROR "The blt_find_libraries required parameter NAMES specifies the library names you are searching for.")
endif()
if ( NOT DEFINED arg_PATHS )
message(FATAL_ERROR "The blt_find_libraries required parameter PATHS specifies the paths to search for NAMES.")
endif()