summaryrefslogtreecommitdiffstats
path: root/macosx/tkMacOSXHLEvents.c
blob: efe42fbb67859a02a64d96c3006869b65b73a402 (plain)
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
/* 
 * tkMacOSXHLEvents.c --
 *
 *        Implements high level event support for the Macintosh.  Currently, 
 *        the only event that really does anything is the Quit event.
 *
 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
 * Copyright 2001, Apple Computer, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tkMacOSXHLEvents.c,v 1.7 2004/02/16 00:19:42 wolfsuit Exp $
 */

#include "tkMacOSXPort.h"
#include "tkMacOSXInt.h"

#include <Carbon/Carbon.h>

/*
 * This is a Tcl_Event structure that the Quit AppleEvent handler
 * uses to schedule the tkReallyKillMe function.
 */
 
typedef struct KillEvent {
    Tcl_Event header;                /* Information that is standard for
                                 * all events. */
    Tcl_Interp *interp;                /* Interp that was passed to the
                                 * Quit AppleEvent */
} KillEvent;

/*
 * Static functions used only in this file.
 */

static OSErr QuitHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);
static OSErr OappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);
static OSErr RappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);
static OSErr OdocHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);
static OSErr PrintHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);
static OSErr ScriptHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);
static OSErr PrefsHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon);

static int MissedAnyParameters _ANSI_ARGS_((const AppleEvent *theEvent));
static int ReallyKillMe _ANSI_ARGS_((Tcl_Event *eventPtr, int flags));
static OSErr FSRefToDString _ANSI_ARGS_((const FSRef *fsref, Tcl_DString *ds));

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXInitAppleEvents --
 *
 *        Initilize the Apple Events on the Macintosh.  This registers the
 *        core event handlers.
 *
 * Results:
 *        None.
 *
 * Side effects:
 *        None.
 *
 *----------------------------------------------------------------------
 */

void 
TkMacOSXInitAppleEvents(
    Tcl_Interp *interp)                /* Interp to handle basic events. */
{
    OSErr err;
    AEEventHandlerUPP        OappHandlerUPP, RappHandlerUPP, OdocHandlerUPP,
        PrintHandlerUPP, QuitHandlerUPP, ScriptHandlerUPP,
        PrefsHandlerUPP;
        
    /*
     * Install event handlers for the core apple events.
     */
    QuitHandlerUPP = NewAEEventHandlerUPP(QuitHandler);
    err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
            QuitHandlerUPP, (long) interp, false);

    OappHandlerUPP = NewAEEventHandlerUPP(OappHandler);
    err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
            OappHandlerUPP, (long) interp, false);

    RappHandlerUPP = NewAEEventHandlerUPP(RappHandler);
    err = AEInstallEventHandler(kCoreEventClass, kAEReopenApplication,
            RappHandlerUPP, (long) interp, false);

    OdocHandlerUPP = NewAEEventHandlerUPP(OdocHandler);
    err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
            OdocHandlerUPP, (long) interp, false);

    PrintHandlerUPP = NewAEEventHandlerUPP(PrintHandler);
    err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
            PrintHandlerUPP, (long) interp, false);

    PrefsHandlerUPP = NewAEEventHandlerUPP(PrefsHandler);
    err = AEInstallEventHandler(kCoreEventClass, kAEShowPreferences,
            PrefsHandlerUPP, (long) interp, false);

    if (interp != NULL) {
        ScriptHandlerUPP = NewAEEventHandlerUPP(ScriptHandler);
        err = AEInstallEventHandler(kAEMiscStandards, kAEDoScript,
            ScriptHandlerUPP, (long) interp, false);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXDoHLEvent --
 *
 *        Dispatch incomming highlevel events.
 *
 * Results:
 *        None.
 *
 * Side effects:
 *        Depends on the incoming event.
 *
 *----------------------------------------------------------------------
 */

int
TkMacOSXDoHLEvent(EventRecord *theEvent)
{
    return AEProcessAppleEvent(theEvent);
}

/*
 *----------------------------------------------------------------------
 *
 * QuitHandler, OappHandler, etc. --
 *
 *        These are the core Apple event handlers.  Only the Quit event does
 *        anything interesting.
 *
 * Results:
 *        None.
 *
 * Side effects:
 *        None.
 *
 *----------------------------------------------------------------------
 */
OSErr QuitHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon;
    KillEvent *eventPtr;
    
    /*
     * Call the exit command from the event loop, since you are not supposed
     * to call ExitToShell in an Apple Event Handler.  We put this at the head
     * of Tcl's event queue because this message usually comes when the Mac is
     * shutting down, and we want to kill the shell as quickly as possible.
     */
    
    eventPtr = (KillEvent *) ckalloc(sizeof(KillEvent));
    eventPtr->header.proc = ReallyKillMe;
    eventPtr->interp = interp;
     
    Tcl_QueueEvent((Tcl_Event *) eventPtr, TCL_QUEUE_HEAD);

    return noErr;
}

static OSErr 
OappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    return noErr;
}

static OSErr 
RappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    ProcessSerialNumber thePSN = {0, kCurrentProcess};
    return SetFrontProcess(&thePSN);
}

/* Called when the user selects 'Preferences...' in MacOS X */
static OSErr 
PrefsHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    Tcl_CmdInfo dummy;
    Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon;
    /*
     * Don't bother if we don't have an interp or
     * the show preferences procedure doesn't exist.
     */

    if ((interp == NULL) || 
            (Tcl_GetCommandInfo(interp, "::tk::mac::ShowPreferences", &dummy)) == 0) {
            return noErr;
    }
    Tcl_GlobalEval(interp, "::tk::mac::ShowPreferences");
    return noErr;
}

static OSErr 
OdocHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon;
    AEDescList fileSpecList;
    FSRef file;
    OSErr err;
    DescType type;
    Size actual;
    long count;
    AEKeyword keyword;
    long index;
    Tcl_DString command;
    Tcl_DString pathName;
    Tcl_CmdInfo dummy;

    /*
     * Don't bother if we don't have an interp or
     * the open document procedure doesn't exist.
     */

    if ((interp == NULL) || 
            (Tcl_GetCommandInfo(interp, "::tk::mac::OpenDocument", &dummy)) == 0) {
            return noErr;
    }
    
    /*
     * If we get any errors wil retrieving our parameters
     * we just return with no error.
     */

    err = AEGetParamDesc(event, keyDirectObject,
            typeAEList, &fileSpecList);
    if (err != noErr) {
        return noErr;
    }

    err = MissedAnyParameters(event);
    if (err != noErr) {
        return noErr;
    }

    err = AECountItems(&fileSpecList, &count);
    if (err != noErr) {
        return noErr;
    }

    Tcl_DStringInit(&command);
    Tcl_DStringAppend(&command, "::tk::mac::OpenDocument", -1);
    for (index = 1; index <= count; index++) {
        err = AEGetNthPtr(&fileSpecList, index, typeFSRef,
                &keyword, &type, (Ptr) &file, sizeof(FSRef), &actual);
        if ( err != noErr ) {
            continue;
        }

        err = FSRefToDString(&file, &pathName);
        if (err == noErr) {
            Tcl_DStringAppendElement(&command, Tcl_DStringValue(&pathName));
            Tcl_DStringFree(&pathName);
        }
    }
    
    Tcl_GlobalEval(interp, Tcl_DStringValue(&command));

    Tcl_DStringFree(&command);
    return noErr;
}

static OSErr 
PrintHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    return noErr;
}

/*
 *----------------------------------------------------------------------
 *
 * ScriptHandler --
 *
 *        This handler process the script event.  
 *
 * Results:
 *        Schedules the given event to be processed.
 *
 * Side effects:
 *        None.
 *
 *----------------------------------------------------------------------
 */

static OSErr 
ScriptHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon)
{
    OSErr theErr;
    AEDescList theDesc;
    int tclErr = -1;
    Tcl_Interp *interp;
    char errString[128];

    interp = (Tcl_Interp *) handlerRefcon;

    /*
     * The do script event receives one parameter that should be data or a file.
     */
    theErr = AEGetParamDesc(event, keyDirectObject, typeWildCard,
            &theDesc);
    if (theErr != noErr) {
        sprintf(errString, "AEDoScriptHandler: GetParamDesc error %d", theErr);
        theErr = AEPutParamPtr(reply, keyErrorString, typeChar, errString,
                strlen(errString));
    } else if (MissedAnyParameters(event)) {
        sprintf(errString, "AEDoScriptHandler: extra parameters");
        AEPutParamPtr(reply, keyErrorString, typeChar, errString,
                strlen(errString));
        theErr = -1771;
    } else {
        if (theDesc.descriptorType == (DescType)typeChar) {
            Tcl_DString encodedText;
            short i;
            Size  size;
            char  * data;
            
            size = AEGetDescDataSize(&theDesc);
            
            data = (char *)ckalloc(size + 1);
            if ( !data ) {
                theErr = -1771;
            }
            else {
                   AEGetDescData(&theDesc,data,size);
                   data [ size ] = 0;
                   for (i = 0; i < size; i++)
                    if (data[i] == '\r')
                     data[i] = '\n';
                   AEReplaceDescData(theDesc.descriptorType, data, 
		           size + 1, &theDesc);
            }
            Tcl_ExternalToUtfDString(NULL, data, size,
                    &encodedText);
            tclErr = Tcl_GlobalEval(interp, Tcl_DStringValue(&encodedText));
            Tcl_DStringFree(&encodedText);
        } else if (theDesc.descriptorType == (DescType)typeAlias) {
            Boolean dummy;
            FSRef file;
            AliasPtr    alias;
            Size        theSize;

            theSize = AEGetDescDataSize(&theDesc);
            alias = (AliasPtr) ckalloc(theSize);
            if (alias) {
                AEGetDescData (&theDesc, alias, theSize);
            
                theErr = FSResolveAlias(NULL, &alias,
                        &file, &dummy);
                ckfree((char*)alias);
            } else {
                theErr = memFullErr;
            }
            if (theErr == noErr) {
                Tcl_DString scriptName;
                theErr = FSRefToDString(&file, &scriptName);
                if (theErr == noErr) {
                    Tcl_EvalFile(interp, Tcl_DStringValue(&scriptName));
                    Tcl_DStringFree(&scriptName);
                }
            } else {
                sprintf(errString, "AEDoScriptHandler: file not found");
                AEPutParamPtr(reply, keyErrorString, typeChar,
                        errString, strlen(errString));
            }
        } else {
            sprintf(errString,
                    "AEDoScriptHandler: invalid script type '%-4.4s', must be 'alis' or 'TEXT'",
                    (char *)(&theDesc.descriptorType));
            AEPutParamPtr(reply, keyErrorString, typeChar,
                    errString, strlen(errString));
            theErr = -1770;
        }
    }

    /*
     * If we actually go to run Tcl code - put the result in the reply.
     */
    if (tclErr >= 0) {
        if (tclErr == TCL_OK)  {
            AEPutParamPtr(reply, keyDirectObject, typeChar,
                Tcl_GetStringResult(interp),
                strlen(Tcl_GetStringResult(interp)));
        } else {
            AEPutParamPtr(reply, keyErrorString, typeChar,
                Tcl_GetStringResult(interp),
                strlen(Tcl_GetStringResult(interp)));
            AEPutParamPtr(reply, keyErrorNumber, typeInteger,
                (Ptr) &tclErr, sizeof(int));
        }
    }
        
    AEDisposeDesc(&theDesc);

    return theErr;
}

/*
 *----------------------------------------------------------------------
 *
 * ReallyKillMe --
 *
 *      This proc tries to kill the shell by running exit,
 *      called from an event scheduled by the "Quit" AppleEvent handler.
 *
 * Results:
 *        Runs the "exit" command which might kill the shell.
 *
 * Side effects:
 *        None.
 *
 *----------------------------------------------------------------------
 */

static int 
ReallyKillMe(Tcl_Event *eventPtr, int flags) 
{
    Tcl_Interp *interp = ((KillEvent *) eventPtr)->interp;
    if (interp != NULL) {
        Tcl_GlobalEval(interp, "exit");
    }
    
    return 1;
}

/*
 *----------------------------------------------------------------------
 *
 * MissedAnyParameters --
 *
 *        Checks to see if parameters are still left in the event.  
 *
 * Results:
 *        True or false.
 *
 * Side effects:
 *        None.
 *
 *----------------------------------------------------------------------
 */
 
static int 
MissedAnyParameters(
    const AppleEvent *theEvent)
{
   DescType returnedType;
   Size actualSize;
   OSErr err;

   err = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard, 
                   &returnedType, NULL, 0, &actualSize);
   
   return (err != errAEDescNotFound);
}

/*
 *----------------------------------------------------------------------
 *
 * FSRefToDString --
 *
 *      Get a POSIX path from an FSRef.
 *
 * Results:
 *      In the parameter ds.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------
 */

static OSErr
FSRefToDString(const FSRef *fsref, Tcl_DString *ds)
{
    UInt8 fileName[PATH_MAX+1];
    OSErr err;

    err = FSRefMakePath(fsref, fileName, sizeof(fileName));
    if (err == noErr) {
        Tcl_ExternalToUtfDString(NULL, fileName, -1, ds);
    }
    return err;
}
PPEND matlab_supported_versions MATLAB_ADDITIONAL_VERSIONS) # endif() # we order from more recent to older if(matlab_supported_versions) list(REMOVE_DUPLICATES matlab_supported_versions) list(SORT matlab_supported_versions) list(REVERSE matlab_supported_versions) endif() set(${matlab_versions} ${matlab_supported_versions}) endmacro() #[=======================================================================[.rst: .. command:: matlab_get_all_valid_matlab_roots_from_registry Populates the Matlab root with valid versions of Matlab or Matlab Runtime (MCR). The returned matlab_roots is organized in triplets ``(type,version_number,matlab_root_path)``, where ``type`` indicates either ``MATLAB`` or ``MCR``. :: matlab_get_all_valid_matlab_roots_from_registry( matlab_versions matlab_roots) ``matlab_versions`` the versions of each of the Matlab or MCR installations ``matlab_roots`` the location of each of the Matlab or MCR installations #]=======================================================================] function(matlab_get_all_valid_matlab_roots_from_registry matlab_versions matlab_roots) # The matlab_versions comes either from # extract_matlab_versions_from_registry_brute_force or # matlab_extract_all_installed_versions_from_registry. set(_matlab_roots_list ) # check for Matlab installations foreach(_matlab_current_version ${matlab_versions}) get_filename_component( current_MATLAB_ROOT "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\${_matlab_current_version};MATLABROOT]" ABSOLUTE) if(EXISTS ${current_MATLAB_ROOT}) list(APPEND _matlab_roots_list "MATLAB" ${_matlab_current_version} ${current_MATLAB_ROOT}) endif() endforeach() # Check for MCR installations foreach(_matlab_current_version ${matlab_versions}) get_filename_component( current_MATLAB_ROOT "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB Runtime\\${_matlab_current_version};MATLABROOT]" ABSOLUTE) # remove the dot string(REPLACE "." "" _matlab_current_version_without_dot "${_matlab_current_version}") if(EXISTS ${current_MATLAB_ROOT}) list(APPEND _matlab_roots_list "MCR" ${_matlab_current_version} "${current_MATLAB_ROOT}/v${_matlab_current_version_without_dot}") endif() endforeach() set(${matlab_roots} ${_matlab_roots_list} PARENT_SCOPE) endfunction() #[=======================================================================[.rst: .. command:: matlab_get_mex_suffix Returns the extension of the mex files (the suffixes). This function should not be called before the appropriate Matlab root has been found. :: matlab_get_mex_suffix( matlab_root mex_suffix) ``matlab_root`` the root of the Matlab/MCR installation ``mex_suffix`` the variable name in which the suffix will be returned. #]=======================================================================] function(matlab_get_mex_suffix matlab_root mex_suffix) # todo setup the extension properly. Currently I do not know if this is # sufficient for all win32 distributions. # there is also CMAKE_EXECUTABLE_SUFFIX that could be tweaked set(mexext_suffix "") if(WIN32) list(APPEND mexext_suffix ".bat") endif() # we first try without suffix, since cmake does not understand a list with # one empty string element find_program( Matlab_MEXEXTENSIONS_PROG NAMES mexext PATHS ${matlab_root}/bin DOC "Matlab MEX extension provider" NO_DEFAULT_PATH ) foreach(current_mexext_suffix IN LISTS mexext_suffix) if(NOT DEFINED Matlab_MEXEXTENSIONS_PROG OR NOT Matlab_MEXEXTENSIONS_PROG) # this call should populate the cache automatically find_program( Matlab_MEXEXTENSIONS_PROG "mexext${current_mexext_suffix}" PATHS ${matlab_root}/bin DOC "Matlab MEX extension provider" NO_DEFAULT_PATH ) endif() endforeach(current_mexext_suffix) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Determining mex files extensions from '${matlab_root}/bin' with program '${Matlab_MEXEXTENSIONS_PROG}'") endif() # the program has been found? if((NOT Matlab_MEXEXTENSIONS_PROG) OR (NOT EXISTS ${Matlab_MEXEXTENSIONS_PROG})) if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Cannot found mexext program. Matlab root is ${matlab_root}") endif() unset(Matlab_MEXEXTENSIONS_PROG CACHE) return() endif() set(_matlab_mex_extension) set(devnull) if(UNIX) set(devnull INPUT_FILE /dev/null) elseif(WIN32) set(devnull INPUT_FILE NUL) endif() if(WIN32) # this environment variable is used to determine the arch on Windows if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(ENV{MATLAB_ARCH} "win64") else() set(ENV{MATLAB_ARCH} "win32") endif() endif() # this is the preferred way. If this does not work properly (eg. MCR on Windows), then we use our own knowledge execute_process( COMMAND ${Matlab_MEXEXTENSIONS_PROG} OUTPUT_VARIABLE _matlab_mex_extension ERROR_VARIABLE _matlab_mex_extension_error OUTPUT_STRIP_TRAILING_WHITESPACE ${devnull}) unset(ENV{MATLAB_ARCH}) if(_matlab_mex_extension_error) if(WIN32) # this is only for intel architecture if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(_matlab_mex_extension "mexw64") else() set(_matlab_mex_extension "mexw32") endif() endif() endif() string(STRIP "${_matlab_mex_extension}" _matlab_mex_extension) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] '${Matlab_MEXEXTENSIONS_PROG}' : determined extension '${_matlab_mex_extension}' and error string is '${_matlab_mex_extension_error}'") endif() unset(Matlab_MEXEXTENSIONS_PROG CACHE) set(${mex_suffix} ${_matlab_mex_extension} PARENT_SCOPE) endfunction() #[=======================================================================[.rst: .. command:: matlab_get_version_from_matlab_run This function runs Matlab program specified on arguments and extracts its version. If the path provided for the Matlab installation points to an MCR installation, the version is extracted from the installed files. :: matlab_get_version_from_matlab_run( matlab_binary_path matlab_list_versions) ``matlab_binary_path`` the location of the `matlab` binary executable ``matlab_list_versions`` the version extracted from Matlab #]=======================================================================] function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_versions) set(${matlab_list_versions} "" PARENT_SCOPE) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Determining the version of Matlab from ${matlab_binary_program}") endif() if(EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Removing previous ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp file") endif() file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") endif() # the log file is needed since on windows the command executes in a new # window and it is not possible to get back the answer of Matlab # the -wait command is needed on windows, otherwise the call returns # immediately after the program launches itself. if(WIN32) set(_matlab_additional_commands "-wait") endif() set(devnull) if(UNIX) set(devnull INPUT_FILE /dev/null) elseif(WIN32) set(devnull INPUT_FILE NUL) endif() # timeout set to 120 seconds, in case it does not start # note as said before OUTPUT_VARIABLE cannot be used in a platform # independent manner however, not setting it would flush the output of Matlab # in the current console (unix variant) execute_process( COMMAND "${matlab_binary_program}" -nosplash -nojvm ${_matlab_additional_commands} -logfile "matlabVersionLog.cmaketmp" -nodesktop -nodisplay -r "version, exit" OUTPUT_VARIABLE _matlab_version_from_cmd_dummy RESULT_VARIABLE _matlab_result_version_call ERROR_VARIABLE _matlab_result_version_call_error TIMEOUT 120 WORKING_DIRECTORY "${_matlab_temporary_folder}" ${devnull} ) if(_matlab_result_version_call MATCHES "timeout") if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Unable to determine the version of Matlab." " Matlab call timed out after 120 seconds.") endif() return() endif() if(${_matlab_result_version_call}) if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call returned with error ${_matlab_result_version_call}.") endif() return() elseif(NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Unable to determine the version of Matlab. The log file does not exist.") endif() return() endif() # if successful, read back the log file(READ "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp" _matlab_version_from_cmd) file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp") set(index -1) string(FIND "${_matlab_version_from_cmd}" "ans" index) if(index EQUAL -1) if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Cannot find the version of Matlab returned by the run.") endif() else() set(matlab_list_of_all_versions_tmp) string(SUBSTRING "${_matlab_version_from_cmd}" ${index} -1 substring_ans) string( REGEX MATCHALL "ans[\r\n\t ]*=[\r\n\t ]*'?([0-9]+(\\.[0-9]+)?)" matlab_versions_regex ${substring_ans}) foreach(match IN LISTS matlab_versions_regex) string( REGEX MATCH "ans[\r\n\t ]*=[\r\n\t ]*'?(([0-9]+)(\\.([0-9]+))?)" current_match ${match}) list(APPEND matlab_list_of_all_versions_tmp ${CMAKE_MATCH_1}) endforeach() if(matlab_list_of_all_versions_tmp) list(REMOVE_DUPLICATES matlab_list_of_all_versions_tmp) endif() set(${matlab_list_versions} ${matlab_list_of_all_versions_tmp} PARENT_SCOPE) endif() endfunction() #[=======================================================================[.rst: .. command:: matlab_add_unit_test Adds a Matlab unit test to the test set of cmake/ctest. This command requires the component ``MAIN_PROGRAM`` and hence is not available for an MCR installation. The unit test uses the Matlab unittest framework (default, available starting Matlab 2013b+) except if the option ``NO_UNITTEST_FRAMEWORK`` is given. The function expects one Matlab test script file to be given. In the case ``NO_UNITTEST_FRAMEWORK`` is given, the unittest script file should contain the script to be run, plus an exit command with the exit value. This exit value will be passed to the ctest framework (0 success, non 0 failure). Additional arguments accepted by :command:`add_test` can be passed through ``TEST_ARGS`` (eg. ``CONFIGURATION <config> ...``). :: matlab_add_unit_test( NAME <name> UNITTEST_FILE matlab_file_containing_unittest.m [CUSTOM_TEST_COMMAND matlab_command_to_run_as_test] [UNITTEST_PRECOMMAND matlab_command_to_run] [TIMEOUT timeout] [ADDITIONAL_PATH path1 [path2 ...]] [MATLAB_ADDITIONAL_STARTUP_OPTIONS option1 [option2 ...]] [TEST_ARGS arg1 [arg2 ...]] [NO_UNITTEST_FRAMEWORK] ) The function arguments are: ``NAME`` name of the unittest in ctest. ``UNITTEST_FILE`` the matlab unittest file. Its path will be automatically added to the Matlab path. ``CUSTOM_TEST_COMMAND`` Matlab script command to run as the test. If this is not set, then the following is run: ``runtests('matlab_file_name'), exit(max([ans(1,:).Failed]))`` where ``matlab_file_name`` is the ``UNITTEST_FILE`` without the extension. ``UNITTEST_PRECOMMAND`` Matlab script command to be ran before the file containing the test (eg. GPU device initialisation based on CMake variables). ``TIMEOUT`` the test timeout in seconds. Defaults to 180 seconds as the Matlab unit test may hang. ``ADDITIONAL_PATH`` a list of paths to add to the Matlab path prior to running the unit test. ``MATLAB_ADDITIONAL_STARTUP_OPTIONS`` a list of additional option in order to run Matlab from the command line. ``-nosplash -nodesktop -nodisplay`` are always added. ``TEST_ARGS`` Additional options provided to the add_test command. These options are added to the default options (eg. "CONFIGURATIONS Release") ``NO_UNITTEST_FRAMEWORK`` when set, indicates that the test should not use the unittest framework of Matlab (available for versions >= R2013a). ``WORKING_DIRECTORY`` This will be the working directory for the test. If specified it will also be the output directory used for the log file of the test run. If not specified the temporary directory ``${CMAKE_BINARY_DIR}/Matlab`` will be used as the working directory and the log location. #]=======================================================================] function(matlab_add_unit_test) if(NOT Matlab_MAIN_PROGRAM) message(FATAL_ERROR "[MATLAB] This functionality needs the MAIN_PROGRAM component (not default)") endif() set(options NO_UNITTEST_FRAMEWORK) set(oneValueArgs NAME UNITTEST_FILE TIMEOUT WORKING_DIRECTORY UNITTEST_PRECOMMAND CUSTOM_TEST_COMMAND) set(multiValueArgs ADDITIONAL_PATH MATLAB_ADDITIONAL_STARTUP_OPTIONS TEST_ARGS) set(prefix _matlab_unittest_prefix) cmake_parse_arguments(PARSE_ARGV 0 ${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" ) if(NOT ${prefix}_NAME) message(FATAL_ERROR "[MATLAB] The Matlab test name cannot be empty") endif() add_test(NAME ${${prefix}_NAME} COMMAND ${CMAKE_COMMAND} "-Dtest_name=${${prefix}_NAME}" "-Dadditional_paths=${${prefix}_ADDITIONAL_PATH}" "-Dtest_timeout=${${prefix}_TIMEOUT}" "-Doutput_directory=${_matlab_temporary_folder}" "-Dworking_directory=${${prefix}_WORKING_DIRECTORY}" "-DMatlab_PROGRAM=${Matlab_MAIN_PROGRAM}" "-Dno_unittest_framework=${${prefix}_NO_UNITTEST_FRAMEWORK}" "-DMatlab_ADDITIONAL_STARTUP_OPTIONS=${${prefix}_MATLAB_ADDITIONAL_STARTUP_OPTIONS}" "-Dunittest_file_to_run=${${prefix}_UNITTEST_FILE}" "-Dcustom_Matlab_test_command=${${prefix}_CUSTOM_TEST_COMMAND}" "-Dcmd_to_run_before_test=${${prefix}_UNITTEST_PRECOMMAND}" -P ${_FindMatlab_SELF_DIR}/MatlabTestsRedirect.cmake ${${prefix}_TEST_ARGS} ${${prefix}_UNPARSED_ARGUMENTS} ) endfunction() #[=======================================================================[.rst: .. command:: matlab_add_mex Adds a Matlab MEX target. This commands compiles the given sources with the current tool-chain in order to produce a MEX file. The final name of the produced output may be specified, as well as additional link libraries, and a documentation entry for the MEX file. Remaining arguments of the call are passed to the :command:`add_library` or :command:`add_executable` command. :: matlab_add_mex( NAME <name> [EXECUTABLE | MODULE | SHARED] SRC src1 [src2 ...] [OUTPUT_NAME output_name] [DOCUMENTATION file.txt] [LINK_TO target1 target2 ...] [R2017b | R2018a] [EXCLUDE_FROM_ALL] [...] ) ``NAME`` name of the target. ``SRC`` list of source files. ``LINK_TO`` a list of additional link dependencies. The target links to ``libmex`` and ``libmx`` by default. ``OUTPUT_NAME`` if given, overrides the default name. The default name is the name of the target without any prefix and with ``Matlab_MEX_EXTENSION`` suffix. ``DOCUMENTATION`` if given, the file ``file.txt`` will be considered as being the documentation file for the MEX file. This file is copied into the same folder without any processing, with the same name as the final mex file, and with extension `.m`. In that case, typing ``help <name>`` in Matlab prints the documentation contained in this file. ``R2017b`` or ``R2018a`` may be given to specify the version of the C API to use: ``R2017b`` specifies the traditional (separate complex) C API, and corresponds to the ``-R2017b`` flag for the `mex` command. ``R2018a`` specifies the new interleaved complex C API, and corresponds to the ``-R2018a`` flag for the `mex` command. Ignored if MATLAB version prior to R2018a. Defaults to ``R2017b``. ``MODULE`` or ``SHARED`` may be given to specify the type of library to be created. ``EXECUTABLE`` may be given to create an executable instead of a library. If no type is given explicitly, the type is ``SHARED``. ``EXCLUDE_FROM_ALL`` This option has the same meaning as for :prop_tgt:`EXCLUDE_FROM_ALL` and is forwarded to :command:`add_library` or :command:`add_executable` commands. The documentation file is not processed and should be in the following format: :: % This is the documentation function ret = mex_target_output_name(input1) #]=======================================================================] function(matlab_add_mex) if(NOT WIN32) # we do not need all this on Windows # pthread options if(CMAKE_CXX_COMPILER_LOADED) check_cxx_compiler_flag(-pthread HAS_MINUS_PTHREAD) elseif(CMAKE_C_COMPILER_LOADED) check_c_compiler_flag(-pthread HAS_MINUS_PTHREAD) endif() # we should use try_compile instead, the link flags are discarded from # this compiler_flag function. #check_cxx_compiler_flag(-Wl,--exclude-libs,ALL HAS_SYMBOL_HIDING_CAPABILITY) endif() set(options EXECUTABLE MODULE SHARED R2017b R2018a EXCLUDE_FROM_ALL) set(oneValueArgs NAME DOCUMENTATION OUTPUT_NAME) set(multiValueArgs LINK_TO SRC) set(prefix _matlab_addmex_prefix) cmake_parse_arguments(${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) if(NOT ${prefix}_NAME) message(FATAL_ERROR "[MATLAB] The MEX target name cannot be empty") endif() if(NOT ${prefix}_OUTPUT_NAME) set(${prefix}_OUTPUT_NAME ${${prefix}_NAME}) endif() if(NOT Matlab_VERSION_STRING VERSION_LESS "9.1") # For 9.1 (R2016b) and newer, add version source file # Add the correct version file depending on which languages are enabled in the project if(CMAKE_C_COMPILER_LOADED) # If C is enabled, use the .c file as it will work fine also with C++ set(MEX_VERSION_FILE "${Matlab_ROOT_DIR}/extern/version/c_mexapi_version.c") elseif(CMAKE_CXX_COMPILER_LOADED) # If C is not enabled, check if CXX is enabled and use the .cpp file # to avoid that the .c file is silently ignored set(MEX_VERSION_FILE "${Matlab_ROOT_DIR}/extern/version/cpp_mexapi_version.cpp") else() # If neither C or CXX is enabled, warn because we cannot add the source. # TODO: add support for fortran mex files message(WARNING "[MATLAB] matlab_add_mex requires that at least C or CXX are enabled languages") endif() endif() if(NOT Matlab_VERSION_STRING VERSION_LESS "9.4") # For 9.4 (R2018a) and newer, add API macro if(${${prefix}_R2018a}) set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2018a") else() set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2017b") endif() endif() set(_option_EXCLUDE_FROM_ALL) if(${prefix}_EXCLUDE_FROM_ALL) set(_option_EXCLUDE_FROM_ALL "EXCLUDE_FROM_ALL") endif() if(${prefix}_EXECUTABLE) add_executable(${${prefix}_NAME} ${_option_EXCLUDE_FROM_ALL} ${${prefix}_SRC} ${MEX_VERSION_FILE} ${${prefix}_DOCUMENTATION} ${${prefix}_UNPARSED_ARGUMENTS}) else() if(${prefix}_MODULE) set(type MODULE) else() set(type SHARED) endif() add_library(${${prefix}_NAME} ${type} ${_option_EXCLUDE_FROM_ALL} ${${prefix}_SRC} ${MEX_VERSION_FILE} ${${prefix}_DOCUMENTATION} ${${prefix}_UNPARSED_ARGUMENTS}) endif() target_include_directories(${${prefix}_NAME} PRIVATE ${Matlab_INCLUDE_DIRS}) if(Matlab_HAS_CPP_API) target_link_libraries(${${prefix}_NAME} ${Matlab_ENGINE_LIBRARY} ${Matlab_DATAARRAY_LIBRARY}) endif() target_link_libraries(${${prefix}_NAME} ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY} ${${prefix}_LINK_TO}) set_target_properties(${${prefix}_NAME} PROPERTIES PREFIX "" OUTPUT_NAME ${${prefix}_OUTPUT_NAME} SUFFIX ".${Matlab_MEX_EXTENSION}") target_compile_definitions(${${prefix}_NAME} PRIVATE ${MEX_API_MACRO} MATLAB_MEX_FILE) # documentation if(NOT ${${prefix}_DOCUMENTATION} STREQUAL "") get_target_property(output_name ${${prefix}_NAME} OUTPUT_NAME) add_custom_command( TARGET ${${prefix}_NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${${prefix}_DOCUMENTATION} $<TARGET_FILE_DIR:${${prefix}_NAME}>/${output_name}.m COMMENT "[MATLAB] Copy ${${prefix}_NAME} documentation file into the output folder" ) endif() # documentation # entry point in the mex file + taking care of visibility and symbol clashes. if(WIN32) if (MSVC) set(_link_flags "${_link_flags} /EXPORT:mexFunction") if(NOT Matlab_VERSION_STRING VERSION_LESS "9.1") # For 9.1 (R2016b) and newer, export version set(_link_flags "${_link_flags} /EXPORT:mexfilerequiredapiversion") endif() set_property(TARGET ${${prefix}_NAME} APPEND PROPERTY LINK_FLAGS ${_link_flags}) endif() # No other compiler currently supported on Windows. set_target_properties(${${prefix}_NAME} PROPERTIES DEFINE_SYMBOL "DLL_EXPORT_SYM=__declspec(dllexport)") else() if(Matlab_VERSION_STRING VERSION_LESS "9.1") # For versions prior to 9.1 (R2016b) set(_ver_map_files ${Matlab_EXTERN_LIBRARY_DIR}/mexFunction.map) else() # For 9.1 (R2016b) and newer set(_ver_map_files ${Matlab_EXTERN_LIBRARY_DIR}/c_exportsmexfileversion.map) endif() if(NOT Matlab_VERSION_STRING VERSION_LESS "9.5") # For 9.5 (R2018b) (and newer?) target_compile_options(${${prefix}_NAME} PRIVATE "-fvisibility=default") # This one is weird, it might be a bug in <mex.h> for R2018b. When compiling with # -fvisibility=hidden, the symbol `mexFunction` cannot be exported. Reading the # source code for <mex.h>, it seems that the preprocessor macro `MW_NEEDS_VERSION_H` # needs to be defined for `__attribute__((visibility("default")))` to be added # in front of the declaration of `mexFunction`. In previous versions of MATLAB this # was not the case, there `DLL_EXPORT_SYM` needed to be defined. # Adding `-fvisibility=hidden` to the `mex` command causes the build to fail. # TODO: Check that this is still necessary in R2019a when it comes out. endif() if(APPLE) if(Matlab_HAS_CPP_API) list(APPEND _ver_map_files ${Matlab_EXTERN_LIBRARY_DIR}/cppMexFunction.map) # This one doesn't exist on Linux set(_link_flags "${_link_flags} -Wl,-U,_mexCreateMexFunction -Wl,-U,_mexDestroyMexFunction -Wl,-U,_mexFunctionAdapter") # On MacOS, the MEX command adds the above, without it the link breaks # because we indiscriminately use "cppMexFunction.map" even for C API MEX-files. endif() set(_export_flag_name -exported_symbols_list) else() # Linux if(HAS_MINUS_PTHREAD) # Apparently, compiling with -pthread generated the proper link flags # and some defines at compilation target_compile_options(${${prefix}_NAME} PRIVATE "-pthread") endif() set(_link_flags "${_link_flags} -Wl,--as-needed") set(_export_flag_name --version-script) endif() foreach(_file ${_ver_map_files}) set(_link_flags "${_link_flags} -Wl,${_export_flag_name},${_file}") endforeach() # The `mex` command doesn't add this define. It is specified here in order # to export the symbol in case the client code decides to hide its symbols set_target_properties(${${prefix}_NAME} PROPERTIES DEFINE_SYMBOL "DLL_EXPORT_SYM=__attribute__((visibility(\"default\")))" LINK_FLAGS "${_link_flags}" ) endif() endfunction() # (internal) # Used to get the version of matlab, using caching. This basically transforms the # output of the root list, with possible unknown version, to a version # This can possibly run Matlab for extracting the version. function(_Matlab_get_version_from_root matlab_root matlab_or_mcr matlab_known_version matlab_final_version) # if the version is not trivial, we query matlab (if not MCR) for that # we keep track of the location of matlab that induced this version #if(NOT DEFINED Matlab_PROG_VERSION_STRING_AUTO_DETECT) # set(Matlab_PROG_VERSION_STRING_AUTO_DETECT "" CACHE INTERNAL "internal matlab location for the discovered version") #endif() if(NOT matlab_known_version STREQUAL "NOTFOUND") # the version is known, we just return it set(${matlab_final_version} ${matlab_known_version} PARENT_SCOPE) set(Matlab_VERSION_STRING_INTERNAL ${matlab_known_version} CACHE INTERNAL "Matlab version (automatically determined)" FORCE) return() endif() if(matlab_or_mcr STREQUAL "UNKNOWN") if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Determining Matlab or MCR") endif() if(EXISTS "${matlab_root}/appdata/version.xml") # we inspect the application version.xml file that contains the product information file(STRINGS "${matlab_root}/appdata/version.xml" productinfo_string NEWLINE_CONSUME) string(REGEX MATCH "<installedProductData.*displayedString=\"([a-zA-Z ]+)\".*/>" product_reg_match ${productinfo_string} ) # default fallback to Matlab set(matlab_or_mcr "MATLAB") if(NOT CMAKE_MATCH_1 STREQUAL "") string(TOLOWER "${CMAKE_MATCH_1}" product_reg_match) if(product_reg_match STREQUAL "matlab runtime") set(matlab_or_mcr "MCR") endif() endif() endif() if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] '${matlab_root}' contains the '${matlab_or_mcr}'") endif() endif() # UNKNOWN is the default behaviour in case we # - have an erroneous matlab_root # - have an initial 'UNKNOWN' if(matlab_or_mcr STREQUAL "MATLAB" OR matlab_or_mcr STREQUAL "UNKNOWN") # MATLAB versions set(_matlab_current_program ${Matlab_MAIN_PROGRAM}) # do we already have a matlab program? if(NOT _matlab_current_program) set(_find_matlab_options) if(matlab_root AND EXISTS ${matlab_root}) set(_find_matlab_options PATHS ${matlab_root} ${matlab_root}/bin NO_DEFAULT_PATH) endif() find_program( _matlab_current_program matlab ${_find_matlab_options} DOC "Matlab main program" ) endif() if(NOT _matlab_current_program OR NOT EXISTS ${_matlab_current_program}) # if not found, clear the dependent variables if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Cannot find the main matlab program under ${matlab_root}") endif() set(Matlab_PROG_VERSION_STRING_AUTO_DETECT "" CACHE INTERNAL "internal matlab location for the discovered version" FORCE) set(Matlab_VERSION_STRING_INTERNAL "" CACHE INTERNAL "internal matlab location for the discovered version" FORCE) unset(_matlab_current_program) unset(_matlab_current_program CACHE) return() endif() # full real path for path comparison get_filename_component(_matlab_main_real_path_tmp "${_matlab_current_program}" REALPATH) unset(_matlab_current_program) unset(_matlab_current_program CACHE) # is it the same as the previous one? if(_matlab_main_real_path_tmp STREQUAL Matlab_PROG_VERSION_STRING_AUTO_DETECT) set(${matlab_final_version} ${Matlab_VERSION_STRING_INTERNAL} PARENT_SCOPE) return() endif() # update the location of the program set(Matlab_PROG_VERSION_STRING_AUTO_DETECT ${_matlab_main_real_path_tmp} CACHE INTERNAL "internal matlab location for the discovered version" FORCE) set(matlab_list_of_all_versions) matlab_get_version_from_matlab_run("${Matlab_PROG_VERSION_STRING_AUTO_DETECT}" matlab_list_of_all_versions) list(LENGTH matlab_list_of_all_versions list_of_all_versions_length) if(list_of_all_versions_length GREATER 0) list(GET matlab_list_of_all_versions 0 _matlab_version_tmp) else() set(_matlab_version_tmp "unknown") endif() # set the version into the cache set(Matlab_VERSION_STRING_INTERNAL ${_matlab_version_tmp} CACHE INTERNAL "Matlab version (automatically determined)" FORCE) # warning, just in case several versions found (should not happen) if((list_of_all_versions_length GREATER 1) AND MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Found several versions, taking the first one (versions found ${matlab_list_of_all_versions})") endif() # return the updated value set(${matlab_final_version} ${Matlab_VERSION_STRING_INTERNAL} PARENT_SCOPE) elseif(EXISTS "${matlab_root}/VersionInfo.xml") # MCR # we cannot run anything in order to extract the version. We assume that the file # VersionInfo.xml exists under the MatlabRoot, we look for it and extract the version from there set(_matlab_version_tmp "unknown") file(STRINGS "${matlab_root}/VersionInfo.xml" versioninfo_string NEWLINE_CONSUME) if(versioninfo_string) # parses "<version>9.2.0.538062</version>" string(REGEX MATCH "<version>(.*)</version>" version_reg_match ${versioninfo_string} ) if(CMAKE_MATCH_1 MATCHES "(([0-9])\\.([0-9]))[\\.0-9]*") set(_matlab_version_tmp "${CMAKE_MATCH_1}") endif() endif() set(${matlab_final_version} "${_matlab_version_tmp}" PARENT_SCOPE) set(Matlab_VERSION_STRING_INTERNAL "${_matlab_version_tmp}" CACHE INTERNAL "Matlab (MCR) version (automatically determined)" FORCE) endif() # Matlab or MCR endfunction() # Utility function for finding Matlab or MCR on Win32 function(_Matlab_find_instances_win32 matlab_roots) # On WIN32, we look for Matlab installation in the registry # if unsuccessful, we look for all known revision and filter the existing # ones. # testing if we are able to extract the needed information from the registry set(_matlab_versions_from_registry) if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(_matlab_win64 ON) else() set(_matlab_win64 OFF) endif() matlab_extract_all_installed_versions_from_registry(_matlab_win64 _matlab_versions_from_registry) # the returned list is empty, doing the search on all known versions if(NOT _matlab_versions_from_registry) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Search for Matlab from the registry unsuccessful, testing all supported versions") endif() extract_matlab_versions_from_registry_brute_force(_matlab_versions_from_registry) endif() # filtering the results with the registry keys matlab_get_all_valid_matlab_roots_from_registry("${_matlab_versions_from_registry}" _matlab_possible_roots) set(${matlab_roots} ${_matlab_possible_roots} PARENT_SCOPE) endfunction() # Utility function for finding Matlab or MCR on OSX function(_Matlab_find_instances_osx matlab_roots) set(_matlab_possible_roots) # on mac, we look for the /Application paths # this corresponds to the behaviour on Windows. On Linux, we do not have # any other guess. matlab_get_supported_releases(_matlab_releases) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Matlab supported versions ${_matlab_releases}. If more version should be supported " "the variable MATLAB_ADDITIONAL_VERSIONS can be set according to the documentation") endif() foreach(_matlab_current_release IN LISTS _matlab_releases) matlab_get_version_from_release_name("${_matlab_current_release}" _matlab_current_version) string(REPLACE "." "" _matlab_current_version_without_dot "${_matlab_current_version}") set(_matlab_base_path "/Applications/MATLAB_${_matlab_current_release}.app") # Check Matlab, has precedence over MCR if(EXISTS ${_matlab_base_path}) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Found version ${_matlab_current_release} (${_matlab_current_version}) in ${_matlab_base_path}") endif() list(APPEND _matlab_possible_roots "MATLAB" ${_matlab_current_version} ${_matlab_base_path}) endif() # Checks MCR set(_mcr_path "/Applications/MATLAB/MATLAB_Runtime/v${_matlab_current_version_without_dot}") if(EXISTS "${_mcr_path}") if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Found MCR version ${_matlab_current_release} (${_matlab_current_version}) in ${_mcr_path}") endif() list(APPEND _matlab_possible_roots "MCR" ${_matlab_current_version} ${_mcr_path}) endif() endforeach() set(${matlab_roots} ${_matlab_possible_roots} PARENT_SCOPE) endfunction() # Utility function for finding Matlab or MCR from the PATH function(_Matlab_find_instances_from_path matlab_roots) set(_matlab_possible_roots) # At this point, we have no other choice than trying to find it from PATH. # If set by the user, this wont change find_program( _matlab_main_tmp NAMES matlab) if(_matlab_main_tmp) # we then populate the list of roots, with empty version if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] matlab found from PATH: ${_matlab_main_tmp}") endif() # resolve symlinks get_filename_component(_matlab_current_location "${_matlab_main_tmp}" REALPATH) # get the directory (the command below has to be run twice) # this will be the matlab root get_filename_component(_matlab_current_location "${_matlab_current_location}" DIRECTORY) get_filename_component(_matlab_current_location "${_matlab_current_location}" DIRECTORY) # Matlab should be in bin # We found the Matlab program list(APPEND _matlab_possible_roots "MATLAB" "NOTFOUND" ${_matlab_current_location}) # we remove this from the CACHE unset(_matlab_main_tmp CACHE) else() find_program( _matlab_mex_tmp NAMES mex) if(_matlab_mex_tmp) # we then populate the list of roots, with empty version if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] mex compiler found from PATH: ${_matlab_mex_tmp}") endif() # resolve symlinks get_filename_component(_mex_current_location "${_matlab_mex_tmp}" REALPATH) # get the directory (the command below has to be run twice) # this will be the matlab root get_filename_component(_mex_current_location "${_mex_current_location}" DIRECTORY) get_filename_component(_mex_current_location "${_mex_current_location}" DIRECTORY) # Matlab Runtime mex compiler should be in bin # We found the Matlab program list(APPEND _matlab_possible_roots "MCR" "NOTFOUND" ${_mex_current_location}) unset(_matlab_mex_tmp CACHE) else() if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] mex compiler not found") endif() endif() endif() set(${matlab_roots} ${_matlab_possible_roots} PARENT_SCOPE) endfunction() # ################################### # Exploring the possible Matlab_ROOTS # this variable will get all Matlab installations found in the current system. set(_matlab_possible_roots) if(Matlab_ROOT_DIR) # if the user specifies a possible root, we keep this one if(NOT EXISTS "${Matlab_ROOT_DIR}") # if Matlab_ROOT_DIR specified but erroneous if(MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] the specified path for Matlab_ROOT_DIR does not exist (${Matlab_ROOT_DIR})") endif() else() # NOTFOUND indicates the code below to search for the version automatically if("${Matlab_VERSION_STRING_INTERNAL}" STREQUAL "") list(APPEND _matlab_possible_roots "UNKNOWN" "NOTFOUND" ${Matlab_ROOT_DIR}) # empty version, empty MCR/Matlab indication else() list(APPEND _matlab_possible_roots "UNKNOWN" ${Matlab_VERSION_STRING_INTERNAL} ${Matlab_ROOT_DIR}) # cached version endif() endif() else() # if the user does not specify the possible installation root, we look for # one installation using the appropriate heuristics. # There is apparently no standard way on Linux. if(CMAKE_HOST_WIN32) _Matlab_find_instances_win32(_matlab_possible_roots_win32) list(APPEND _matlab_possible_roots ${_matlab_possible_roots_win32}) elseif(APPLE) _Matlab_find_instances_osx(_matlab_possible_roots_osx) list(APPEND _matlab_possible_roots ${_matlab_possible_roots_osx}) endif() endif() list(LENGTH _matlab_possible_roots _numbers_of_matlab_roots) if(_numbers_of_matlab_roots EQUAL 0) # if we have not found anything, we fall back on the PATH _Matlab_find_instances_from_path(_matlab_possible_roots) endif() if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Matlab root folders are ${_matlab_possible_roots}") endif() # take the first possible Matlab root list(LENGTH _matlab_possible_roots _numbers_of_matlab_roots) set(Matlab_VERSION_STRING "NOTFOUND") set(Matlab_Or_MCR "UNKNOWN") if(_numbers_of_matlab_roots GREATER 0) if(Matlab_FIND_VERSION_EXACT) list(FIND _matlab_possible_roots ${Matlab_FIND_VERSION} _list_index) if(_list_index LESS 0) set(_list_index 1) endif() math(EXPR _matlab_or_mcr_index "${_list_index} - 1") math(EXPR _matlab_root_dir_index "${_list_index} + 1") list(GET _matlab_possible_roots ${_matlab_or_mcr_index} Matlab_Or_MCR) list(GET _matlab_possible_roots ${_list_index} Matlab_VERSION_STRING) list(GET _matlab_possible_roots ${_matlab_root_dir_index} Matlab_ROOT_DIR) else() list(GET _matlab_possible_roots 0 Matlab_Or_MCR) list(GET _matlab_possible_roots 1 Matlab_VERSION_STRING) list(GET _matlab_possible_roots 2 Matlab_ROOT_DIR) # adding a warning in case of ambiguity if(_numbers_of_matlab_roots GREATER 3 AND MATLAB_FIND_DEBUG) message(WARNING "[MATLAB] Found several distributions of Matlab. Setting the current version to ${Matlab_VERSION_STRING} (located ${Matlab_ROOT_DIR})." " If this is not the desired behaviour, use the EXACT keyword or provide the -DMatlab_ROOT_DIR=... on the command line") endif() endif() endif() # check if the root changed wrt. the previous defined one, if so # clear all the cached variables for being able to reconfigure properly if(DEFINED Matlab_ROOT_DIR_LAST_CACHED) if(NOT Matlab_ROOT_DIR_LAST_CACHED STREQUAL Matlab_ROOT_DIR) set(_Matlab_cached_vars Matlab_VERSION_STRING Matlab_INCLUDE_DIRS Matlab_MEX_LIBRARY Matlab_MEX_COMPILER Matlab_MCC_COMPILER Matlab_MAIN_PROGRAM Matlab_MX_LIBRARY Matlab_ENG_LIBRARY Matlab_MAT_LIBRARY Matlab_ENGINE_LIBRARY Matlab_DATAARRAY_LIBRARY Matlab_MEX_EXTENSION Matlab_SIMULINK_INCLUDE_DIR # internal Matlab_MEXEXTENSIONS_PROG Matlab_ROOT_DIR_LAST_CACHED #Matlab_PROG_VERSION_STRING_AUTO_DETECT #Matlab_VERSION_STRING_INTERNAL ) foreach(_var IN LISTS _Matlab_cached_vars) if(DEFINED ${_var}) unset(${_var} CACHE) endif() endforeach() endif() endif() set(Matlab_ROOT_DIR_LAST_CACHED ${Matlab_ROOT_DIR} CACHE INTERNAL "last Matlab root dir location") set(Matlab_ROOT_DIR ${Matlab_ROOT_DIR} CACHE PATH "Matlab installation root path" FORCE) # Fix the version, in case this one is NOTFOUND _Matlab_get_version_from_root( "${Matlab_ROOT_DIR}" "${Matlab_Or_MCR}" ${Matlab_VERSION_STRING} Matlab_VERSION_STRING ) if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] Current version is ${Matlab_VERSION_STRING} located ${Matlab_ROOT_DIR}") endif() # MATLAB 9.4 (R2018a) and newer have a new C++ API # This API pulls additional required libraries. if(NOT ${Matlab_VERSION_STRING} VERSION_LESS "9.4") set(Matlab_HAS_CPP_API 1) endif() if(Matlab_ROOT_DIR) file(TO_CMAKE_PATH ${Matlab_ROOT_DIR} Matlab_ROOT_DIR) endif() if(CMAKE_SIZEOF_VOID_P EQUAL 4) set(_matlab_64Build FALSE) else() set(_matlab_64Build TRUE) endif() if(APPLE) set(_matlab_bin_prefix "mac") # i should be for intel set(_matlab_bin_suffix_32bits "i") set(_matlab_bin_suffix_64bits "i64") elseif(UNIX) set(_matlab_bin_prefix "gln") set(_matlab_bin_suffix_32bits "x86") set(_matlab_bin_suffix_64bits "xa64") else() set(_matlab_bin_prefix "win") set(_matlab_bin_suffix_32bits "32") set(_matlab_bin_suffix_64bits "64") endif() set(MATLAB_INCLUDE_DIR_TO_LOOK ${Matlab_ROOT_DIR}/extern/include) if(_matlab_64Build) set(_matlab_current_suffix ${_matlab_bin_suffix_64bits}) else() set(_matlab_current_suffix ${_matlab_bin_suffix_32bits}) endif() set(Matlab_BINARIES_DIR ${Matlab_ROOT_DIR}/bin/${_matlab_bin_prefix}${_matlab_current_suffix}) set(Matlab_EXTERN_LIBRARY_DIR ${Matlab_ROOT_DIR}/extern/lib/${_matlab_bin_prefix}${_matlab_current_suffix}) set(Matlab_EXTERN_BINARIES_DIR ${Matlab_ROOT_DIR}/extern/bin/${_matlab_bin_prefix}${_matlab_current_suffix}) if(WIN32) if(MINGW) set(_matlab_lib_dir_for_search ${Matlab_EXTERN_LIBRARY_DIR}/mingw64) else() set(_matlab_lib_dir_for_search ${Matlab_EXTERN_LIBRARY_DIR}/microsoft) endif() set(_matlab_lib_prefix_for_search "lib") else() set(_matlab_lib_dir_for_search ${Matlab_BINARIES_DIR} ${Matlab_EXTERN_BINARIES_DIR}) set(_matlab_lib_prefix_for_search "lib") endif() unset(_matlab_64Build) if(NOT DEFINED Matlab_MEX_EXTENSION) set(_matlab_mex_extension "") matlab_get_mex_suffix("${Matlab_ROOT_DIR}" _matlab_mex_extension) # This variable goes to the cache. set(Matlab_MEX_EXTENSION ${_matlab_mex_extension} CACHE STRING "Extensions for the mex targets (automatically given by Matlab)") unset(_matlab_mex_extension) endif() if(MATLAB_FIND_DEBUG) message(STATUS "[MATLAB] [DEBUG]_matlab_lib_prefix_for_search = ${_matlab_lib_prefix_for_search} | _matlab_lib_dir_for_search = ${_matlab_lib_dir_for_search}") endif() # internal # This small stub around find_library is to prevent any pollution of CMAKE_FIND_LIBRARY_PREFIXES in the global scope. # This is the function to be used below instead of the find_library directives. function(_Matlab_find_library _matlab_library_prefix) set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} ${_matlab_library_prefix}) find_library(${ARGN}) endfunction() set(_matlab_required_variables) # Order is as follow: # - unconditionally required libraries/headers first # - then library components # - then program components # the MEX library/header are required find_path( Matlab_INCLUDE_DIRS mex.h PATHS ${MATLAB_INCLUDE_DIR_TO_LOOK} NO_DEFAULT_PATH ) list(APPEND _matlab_required_variables Matlab_INCLUDE_DIRS) _Matlab_find_library( ${_matlab_lib_prefix_for_search} Matlab_MEX_LIBRARY mex PATHS ${_matlab_lib_dir_for_search} NO_DEFAULT_PATH ) list(APPEND _matlab_required_variables Matlab_MEX_LIBRARY) # the MEX extension is required list(APPEND _matlab_required_variables Matlab_MEX_EXTENSION) # the matlab root is required list(APPEND _matlab_required_variables Matlab_ROOT_DIR) # The MX library is required _Matlab_find_library( ${_matlab_lib_prefix_for_search} Matlab_MX_LIBRARY mx PATHS ${_matlab_lib_dir_for_search} NO_DEFAULT_PATH ) list(APPEND _matlab_required_variables Matlab_MX_LIBRARY) if(Matlab_MX_LIBRARY) set(Matlab_MX_LIBRARY_FOUND TRUE) endif() if(Matlab_HAS_CPP_API) # The MatlabEngine library is required for R2018a+ _Matlab_find_library( ${_matlab_lib_prefix_for_search} Matlab_ENGINE_LIBRARY MatlabEngine PATHS ${_matlab_lib_dir_for_search} DOC "MatlabEngine Library" NO_DEFAULT_PATH ) list(APPEND _matlab_required_variables Matlab_ENGINE_LIBRARY) if(Matlab_ENGINE_LIBRARY) set(Matlab_ENGINE_LIBRARY_FOUND TRUE) endif() # The MatlabDataArray library is required for R2018a+ _Matlab_find_library( ${_matlab_lib_prefix_for_search} Matlab_DATAARRAY_LIBRARY MatlabDataArray PATHS ${_matlab_lib_dir_for_search} DOC "MatlabDataArray Library" NO_DEFAULT_PATH ) list(APPEND _matlab_required_variables Matlab_DATAARRAY_LIBRARY) if(Matlab_DATAARRAY_LIBRARY) set(Matlab_DATAARRAY_LIBRARY_FOUND TRUE) endif() endif() # Component ENG library if("ENG_LIBRARY" IN_LIST Matlab_FIND_COMPONENTS) _Matlab_find_library( ${_matlab_lib_prefix_for_search} Matlab_ENG_LIBRARY eng PATHS ${_matlab_lib_dir_for_search} NO_DEFAULT_PATH ) if(Matlab_ENG_LIBRARY) set(Matlab_ENG_LIBRARY_FOUND TRUE) endif() endif() # Component MAT library if("MAT_LIBRARY" IN_LIST Matlab_FIND_COMPONENTS) _Matlab_find_library( ${_matlab_lib_prefix_for_search} Matlab_MAT_LIBRARY mat PATHS ${_matlab_lib_dir_for_search} NO_DEFAULT_PATH ) if(Matlab_MAT_LIBRARY) set(Matlab_MAT_LIBRARY_FOUND TRUE) endif() endif() # Component Simulink if("SIMULINK" IN_LIST Matlab_FIND_COMPONENTS) find_path( Matlab_SIMULINK_INCLUDE_DIR simstruc.h PATHS "${Matlab_ROOT_DIR}/simulink/include" NO_DEFAULT_PATH ) if(Matlab_SIMULINK_INCLUDE_DIR) set(Matlab_SIMULINK_FOUND TRUE) list(APPEND Matlab_INCLUDE_DIRS "${Matlab_SIMULINK_INCLUDE_DIR}") endif() endif() # component Matlab program if("MAIN_PROGRAM" IN_LIST Matlab_FIND_COMPONENTS) find_program( Matlab_MAIN_PROGRAM matlab PATHS ${Matlab_ROOT_DIR} ${Matlab_ROOT_DIR}/bin DOC "Matlab main program" NO_DEFAULT_PATH ) if(Matlab_MAIN_PROGRAM) set(Matlab_MAIN_PROGRAM_FOUND TRUE) endif() endif() # component Mex Compiler if("MEX_COMPILER" IN_LIST Matlab_FIND_COMPONENTS) find_program( Matlab_MEX_COMPILER "mex" PATHS ${Matlab_BINARIES_DIR} DOC "Matlab MEX compiler" NO_DEFAULT_PATH ) if(Matlab_MEX_COMPILER) set(Matlab_MEX_COMPILER_FOUND TRUE) endif() endif() # component MCC Compiler if("MCC_COMPILER" IN_LIST Matlab_FIND_COMPONENTS) find_program( Matlab_MCC_COMPILER "mcc" PATHS ${Matlab_BINARIES_DIR} DOC "Matlab MCC compiler" NO_DEFAULT_PATH ) if(Matlab_MCC_COMPILER) set(Matlab_MCC_COMPILER_FOUND TRUE) endif() endif() set(Matlab_LIBRARIES ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY} ${Matlab_ENG_LIBRARY} ${Matlab_MAT_LIBRARY} ${Matlab_DATAARRAY_LIBRARY} ${Matlab_ENGINE_LIBRARY}) find_package_handle_standard_args( Matlab FOUND_VAR Matlab_FOUND REQUIRED_VARS ${_matlab_required_variables} VERSION_VAR Matlab_VERSION_STRING HANDLE_COMPONENTS) unset(_matlab_required_variables) unset(_matlab_bin_prefix) unset(_matlab_bin_suffix_32bits) unset(_matlab_bin_suffix_64bits) unset(_matlab_current_suffix) unset(_matlab_lib_dir_for_search) unset(_matlab_lib_prefix_for_search) if(Matlab_INCLUDE_DIRS AND Matlab_LIBRARIES) mark_as_advanced( Matlab_MEX_LIBRARY Matlab_MX_LIBRARY Matlab_ENG_LIBRARY Matlab_ENGINE_LIBRARY Matlab_DATAARRAY_LIBRARY Matlab_MAT_LIBRARY Matlab_INCLUDE_DIRS Matlab_FOUND Matlab_MAIN_PROGRAM Matlab_MEXEXTENSIONS_PROG Matlab_MEX_EXTENSION ) endif() cmake_policy(POP)