forked from qipeng/convolutionalRBM.m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvmex.pl
3194 lines (2817 loc) · 103 KB
/
nvmex.pl
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
#
# Name:
# mex/mbuild Perl script for PC only.
#
# mex compilation program for MATLAB C/C++ and Fortran
# language MEX-files
#
# mbuild compilation program for executable programs for
# the MATLAB compiler.
#
# Usage:
# Use mex -h or mbuild -h to see usage.
#
# Description:
# This Perl program provides dual functionality for MEX and MBUILD.
# The input argument '-mb' targets the routine to do MBUILD.
# Otherwise, it does MEX.
#
# Options:
# See the 'describe' subroutine below for the MEX options.
# See the 'describe_mb' subroutine below for the MBUILD options.
#
# Options (undocumented):
#
# -setup:$compiler[:$optionfile_type]
#
# $compiler must be taken from the option file name:
#
# <compiler>[<optionfile_type>]opts.bat (mex)
# <compiler>compp.bat (mbuild)
#
# Currently, <optionfile_type> = 'engmat' for mex only.
#
# -f $destination
#
# Where to put the options file. $destination must
# be $dir/$file where at least $dir exists and is
# writable. It can be used only with -setup. If
# not used the file is put in the default location:
#
# PC:
# --
#
# <UserProfile>\Application Data\MathWorks\MATLAB\R<version>
#
# where
#
# <UserProfile> Is the value determined by Perl
# subroutine get_user_profile_dir
#
# <version> MATLAB Release number like 14.
#
# UNIX/Linux/Mac:
# --------------
#
# $HOME/.matlab/R<version>
#
# where
#
# <version> MATLAB Release number like 14.
#
# Option files:
#
# mex: $MATLAB\bin\$ARCH\mexopts\*.stp
# $MATLAB\bin\$ARCH\mexopts\*.bat
#
# *opts.bat files are 'installed' by -setup.
# *engmatopts.bat files are arguments using -f only.
#
# mbuild: $MATLAB\win\$ARCH\mbuildopts\*.stp
# $MATLAB\win\$ARCH\mbuildopts\*.bat
#
#
# Call structure:
#
# mex.pl
# -> mexsetup.pm
# MEX:
# -> mexopts/<compiler>opts.stp
# (compiler == msvc50 || msvc60)
# -> msvc_modules_installer.pm
# -> mexopts/<compiler>opts.bat
# (compiler == bcc53 || bcc54 || bcc55 || bcc55free
# bcc56)
# -> link_borland_mex.pl
# -> mexopts/<compiler>engmatopts.bat
#
# MBUILD:
# -> mbuildopts/<compiler>compp.stp
# (compiler == msvc50 || msvc60)
# -> msvc_modules_installer.pm
# -> mbuildopts/<compiler>compp.bat
# (compiler == bcc54 || bcc55 || bcc55free || bcc56)
# -> link_borland_mex.pl
#
#
# Copyright 1984-2007 The MathWorks, Inc.
# $Revision: 1.1.6.26.4.1 $
#__________________________________________________________________________
#
#=======================================================================
require 5.008_008;
sub tool_name
{
#===================================================================
# tool_name: Returns the tool name, i.e. mex or mbuild.
#===================================================================
if ($main::mbuild eq "yes")
{
"mbuild";
}
else
{
"mex";
}
}
#=======================================================================
use File::DosGlob 'glob';
use File::Basename;
use File::Path;
use File::Spec;
use File::Copy;
use File::Temp qw/ tempdir/;
use Cwd;
BEGIN
{
#===================================================================
# Set @INC for 5.00503 perl.
# If perl gets upgraded, this may have to be changed.
#===================================================================
if ( $] < 5.00503 ) {
die "ERROR: This script requires perl 5.00503 or higher.
You have perl $] installed on your machine and you did not set MATLAB variable,
so the correct version of perl could not be found";
}
$main::mbuild = scalar(grep {/^-mb$/i} @ARGV) ? 'yes' : 'no';
$main::cmd_name = $0;
my $isUNC = 0;
if ( $] < 5.006001 && $main::cmd_name =~ m%^[\\/]{2}% ) {
$isUNC = 1;
}
$main::cmd_name = File::Spec->canonpath($main::cmd_name);
if ($isUNC) {
$main::cmd_name = "\\" . $main::cmd_name;
}
$main::cmd_name =~ s%\.*[^\\\.][^\\]*\\+\.\.(\\|$)%%g; # xxx\dir\..\yyy --> xxx\yyy
$main::cmd_name = Win32::GetShortPathName($main::cmd_name);
(my $unused, $main::script_directory) = fileparse($main::cmd_name);
#($main::matlabroot = $main::cmd_name) =~ s%\\bin\\mex.pl$%%;
($main::matlabroot = $main::cmd_name) =~ s%\\bin\\nvmex.pl$%%;
$main::cmd_name = uc($main::cmd_name);
push(@INC, $main::script_directory);
}
#=======================================================================
END{
#===================================================================
# This block is executed at the end of execution of mex.pl
#===================================================================
if ($main::temp_dir) {
my $savedErrorStatus = $?;
# We need to use the system version of RMDIR because the Perl
# RMTREE function does not handle being run out of a Unicode path.
# Call RMDIR trhough CMD to ensure we are using the system version
system("cmd /c rmdir /Q /S $main::temp_dir");
$? = $savedErrorStatus;
}
}
#=======================================================================
use mexsetup;
use mexutils;
require "shellwords.pl"; # Found in $MATLAB/sys/perl/win32/lib
# This is necessary to break up the text in
# the file into shell arguments. This is used
# to support the @<rspfile> argument.
if ( ( $ENV{'PROCESSOR_ARCHITECTURE'} eq "AMD64" ) ||
( $ENV{'PROCESSOR_ARCHITEW6432'} eq "AMD64" ) ) {
if ( -f mexCatfile($main::matlabroot, "bin", "win64", "matlab.exe") ) {
$ARCH = "win64";
} elsif (-f mexCatfile($main::matlabroot, "bin", "win32", "matlab.exe") ) {
$ARCH = "win32";
} else {
$ARCH = "UNKNOWN";
}
} elsif ( $ENV{'PROCESSOR_ARCHITECTURE'} eq "x86" ) {
$ARCH = "win32";
} else {
$ARCH = "UNKNOWN";
}
# Support command line override
if (grep /^-win32$/, @ARGV) {
$ARCH = "win32";
}
$ENV{'ARCH'} = $ARCH;
########################################################################
#=======================================================================
# Common subroutines:
#=======================================================================
#
# compile_files: Compile files and form list of files to
# link.
# compile_resource: Compile the resource.
# do_setup: Do only the setup.
# emit_compile_step: Output compile step to makefile.
# emit_delete_resource_file: Output delete resource file to makefile.
# emit_link_dependency: Output link dependency to makefile.
# emit_linker_step: Output linker step to makefile.
# emit_makedef: Output makedef step to makefile.
# emit_makefile_terminator: Output terminator for makefile.
# emit_pre_or_postlink_step: Output postlink step to makefile.
# emit_resource_compiler: Output resource compile step to makefile.
# emit_resource_linker_step: Output resource linker step to makefile.
# expand_wildcards: Expand possible wildcards in the arguments
# for perl >= 5.00503
# expire: Die but with cleanup.
# find_options_file: Find the options file.
# fix_common_variables: Fix common variables.
# fix_flag_variables: Fix the flag variables.
# files_to_remove: Add files to remove list.
# get_user_profile_dir Returns UserProfile directory
# init_common: Common initialization.
# linker_arguments: Create response file of linker arguments or
# just string.
# link_files: Link files.
# options_file: Get options file if not passed as an
# argument. Source the options file.
# parse_common_dash_args: Parse the common dash arguments.
# parse_common_nodash_args: Parse the common non-dash arguments.
# pre_or_postlink: Do prelink or postlink steps based on input.
# process_overrides: Process command line overrides.
# process_response_file: Run shellwords on filename argument.
# rectify_path: Check path for system directories and add
# them if not present.
# resource_linker: Run resource linker.
# RunCmd: Run a single command.
# search_path: Search DOS PATH environment for $binary_name
# argument
# set_common_variables: Set more common variables.
# smart_quote: Add quotes around strings with space.
# start_makefile: Open and write the main dependency to the
# makefile.
# tool_name: Returns the tool name, i.e. mex or mbuild. This
# function is defined at the top of the script since
# it is used in the BEGIN block.
#
#-----------------------------------------------------------------------
#
# Common variables:
#
# perl:
#
# FILES_TO_REMOVE
# FILES_TO_LINK
# FLAGS
# LINKFLAGS
# MAKEFILE
#
# DOS environment:
#
# PATH system path
# MATLAB MATLAB root
#
# [$ENV: get in script]
# MEX_DEBUG This is for debugging this script.
#
# [$ENV: set in script]
# LIB_NAME
# MATLAB MATLAB root
# MATLAB_BIN
# MATLAB_EXTLIB
# OUTDIRN
# OUTDIR
# RES_NAME
# RES_PATH
#
#=======================================================================
sub compile_files
{
#===================================================================
# compile_files: Compile files and form list of files to link.
#===================================================================
# Loop over @FILES to compile each file. Keep files we actually
# compile separate from the ones we don't compile but need to link.
# This way, we can later clean up all .obj files we created.
#
my $file;
for (;$file = shift(@FILES);) {
my ($FILEDIR, $FILENAME, $FILEEXT) = fileparts($file);
if ($FILEEXT =~ /($COMPILE_EXTENSION)$/i ) {
my ($target_name, $name_arg);
if ($NAME_OBJECT) {
if (!$compile_only) {
$target_name = mexCatfile($main::temp_dir, "$FILENAME.obj");
} else {
$target_name = mexCatfile($ENV{'OUTDIR'}, "$FILENAME.obj");
}
$name_arg = $NAME_OBJECT . smart_quote($target_name);
}
else {
$target_name = "$FILENAME.obj";
$name_arg = "";
}
my ($args) = "$ARG_FLAGS $COMPFLAGS $name_arg $FLAGS " . smart_quote(File::Spec->rel2abs($file));
if (!$makefilename)
{
my $messages = RunCmd("$COMPILER $args");
# Check for error; $? might not work, so also check for resulting file
#
if ($? != 0 || !(-e "$target_name" || $main::no_execute)) {
print "$messages" unless $verbose; # verbose => printed in RunCmd
expire("Error: Compile of '$file' failed.");
}
if (!$compile_only)
{
push(@FILES_TO_REMOVE, "$target_name");
}
}
else
{
emit_compile_step();
}
push(@FILES_TO_LINK, "$LINK_FILE " . smart_quote($target_name));
push(@FILES_TO_LINK_BASE, smart_quote($target_name));
}
elsif ($FILEEXT =~ /lib$/i)
{
push(@FILES_TO_LINK, "$LINK_LIB " . smart_quote($file));
push(@FILES_TO_LINK_BASE, smart_quote($file));
}
else
{
push(@FILES_TO_LINK, "$LINK_FILE " . smart_quote($file));
push(@FILES_TO_LINK_BASE, smart_quote($file));
}
}
}
#=======================================================================
sub compile_resource
{
#===================================================================
# compile_resource: Compile the resource.
#===================================================================
my ($rc_line) = '';
$rc_line .= " -DARRAY_ACCESS_INLINING" if ($inline);
$rc_line .= " -DV5_COMPAT" if ($v5);
my $rc_basename = mexCatfile($ENV{'RES_PATH'}, $ENV{'RES_NAME'});
$rc_line .= " " . smart_quote("$rc_basename.rc");
if (!$makefilename)
{
my $messages = RunCmd("$RC_COMPILER $rc_line");
# Check for error; $? might not work, so also check for string "error"
#
if ($? != 0 || $messages =~ /\b(error|fatal)\b/i) {
print "$messages" unless $verbose; # verbose => printed out in RunCmd
expire("Error: Resource compile of '$ENV{'RES_NAME'}.rc' failed.");
}
push(@FILES_TO_REMOVE, "$rc_basename.res");
}
else
{
emit_resource_compiler();
}
push(@FILES_TO_LINK, smart_quote("$rc_basename.res"));
}
#=======================================================================
sub do_setup
{
#===================================================================
# do_setup: Do only the setup.
#===================================================================
my $setupFailure;
if ($setup) {
@setup_args = ();
# 4th arg is 0 == no automode
$setupFailure = setup(tool_name(), $main::mexopts_directory,
['ANY'], 0, get_user_profile_dir(), \@setup_args, $reglibs);
} else {
# 4th arg is 2 == full automode
$setupFailure = setup(tool_name(), $main::mexopts_directory,
['ANY'], 2, get_user_profile_dir(), \@setup_args, $reglibs);
}
if ($main::mbuild eq 'no') {
if ($called_from_matlab) {
describe("largeArrayDimsWillBeDefaultWarningwithHTML");
} else {
describe("largeArrayDimsWillBeDefaultWarning");
}
}
if ($setupFailure) {
exit(1);
}
}
#=======================================================================
sub emit_compile_step
{
#===================================================================
# emit_compile_step: Output compile step to makefile.
#===================================================================
# Emit compile dependency rule
#
print MAKEFILE smart_quote($target_name) . " : " . smart_quote($file);
print MAKEFILE "\n\t$COMPILER $args\n\n";
}
#=======================================================================
sub emit_delete_resource_file
{
#===================================================================
# emit_delete_resource_file: Output delete resource file to makefile.
#===================================================================
my $res_pathname = mexCatfile($ENV{'OUTDIR'},"$ENV{'RES_NAME'}.res");
print MAKEFILE "\tif exist \"$res_pathname\" del \"$res_pathname\"\n";
}
#=======================================================================
sub emit_link_dependency
{
#===================================================================
# emit_link_dependency: Output link dependency to makefile.
#===================================================================
# Emit link dependency rule
#
print MAKEFILE mexCatfile($ENV{'OUTDIR'}, "$ENV{'MEX_NAME'}.$bin_extension");
print MAKEFILE " : @FILES_TO_LINK_BASE\n";
}
#=======================================================================
sub emit_linker_step
{
#===================================================================
# emit_linker_step: Output linker step to makefile.
#===================================================================
print MAKEFILE "\t$LINKER $ARGS\n";
}
#=======================================================================
sub emit_makedef
{
#===================================================================
# emit_makedef: Output makedef step to makefile.
#===================================================================
print MAKEFILE "\t$makedef\n";
}
#=======================================================================
sub emit_makefile_terminator
{
#===================================================================
# emit_makefile_terminator: Output terminator for makefile.
#===================================================================
print MAKEFILE "\n";
}
#=======================================================================
sub emit_pre_or_postlink_step
{
#===================================================================
# emit_pre_or_postlink_step: Output prelink or postlink step to makefile.
#===================================================================
my ($step) = @_;
print MAKEFILE "\t$step\n";
}
#=======================================================================
sub emit_resource_compiler
{
#===================================================================
# emit_resource_compiler: Output resource compile step to makefile.
#===================================================================
print MAKEFILE "\t$RC_COMPILER $rc_line\n";
}
#=======================================================================
sub emit_resource_linker_step
{
#===================================================================
# emit_resource linker_step: Output resource linker step to makefile
#===================================================================
print MAKEFILE "\t$RC_LINKER $rc_line\n";
}
#=======================================================================
sub expand_wildcards
{
#===================================================================
# expand_wildcards: Expand possible wildcards in the arguments
# for perl >= 5.00503
#===================================================================
if ($] >= 5.00503) {
my (@a) = map { /\*/ ? glob($_) : $_ } @ARGV;
if ( "@a" ne "@ARGV" ) {
@ARGV = @a;
}
}
}
#=======================================================================
sub expire
{
#===================================================================
# expire: Issue message and exit. This is like "die" except that
# it cleans up intermediate files before exiting.
# expire("normally") exits normally (doesn't die).
#===================================================================
# Clean up compiled files, unless we're only compiling
#
unlink @FILES_TO_REMOVE;
if ($makefilename)
{
close(MAKEFILE);
if ($_[0] ne "normally")
{
unlink $makefilename;
}
}
if ($ARCH eq "win32" && $foundMEXW32ExtensionConflict && ! $main::no_execute) {
if ($called_from_matlab) {
describe("extension_wont_work_preR14sp3withHTML");
} else {
describe("extension_wont_work_preR14sp3");
}
}
die "\n $main::cmd_name: $_[0]\n\n" unless ($_[0] eq "normally");
exit(0);
}
#=======================================================================
sub find_options_file
{
#===================================================================
# find_options_file: Find the options file.
#===================================================================
# inputs:
#
my ($OPTFILE_NAME, $language, $no_setup) = @_;
# outputs: ($OPTFILE_NAME,$source_dir,$sourced_msg)
#
my ($source_dir, $sourced_msg);
# locals:
#
my ($REGISTERED_COMPILER, @JUNK);
if (-e mexCatfile(File::Spec->curdir(), $OPTFILE_NAME))
{
chop($source_dir = `cd`);
}
elsif (-e mexCatfile(get_user_profile_dir(), $OPTFILE_NAME))
{
$source_dir = get_user_profile_dir();
}
elsif (-e mexCatfile($main::mexopts_directory, $OPTFILE_NAME))
{
$source_dir = "$main::mexopts_directory";
}
else
{
if (!$no_setup)
{
# Not a preset so create an empty setup argument list
#
@setup_args = ();
# No options file found, so try to detect the compiler
#
if($silent_setup)
{
setup(tool_name(), $main::mexopts_directory, [uc($lang)],
2, get_user_profile_dir(), \@setup_args); # 2 == silent automode
}
else
{
setup(tool_name(), $main::mexopts_directory, [uc($lang)],
1, get_user_profile_dir(), \@setup_args); # 1 == automode
}
if ($main::mbuild eq 'no') {
if ($called_from_matlab) {
describe("largeArrayDimsWillBeDefaultWarningwithHTML");
} else {
describe("largeArrayDimsWillBeDefaultWarning");
}
}
}
if (-e mexCatfile(get_user_profile_dir(), $OPTFILE_NAME))
{
$source_dir = get_user_profile_dir();
}
else
{
expire("Error: No compiler options file could be found to compile source code. Please run \"" . tool_name() . " -setup\" to rectify.");
}
}
$OPTFILE_NAME = mexCatfile($source_dir, $OPTFILE_NAME);
$sourced_msg = "-> Default options filename found in $source_dir";
($OPTFILE_NAME, $source_dir, $sourced_msg);
}
#=======================================================================
sub fix_common_variables
{
#===================================================================
# fix_common_variables: Fix common variables.
#===================================================================
$bin_extension = $NAME_OUTPUT;
$bin_extension =~ s/\"//g;
$bin_extension =~ s/.*\.([^.]*)$/$1/;
# WATCOM Compiler can't handle MATLAB installations with spaces in
# path names.
#
if ($COMPILER =~ /(wpp)|(wcc)|(wcl)/ && $MATLAB =~ " ")
{
expire("You have installed MATLAB into a directory whose name contains spaces. " .
"The WATCOM compiler cannot handle that. Either rename your MATLAB " .
"directory (currently $MATLAB) or run mex -setup and select a " .
"different compiler.");
}
# Decide how to optimize or debug
#
if (! $debug) { # Normal case
$LINKFLAGS = "$LINKFLAGS $LINKOPTIMFLAGS";
} elsif (! $optimize) { # Debug; don't optimize
$LINKFLAGS = "$LINKDEBUGFLAGS $LINKFLAGS";
} else { # Debug and optimize
$LINKFLAGS = "$LINKDEBUGFLAGS $LINKFLAGS $LINKOPTIMFLAGS ";
}
# Add inlining if switch was set
#
$FLAGS = "$FLAGS -DARRAY_ACCESS_INLINING" if ( $inline );
$FLAGS = "$FLAGS -DMX_COMPAT_32" if ( $v7_compat eq "yes" );
}
#=======================================================================
sub fix_flag_variables
{
#===================================================================
# fix_flag_variables: Fix the flag variables.
#===================================================================
# Based on the language we're using, possibly adjust the flags
#
if ($lang eq "cpp" && $CPPCOMPFLAGS ne "")
{
$COMPFLAGS = $CPPCOMPFLAGS;
$LINKFLAGS = "$LINKFLAGS $CPPLINKFLAGS";
$DEBUGFLAGS = $CPPDEBUGFLAGS;
$OPTIMFLAGS = $CPPOPTIMFLAGS;
}
}
#=======================================================================
sub files_to_remove
{
#===================================================================
# files_to_remove: Add files to remove list.
#===================================================================
push(@FILES_TO_REMOVE,
("$ENV{'MEX_NAME'}.bak"));
}
#=======================================================================
sub get_user_profile_dir
{
#===================================================================
# get_user_profile_dir: Return UserProfile directory
#===================================================================
# UserProfile environment variable is set by NT variants of Windows
my $appData = getAppDataDir();
if ($appData eq "")
{
$appData = $ENV{'windir'} . "Application Data";
}
$userProfile = mexCatdir($appData, "MathWorks", "MATLAB", getVersion());
$userProfile;
} # get_user_profile_dir
#=======================================================================
sub getAppDataDir
{
return Win32::GetFolderPath(Win32::CSIDL_APPDATA);
}
#=======================================================================
sub getVersion
{
my $mexScriptsDir = mexCatdir($main::script_directory,"util","mex");
my $versionFileName = mexCatfile($mexScriptsDir,"version.txt");
open(verDat, $versionFileName);
$ver=<verDat>;
close(verDat);
chomp($ver);
return $ver;
}
#=======================================================================
sub init_common
{
#===================================================================
# init_common: Common initialization.
#===================================================================
$ENV{'MATLAB'} = $main::matlabroot;
expand_wildcards();
$sourced_msg = 'none';
$| = 1; # Force immediate output flushing
open(STDERR,">&STDOUT"); # redirect stderr to stdout for matlab
select((select(STDERR), $|=1 )[0]); # force immediate flushing for STDERR
# Fix the path if necessary.
#
rectify_path();
# Trap case where an invalid options file is used, by checking the
# value of the compiler.
#
$COMPILER = "none";
# Create a temporary directory for temporary files
# tempdir will be deleted on exit even if perl script exits early
if ($ENV{'TEMP'} && -d $ENV{'TEMP'} && -w $ENV{'TEMP'})
{
$main::temp_dir = tempdir( tool_name() . "_" . "XXXXXX", DIR => $ENV{'TEMP'}) or
expire("Error: Could not create directory \"$main::temp_dir\": $!.");
}
else
{
expire("Error: The environment variable %TEMP% must contain the name " .
"of a writable directory.");
}
}
#=======================================================================
sub linker_arguments
{
#===================================================================
# linker_arguments: Create response file of linker arguments or just
# string.
#===================================================================
# NAME_OUTPUT always goes in the list, but it may be blank (in which
# case it's harmless to put it in). Leaving the variable blank is
# equivalent to assuming that the output will be named after the
# first object file in the list.
#
$ARGS = '';
if ( $ENV{'RSP_FILE_INDICATOR'} )
{
my $response_file;
if ($makefilename)
{
$response_file = mexCatfile($ENV{'OUTDIR'}, "$ENV{'MEX_NAME'}_master.rsp");
}
else
{
$response_file = mexCatfile($main::temp_dir,
tool_name() . "_tmp.rsp");
}
open(RSPFILE, ">$response_file") || expire("Error: Can't open file '$response_file': $!");
print RSPFILE " @FILES_TO_LINK";
close(RSPFILE);
$ARGS = "$NAME_OUTPUT $LINKFLAGS " .
smart_quote("$ENV{'RSP_FILE_INDICATOR'}$response_file") .
" $IMPLICIT_LIBS $LINKFLAGSPOST";
if ($verbose && !$makefilename)
{
print " Contents of $response_file:\n";
print " @FILES_TO_LINK\n\n";
}
}
else
{
$ARGS = "$NAME_OUTPUT $LINKFLAGS @FILES_TO_LINK $IMPLICIT_LIBS $LINKFLAGSPOST";
}
}
#=======================================================================
# this function moves the said files from CWD to output directory
sub move_files_to_outdir
{
# get list of files
foreach $file(@_)
{
my $output_pathname = mexCatfile($ENV{'OUTDIR'}, $file);
print "Manually moving $file to $output_pathname\n" if $verbose;
if (-e $file) {
print "Renaming $file to $output_pathname\n" if $verbose;
rename($file, $output_pathname) == 1 ||
expire("Error: Rename of '$output_pathname' failed: $!");
}
}
}
#=======================================================================
sub link_files
{
#===================================================================
# link_files: Link files.
#===================================================================
if (!$makefilename)
{
my $messages = RunCmd("$LINKER $ARGS");
my $output_pathname = mexCatfile($ENV{'OUTDIR'}, "$ENV{'MEX_NAME'}.$bin_extension");
# LCC doesn't pay attention to -"output dir\file" as an option
# it puts the file into the current directory. If that's the case
# move the file to dir
if ($ENV{'COMPILER'} eq "lcc" &&
$ENV{'OUTDIR'} ne "" &&
$ENV{'OUTDIR'} ne mexCanonpath(getcwd)) {
&move_files_to_outdir("$ENV{'MEX_NAME'}.$bin_extension",
"$ENV{'MEX_NAME'}.lib",
"$ENV{'MEX_NAME'}.exp");
}
if ($ARCH eq "win32")
{
my $fileWithMexext = mexCatfile($ENV{'OUTDIR'}, "$ENV{'MEX_NAME'}.mexw32");
my $fileWithDll = mexCatfile($ENV{'OUTDIR'}, "$ENV{'MEX_NAME'}.dll");
# Make sure that there are not two viable MEX-files in the same
# directory with different extensions. On win32 both MEXw32 and
# DLL are valid. If both are found one is renamed with the .old
# tagged onto the end.
# Create DLL, so rename MEXEXT
if (($output_pathname eq $fileWithDll) &&
-e $fileWithDll && -e $fileWithMexext) {
backup_the_other_extension($fileWithMexext);
}
# Create MEXEXT, so rename DLL
if (($output_pathname eq $fileWithMexext) &&
-e $fileWithMexext && -e $fileWithDll) {
backup_the_other_extension($fileWithDll);
$foundMEXW32ExtensionConflict = "true";
}
}
# Check for error; $? might not work, so also check for resulting file
#
if ($? != 0 || !(-e $output_pathname || $main::no_execute ))
{
print "$messages" unless $verbose; # verbose => printed in RunCmd
expire("Error: Link of '$output_pathname' failed.");
}
# If we got a file, make sure there were no errors
#
if ($messages =~ /\b(error|fatal)\b/i) {
print $messages unless $verbose; # verbose => printed in RunCmd
expire("Error: Link of '$output_pathname' failed.");
}
if ($COMPILER =~ /bcc/ && $debug ne "yes")
{
push(@FILES_TO_REMOVE, mexCatfile($ENV{'OUTDIR'}, "$ENV{'MEX_NAME'}.tds"));
}
}
else
{
emit_linker_step();
}
}
#=======================================================================
sub backup_the_other_extension
{
#===================================================================
# backup_the_other_extension: Rename the MEXEXT that is not being created.
#===================================================================
my ($oldFileToRename) = @_;
if (! $main::no_execute)
{
rename($oldFileToRename,"$oldFileToRename.old") == 1 ||
expire("Error: Rename of '$oldFileToRename' to '$oldFileToRename.old' failed: $!");
print "\n Warning: Renaming \"$oldFileToRename\" to ",
"\"$oldFileToRename.old\" to avoid name conflicts.\n\n";
}
else
{
print "\n--> rename $oldFileToRename $oldFileToRename.old\n\n"
}
}
#=======================================================================
sub options_file
{
#===================================================================
# options_file: Get options file if not passed as an argument.
# Source the options file.
#===================================================================
# MATHWORKS ONLY: MathWorks-specific environment variables
# used only for internal regression testing.
if ($OPTFILE_NAME =~ /msvc80free/ && $ENV{'MWE_VS80FREE_COMNTOOLS'}) {
print("Setting VS80COMNTOOLS for use with Microsoft Visual Studio 2005 Express Edition (MathWorks-only diagnostic - do not geck)\n");
$ENV{'VS80COMNTOOLS'} = $ENV{'MWE_VS80FREE_COMNTOOLS'};
} elsif ($OPTFILE_NAME =~ /cvf(\d+)/ && $ENV{'CVF'.$1}) {
print("Setting DF_ROOT for use with Compaq Digital Fortran $1 (MathWorks-only diagnostic - do not geck)\n");
$ENV{'DF_ROOT'} = $ENV{'CVF'.$1};
}
# Search and locate options file if not specified on the command
# line.
#
if ($sourced_msg eq 'none') {
($OPTFILE_NAME, $source_dir, $sourced_msg) = find_options_file($OPTFILE_NAME, $lang, $no_setup);
}
# Parse the batch file. DOS batch language is too limited.
#
open (OPTIONSFILE, $OPTFILE_NAME) || expire("Error: Can't open file '$OPTFILE_NAME': $!");
while ($_ = <OPTIONSFILE>) {
chomp;
next if (!(/^\s*set /i)); # Ignore everything but set commands
s/^\s*set //i; # Remove "set " command itself
s/\s+$//; # Remove trailing whitespace
s/\\$//g; # Remove trailing \'s
s/\\/\\\\/g; # Escape all other \'s with another \
s/%(\w+)%/'.\$ENV{'$1'}.'/g; # Replace %VAR% with $ENV{'VAR'}
s/%%/%/g; # Replace %%s with %s
my $perlvar = '$' . $_ . "\';";
$perlvar =~ s/=/='/;
my $dosvar = '$ENV{'."'".$_."';";
$dosvar =~ s/=/'}='/;
eval($perlvar);
eval($dosvar);
# We need a special case for the WATCOM compiler because it
# can't handle directories with spaces or quotes in their
# names. So only put the quotes around the MATLAB directory
# name if it has spaces in it.
#
$ML_DIR = smart_quote($MATLAB);
# Set the special MATLAB_BIN environment variable
#
if ( (! $ENV{'MATLAB'} eq "") && $ENV{'MATLAB_BIN'} eq "" ) {