-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCMakeUnit.cmake
1557 lines (1149 loc) · 49.9 KB
/
CMakeUnit.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
# /CMakeUnit.cmake
#
# A Simple CMake Unit Testing Framework - matchers
# and utility library.
#
# This file provides some simple matchers for CMakeUnit
# which test scripts can use to verify certain details about
# what CMake knows about targets and properties set up.
#
# Tests are constructed in the xUnit style, a single, generalized
# cmake_unit_assert_that function is provided along with a standard
# library of matchers. Assert things as so:
#
# cmake_unit_assert_that (MY_VARIABLE compare_as STRING EQUAL "value")
# cmake_unit_assert_that ("/File" file_contents compare_as "hello")
# cmake_unit_assert_that (target is_linked_to other_target)
#
# This library also provides some utility functions which are useful
# in implementing CMake tests, like functions to generate source
# files and simple executables or to find the location of such
# libraries and executables at verify-time.
#
# See /LICENCE.md for Copyright information
include ("cmake/cmake-include-guard/IncludeGuard")
cmake_include_guard (SET_MODULE_PATH)
include (CMakeParseArguments)
include (GenerateExportHeader)
include ("cmake/cmake-call-function/CallFunction")
include ("cmake/cmake-forward-arguments/ForwardArguments")
include ("cmake/cmake-opt-arg-parsing/OptimizedParseArguments")
include ("cmake/cmake-spacify-list/SpacifyList")
set (_CMAKE_UNIT_LIST_DIR "${CMAKE_CURRENT_LIST_DIR}")
# _cmake_unit_get_hash_for_file
#
# Lazy-compute a hash for the specified file
#
# FILE: File to compute hash for.
# RETURN_HASH: Variable to place hash in.
function (_cmake_unit_get_hash_for_file FILE RETURN_HASH)
set (CALLING_FILE_HASH_PROPERTY "${CALLING_FILE}_HASH")
get_property (CALLING_FILE_HASH_SET
GLOBAL PROPERTY "${CALLING_FILE_HASH_PROPERTY}"
SET)
if (NOT CALLING_FILE_HASH_SET)
file (SHA1 "${FILE}"
_COMPUTE_USER_HASH_CURRENT_USER_FILE_SHA1)
set_property (GLOBAL PROPERTY "${CALLING_FILE_HASH_PROPERTY}"
"${_COMPUTE_USER_HASH_CURRENT_USER_FILE_SHA1}")
endif ()
get_property (CALLING_FILE_HASH
GLOBAL PROPERTY "${CALLING_FILE_HASH_PROPERTY}")
set (${RETURN_HASH} "${CALLING_FILE_HASH}" PARENT_SCOPE)
endfunction ()
# cmake_unit_should_write
#
# Determine if the CALLING_FILE is newer than FILE and if so, write the
# CALLING_FILE's hash on disk and return true.
#
# You should use this function if you need to write the file in a special
# way that does not allow its contents to be passed directly to
# cmake_unit_write_if_newer.
#
# FILE: File proposed to be written to.
# CALLING_FILE: File to compare with.
# SHOULD_WRITE_RETURN: Name of variable to set should-write status to.
function (cmake_unit_should_write FILE CALLING_FILE SHOULD_WRITE_RETURN)
set (HASH_FILE "${FILE}.stamp.sha1")
set (SHOULD_WRITE TRUE)
_cmake_unit_get_hash_for_file ("${CALLING_FILE}" CALLING_FILE_HASH)
if (EXISTS "${HASH_FILE}")
file (READ "${HASH_FILE}" HASH_FILE_CONTENTS)
if ("${HASH_FILE_CONTENTS}" STREQUAL "${CALLING_FILE_HASH}")
set (SHOULD_WRITE FALSE)
endif ()
endif ()
if (SHOULD_WRITE)
file (WRITE "${HASH_FILE}" "${CALLING_FILE_HASH}")
endif ()
set (${SHOULD_WRITE_RETURN} ${SHOULD_WRITE} PARENT_SCOPE)
endfunction ()
# cmake_unit_write_if_newer
#
# Write contents as specified in ARGN to file specified in FILE
# if the CALLING_FILE is newer than FILE, or FILE does not exist.
#
# FILE: File to write to.
# CALLING_FILE: File to compare with.
function (cmake_unit_write_if_newer FILE CALLING_FILE)
cmake_unit_should_write ("${FILE}" "${CALLING_FILE}" PERFORM_WRITE)
if (PERFORM_WRITE)
file (WRITE "${FILE}" ${ARGN})
endif ()
endfunction ()
# cmake_unit_escape_string
#
# Escape all regex control characters from INPUT and store in
# OUTPUT_VARIABLE
#
# INPUT: Input string
# OUTPUT_VARIABLE: Name of variable to store escaped string into
function (cmake_unit_escape_string INPUT OUTPUT_VARIABLE)
string (REPLACE "\\" "\\\\" INPUT "${INPUT}")
string (REPLACE "(" "\\(" INPUT "${INPUT}")
string (REPLACE ")" "\\)" INPUT "${INPUT}")
string (REPLACE "[" "\\[" INPUT "${INPUT}")
string (REPLACE "]" "\\]" INPUT "${INPUT}")
string (REPLACE "*" "\\*" INPUT "${INPUT}")
string (REPLACE "+" "\\+" INPUT "${INPUT}")
string (REPLACE "$" "\\$" INPUT "${INPUT}")
string (REPLACE "^" "\\^" INPUT "${INPUT}")
string (REPLACE "}" "\\}" INPUT "${INPUT}")
string (REPLACE "{" "\\{" INPUT "${INPUT}")
set (${OUTPUT_VARIABLE} "${INPUT}" PARENT_SCOPE)
endfunction ()
# Helper macro to append an accumulated command list to
# ADD_CUSTOM_COMMAND_PRINT_STRINGS
#
# Do not call this macro outside of the add_custom_command
# wrapper. Do not change it to a function.
macro (_cmake_unit_append_command_being_accumulated)
if (ACCUMULATING_COMMAND)
string (REPLACE ";" " "
STRINGIFIED_COMMAND "${COMMAND_BEING_ACCUMULATED}")
list (APPEND
ADD_CUSTOM_COMMAND_PRINT_STRINGS # NOLINT:unused/var_in_func
COMMAND "${STRINGIFIED_COMMAND}")
unset (COMMAND_BEING_ACCUMULATED)
set (ACCUMULATING_COMMAND FALSE)
endif ()
endmacro ()
# Wraps add_custom_command to print out the COMMAND line on generators that
# wont print that even when verbose mode is enabled.
#
# This function must be named add_custom_command, so we are disabling
# structure/namespace here
function (add_custom_command) # NOLINT:structure/namespace
set (ADD_CUSTOM_COMMAND_KNOWN_ARGUMENTS
OUTPUT
COMMAND
MAIN_DEPENDENCY
DEPENDS
IMPLICIT_DEPENDS
WORKING_DIRECTORY
COMMENT
VERBATIM)
set (ADD_CUSTOM_COMMAND_PRINT_STRINGS)
# COMMAND can be repeated multiple times inside of add_custom_command
# so we can't use cmake_parse_arguments to extract it. Instead we
# need to loop through all the arguments and find instances of
# COMMAND. Once one is found, we'll stop and add a a new COMMAND to
# append-arguments list to print it
set (ACCUMULATING_COMMAND FALSE)
set (COMMAND_BEING_ACCUMULATED)
foreach (ARG ${ARGN})
foreach (KNOWN_ARG ${ADD_CUSTOM_COMMAND_KNOWN_ARGUMENTS})
if (KNOWN_ARG STREQUAL ARG)
# Hit a new list argument. If we're accumulating a command,
# stop and add it to our ADD_CUSTOM_COMMAND_PRINT_STRINGS list
_cmake_unit_append_command_being_accumulated ()
endif ()
endforeach ()
if (ACCUMULATING_COMMAND)
list (APPEND COMMAND_BEING_ACCUMULATED
${ARG})
endif ()
# Avoid CMP0054 violation
set (COMMAND_ARG "COMMAND")
# If the arg we just hit was COMMAND, then start
# accumulating commands
if (ARG STREQUAL COMMAND_ARG)
set (ACCUMULATING_COMMAND TRUE)
endif ()
endforeach ()
# End of list. If a command was being accumulated, add it now
_cmake_unit_append_command_being_accumulated ()
# Now loop over ADD_CUSTOM_COMMAND_PRINT_STRINGS to build the
# ADD_CUSTOM_COMMAND_APPEND_ARGUMENTS list, but only if
# we are actually in a test
set (PHASE "${_CMAKE_UNIT_PHASE}") # NOLINT:access/private_var
if (PHASE STREQUAL "CONFIGURE")
set (ADD_CUSTOM_COMMAND_APPEND_ARGUMENTS)
foreach (STRING ${ADD_CUSTOM_COMMAND_PRINT_STRINGS})
list (APPEND ADD_CUSTOM_COMMAND_APPEND_ARGUMENTS
COMMAND "${CMAKE_COMMAND}" -E echo "${STRING}")
endforeach ()
endif ()
# Obviously, the private function must be accessed
_add_custom_command (${ARGN} # NOLINT:access/other_private
${ADD_CUSTOM_COMMAND_APPEND_ARGUMENTS})
endfunction ()
set (_CMAKE_UNIT_SOURCE_FILE_OPTION_ARGS)
set (_CMAKE_UNIT_SOURCE_FILE_SINGLEVAR_ARGS NAME FUNCTIONS_EXPORT_TARGET)
set (_CMAKE_UNIT_SOURCE_FILE_MULTIVAR_ARGS
INCLUDES
DEFINES
FUNCTIONS
PREPEND_CONTENTS
INCLUDE_DIRECTORIES)
function (_cmake_unit_get_created_source_file_contents CONTENTS_RETURN
NAME_RETURN)
set (GET_CREATED_SOURCE_FILE_OPTION_ARGS
${_CMAKE_UNIT_SOURCE_FILE_OPTION_ARGS})
set (GET_CREATED_CONTENTS_SINGLEVAR_ARGS
${_CMAKE_UNIT_SOURCE_FILE_SINGLEVAR_ARGS})
set (GET_CREATED_CONTENTS_MULTIVAR_ARGS
${_CMAKE_UNIT_SOURCE_FILE_MULTIVAR_ARGS})
cmake_parse_arguments (GET_CREATED
"${GET_CREATED_SOURCE_FILE_OPTION_ARGS}"
"${GET_CREATED_CONTENTS_SINGLEVAR_ARGS}"
"${GET_CREATED_CONTENTS_MULTIVAR_ARGS}"
${ARGN})
if (NOT GET_CREATED_NAME)
set (GET_CREATED_NAME "Source.cpp")
endif ()
# Detect intended file type from filename
get_filename_component (EXTENSION "${GET_CREATED_NAME}" EXT)
if (NOT EXTENSION)
message (FATAL_ERROR "Need to specify an extension in order to get "
"correct source file contents for this file. The "
"current name is ${GET_CREATED_NAME}.")
endif ()
string (SUBSTRING "${EXTENSION}" 1 -1 EXTENSION)
set (SOURCE_EXTENSIONS
${CMAKE_C_SOURCE_FILE_EXTENSIONS}
${CMAKE_CXX_SOURCE_FILE_EXTENSIONS})
list (FIND SOURCE_EXTENSIONS ${EXTENSION} SOURCE_INDEX)
if (SOURCE_INDEX EQUAL -1)
set (SOURCE_TYPE HEADER)
else ()
set (SOURCE_TYPE SOURCE)
endif ()
# Header guards (if header)
if (SOURCE_TYPE STREQUAL "HEADER")
get_filename_component (HEADER_BASENAME "${GET_CREATED_NAME}" NAME)
string (REPLACE "." "_" HEADER_BASENAME "${HEADER_BASENAME}")
string (TOUPPER "${HEADER_BASENAME}" HEADER_GUARD)
list (APPEND CONTENTS
"#ifndef ${HEADER_GUARD}"
"#define ${HEADER_GUARD}")
endif ()
# If this is a "source" file and FUNCTIONS_EXPORT_TARGET is set then
# we're building a library. As such, we need to insert some platform
# specific defines to indicate that functions should be exported.
if (GET_CREATED_FUNCTIONS_EXPORT_TARGET)
set (EXPORT_HEADER "${GET_CREATED_FUNCTIONS_EXPORT_TARGET}_export.h")
set (EXPORT_HEADER_PATH "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_HEADER}")
list (APPEND CONTENTS "#include \"${EXPORT_HEADER_PATH}\"")
string (TOUPPER "${GET_CREATED_FUNCTIONS_EXPORT_TARGET}"
EXPORT_TARGET_UPPER)
set (EXPORT_MACRO "${EXPORT_TARGET_UPPER}_EXPORT ")
endif ()
# Defines
foreach (DEFINE ${GET_CREATED_DEFINES})
list (APPEND CONTENTS
"#define ${DEFINE}")
endforeach ()
# Includes
foreach (INCLUDE ${GET_CREATED_INCLUDES})
set (INCLUDED_AT_GLOBAL_SCOPE FALSE)
foreach (DIR ${GET_CREATED_INCLUDE_DIRECTORIES})
string (LENGTH "${DIR}" DIR_LENGTH)
string (LENGTH "${INCLUDE}" INCLUDE_LENGTH)
# If DIR_LENGTH is greater than INCLUDE_LENGTH then
# the INCLUDE is definitely not within DIR. Avoid a STRING error.
if (DIR_LENGTH LESS INCLUDE_LENGTH)
string (SUBSTRING "${INCLUDE}" 0 ${DIR_LENGTH} INCLUDE_BEGIN)
# If its the same, then this include was part of the specified
# DIR, so put the rest of it in angle brackets
if ("${INCLUDE_BEGIN}" STREQUAL "${DIR}")
math (EXPR INCLUDE_END_START "${DIR_LENGTH} + 1")
string (SUBSTRING "${INCLUDE}" ${INCLUDE_END_START} -1
INCLUDE_END)
list (APPEND CONTENTS
"#include <${INCLUDE_END}>")
set (INCLUDED_AT_GLOBAL_SCOPE TRUE)
break ()
endif ()
endif ()
endforeach ()
if (NOT INCLUDED_AT_GLOBAL_SCOPE)
list (APPEND CONTENTS
"#include \"${INCLUDE}\"")
endif ()
endforeach ()
# Forward declare all functions
foreach (FUNCTION ${GET_CREATED_FUNCTIONS})
# EXPORT_MACRO might be empty, so there's no space here
# (we insert the space in the nonempty case)
list (APPEND CONTENTS
"${EXPORT_MACRO}int ${FUNCTION} ()@SEMICOLON@")
endforeach ()
# Prepend Contents - these must come after includes, defines
# and function decls
if (GET_CREATED_PREPEND_CONTENTS)
list (APPEND CONTENTS "${GET_CREATED_PREPEND_CONTENTS}")
endif ()
# Function definitions, but only if we're a source
if ("${SOURCE_TYPE}" STREQUAL "SOURCE")
foreach (FUNCTION ${GET_CREATED_FUNCTIONS})
list (APPEND CONTENTS
"int ${FUNCTION} ()"
"{"
" return 0@SEMICOLON@"
"}")
endforeach ()
endif ()
# End header guard
if ("${SOURCE_TYPE}" STREQUAL "HEADER")
list (APPEND CONTENTS
"#endif")
endif ()
set (${NAME_RETURN} "${GET_CREATED_NAME}" PARENT_SCOPE)
set (${CONTENTS_RETURN} "${CONTENTS}" PARENT_SCOPE)
endfunction ()
function (_cmake_unit_write_out_file_without_semicolons NAME)
cmake_parse_arguments (WRITE_OUT_FILE
""
"GENERATING_FILE;DIRECTORY"
"CONTENTS"
${ARGN})
string (REPLACE ";" "\n" CONTENTS "${WRITE_OUT_FILE_CONTENTS}")
string (REPLACE "@SEMICOLON@" ";" CONTENTS "${CONTENTS}")
set (SHOULD_WRITE TRUE)
if (WRITE_OUT_FILE_GENERATING_FILE)
cmake_unit_should_write ("${CMAKE_CURRENT_SOURCE_DIR}/${NAME}"
"${WRITE_OUT_FILE_GENERATING_FILE}"
SHOULD_WRITE)
endif ()
if (NOT WRITE_OUT_FILE_DIRECTORY)
set (WRITE_OUT_FILE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
endif ()
if (SHOULD_WRITE)
get_filename_component (PARENT_PATH
"${TMP_BINARY_DIR_LOCATION}"
DIRECTORY)
file (MAKE_DIRECTORY "${PARENT_PATH}")
file (WRITE "${WRITE_OUT_FILE_DIRECTORY}/${NAME}"
"${CONTENTS}\n")
endif ()
endfunction ()
# cmake_unit_create_source_file_before_build
#
# Writes out a source file, for use with add_library, add_executable
# or source scanners during the configure phase.
#
# If the source is detected as a header based on the NAME property such that
# it does not have a C or C++ extension, then header guards will be written
# and function definitions will not be included.
#
# [Optional] NAME: Name of the source file. May include slashes which will
# be interpreted as a subdirectory relative to
# CMAKE_CURRENT_SOURCE_DIR. The default is Source.cpp
# [Optional] INCLUDES: A list of files, relative or absolute paths, to #include
# [Optional] DEFINES: A list of #defines (macro name only)
# [Optional] FUNCTIONS: A list of functions.
# [Optional] PREPEND_CONTENTS: Contents to include in the file after
# INCLUDES, DEFINES and Function Declarations,
# but before Function Definitions
# [Optional] INCLUDE_DIRECTORIES: A list of directories such that, if an entry
# in the INCLUDES list has the same directory
# name as an entry in INCLUDE_DIRECTORIES then
# the entry will be angle-brackets <include>
# with the path relative to that include
# directory.
# [Optional] GENERATING_FILE: File which is responsible for generating the
# specified file, used to avoid redundant writes
# into the source directory.
function (cmake_unit_create_source_file_before_build)
set (CREATE_BEFORE_BUILD_OPTION_ARGS
${_CMAKE_UNIT_SOURCE_FILE_OPTION_ARGS})
set (CREATE_BEFORE_BUILD_SINGLEVAR_ARGS
GENERATING_FILE
${_CMAKE_UNIT_SOURCE_FILE_SINGLEVAR_ARGS})
set (CREATE_BEFORE_BUILD_MULTIVAR_ARGS
${_CMAKE_UNIT_SOURCE_FILE_MULTIVAR_ARGS})
cmake_parse_arguments (CREATE_BEFORE_BUILD
"${CREATE_BEFORE_BUILD_OPTION_ARGS}"
"${CREATE_BEFORE_BUILD_SINGLEVAR_ARGS}"
"${CREATE_BEFORE_BUILD_MULTIVAR_ARGS}"
${ARGN})
set (GENERATING_FILE "${CREATE_BEFORE_BUILD_GENERATING_FILE}")
cmake_forward_arguments (CREATE_BEFORE_BUILD GET_CREATED_ARGUMENTS
OPTION_ARGS
${_CMAKE_UNIT_SOURCE_FILE_OPTION_ARGS}
SINGLEVAR_ARGS
${_CMAKE_UNIT_SOURCE_FILE_SINGLEVAR_ARGS}
MULTIVAR_ARGS
${_CMAKE_UNIT_SOURCE_FILE_MULTIVAR_ARGS})
_cmake_unit_get_created_source_file_contents (CONTENTS NAME
${GET_CREATED_ARGUMENTS})
_cmake_unit_write_out_file_without_semicolons ("${NAME}"
CONTENTS ${CONTENTS}
GENERATING_FILE
"${GENERATING_FILE}")
endfunction ()
# cmake_unit_generate_source_file_during_build
#
# Generates a source file, for use with add_library, add_executable
# or source scanners during the build phase.
#
# If the source is detected as a header based on the NAME property such that
# it does not have a C or C++ extension, then header guards will be written
# and function definitions will not be included.
#
# TARGET_RETURN: Name of the target that this source file will be generated on.
# [Optional] NAME: Name of the source file. May include slashes which will
# be interpreted as a subdirectory relative to
# CMAKE_CURRENT_SOURCE_DIR. The default is Source.cpp
# [Optional] FUNCTIONS_EXPORT_TARGET: The target that this source file is
# built for. Generally this is used
# if it is necessary to export functions
# from this source file.
# cmake_unit_create_simple_library uses
# this argument for instance.
# [Optional] INCLUDES: A list of files, relative or absolute paths, to #include
# [Optional] DEFINES: A list of #defines (macro name only)
# [Optional] FUNCTIONS: A list of functions.
# [Optional] PREPEND_CONTENTS: Contents to include in the file after
# INCLUDES, DEFINES and Function Declarations,
# but before Function Definitions
# [Optional] INCLUDE_DIRECTORIES: A list of directories such that, if an entry
# in the INCLUDES list has the same directory
# name as an entry in INCLUDE_DIRECTORIES then
# the entry will be angle-brackets <include>
# with the path relative to that include
# directory.
function (cmake_unit_generate_source_file_during_build TARGET_RETURN)
# Write out to temporary location, which we'll later move into place
# during the build
_cmake_unit_get_created_source_file_contents (CONTENTS NAME ${ARGN})
string (RANDOM SALT)
_cmake_unit_write_out_file_without_semicolons ("${NAME}${SALT}"
DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}"
CONTENTS ${CONTENTS})
set (TMP_BINARY_DIR_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${NAME}${SALT}")
set (WRITE_SOURCE_FILE_SCRIPT
"${CMAKE_CURRENT_BINARY_DIR}/Write${NAME}${SALT}.cmake")
file (WRITE "${WRITE_SOURCE_FILE_SCRIPT}"
"if (NOT EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/${NAME}\")\n"
" file (READ \"${TMP_BINARY_DIR_LOCATION}\"\n"
" GENERATED_FILE_CONTENTS)\n"
" file (WRITE \"${CMAKE_CURRENT_BINARY_DIR}/${NAME}\"\n"
" \"\${GENERATED_FILE_CONTENTS}\")\n"
" file (REMOVE \"${TMP_BINARY_DIR_LOCATION}\")\n"
"endif ()\n")
# Generate target name, convert temporary location to lowercase, limit to
# 10 characters so that we don't hit file name size limits on Windows.
string (MD5 TARGET_NAME_HASH "${TMP_BINARY_DIR_LOCATION}")
string (SUBSTRING "${TARGET_NAME_HASH}" 0 10 TARGET_NAME_HASH)
set (INVOKE_IF_EXISTS_SCRIPT
"${_CMAKE_UNIT_LIST_DIR}/util/InvokeIfExists.cmake")
set (TARGET_NAME "generate_${TARGET_NAME_HASH}")
add_custom_command (OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${NAME}"
COMMAND "${CMAKE_COMMAND}"
"-DSCRIPT=${WRITE_SOURCE_FILE_SCRIPT}"
-P
"${INVOKE_IF_EXISTS_SCRIPT}")
add_custom_target (${TARGET_NAME}
SOURCES "${CMAKE_CURRENT_BINARY_DIR}/${NAME}")
add_custom_target (${TARGET_NAME}_cleanup ALL
COMMAND
"${CMAKE_COMMAND}"
-E
remove
-f
"${WRITE_SOURCE_FILE_SCRIPT}")
add_dependencies (${TARGET_NAME}_cleanup ${TARGET_NAME})
set (${TARGET_RETURN} "${TARGET_NAME}" PARENT_SCOPE)
endfunction ()
function (_cmake_unit_create_source_for_simple_target NAME
SOURCE_LOCATION_RETURN)
set (SOURCE_LOCATION "${NAME}.cpp")
cmake_unit_create_source_file_before_build (NAME "${SOURCE_LOCATION}"
${ARGN})
set (${SOURCE_LOCATION_RETURN} "${SOURCE_LOCATION}" PARENT_SCOPE)
endfunction ()
# cmake_unit_create_simple_executable
#
# Creates a simple executable by the name "NAME" which will always have a
# "main" function.
#
# NAME: Name of executable
function (cmake_unit_create_simple_executable NAME)
set (CREATE_SIMPLE_EXECUTABLE_SINGLEVAR_ARGS FUNCTIONS GENERATING_FILE)
cmake_parse_arguments (CREATE_SIMPLE_EXECUTABLE
""
"${CREATE_SIMPLE_EXECUTABLE_SINGLEVAR_ARGS}"
""
${ARGN})
# Ensure there is always a main in our source file
set (CREATE_SOURCE_FUNCTIONS ${CREATE_SIMPLE_EXECUTABLE_FUNCTIONS} main)
cmake_forward_arguments (CREATE_SIMPLE_EXECUTABLE GENERATE_FWD
SINGLEVAR_ARGS GENERATING_FILE)
_cmake_unit_create_source_for_simple_target (${NAME} LOCATION
${ARGN}
FUNCTIONS
${CREATE_SOURCE_FUNCTIONS}
${GENERATING_FWD})
add_executable (${NAME} "${LOCATION}")
endfunction ()
# cmake_unit_create_simple_library
#
# Creates a simple library by the name "NAME".
#
# NAME: Name of library
# TYPE: Type of the library (SHARED, STATIC)
# FUNCTIONS: Functions that the library should have.
function (cmake_unit_create_simple_library NAME TYPE)
set (CREATE_SIMPLE_LIBRARY_OPTION_ARGS
${_CMAKE_UNIT_SOURCE_FILE_OPTION_ARGS})
set (CREATE_SIMPLE_LIBRARY_SINGLEVAR_ARGS
GENERATING_FILE
${_CMAKE_UNIT_SOURCE_FILE_SINGLEVAR_ARGS})
set (CREATE_SIMPLE_LIBRARY_MULTIVAR_ARGS
${_CMAKE_UNIT_SOURCE_FILE_MULTIVAR_ARGS})
cmake_parse_arguments (CREATE_SIMPLE_LIBRARY
"${CREATE_SIMPLE_LIBRARY_OPTION_ARGS}"
"${CREATE_SIMPLE_LIBRARY_SINGLEVAR_ARGS}"
"${CREATE_SIMPLE_LIBRARY_MULTIVAR_ARGS}"
${ARGN})
# Check if there are any functions - if there are not, then we will
# need to add an internal one to ensure that linking the library
# is successful. If no functions are added, then certain compilers
# will not write a file containing our object code.
if (NOT CREATE_SIMPLE_LIBRARY_FUNCTIONS)
set (CREATE_SIMPLE_LIBRARY_FUNCTIONS internal_cmake_unit_function__)
endif ()
cmake_forward_arguments (CREATE_SIMPLE_LIBRARY CREATE_FWD
OPTION_ARGS
${CREATE_SIMPLE_LIBRARY_OPTION_ARGS}
SINGLEVAR_ARGS
${CREATE_SIMPLE_LIBRARY_SINGLEVAR_ARGS}
MULTIVAR_ARGS
${CREATE_SIMPLE_LIBRARY_MULTIVAR_ARGS})
_cmake_unit_create_source_for_simple_target (${NAME} LOCATION
${CREATE_FWD}
FUNCTIONS_EXPORT_TARGET
${NAME})
add_library (${NAME} ${TYPE} "${LOCATION}")
generate_export_header (${NAME})
endfunction ()
# cmake_unit_get_target_location_from_exports
#
# For an exports file EXPORTS and target TARGET, finds the location of a
# target from an already generated EXPORTS file.
#
# This function should be run in the verify phase in order to determine the
# location of a binary or library built by CMake. The initial configure
# step should run export (TARGETS ...) in order to generate this file.
#
# This function should always be used where a binary or library needs to
# be invoked after build. Different platforms put the completed binaries
# in different places and also give them a different name. This function
# will resolve all those issues.
#
# EXPORTS: Full path to EXPORTS file to read
# TARGET: Name of TARGET as it will be found in the EXPORTS file
# LOCATION_RETURN: Variable to write target's LOCATION property into.
function (cmake_unit_get_target_location_from_exports EXPORTS
BINARY_DIR
TARGET
LOCATION_RETURN)
# We create a new project which includes the exports file (as we
# cannot do so whilst in script mode) and then prints the location
# on the stderr. We'll capture this and return it.
set (DETERMINE_LOCATION_DIRECTORY
"${BINARY_DIR}/dle_${TARGET}")
set (DETERMINE_LOCATION_BINARY_DIRECTORY
"${DETERMINE_LOCATION_DIRECTORY}/build")
set (DETERMINE_LOCATION_CAPTURE
"${DETERMINE_LOCATION_BINARY_DIRECTORY}/Capture")
set (DETERMINE_LOCATION_CMAKELISTS_TXT_FILE
"${DETERMINE_LOCATION_DIRECTORY}/CMakeLists.txt")
set (DETERMINE_LOCATION_CMAKELISTS_TXT
"include (\"${EXPORTS}\")\n"
"get_property (LOCATION TARGET ${TARGET} PROPERTY LOCATION)\n"
"file (WRITE \"${DETERMINE_LOCATION_CAPTURE}\" \"\${LOCATION}\")\n")
string (REPLACE ";" ""
DETERMINE_LOCATION_CMAKELISTS_TXT
"${DETERMINE_LOCATION_CMAKELISTS_TXT}")
file (MAKE_DIRECTORY "${DETERMINE_LOCATION_DIRECTORY}")
file (MAKE_DIRECTORY "${DETERMINE_LOCATION_BINARY_DIRECTORY}")
file (WRITE "${DETERMINE_LOCATION_CMAKELISTS_TXT_FILE}"
"${DETERMINE_LOCATION_CMAKELISTS_TXT}")
set (DETERMINE_LOCATION_OUTPUT_LOG
"${DETERMINE_LOCATION_BINARY_DIRECTORY}/DetermineLocationOutput.txt")
set (DETERMINE_LOCATION_ERROR_LOG
"${DETERMINE_LOCATION_BINARY_DIRECTORY}/DetermineLocationError.txt")
execute_process (COMMAND "${CMAKE_COMMAND}" -Wno-dev
"${DETERMINE_LOCATION_DIRECTORY}"
OUTPUT_FILE ${DETERMINE_LOCATION_OUTPUT_LOG}
ERROR_FILE ${DETERMINE_LOCATION_ERROR_LOG}
RESULT_VARIABLE RESULT
WORKING_DIRECTORY "${DETERMINE_LOCATION_BINARY_DIRECTORY}")
if (NOT RESULT EQUAL 0)
message (FATAL_ERROR
"Error whilst getting location of ${TARGET}\n"
"See ${DETERMINE_LOCATION_ERROR_LOG} for details")
endif ()
file (READ ${DETERMINE_LOCATION_CAPTURE} LOCATION)
set (${LOCATION_RETURN} "${LOCATION}" PARENT_SCOPE)
endfunction ()
# cmake_unit_export_cfg_int_dir
#
# Exports the current CMAKE_CFG_INTDIR variable (known at configure-time)
# and writes it into the file specified at LOCATION. This file could be read
# after the build to determine the CMAKE_CFG_INTDIR property
#
# LOCATION: Filename to write CMAKE_CFG_INTDIR variable to.
function (cmake_unit_export_cfg_int_dir LOCATION)
get_filename_component (LOCATION_NAME "${LOCATION}" NAME)
set (WRITE_TO_OUTPUT_SCRIPT_FILE "${LOCATION}.write.cmake")
set (WRITE_TO_OUTPUT_FILE_SCRIPT_CONTENTS
"file (WRITE \"${LOCATION}\" \"\${INTDIR}\")\n")
file (WRITE "${WRITE_TO_OUTPUT_SCRIPT_FILE}"
"${WRITE_TO_OUTPUT_FILE_SCRIPT_CONTENTS}")
add_custom_command (OUTPUT ${LOCATION}
COMMAND "${CMAKE_COMMAND}"
-DINTDIR=${CMAKE_CFG_INTDIR}
-P
"${WRITE_TO_OUTPUT_SCRIPT_FILE}")
add_custom_target (write_cfg_int_dir_${LOCATION_NAME} ALL
SOURCES ${LOCATION})
endfunction ()
# cmake_unit_import_cfg_int_dir
#
# Reads OUTPUT_FILE to import the value of the CMAKE_CFG_INTDIR property
# and stores the value inside of LOCATION_RETURN. This should be run in the
# verify phase to get the CMAKE_CFG_INTDIR property for the configure phase
# generator. Use cmake_unit_export_cfg_int_dir in the configure phase
# to export the CMAKE_CFG_INTDIR property.
#
# OUTPUT_FILE: Filename to read CMAKE_CFG_INTDIR variable from.
# LOCATION_RETURN: Variable to store CMAKE_CFG_INTDIR value into.
function (cmake_unit_import_cfg_int_dir OUTPUT_FILE LOCATION_RETURN)
file (READ "${OUTPUT_FILE}" LOCATION)
set (${LOCATION_RETURN} "${LOCATION}" PARENT_SCOPE)
endfunction ()
function (_cmake_unit_slice_list LIST
SLICE_BEGIN
SLICE_END
RESULT_LIST_VARIABLE)
set (RESULT_LIST)
list (LENGTH ${LIST} LIST_LENGTH)
math (EXPR LIST_LENGTH "${LIST_LENGTH} - 1")
if (SLICE_END GREATER LIST_LENGTH)
set (SLICE_END "${LIST_LENGTH}")
endif ()
if (NOT SLICE_END LESS 0)
foreach (INDEX RANGE ${SLICE_BEGIN} ${SLICE_END})
list (GET ${LIST} ${INDEX} _VALUE)
list (APPEND RESULT_LIST ${_VALUE})
endforeach ()
endif ()
set (${RESULT_LIST_VARIABLE} ${RESULT_LIST} PARENT_SCOPE)
endfunction ()
function (_cmake_unit_extract_result_from_argn)
cmake_parse_arguments (EXTRACT_RESULTS
""
"SLICE_FROM;RESULT_VAR;ARGN_VAR"
"ARGN"
${ARGN})
list (GET EXTRACT_RESULTS_ARGN -1 RESULT_VARIABLE)
list (LENGTH EXTRACT_RESULTS_ARGN ARGN_LENGTH)
math (EXPR ARGN_LENGTH "${ARGN_LENGTH} - 2")
if (NOT DEFINED EXTRACT_RESULTS_SLICE_FROM)
set (EXTRACT_RESULTS_SLICE_FROM "0")
endif ()
_cmake_unit_slice_list (EXTRACT_RESULTS_ARGN
"${EXTRACT_RESULTS_SLICE_FROM}"
"${ARGN_LENGTH}"
REMAINING_ARGN)
set (${EXTRACT_RESULTS_RESULT_VAR} ${RESULT_VARIABLE} PARENT_SCOPE)
set (${EXTRACT_RESULTS_ARGN_VAR} ${REMAINING_ARGN} PARENT_SCOPE)
endfunction ()
# cmake_unit_register_matcher_namespace
#
# Tell cmake-unit about a namespace that matchers live in. If you define
# a function namespace_matcher and register the namespace "namespace", the
# matcher "matcher" can be used with cmake_unit_assert_that. Namespaces
# registered later take priority over namespaces registered earlier.
#
# NAMESPACE: The namespace to register.
function (cmake_unit_register_matcher_namespace NAMESPACE)
set_property (GLOBAL APPEND PROPERTY "_CMAKE_UNIT_MATCHER_NAMESPACE"
"${NAMESPACE}")
endfunction ()
cmake_unit_register_matcher_namespace (cmake_unit)
# cmake_unit_eval_matcher
#
# Evaluates MATCHER against VARIABLE with ARGN, returns result in
# last variable specified. Register additional matcher namespaces
# with cmake_unit_register_matcher_namespace. By default, the
# cmake_unit namespace is available.
#
# VARIABLE: Name of variable to test.
# MATCHER: Name of matcher to run against VARIABLE.
function (cmake_unit_eval_matcher VARIABLE MATCHER)
_cmake_unit_extract_result_from_argn (RESULT_VAR RESULT_VARIABLE
ARGN_VAR REMAINING_ARGN
ARGN ${ARGN})
get_property (MATCHER_NAMESPACES GLOBAL PROPERTY
"_CMAKE_UNIT_MATCHER_NAMESPACE")
list (REVERSE MATCHER_NAMESPACES)
foreach (NAMESPACE ${MATCHER_NAMESPACES})
if (COMMAND "${NAMESPACE}_${MATCHER}")
cmake_call_function ("${NAMESPACE}_${MATCHER}"
"${VARIABLE}"
${REMAINING_ARGN}
RESULT)
break ()
endif ()
endforeach ()
set (${RESULT_VARIABLE} "${RESULT}" PARENT_SCOPE)
endfunction ()
# cmake_unit_assert_that
#
# Runs ASSERTION_FUNCTION against ${VARIABLE_NAME} with ARGN. If it fails,
# an error is reported, otherwise silently succeeds.
#
# VARIABLE: Name of variable to test.
# ASSERTION_FUNCTION: Function to run against argument. Note that the
# cmake_unit prefix gets added automatically, so you
# only need to pass the name without that prefix. For
# example cmake_unit_assert_that (VARIABLE target_exists)
function (cmake_unit_assert_that VARIABLE ASSERTION_FUNCTION)
cmake_unit_eval_matcher ("${VARIABLE}"
"${ASSERTION_FUNCTION}"
${ARGN}
RESULT)
if (NOT RESULT STREQUAL "TRUE")
message (SEND_ERROR
"Expected ${RESULT}, instead ${VARIABLE} was ${${VARIABLE}}")
endif ()
endfunction ()
# cmake_unit_not
#
# Runs ASSERTION_FUNCTION and fails if VARIABLE_NAME matches the function.
# This should be used in combination with cmake_unit_assert_that, for instance,
# cmake_unit_assert_that (VARIABLE_NAME not target_exists)
function (cmake_unit_not)
list (GET CALLER_ARGN 0 VARIABLE_NAME)
list (GET CALLER_ARGN 1 ASSERTION_FUNCTION)
_cmake_unit_extract_result_from_argn (SLICE_FROM 2
RESULT_VAR RESULT_VARIABLE
ARGN_VAR REMAINING_ARGN
ARGN ${CALLER_ARGN})
cmake_unit_eval_matcher ("${VARIABLE_NAME}"
${ASSERTION_FUNCTION}
${REMAINING_ARGN}
RESULT)
set (${RESULT_VARIABLE}
PARENT_SCOPE)
if (NOT RESULT STREQUAL "TRUE")
set (${RESULT_VARIABLE} "TRUE" PARENT_SCOPE)
endif ()
endfunction ()
# cmake_unit_is_true
#
# Matches if the variable provided is either TRUE or ON.
function (cmake_unit_is_true)
list (GET CALLER_ARGN 0 VARIABLE)