-
Notifications
You must be signed in to change notification settings - Fork 0
/
APT_compile.m
231 lines (205 loc) · 8.7 KB
/
APT_compile.m
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
function APT_compile(varargin)
global APT_PARAMS;
if isempty(APT_PARAMS)
APT_params();
end
%======================================================================
params = struct('NoJVM', 1, ...
'SingleThread', 1, ...
'Ignore', {{}});
%======================================================================
fun_names = {};
i = 1;
while i <= length(varargin)
if iscell(varargin{i})
fun_names = [fun_names varargin{i}];
elseif ischar(varargin{i})
f = fields(params);
fID = find(strcmp(varargin{i}, f), 1);
if ~isempty(fID)
params.(f{fID}) = varargin{i+1};
i = i + 2;
continue;
else
if strcmp(which(varargin{i}), '')
error('Arguments should function names or options, unable to find: %s ', varargin{i});
else
fun_names = [fun_names {varargin{i}}];
end
end
else
error('Arguments should function names or options.');
end
i = i + 1;
end
% Add APT path to Ignore path
params.Ignore{end+1} = fileparts(which(mfilename()));
if nargin < 1
fun_names = {};
elseif iscell(fun_names)
for i = 1 : length(fun_names)
if ~ischar(fun_names{i})
error('First argument should be a cell of strings or a string.');
end
end
else
if ischar(fun_names)
fun_names = {fun_names};
else
error('First argument should be a cell of strings or a string.');
end
end
for i = 1 : length(fun_names)
if strcmp(fun_names{i}(end-1:end), '.m')
fun_names{i} = fun_names{i}(1:end-2);
end
end
drive = APT_get_drive();
temp_dir = fullfile(drive, APT_PARAMS.temp_dir);
[s,m] = mkdir(temp_dir);
% Compiling
if isempty(fun_names)
src_path = get_src_path(params.Ignore);
src_path = sprintf(' -a ''%s''', src_path{:});
else
src_path = sprintf(' -a ''%s''', fun_names{:});
end
flags = '';
if params.NoJVM
fprintf('Use APT_compile(''NoJVM'', 0) to compile with the JVM\n');
flags = [flags '-R -nojvm '];
end
if params.SingleThread
fprintf('Use APT_compile(''SingleThread'', 0) to compile with multi-threading\n');
flags = [flags '-R -singleCompThread '];
end
path_cell = get_non_builtin_path_cell();
path_infos = sprintf(' -I ''%s''', path_cell{:});
fprintf('Compiling, it may take some time...\n');
cmd = [matlabroot '/bin/mcc -m -R -nodisplay ' flags path_infos ' -d ' temp_dir ' -o _' APT_PARAMS.exec_name src_path ' ' APT_PARAMS.main_func];
system(cmd);
movefile(fullfile(temp_dir, ['_' APT_PARAMS.exec_name]), fullfile(temp_dir, APT_PARAMS.exec_name));
fid = fopen(fullfile(temp_dir, [APT_PARAMS.exec_name '.inf']), 'wt');
fprintf(fid, 'flags="%s";\n', flags);
fprintf(fid, 'funs="%s";\n', sprintf('%s ', fun_names{:}));
fclose(fid);
% Overwriting matlab runner
generate_runner();
% Cleaning
system(sprintf('rm %s %s %s %s', fullfile(temp_dir, '*.c'), ...
fullfile(temp_dir, '*.prj'), ...
fullfile(temp_dir, '*.txt'), ...
fullfile(temp_dir, '*.log')));
fprintf('Compilation finished successfully.\n');
end
%==========================================================================
function generate_runner()
global APT_PARAMS;
% runner_orig = fullfile(drive, APT_PARAMS.temp_dir, ['run__' APT_PARAMS.exec_name '.sh']);
% runner = fullfile(drive, APT_PARAMS.temp_dir, ['run_' APT_PARAMS.exec_name '.sh']);
% system(sprintf('sed "/echo LD_LIBRARY_PATH/d;/\$\*/d;s|shift 1|LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:\$2\nexport LD_LIBRARY_PATH;\necho LD_LIBRARY_PATH is \${LD_LIBRARY_PATH};\nshift 2\necho \"\" > \${exe_dir}/\$1/scripts/launched\$2\n\${exe_dir}/%s \"\$1\" \"\$2\" \"\$3\" \"\$4\"|" %s > %s', APT_PARAMS.exec_name, runner_orig, runner));
drive = APT_get_drive();
runner = fullfile(drive, APT_PARAMS.temp_dir, ['run_' APT_PARAMS.exec_name '.sh']);
fid=fopen(runner,'w');
fprintf(fid, '#!/bin/sh\n');
fprintf(fid, '# script for execution of deployed applications\n');
fprintf(fid, '#\n');
fprintf(fid, '# Sets up the MCR environment for the current $ARCH and executes\n');
fprintf(fid, '# the specified command.\n');
fprintf(fid, '#\n');
fprintf(fid, 'exe_dir=`dirname $0`\n');
fprintf(fid, 'echo "------------------------------------------"\n');
fprintf(fid, 'if [ "x$1" = "x" ]; then\n');
fprintf(fid, ' echo Usage:\n');
fprintf(fid, ' echo $0 \\<deployedMCRroot\\> args\n');
fprintf(fid, 'else\n');
fprintf(fid, ' echo Setting up environment variables\n');
fprintf(fid, ' MCRROOT=$1\n');
fprintf(fid, ' echo ---\n');
fprintf(fid, ' MWE_ARCH="glnxa64" ;\n');
fprintf(fid, ' if [ "$MWE_ARCH" = "sol64" ] ; then\n');
fprintf(fid, ' LD_LIBRARY_PATH=.:/usr/lib/lwp:${MCRROOT}/runtime/glnxa64 ;\n');
fprintf(fid, ' else\n');
fprintf(fid, ' LD_LIBRARY_PATH=.:${MCRROOT}/runtime/glnxa64 ;\n');
fprintf(fid, ' fi\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MCRROOT}/bin/glnxa64 ;\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MCRROOT}/sys/os/glnxa64;\n');
fprintf(fid, ' if [ "$MWE_ARCH" = "maci" -o "$MWE_ARCH" = "maci64" ]; then\n');
fprintf(fid, ' DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/System/Library/Frameworks/JavaVM.framework/JavaVM:/System/Library/Frameworks/JavaVM.framework/Libraries;\n');
fprintf(fid, ' else\n');
fprintf(fid, ' MCRJRE=${MCRROOT}/sys/java/jre/glnxa64/jre/lib/amd64 ;\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MCRJRE}/native_threads ;\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MCRJRE}/server ;\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MCRJRE}/client ;\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MCRJRE} ; \n');
fprintf(fid, ' fi\n');
fprintf(fid, ' XAPPLRESDIR=${MCRROOT}/X11/app-defaults ;\n');
fprintf(fid, ' LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$2\n');
fprintf(fid, ' export LD_LIBRARY_PATH;\n');
fprintf(fid, ' export XAPPLRESDIR;\n');
fprintf(fid, ' echo LD_LIBRARY_PATH is ${LD_LIBRARY_PATH};\n');
fprintf(fid, ' shift 2\n');
fprintf(fid, ' echo "" > ${exe_dir}/$1/scripts/launched$2\n');
fprintf(fid, ' ${exe_dir}/%s "$1" "$2" "$3" "$4"\n', APT_PARAMS.exec_name);
fprintf(fid, 'fi\n');
fprintf(fid, 'exit\n');
end
%==========================================================================
function src_path = get_src_path(ignore)
src_path = find_user_paths(ignore);
if isempty(find(strcmp(src_path, cd()), 1))
src_path = [cd() src_path];
end
src_path = repmat(reshape(src_path, 1, numel(src_path)), 2, 1);
for i = 1 : size(src_path, 2)
src_path{1, i} = fullfile(src_path{1, i}, '*.m');
src_path{2, i} = fullfile(src_path{2, i}, ['*.' mexext]);
end
src_path = reshape(src_path, 1, numel(src_path));
java_path = javaclasspath('-dynamic');
src_path = [src_path reshape(java_path, 1, numel(java_path))];
end
%==========================================================================
function dirs = find_user_paths(ignore)
dirs = path2cell();
n_dirs = length(dirs);
ignore{end + 1} = matlabroot;
select = true(n_dirs, 1);
for i = 1 : n_dirs
for j = 1 : length(ignore)
if ignore{j}(1) ~= '/'
ignore{j} = fullfile(cd(), ignore{j});
end
len = length(ignore{j});
if length(dirs{i}) >= len && strcmp(ignore{j}, dirs{i}(1 : len))
select(i) = false;
end
end
end
dirs = dirs(select);
end
%==========================================================================
function dirs = get_non_builtin_path_cell()
dirs = regexp(path, pathsep, 'split');
valid_dir = true(1, numel(dirs));
%ignoreroot = toolboxdir('');
ignoreroot = matlabroot;
for i = 1 : numel(dirs)
if ~strfind(dirs{i}, ignoreroot) == 0
valid_dir(i) = 0;
end
end
dirs = dirs(valid_dir);
end
%==========================================================================
function dirs = path2cell()
dirs = regexp(path, pathsep, 'split');
valid_dir = true(1, numel(dirs));
for i = 1 : numel(dirs)
if ~isdir(dirs{i})
valid_dir(i) = 0;
fprintf('Warning, invalid directory in matlab path: %s\n', dirs{i});
end
end
dirs = dirs(valid_dir);
end