-
Notifications
You must be signed in to change notification settings - Fork 16
/
MatlabTools.cmake
2000 lines (1967 loc) · 77.8 KB
/
MatlabTools.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) 2011-2012 University of Pennsylvania
# Copyright (c) 2013-2014 Andreas Schuh
# All rights reserved.
#
# See COPYING file for license information or visit
# http://opensource.andreasschuh.com/cmake-basis/download.html#license
# ============================================================================
##############################################################################
# @file MatlabTools.cmake
# @brief Enables use of MATLAB Compiler and build of MEX-files.
#
# @ingroup CMakeTools
##############################################################################
if (__BASIS_MATLABTOOLS_INCLUDED)
return ()
else ()
set (__BASIS_MATLABTOOLS_INCLUDED TRUE)
endif ()
# ============================================================================
# modules
# ============================================================================
# Note: Required because generate_matlab_executable.cmake uses this module.
include (CMakeParseArguments)
include ("${CMAKE_CURRENT_LIST_DIR}/CommonTools.cmake")
include ("${CMAKE_CURRENT_LIST_DIR}/UtilitiesTools.cmake")
# ============================================================================
# options
# ============================================================================
## @addtogroup BasisSettings
# @{
## @brief Enable/Disable compilation of MATLAB sources if the MATLAB Compiler is available.
option (BASIS_COMPILE_MATLAB "Enable compilation of MATLAB sources if MATLAB Compiler (mcc) is available." ON)
mark_as_advanced (BASIS_COMPILE_MATLAB)
# ----------------------------------------------------------------------------
## @brief Add global MATLAB MEX-script options to CMake cache
#
# @par MATLAB MEX-script options:
# <table border="0">
# <tr>
# @tp @b BASIS_MEX_TIMEOUT @endtp
# <td>Timeout for MEX script execution.</td>
# </tr>
# <tr>
# @tp @b BASIS_MEX_FLAGS @endtp
# <td>Compile flags used to build MEX-files using the MEX script.</td>
# </tr>
# </table>
macro(basis_add_mex_options)
if (MATLAB_MEX_EXECUTABLE)
set (BASIS_MEX_TIMEOUT "600" CACHE STRING "Timeout for MEX script execution")
set (BASIS_MEX_FLAGS "" CACHE STRING "Common MEX switches (separated by ' '; use '\\' to mask ' ').")
mark_as_advanced (BASIS_MEX_FLAGS)
mark_as_advanced (BASIS_MEX_TIMEOUT)
endif ()
endmacro ()
# ----------------------------------------------------------------------------
## @brief Add global MATLAB Compiler (mcc) options to CMake cache
#
# @par MATLAB Compiler options:
# <table border="0">
# <tr>
# @tp @b BASIS_MCC_MATLAB_MODE @endtp
# <td>Enable/Disable invocation of MATLAB Compiler in MATLAB mode.</td>
# </tr>
# <tr>
# @tp @b BASIS_MCC_FLAGS @endtp
# <td>Compile flags used to build MATLAB Compiler targets.</td>
# </tr>
# <tr>
# @tp @b BASIS_MCC_TIMEOUT @endtp
# <td>Timeout for building MATLAB Compiler targets.</td>
# </tr>
# <tr>
# @tp @b BASIS_MCC_RETRY_ATTEMPTS @endtp
# <td>Maximum number of retries on MATLAB Compiler license checkout.</td>
# </tr>
# <tr>
# @tp @b BASIS_MCC_RETRY_DELAY @endtp
# <td>Delay between retries to build MATLAB Compiler compiled targets on license checkout errors.</td>
# </tr>
# </table>
macro(basis_add_mcc_options)
option (
BASIS_MCC_MATLAB_MODE
"Prefer MATLAB mode over standalone mode to invoke MATLAB Compiler to release MCC licence ASAP."
OFF # using MATLAB mode is preferred when the license is shared
# among users as it releases the license immediately once done
)
set (
BASIS_MCC_FLAGS
"-R -singleCompThread"
CACHE STRING
"Common MATLAB Compiler flags (separated by ' '; use '\\' to mask ' ')."
)
set (BASIS_MCC_TIMEOUT "1800" CACHE STRING "Timeout for MATLAB Compiler execution")
set (BASIS_MCC_RETRY_ATTEMPTS "4" CACHE STRING "Maximum number of retries on MATLAB Compiler license checkout error.")
set (BASIS_MCC_RETRY_DELAY "30" CACHE STRING "Delay between retries to build MATLAB Compiler compiled targets on license checkout error.")
mark_as_advanced (BASIS_MCC_MATLAB_MODE)
mark_as_advanced (BASIS_MCC_FLAGS)
mark_as_advanced (BASIS_MCC_TIMEOUT)
mark_as_advanced (BASIS_MCC_RETRY_ATTEMPTS)
mark_as_advanced (BASIS_MCC_RETRY_DELAY)
endmacro ()
## @}
# end of Doxygen group
# ============================================================================
# utilities
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Determine version of MATLAB installation.
#
# @param [out] VERSION Value returned by the "version" command of MATLAB or
# an empty string if execution of MATLAB failed.
#
# @returns Sets the variable named by @p VERSION to the full MATLAB version.
#
# @ingroup CMakeUtilities
function (basis_get_full_matlab_version VERSION)
if (NOT MATLAB_EXECUTABLE)
set (${VERSION} "" PARENT_SCOPE)
return ()
endif ()
set (WORKING_DIR "${CMAKE_BINARY_DIR}/CMakeFiles")
set (OUTPUT_FILE "${WORKING_DIR}/MatlabVersion.txt")
# read MATLAB version from existing output file
set (_MATLAB_VERSION)
if (EXISTS "${OUTPUT_FILE}")
file (READ "${OUTPUT_FILE}" LINES)
string (REGEX REPLACE "\n" ";" LINES "${LINES}")
string (REGEX REPLACE "^;|;$" "" LINES "${LINES}")
list (LENGTH LINES NLINES)
if (NLINES EQUAL 2)
list (GET LINES 0 _MATLAB_EXECUTABLE)
list (GET LINES 1 _MATLAB_VERSION)
basis_sanitize_for_regex (RE "${MATLAB_EXECUTABLE}")
if (NOT _MATLAB_EXECUTABLE MATCHES "^${RE}$")
set (_MATLAB_VERSION)
endif ()
unset (RE)
endif ()
endif ()
# run matlab command to write return value of "version" command to text file
if (NOT _MATLAB_VERSION)
message (STATUS "Determining MATLAB version...")
set (CMD "${MATLAB_EXECUTABLE}" -nodesktop -nosplash -singleCompThread)
if (WIN32)
list (APPEND CMD -automation)
else ()
list (APPEND CMD -nojvm)
endif ()
file (WRITE "${WORKING_DIR}/basis_get_full_matlab_version.m"
"% DO NOT EDIT. Automatically created by BASIS (basis_get_full_matlab_version).
fid = fopen ('${OUTPUT_FILE}', 'w')
if fid == -1, fprintf(2, '??? Error: Failed to open file ${OUTPUT_FILE} for writing!'), quit force, end
fprintf (fid, '${MATLAB_EXECUTABLE}\\n%s\\n', version)
fclose (fid)
quit force
"
)
execute_process (
COMMAND ${CMD} -r "cd('${WORKING_DIR}');basis_get_full_matlab_version;"
WORKING_DIRECTORY "${WORKING_DIR}"
RESULT_VARIABLE RETVAL
TIMEOUT 600 # MATLAB startup can be *very* slow the first time
ERROR_VARIABLE STDERR
OUTPUT_QUIET
)
if (NOT RETVAL EQUAL 0 OR STDERR MATCHES "\\?\\?\\? Error")
set (${VERSION} "" PARENT_SCOPE)
if (RETVAL MATCHES "timeout")
set (REASON ": ${RETVAL}")
elseif (NOT RETVAL EQUAL 0)
set (REASON " with exit code: ${RETVAL}")
else ()
set (REASON ": Failed to open file ${OUTPUT_FILE} for writing")
endif ()
message (STATUS "Determining MATLAB version... - failed${REASON}")
return ()
endif ()
# wait until MATLAB process terminated entirely and wrote the (buffered?) file
set (nsleep_count 0)
set (nsleep_max 30)
while (NOT EXISTS "${OUTPUT_FILE}")
math (EXPR nsleep_count "${nsleep_count}+1")
if (nsleep_count GREATER nsleep_max)
message (STATUS "Determining MATLAB version... - failed: File ${OUTPUT_FILE} still non-existent after ${nsleep_max}s of successful MATLAB termination")
return ()
endif ()
if (WIN32)
execute_process (COMMAND ping 1.1.1.1 -n 1 -w 1000 OUTPUT_QUIET)
else ()
execute_process (COMMAND sleep 1 OUTPUT_QUIET)
endif ()
endwhile ()
# read MATLAB version from text file
file (READ "${OUTPUT_FILE}" LINES)
string (REGEX REPLACE "\n" ";" LINES "${LINES}")
string (REGEX REPLACE "^;|;$" "" LINES "${LINES}")
list (LENGTH LINES NLINES)
if (NLINES EQUAL 2)
list (GET LINES 1 _MATLAB_VERSION)
else ()
set (${VERSION} "" PARENT_SCOPE)
message (STATUS "Determining MATLAB version... - failed")
return ()
endif ()
if (BASIS_VERBOSE)
message (STATUS "Determining MATLAB version... - done: ${_MATLAB_VERSION}")
else ()
message (STATUS "Determining MATLAB version... - done")
endif ()
endif ()
# return
set (${VERSION} "${_MATLAB_VERSION}" PARENT_SCOPE)
endfunction ()
# ----------------------------------------------------------------------------
## @brief Get version of MATLAB installation.
#
# @param [out] ARGV1 If given, the named variable is set to the version string
# ("<major>.<minor>.<patch>") of the MATLAB installation.
# Otherwise, the variables @c MATLAB_VERSION_STRING,
# @c MATLAB_VERSION_MAJOR, @c MATLAB_VERSION_MINOR,
# @c MATLAB_VERSION_PATCH, and @c MATLAB_RELEASE are set
# in the scope of the caller.
#
# @ingroup CMakeUtilities
function (basis_get_matlab_version)
if (ARGC GREATER 1)
message (FATAL_ERROR "basis_get_matlab_version(): Too many arguments!")
endif ()
basis_get_full_matlab_version (VERSION)
if (VERSION MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)")
set (VERSION_STRING "${CMAKE_MATCH_0}")
set (VERSION_MAJOR "${CMAKE_MATCH_1}")
set (VERSION_MINOR "${CMAKE_MATCH_2}")
set (VERSION_PATCH "${CMAKE_MATCH_3}")
else ()
set (VERSION_STRING "0.0")
set (VERSION_MAJOR "0")
set (VERSION_MINOR "0")
set (VERSION_PATCH "0")
endif ()
if (ARGC EQUAL 1)
set (${ARGV0} "${VERSION_STRING}" PARENT_SCOPE)
else ()
set (MATLAB_VERSION_STRING "${VERSION_STRING}" PARENT_SCOPE)
set (MATLAB_VERSION_MAJOR "${VERSION_MAJOR}" PARENT_SCOPE)
set (MATLAB_VERSION_MINOR "${VERSION_MINOR}" PARENT_SCOPE)
set (MATLAB_VERSION_PATCH "${VERSION_PATCH}" PARENT_SCOPE)
if (VERSION MATCHES ".*\\\((.+)\\\)")
set (MATLAB_RELEASE "${CMAKE_MATCH_1}" PARENT_SCOPE)
else ()
set (MATLAB_RELEASE "" PARENT_SCOPE)
endif ()
endif ()
endfunction ()
# ----------------------------------------------------------------------------
## @brief Get release version of MATLAB installation.
#
# @param [out] ARGV1 If given, the named variable is set to the release string
# of the MATLAB installation, e.g., "R2009b". Otherwise,
# the variable @c MATLAB_RELEASE is set in the scope of the
# caller.
#
# @ingroup CMakeUtilities
function (basis_get_matlab_release)
if (ARGC GREATER 1)
message (FATAL_ERROR "basis_get_matlab_release(): Too many arguments!")
endif ()
basis_get_full_matlab_version (VERSION)
if (VERSION MATCHES ".*\\\((.+)\\\)")
set (RELEASE "${CMAKE_MATCH_1}")
else ()
set (RELEASE "")
endif ()
if (ARGC EQUAL 1)
set (${ARGV0} "${RELEASE}" PARENT_SCOPE)
else ()
set (MATLAB_RELEASE "${RELEASE}")
endif ()
endfunction ()
# ----------------------------------------------------------------------------
## @brief Determine extension of MEX-files for this architecture.
#
# @param [out] ARGN The first argument ARGV0 is set to the extension of
# MEX-files (excluding '.'). If the CMake variable MEX_EXT
# is set, its value is returned. Otherwise, this function
# tries to determine it from the system information.
# If the extension could not be determined, an empty string
# is returned. If no argument is given, the extension is
# cached as the variable MEX_EXT.
#
# @returns Sets the variable named by the first argument to the
# platform-specific extension of MEX-files.
#
# @ingroup CMakeUtilities
function (basis_mexext)
# default return value
set (MEXEXT "${MEX_EXT}")
# use MEXEXT if possible
if (NOT MEXEXT AND MATLAB_MEXEXT_EXECUTABLE)
execute_process (
COMMAND "${MATLAB_MEXEXT_EXECUTABLE}"
RESULT_VARIABLE RETVAL
OUTPUT_VARIABLE MEXEXT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (RETVAL)
set (MEXEXT "")
endif ()
endif ()
# otherwise, determine extension given CMake variables describing the system
if (NOT MEXEXT)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
CMAKE_SIZEOF_VOID_P MATCHES 8)
set (MEXEXT "mexa64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "x86" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "i686")
set (MEXEXT "mexglx")
endif ()
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
CMAKE_SIZEOF_VOID_P MATCHES 8)
set (MEXEXT "mexw64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "x86" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "i686")
set (MEXEXT "mexw32")
endif ()
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
CMAKE_SIZEOF_VOID_P MATCHES 8)
set (MEXEXT "mexmaci64")
else ()
set (MEXEXT "mexmaci")
endif ()
elseif (CMAKE_SYSTEM_NAME MATCHES "SunOS")
set (MEXEXT "mexs64")
endif ()
endif ()
# return value
if (ARGC GREATER 0)
set ("${ARGV0}" "${MEXEXT}" PARENT_SCOPE)
else ()
if (NOT DEFINED MEX_EXT)
set (MARKIT 1)
else ()
set (MARKIT 0)
endif ()
set (MEX_EXT "${MEXEXT}" CACHE STRING "The extension of MEX-files for this architecture." FORCE)
if (MARKIT)
mark_as_advanced (MEX_EXT)
endif ()
endif ()
endfunction ()
# ----------------------------------------------------------------------------
## @brief This function writes a MATLAB M-file with addpath() statements.
#
# This function writes an MATLAB M-file into the top directory of the build
# tree which contains an addpath() statement for each directory that was added
# via basis_include_directories().
#
# @returns Creates file add_\<project\>_paths.m in the current binary directory.
#
# @ingroup CMakeUtilities
function (basis_create_addpaths_mfile)
basis_get_project_property (INCLUDE_DIRS PROPERTY PROJECT_INCLUDE_DIRS)
basis_write_addpaths_mfile ("${CMAKE_CURRENT_BINARY_DIR}/add_${PROJECT_NAME_L}_paths.m" ${INCLUDE_DIRS})
endfunction ()
# ----------------------------------------------------------------------------
## @brief This function writes a MATLAB M-file with addpath() statements.
#
# @param [in] MFILE Name of M-file.
# @param [in] ARGN The remaining arguments are the paths which should be added
# to the search path of MATLAB when this M-file is being
# executed. If the option APPEND is given, the paths are
# appended to the specified M-file. Otherwise, any existing
# file will be overwritten. The given directory paths can
# be relative, in which case they are interpreted relative
# to the location of the written M-file using the mfilename()
# function of MATLAB.
#
# @ingroup CMakeUtilities
macro (basis_write_addpaths_mfile MFILE)
CMAKE_PARSE_ARGUMENTS (ARGN "APPEND" "" "" ${ARGN})
if (NOT ARGN_APPEND)
file (WRITE "${MFILE}" "% DO NOT edit. This file is automatically generated by BASIS.
[mfiledir, ~, ~, ~] = fileparts(mfilename('fullpath'));\n")
endif ()
foreach (P IN LISTS ARGN_UNPARSED_ARGUMENTS)
if (P MATCHES "^\\.?$")
file (APPEND "${MFILE}" "addpath(mfiledir);\n")
elseif (IS_ABSOLUTE "${P}")
file (APPEND "${MFILE}" "addpath('${P}');\n")
else ()
file (APPEND "${MFILE}" "addpath([mfiledir '/${P}']);\n")
endif ()
endforeach ()
endmacro ()
# ----------------------------------------------------------------------------
## @brief Generate MATLAB wrapper executable.
#
# This function writes a Bash script on Unix or a Windows Command script on
# Windows platforms which execute the specified MATLAB command using the -r
# option of the matlab executable and the -nodesktop and -nosplash options.
# It is used by the build scripts generated by the basis_build_mcc_target()
# in order to build an executable from MATLAB source files without the use
# of the MATLAB Compiler. In this case, the MATLAB source files are simply
# copied to the installation directory and the wrapper script written by
# this function used to execute the main function with the command-line
# arguments passed on to this executable.
#
# @param [in] OUTPUT_FILE Name of the output executable file.
# @param [in] ARGN The remaining options
# @par
# <table border=0>
# <tr>
# @tp @b DESTINATION dir @endtp
# <td>Installation destination. (default: directory of @c OUTPUT_FILE)</td>
# </tr>
# <tr>
# @tp @b COMMAND name @endtp
# <td>Name of the MATLAB command to execute, i.e.,
# the name of the main function.</td>
# </tr>
# <tr>
# @tp @b STARTUP mfile @endtp
# <td>Absolute path of a startup M-file.</td>
# </tr>
# <tr>
# @tp @b MATLABPATH dir1[ dir2...]
# <td>List of directories to be added to the MATLAB search path.</td>
# </tr>
# <tr>
# @tp @b OPTIONS opt1[ opt2...]
# <td>Additional options to pass on to the <tt>matlab</tt> executable.</td>
# </tr>
# </table>
function (basis_generate_matlab_executable OUTPUT_FILE)
CMAKE_PARSE_ARGUMENTS (ARGN "" "COMMAND;STARTUP;DESTINATION" "OPTIONS;MATLABPATH" ${ARGN})
if (NOT MATLAB_EXECUTABLE)
set (MATLAB_EXECUTABLE matlab)
endif ()
if (NOT OUTPUT_FILE)
message ("basis_generate_matlab_executable(): Missing OUTPUT_FILE argument!")
endif ()
if (NOT ARGN_DESTINATION)
get_filename_component (ARGN_DESTINATION "${OUTPUT_FILE}" PATH)
endif ()
# source path
set (MATLABPATH)
foreach (P IN LISTS ARGN_MATLABPATH)
if (P MATCHES "^\\.?$")
set (P "$__DIR__")
elseif (NOT IS_ABSOLUTE "${P}")
set (P "$__DIR__/${P}")
endif ()
list (APPEND MATLABPATH "${P}")
endforeach ()
if (MATLABPATH)
list (REMOVE_DUPLICATES MATLABPATH)
endif ()
# startup script
if (ARGN_STARTUP)
get_filename_component (STARTUP_COMMAND "${ARGN_STARTUP}" NAME_WE)
get_filename_component (STARTUP_DIR "${ARGN_STARTUP}" PATH)
get_filename_component (STARTUP_PKG "${STARTUP_DIR}" NAME)
if (STARTUP_PKG MATCHES "^\\+")
get_filename_component (STARTUP_DIR "${STARTUP_DIR}" PATH)
string (REGEX REPLACE "^\\+" "" STARTUP_PKG "${STARTUP_PKG}")
set (STARTUP_COMMAND "${STARTUP_PKG}.${STARTUP_COMMAND}")
endif ()
if (IS_ABSOLUTE "${STARTUP_DIR}")
file (RELATIVE_PATH STARTUP_DIR "${ARGN_DESTINATION}" "${STARTUP_DIR}")
endif ()
if (STARTUP_DIR)
set (STARTUP_DIR "$__DIR__/${STARTUP_DIR}")
else ()
set (STARTUP_DIR "$__DIR__")
endif ()
list (FIND MATLABPATH "${STARTUP_DIR}" IDX)
if (IDX EQUAL -1)
set (STARTUP_CODE ", addpath('${STARTUP_DIR}')")
else ()
set (STARTUP_CODE)
endif ()
set (STARTUP_CODE "${STARTUP_CODE}, ${STARTUP_COMMAND}")
else ()
set (STARTUP_CODE)
endif ()
# write wrapper executable
if (MATLABPATH)
basis_list_to_delimited_string (MATLABPATH "', '" NOAUTOQUOTE ${MATLABPATH})
set (MATLABPATH ", addpath('${MATLABPATH}', '-begin')")
else ()
set (MATLABPATH)
endif ()
file (WRITE "${OUTPUT_FILE}"
# note that Bash variables within the script are denoted by $var
# instead of ${var} to prevent CMake from substituting these patterns
"#! /bin/bash
readonly __DIR__=\"${BASIS_BASH___DIR__}\"
errlog=
finish()
{
local status=0
if [[ -n \"$errlog\" ]]; then
grep '??? Error' \"$errlog\" &> /dev/null
[[ $? -ne 0 ]] || status=1
/bin/rm \"$errlog\"
fi
exit $status
}
if [[ -d \"$TMPDIR\" ]]; then
tmpdir=$TMPDIR
else
tmpdir=/tmp
fi
errlog=`mktemp \"$tmpdir/${ARGN_COMMAND}-log.XXXXXX\"`
[[ $? -eq 0 ]] || {
echo \"Failed to create temporary log file in '$tmpdir'!\" 1>&2
exit 1
}
args=
while [[ $# -gt 0 ]]; do
[[ -z \"$args\" ]] || args=\"$args, \"
args=\"$args'$1'\"
shift
done
echo 'Launching MATLAB to execute ${ARGN_COMMAND} function...'
trap finish EXIT # DO NOT install trap earlier !
'${MATLAB_EXECUTABLE}' -nodesktop -nosplash ${ARGN_OPTIONS} \\
-r \"try${MATLABPATH}${STARTUP_CODE}, ${ARGN_COMMAND}($args), catch err, fprintf(2, ['??? Error executing ${ARGN_COMMAND}\\n' err.message '\\n']), end, quit force\" \\
2> >(tee \"$errlog\" >&2)"
) # end of file(WRITE) command
if (UNIX)
execute_process (COMMAND /bin/chmod +x "${OUTPUT_FILE}")
endif ()
endfunction ()
# ============================================================================
# MEX-file target
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Add MEX-file target.
#
# @note This function should not be used directly. Instead, it is called
# by basis_add_library() if the (detected) programming language
# of the given source code files is @c CXX (i.e., C/C++) and the @c MEX
# type option is given.
#
# This function is used to add a shared library target which is built
# using the MATLAB MEX script (mex).
#
# By default, the BASIS C++ utilities library is added as link dependency.
# If none of the BASIS C++ utilities are used by this target, the option
# NO_BASIS_UTILITIES can be given. To enable this option by default, set the
# variable @c BASIS_UTILITIES to @c FALSE, best in the <tt>Settings.cmake</tt>
# file located in the @c PROJECT_CONFIG_DIR (add such file if missing).
# If the use of the BASIS C++ utilities is disabled by default, the
# @c USE_BASIS_UTILITIES option can be used to enable them for this target
# only. Note that the utilities library is a static library and thus the linker
# would simply not include any of the BASIS utility functions in the final
# binary file if not used. The only advantage of setting @c BASIS_UTILITIES to
# @c FALSE or to always specify @c NO_BASIS_UTILITIES if no target uses the
# utilities is that the BASIS utilities library will not be build in this case.
#
# A custom CMake build target with the following properties is added by this
# function to the build system. These properties are used by
# basis_build_mex_target() to generate a build script written in CMake
# code which is executed by a custom CMake command. Before the invokation of
# basis_build_mex_target(), the target properties can be modified using
# basis_set_target_properties().
#
# @note Custom BASIS build targets are finalized by BASIS using basis_project_end(),
# i.e., the end of the root CMake configuration file of the (sub-)project.
#
# @par Properties on script library targets
# <table border=0>
# <tr>
# @tp @b MFILE file @endtp
# <td>MATLAB source file with function prototype and documentation of MEX-file.
# (default: none)</td>
# </tr>
# <tr>
# @tp @b PREFIX prefix @endtp
# <td>Output prefix of build MEX-file such as package name
# (the prefix must include the leading + and trailing /).</td>
# </tr>
# </table>
#
# @attention Properties documented as read-only must not be modified.
#
# An install command for the added library target is added by this function
# as well. The MEX-file will be installed as part of the specified @p COMPONENT
# in the @c INSTALL_LIBRARY_DIR on Unix and @c INSTALL_RUNTIME_DIR on Windows.
#
# @param [in] TARGET_NAME Name of build target.
# @param [in] ARGN The remaining arguments are parsed and the following
# arguments extracted. All unparsed arguments are treated
# as the source files of the MEX-file.
# @par
# <table border="0">
# <tr>
# @tp @b COMPONENT name @endtp
# <td>Name of installation component as part of which this MEX-file is being
# installed if the @c LIBRARY_INSTALL_DIRECTORY property is not "none".
# (default: @c BASIS_LIBRARY_COMPONENT)</td>
# </tr>
# <tr>
# @tp @b [NO]EXPORT @endtp
# <td>Whether to export this target. (default: @c TRUE)</td>
# </tr>
# <tr>
# @tp @b NO_BASIS_UTILITIES @endtp
# <td>Specify that the BASIS utilities are not used by this MEX-file and
# hence no link dependency on the BASIS utilities shall be added.
# (default: @c NOT BASIS_UTILITIES)</td>
# </tr>
# <tr>
# @tp @b USE_BASIS_UTILITIES @endtp
# <td>Specify that the BASIS utilities are used and required by this MEX-file
# and hence a link dependency on the BASIS utilities must be added.
# (default: @c BASIS_UTILITIES)</td>
# </tr>
# </table>
#
# @returns Adds custom target to build MEX-file using the MEX script.
#
# @sa basis_add_library()
#
# @ingroup CMakeUtilities
function (basis_add_mex_file TARGET_NAME)
# check target name
basis_check_target_name ("${TARGET_NAME}")
basis_make_target_uid (TARGET_UID "${TARGET_NAME}")
message (STATUS "Adding MEX-file ${TARGET_UID}...")
# required commands available ?
if (NOT MATLAB_MEX_EXECUTABLE)
message (FATAL_ERROR "MATLAB MEX script (mex) not found! It is required to build target ${TARGET_UID}."
" Forgot to add MATLAB{mex} as dependency? Otherwise, set MATLAB_MEX_EXECUTABLE manually and try again.")
endif ()
basis_add_mex_options()
# parse arguments
CMAKE_PARSE_ARGUMENTS (
ARGN
"USE_BASIS_UTILITIES;NO_BASIS_UTILITIES;EXPORT;NOEXPORT"
"COMPONENT;DESTINATION"
""
${ARGN}
)
set (SOURCES ${ARGN_UNPARSED_ARGUMENTS})
basis_set_flag (ARGN EXPORT ${BASIS_EXPORT})
if (ARGN_USE_BASIS_UTILITIES AND ARGN_NO_BASIS_UTILITIES)
message (FATAL_ERROR "Target ${TARGET_UID}: Options USE_BASIS_UTILITIES and NO_BASIS_UTILITIES are mutually exclusive!")
endif ()
if (ARGN_USE_BASIS_UTILITIES)
set (USES_BASIS_UTILITIES TRUE)
elseif (ARGN_NO_BASIS_UTILITIES)
set (USES_BASIS_UTILITIES FALSE)
else ()
set (USES_BASIS_UTILITIES ${BASIS_UTILITIES})
endif ()
basis_mexext (MEXEXT)
# IS_TEST flag
basis_sanitize_for_regex (RE "${PROJECT_TESTING_DIR}")
if (CMAKE_CURRENT_SOURCE_DIR MATCHES "^${RE}")
set (IS_TEST TRUE)
else ()
set (IS_TEST FALSE)
endif ()
# installation component
if (NOT ARGN_COMPONENT)
set (ARGN_COMPONENT "${BASIS_LIBRARY_COMPONENT}")
endif ()
if (NOT ARGN_COMPONENT)
set (ARGN_COMPONENT "Unspecified")
endif ()
# installation directory
if (ARGN_DESTINATION)
if (ARGN_DESTINATION MATCHES "^[nN][oO][nN][eE]$")
set (ARGN_DESTINATION)
elseif (IS_ABSOLUTE "${ARGN_DESTINATION}")
file (RELATIVE_PATH ARGN_DESTINATION "${CMAKE_INSTALL_PREFIX}" "${ARGN_DESTINATION}")
endif ()
else ()
set (ARGN_DESTINATION "${INSTALL_MATLAB_LIBRARY_DIR}")
endif ()
# configure (.in) source files
basis_configure_sources (SOURCES ${SOURCES})
# add custom target
add_custom_target (${TARGET_UID} ALL SOURCES ${SOURCES})
get_directory_property (INCLUDE_DIRS INCLUDE_DIRECTORIES)
get_directory_property (LINK_DIRS LINK_DIRECTORIES)
if (MATLAB_LIBRARY_DIR)
list (INSERT LINK_DIRS 0 "${MATLAB_LIBRARY_DIR}")
endif ()
_set_target_properties (
${TARGET_UID}
PROPERTIES
LANGUAGE "CXX"
BASIS_TYPE MEX
BASIS_UTILITIES ${USES_BASIS_UTILITIES}
BASIS_INCLUDE_DIRECTORIES "${INCLUDE_DIRS}"
BASIS_LINK_DIRECTORIES "${LINK_DIRS}"
BUILD_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${TARGET_UID}"
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${BINARY_MATLAB_LIBRARY_DIR}"
LIBRARY_INSTALL_DIRECTORY "${ARGN_DESTINATION}"
LIBRARY_COMPONENT "${ARGN_COMPONENT}"
COMPILE_FLAGS "${BASIS_MEX_FLAGS}"
LINK_FLAGS ""
LINK_DEPENDS "${LINK_DEPENDS}"
PREFIX ""
OUTPUT_NAME ""
SUFFIX ".${MEXEXT}"
MFILE ""
TEST ${IS_TEST}
EXPORT ${EXPORT}
)
# link to BASIS utilities
if (USES_BASIS_UTILITIES)
basis_target_link_libraries (.${TARGET_UID} basis)
endif ()
# add target to list of targets
basis_set_project_property (APPEND PROPERTY TARGETS "${TARGET_UID}")
message (STATUS "Adding MEX-file ${TARGET_UID}... - done")
endfunction ()
# ============================================================================
# MATLAB Compiler target
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Add MATLAB Compiler target.
#
# @note This function should not be used directly. Instead, it is called
# by either basis_add_executable() or basis_add_library() if the
# (detected) programming language of the given source code files is
# @c MATLAB.
#
# This function is used to add an executable or shared library target which is
# built using the MATLAB Compiler (MCC).
#
# A custom CMake build target with the following properties is added by this
# function to the build system. These properties are used by
# basis_build_mcc_target() to generate a build script written in CMake
# code which is executed by a custom CMake command. Before the invokation of
# basis_build_mcc_target(), the target properties can be modified using
# basis_set_target_properties().
#
# @note Custom BASIS build targets are finalized by BASIS using basis_project_end(),
# i.e., the end of the root CMake configuration file of the (sub-)project.
#
# @par Properties on MATLAB Compiler targets
# <table border=0>
# <tr><td>TODO</td></tr>
# </table>
#
# An install command for the added executable or library target is added by
# this function as well. The executable will be installed as part of the
# @p RUNTIME_COMPONENT in the directory @c INSTALL_RUNTIME_DIR. The runtime
# library will be installed as part of the @p RUNTIME_COMPONENT in the directory
# @c INSTALL_LIBRARY_DIR on Unix and @c INSTALL_RUNTIME_DIR on Windows.
# Static/import libraries will be installed as part of the @p LIBRARY_COMPONENT
# in the directory @c INSTALL_ARCHIVE_DIR.
#
# @note If this function is used within the @c PROJECT_TESTING_DIR, the built
# executable is output to the @c BINARY_TESTING_DIR directory tree instead.
# Moreover, no installation rules are added. Test executables are further
# not exported, regardless of the value of the @c EXPORT property.
#
# @param [in] TARGET_NAME Name of build target.
# @param [in] ARGN The remaining arguments are parsed and the following
# arguments extracted. All unparsed arguments are treated
# as the MATLAB or C/C++ source files, respectively.
# @par
# <table border="0">
# <tr>
# @tp <b>EXECUTABLE</b>|<b>LIBEXEC</b>|<b>SHARED</b> @endtp
# <td>Type of the MATLAB Compiler target which can be either a stand-alone
# executable, an auxiliary executable, or a shared library.
# (default: @c EXECUTABLE)</td>
# </tr>
# <tr>
# @tp @b COMPONENT name @endtp
# <td>Name of component as part of which this executable or library will be
# installed if the @c RUNTIME_INSTALL_DIRECTORY or @c LIBRARY_INSTALL_DIRECTORY
# property is not "none". Used only if @p RUNTIME_COMPONENT or
# @p LIBRARY_COMPONENT not specified.
# (default: see @p RUNTIME_COMPONENT and @p LIBRARY_COMPONENT arguments)</td>
# </tr>
# <tr>
# @tp @b DESTINATION dir @endtp
# <td>Installation directory for executable or runtime and library component
# of shared library relative to @c CMAKE_INSTALL_PREFIX. Used only if
# @p RUNTIME_DESTINATION or @p LIBRARY_DESTINATION not specified.
# If "none" (case-insensitive) is given as argument, no default installation
# rules are added. (default: see @p RUNTIME_DESTINATION and
# @p LIBRARY_DESTINATION arguments)</td>
# </tr>
# <tr>
# @tp @b LIBRARY_COMPONENT name @endtp
# <td>Name of component as part of which import/static library will be intalled
# if a shared library is build and the @c LIBRARY_INSTALL_DIRECTORY property is
# not "none". (default: @c COMPONENT if specified or @c BASIS_LIBRARY_COMPONENT
# otherwise)</td>
# </tr>
# <tr>
# @tp @b LIBRARY_DESTINATION dir @endtp
# <td>Installation directory of the library component relative to
# @c CMAKE_INSTALL_PREFIX. If "none" (case-insensitive) is given as argument or
# an executable is build, no installation rule for the library component is added.
# (default: @c INSTALL_ARCHIVE_DIR)</td>
# </tr>
# <tr>
# @tp @b HEADER_DESTINATION dir @endtp
# <td>Installation directory of the library header file relative to
# @c INSTALL_INCLUDE_DIR. If "none" (case-insensitive) is given as argument or
# an executable is build, no installation rule for the library header file is added.
# (default: @c INSTALL_INCLUDE_DIR)</td>
# </tr>
# <tr>
# @tp @b RUNTIME_COMPONENT name @endtp
# <td>Name of component as part of which executable or runtime library, respectively,
# will be installed if the @c RUNTIME_INSTALL_DIRECTORY property is not "none".
# (default: @c COMPONENT if specified or @c BASIS_RUNTIME_COMPONENT otherwise)</td>
# </tr>
# <tr>
# @tp @b RUNTIME_DESTINATION dir @endtp
# <td>Installation directory of the executable or runtime component of the shared library
# relative to @c CMAKE_INSTALL_PREFIX. If "none" (case-insensitive) is given as argument,
# no installation rule for the runtime library is added.
# (default: @c INSTALL_LIBRARY_DIR for shared libraries on Unix or
# @c INSTALL_RUNTIME_DIR otherwise)</td>
# </tr>
# <tr>
# @tp @b [NO]EXPORT @endtp
# <td>Whether to export this target. (default: @c TRUE)</td>
# </tr>
# <tr>
# @tp @b NO_BASIS_UTILITIES @endtp
# <td>Specify that the BASIS utilities are not used by this executable or shared library
# and hence no link dependency on the BASIS utilities shall be added.
# (default: @c NOT BASIS_UTILITIES)</td>
# </tr>
# <tr>
# @tp @b USE_BASIS_UTILITIES @endtp
# <td>Specify that the BASIS utilities are used and required by this executable
# or shared library, respectively, and hence a link dependency on the BASIS utilities
# must be added.
# (default: @c BASIS_UTILITIES)</td>
# </tr>
# </table>
#
# @todo Consider NO_BASIS_UTILITIES and USE_BASIS_UTILITIES options after the BASIS
# utilities for MATLAB have been implemented.
#
# @returns Adds custom target which builds depending on the @p BASIS_TYPE property
# either an executable or a shared library using the MATLAB Compiler.
#
# @sa basis_add_executable()
# @sa basis_add_library()
#
# @ingroup CMakeUtilities
function (basis_add_mcc_target TARGET_NAME)
# check target name
basis_check_target_name ("${TARGET_NAME}")
basis_make_target_uid (TARGET_UID "${TARGET_NAME}")
# parse arguments
CMAKE_PARSE_ARGUMENTS (
ARGN
"SHARED;EXECUTABLE;LIBEXEC;USE_BASIS_UTILITIES;NO_BASIS_UTILITIES;EXPORT;NOEXPORT"
"COMPONENT;RUNTIME_COMPONENT;LIBRARY_COMPONENT;DESTINATION;RUNTIME_DESTINATION;LIBRARY_DESTINATION;HEADER_DESTINATION"
""
${ARGN}
)
set (SOURCES "${ARGN_UNPARSED_ARGUMENTS}")
basis_set_flag (ARGN EXPORT ${BASIS_EXPORT})
if (ARGN_USE_BASIS_UTILITIES AND ARGN_NO_BASIS_UTILITIES)
message (FATAL_ERROR "Target ${TARGET_UID}: Options USE_BASIS_UTILITIES and NO_BASIS_UTILITIES are mutually exclusive!")
endif ()
if (ARGN_USE_BASIS_UTILITIES)
set (USES_BASIS_UTILITIES TRUE)
elseif (ARGN_NO_BASIS_UTILITIES)
set (USES_BASIS_UTILITIES FALSE)
else ()
set (USES_BASIS_UTILITIES ${BASIS_UTILITIES})
endif ()
if (ARGN_SHARED AND (ARGN_EXECUTABLE OR ARGN_LIBEXEC))
message (FATAL_ERROR "Target ${TARGET_UID}: Options SHARED and EXECUTABLE or LIBEXEC are mutually exclusive!")
endif ()
if (ARGN_SHARED)
set (TYPE LIBRARY)
else ()
set (TYPE EXECUTABLE)
endif ()
string (TOLOWER "${TYPE}" type)
message (STATUS "Adding MATLAB ${type} ${TARGET_UID}...")
# IS_TEST flag
basis_sanitize_for_regex (RE "${PROJECT_TESTING_DIR}")
if (CMAKE_CURRENT_SOURCE_DIR MATCHES "^${RE}")
set (IS_TEST TRUE)
else ()
set (IS_TEST FALSE)
endif ()
# output directory
if (IS_TEST)
set (LIBRARY_OUTPUT_DIRECTORY "${TESTING_LIBRARY_DIR}")
if (ARGN_LIBEXEC)
set (RUNTIME_OUTPUT_DIRECTORY "${TESTING_LIBEXEC_DIR}")
else ()
set (RUNTIME_OUTPUT_DIRECTORY "${TESTING_RUNTIME_DIR}")
endif ()
else ()
set (LIBRARY_OUTPUT_DIRECTORY "${BINARY_LIBRARY_DIR}")
if (ARGN_LIBEXEC)
set (RUNTIME_OUTPUT_DIRECTORY "${BINARY_LIBEXEC_DIR}")
else ()
set (RUNTIME_OUTPUT_DIRECTORY "${BINARY_RUNTIME_DIR}")
endif ()
endif ()
# installation component
if (ARGN_COMPONENT)
if (NOT ARGN_LIBRARY_COMPONENT)
set (ARGN_LIBRARY_COMPONENT "${ARGN_COMPONENT}")
endif ()
if (NOT ARGN_RUNTIME_COMPONENT)
set (ARGN_RUNTIME_COMPONENT "${ARGN_COMPONENT}")
endif ()
endif ()
if (NOT ARGN_RUNTIME_COMPONENT)
set (ARGN_RUNTIME_COMPONENT "${BASIS_RUNTIME_COMPONENT}")
endif ()
if (NOT ARGN_RUNTIME_COMPONENT)
set (ARGN_RUNTIME_COMPONENT "Unspecified")
endif ()
if (NOT ARGN_LIBRARY_COMPONENT)
set (ARGN_LIBRARY_COMPONENT "${BASIS_LIBRARY_COMPONENT}")
endif ()
if (NOT ARGN_LIBRARY_COMPONENT)
set (ARGN_LIBRARY_COMPONENT "Unspecified")
endif ()
# installation directories
if (ARGN_DESTINATION)
if (NOT ARGN_RUNTIME_DESTINATION)
set (ARGN_RUNTIME_DESTINATION "${ARGN_DESTINATION}")
endif ()
if (NOT ARGN_LIBRARY_DESTINATION)
set (ARGN_LIBRARY_DESTINATION "${ARGN_DESTINATION}")
endif ()
if (NOT ARGN_HEADER_DESTINATION)
set (ARGN_HEADER_DESTINATION "${ARGN_DESTINATION}")
endif ()
endif ()
if (NOT ARGN_RUNTIME_DESTINATION AND NOT IS_TEST)
if (ARGN_LIBEXEC)
set (ARGN_RUNTIME_DESTINATION "${INSTALL_LIBEXEC_DIR}")
else ()
set (ARGN_RUNTIME_DESTINATION "${INSTALL_RUNTIME_DIR}")
endif ()
endif ()
if (NOT ARGN_LIBRARY_DESTINATION AND NOT IS_TEST)
set (ARGN_LIBRARY_DESTINATION "${INSTALL_LIBRARY_DIR}")
endif ()
if (NOT ARGN_HEADER_DESTINATION AND NOT IS_TEST)
set (ARGN_HEADER_DESTINATION ".")
endif ()
if (ARGN_RUNTIME_DESTINATION MATCHES "^[nN][oO][nN][eE]$")
set (ARGN_RUNTIME_DESTINATION)