-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrtwmakecfg.m
390 lines (327 loc) · 12.1 KB
/
rtwmakecfg.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
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
function makeInfo = rtwmakecfg()
%RTWMAKECFG adds include and source directories to the make files.
% makeInfo=RTWMAKECFG returns a structured array containing build info.
% Please refer to the rtwmakecfg API section in the Simulink Coder
% documentation for details on the format of this structure.
%
% Simulink version : 8.1 (R2013a) 13-Feb-2013
% MATLAB file generated on : 18-Sep-2013 18:54:59
% Verify the Simulink version
verify_simulink_version();
% Get the current directory
currDir = pwd;
% Get the MATLAB search paths and remove the toolbox sub-directories except simfeatures
pSep = pathsep;
matlabPaths = regexp([matlabpath pSep], ['.[^' pSep ']*' pSep], 'match');
if ~isempty(matlabPaths)
filteredPathIndices = strncmp(fullfile(matlabroot,'toolbox'), matlabPaths, numel(fullfile(matlabroot,'toolbox')));
lctPath = fileparts(which('sldemo_lct_builddemos'));
if ~isempty(lctPath)
filteredPathIndices(strncmp([lctPath pSep], matlabPaths, numel([lctPath pSep]))) = 0;
end
lctPath = fileparts(which('rtwdemo_lct_builddemos'));
if ~isempty(lctPath)
filteredPathIndices(strncmp([lctPath pSep], matlabPaths, numel([lctPath pSep]))) = 0;
end
matlabPaths(filteredPathIndices) = [];
matlabPaths = strrep(matlabPaths, pSep, '');
end
% Declare cell arrays for storing the paths found
allIncPaths = {};
allSrcPaths = {};
% Get the build type
isSimTarget = is_simulation_target();
allLibs = {};
% Get the serialized paths information
info = get_serialized_info();
% Get all S-Function's name in the current model
sfunNames = {};
if ~isempty(bdroot)
sfunBlks = find_system(bdroot,...
'LookUnderMasks', 'all',...
'FollowLinks', 'on',...
'BlockType', 'S-Function'...
);
sfunNames = get_param(sfunBlks, 'FunctionName');
end
for ii = 1:length(info)
% If the S-Function isn't part of the current build then skip its path info
if isempty(strmatch(info(ii).SFunctionName, sfunNames, 'exact'))
continue
end
% Path to the S-function source file
if strcmp(info(ii).Language, 'C')
fext = '.c';
else
fext = '.cpp';
end
pathToSFun = fileparts(which([info(ii).SFunctionName,fext]));
if isempty(pathToSFun)
pathToSFun = currDir;
end
% Default search paths for this S-function
defaultPaths = [{pathToSFun} {currDir}];
allPaths = [defaultPaths matlabPaths];
% Verify if IncPaths are absolute or relative and then complete
% relative paths with the full S-function dir or current dir or MATLAB path
incPaths = info(ii).IncPaths;
for jj = 1:length(incPaths)
[fullPath, isFound] = resolve_path_info(correct_path_sep(incPaths{jj}), allPaths);
if (isFound==0)
DAStudio.error('Simulink:tools:LCTErrorCannotFindIncludePath',...
incPaths{jj});
else
incPaths{jj} = fullPath;
end
end
incPaths = [incPaths defaultPaths];
% Verify if SrcPaths are Absolute or Relative and then complete
% relative paths with the full S-function dir or current dir or MATLAB path
srcPaths = info(ii).SrcPaths;
for jj = 1:length(srcPaths)
[fullPath, isFound] = resolve_path_info(correct_path_sep(srcPaths{jj}), allPaths);
if (isFound==0)
DAStudio.error('Simulink:tools:LCTErrorCannotFindSourcePath',...
srcPaths{jj});
else
srcPaths{jj} = fullPath;
end
end
srcPaths = [srcPaths defaultPaths];
% Common search paths for Source files specified with path
srcSearchPaths = [srcPaths matlabPaths];
% Add path to source files if not specified and complete relative
% paths with the full S-function dir or current dir or search
% paths and then extract only the path part to add it to the srcPaths
sourceFiles = info(ii).SourceFiles;
pathFromSourceFiles = cell(1, length(sourceFiles));
for jj = 1:length(sourceFiles)
[fullName, isFound] = resolve_file_info(correct_path_sep(sourceFiles{jj}), srcSearchPaths);
if isFound==0
DAStudio.error('Simulink:tools:LCTErrorCannotFindSourceFile',...
sourceFiles{jj});
else
% Extract the path part only
[fpath, fname, fext] = fileparts(fullName);
pathFromSourceFiles{jj} = fpath;
end
end
srcPaths = [srcPaths pathFromSourceFiles];
% Verify if LibPaths are Absolute or Relative and then complete
% relative paths with the full S-function dir or current dir or MATLAB path
libPaths = info(ii).LibPaths;
for jj = 1:length(libPaths)
[fullPath, isFound] = resolve_path_info(correct_path_sep(libPaths{jj}), allPaths);
if (isFound==0)
DAStudio.error('Simulink:tools:LCTErrorCannotFindLibraryPath',...
libPaths{jj});
else
libPaths{jj} = fullPath;
end
end
libPaths = [libPaths defaultPaths];
% Common search paths for Host and Target Lib with relative paths
libSearchPaths = [libPaths matlabPaths];
if (isSimTarget==1)
% Add path to host lib files if not specified and complete relative
% paths with the full S-function dir or current dir or MATLAB path
libFiles = info(ii).HostLibFiles;
for jj = 1:length(libFiles)
[fullName, isFound] = resolve_file_info(correct_path_sep(libFiles{jj}), libSearchPaths);
if isFound==0
DAStudio.error('Simulink:tools:LCTErrorCannotFindLibraryFile',...
libFiles{jj});
else
libFiles{jj} = fullName;
end
end
else
% Add path to target lib files if not specified and complete relative
% paths with the full S-function dir or current dir or MATLAB path
libFiles = info(ii).TargetLibFiles;
for jj = 1:length(libFiles)
[fullName, isFound] = resolve_file_info(correct_path_sep(libFiles{jj}), libSearchPaths);
if isFound==0
DAStudio.error('Simulink:tools:LCTErrorCannotFindLibraryFile',...
libFiles{jj});
else
libFiles{jj} = fullName;
end
end
end
% Concatenate known include and source directories
allIncPaths = RTW.uniquePath([allIncPaths incPaths]);
allSrcPaths = RTW.uniquePath([allSrcPaths srcPaths]);
% Concatenate Host or Target libraries
allLibs = RTW.uniquePath([allLibs libFiles]);
end
% Additional include directories
makeInfo.includePath = correct_path_name(allIncPaths);
% Additional source directories
makeInfo.sourcePath = correct_path_name(allSrcPaths);
% Additional libraries according to the build type
makeInfo.linkLibsObjs = correct_path_name(allLibs);
%--------------------------------------------------------------------------
function verify_simulink_version()
% Retrieve Simulink version
slVer = ver('simulink');
factor = 1.0;
thisVer = 0.0;
for ii = 1:length(slVer.Version)
if slVer.Version(ii)=='.'
factor = factor/10.0;
else
thisVer = thisVer + sscanf(slVer.Version(ii), '%d')*factor;
end
end
% Verify that the actual plateform supports the function used
if thisVer < 6.4
DAStudio.error('Simulink:tools:LCTErrorBadSimulinkVersion', sprintf('%g',thisVer))
end
%--------------------------------------------------------------------------
function [fullPath, isFound] = resolve_path_info(fullPath, searchPaths)
% Initialize output value
isFound = 0;
if is_absolute_path(fullPath)==1
% Verify that the dir exists
if exist(fullPath, 'dir')
isFound = 1;
end
else
% Walk through the search path
for ii = 1:length(searchPaths)
thisFullPath = fullfile(searchPaths{ii}, fullPath);
% If this candidate path exists then exit
if exist(thisFullPath, 'dir')
isFound = 1;
fullPath = thisFullPath;
break
end
end
end
%--------------------------------------------------------------------------
function [fullName, isFound] = resolve_file_info(fullName, searchPaths)
% Initialize output value
isFound = 0;
% Extract file parts
[fPath, fName, fExt] = fileparts(fullName);
if is_absolute_path(fPath)==1
% If the file has no extension then try to add it
if isempty(fExt)
fExt = find_file_extension(fullfile(fPath, fName));
fullName = fullfile(fPath, [fullName,fExt]);
end
% Verify that the file exists
if exist(fullName, 'file')
isFound = 1;
end
else
% Walk through the search path
for ii = 1:length(searchPaths)
thisFullName = fullfile(searchPaths{ii}, fullName);
% If the file has no extension then try to add it
if isempty(fExt)
fExt = find_file_extension(thisFullName);
thisFullName = [thisFullName,fExt];
end
% If this candidate path exists then exit
if exist(thisFullName, 'file')
fullName = thisFullName;
isFound = 1;
break
end
end
end
%--------------------------------------------------------------------------
function fext = find_file_extension(fullName)
% Initialize output value
fext = [];
% Use 'dir' because this command has the same behavior both
% on PC and Unix
theDir = dir([fullName,'.*']);
if ~isempty(theDir)
for ii = 1:length(theDir)
if theDir(ii).isdir
continue
end
[fpath, fname, fext] = fileparts(theDir(ii).name);
if ~isempty(fext)
break % stop on first occurrence
end
end
end
%--------------------------------------------------------------------------
function isSimTarget = is_simulation_target()
% Default output value
isSimTarget = 0;
% Get the current model and the code generation type to decide
% if we must link with the host libraries or with the target libraries
modelName = get_param(0, 'CurrentSystem');
if ~isempty(modelName)
sysTarget = get_param(modelName, 'RTWSystemTargetFile');
isSimTarget = ~isempty([findstr(sysTarget, 'rtwsfcn') findstr(sysTarget, 'accel')]);
mdlRefSimTarget = get_param(modelName,'ModelReferenceTargetType');
isSimTarget = strcmpi(mdlRefSimTarget, 'SIM') || isSimTarget;
% Verify again it's not Accelerator
if ~isSimTarget
simMode = get_param(modelName, 'SimulationMode');
simStat = get_param(modelName, 'SimulationStatus');
isSimTarget = strcmp(simStat, 'initializing') & strcmp(simMode, 'accelerator');
end
end
%--------------------------------------------------------------------------
function bool = is_absolute_path(thisPath)
if isempty(thisPath)
bool = 0;
return
end
if(thisPath(1)=='.')
% Relative path
bool = 0;
else
if(ispc && length(thisPath)>=2)
% Absolute path on PC start with drive letter or \(for UNC paths)
bool = (thisPath(2)==':') | (thisPath(1)=='\');
else
% Absolute paths on unix start with '/'
bool = thisPath(1)=='/';
end
end
%--------------------------------------------------------------------------
function thePath = correct_path_sep(thePath)
if isunix
wrongFilesepChar = '\';
filesepChar = '/';
else
wrongFilesepChar = '/';
filesepChar = '\';
end
seps = find(thePath==wrongFilesepChar);
if(~isempty(seps))
thePath(seps) = filesepChar;
end
%--------------------------------------------------------------------------
function thePaths = correct_path_name(thePaths)
for ii = 1:length(thePaths)
thePaths{ii} = rtw_alt_pathname(thePaths{ii});
end
thePaths = RTW.uniquePath(thePaths);
%--------------------------------------------------------------------------
function info = get_serialized_info()
% Allocate the output structure array
info(1:1) = struct(...
'SFunctionName', '',...
'IncPaths', {{}},...
'SrcPaths', {{}},...
'LibPaths', {{}},...
'SourceFiles', {{}},...
'HostLibFiles', {{}},...
'TargetLibFiles', {{}},...
'Language', ''...
);
% Dependency info for S-function 'arrobot_setdigout_sfn'
info(1).SFunctionName = 'arrobot_setdigout_sfn';
info(1).IncPaths = {'../include', '.'};
info(1).HostLibFiles = {'ariac_vc10_x64.lib', '..\lib64\AriaVC10.lib'};
info(1).TargetLibFiles = {'ariac_vc10_x64.lib', '..\lib64\AriaVC10.lib'};
info(1).Language = 'C';