0001 function mexfileOutput = dsPrepareMEX(mfileInput, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010 args = varargin;
0011
0012
0013 if isstruct(args{1})
0014
0015 keyvals = dsOptions2Keyval(args{1});
0016
0017 keyvals = horzcat(keyvals,args(2:end));
0018 else
0019 keyvals = args;
0020 end
0021
0022 options=dsCheckOptions(keyvals,{...
0023 'verbose_flag',0,{0,1},...
0024 'mex_dir_flag',1,{0,1},...
0025 'mex_dir',[],[],...
0026 'codegen_args',[],[],...
0027 'cluster_flag',0,{0,1},...
0028 },false);
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 if isempty(options.mex_dir)
0041 options.mex_dir = dsGetConfig('mex_path');
0042 end
0043
0044 mex_dir = options.mex_dir;
0045
0046
0047 [fpath,fname] = fileparts(mfileInput);
0048 mexfileOutput = fullfile(fpath,[fname '_mex']);
0049
0050 if ~exist(mexfileOutput,'file')
0051 if options.verbose_flag
0052 fprintf('Compiling file: %s\n',mfileInput);
0053 fprintf(' to: %s\n',mexfileOutput);
0054 end
0055
0056 compile_start_time=tic;
0057
0058 makeMex(mfileInput, options);
0059
0060 if options.verbose_flag
0061 fprintf('\tMEX generation complete!\n\tElapsed time: %g seconds.\n',toc(compile_start_time));
0062
0063 end
0064
0065 codemex_dir=fullfile(fileparts(mexfileOutput),'codemex');
0066 if exist(codemex_dir,'dir')
0067 if options.verbose_flag
0068 fprintf('\tRemoving temporary codemex directory: %s\n',codemex_dir);
0069 end
0070 rmdir(codemex_dir,'s');
0071 end
0072 else
0073 if options.verbose_flag
0074 fprintf('Using previous compiled file: %s\n',mexfileOutput);
0075 end
0076 end
0077
0078
0079 if ~isempty(mex_dir) && ~options.cluster_flag && options.mex_dir_flag
0080 [~,solvefile] = fileparts2(mfileInput);
0081 [~,mexfile] = fileparts2(mexfileOutput);
0082
0083 if isempty(dir(fullfile(mex_dir,[solvefile '.m'])))
0084 if options.verbose_flag
0085 fprintf('Solve file %s does not yet exist in mex_dir %s. Copying... \n',solvefile,mex_dir);
0086 end
0087 if ~exist(mex_dir,'dir'); error('Cannot find %s! Make sure it exists and is specified as an *absolute* path',mex_dir); end
0088 copyfile(mfileInput,mex_dir);
0089 end
0090
0091 if isempty(dir(fullfile(mex_dir,[mexfile '*'])))
0092 if options.verbose_flag
0093 fprintf('Mex file %s does not yet exist in mex_dir %s. Copying... \n',mexfile,mex_dir);
0094 end
0095 if ~exist(mex_dir,'dir'); error('Cannot find %s! Make sure it exists and is specified as an *absolute* path',mex_dir); end
0096 copyfile([mexfileOutput,'*'],mex_dir);
0097 end
0098 end
0099
0100 end
0101
0102
0103 function makeMex(file, options)
0104
0105 cfg = coder.config('mex');
0106
0107
0108 cfg.DynamicMemoryAllocation = 'AllVariableSizeArrays';
0109
0110
0111 if isempty(options.codegen_args)
0112 eval(['codegen -d codemex -config cfg ',file]);
0113 else
0114
0115 eval(['codegen -args options.codegen_args -d codemex -config cfg ',file]);
0116 end
0117
0118
0119 end