-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild_libraries.m
More file actions
204 lines (161 loc) · 5.28 KB
/
build_libraries.m
File metadata and controls
204 lines (161 loc) · 5.28 KB
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
function build_libraries()
% BUILD_LIBRARIES Create both static (.a) and dynamic (.so) libraries from RTW source
%
% This script builds both shared and static libraries for all examples
% using the same approach as Rosetta-Simulink project.
% Define models to build
models = {'dllModel', 'discrete_tf', 'bouncing_ball'};
example_dirs = {'Example1', 'Example2', 'Example3'};
fprintf('Building libraries for all examples...\n');
for i = 1:length(models)
model = models{i};
example_dir = example_dirs{i};
fprintf('\nBuilding %s in %s...\n', model, example_dir);
% Build shared library (.so)
build_shared_lib(model, example_dir);
% Build static library (.a)
build_static_lib(model, example_dir);
end
fprintf('\n✅ All libraries built successfully!\n');
end
function build_shared_lib(modelName, exampleDir)
% BUILD_SHARED_LIB Create a shared library (.so) from RTW source
%
% This uses the generated Makefile or manual compilation
rtw_dir = fullfile(exampleDir, [modelName '_ert_shrlib_rtw']);
so_file = fullfile(exampleDir, [modelName '.so']);
% Check if RTW directory exists
if ~isfolder(rtw_dir)
error('RTW directory "%s" does not exist.', rtw_dir);
end
% Try to use generated Makefile first
mk_file = fullfile(rtw_dir, [modelName '.mk']);
if isfile(mk_file)
fprintf(' Using generated Makefile for %s.so...\n', modelName);
% Set MATLAB paths for the build
old_dir = pwd;
cd(rtw_dir);
try
% Override MATLAB paths in environment
setenv('MATLAB_ROOT', '/opt/MATLAB/R2018a');
setenv('MATLAB_BIN', '/opt/MATLAB/R2018a/bin');
setenv('MATLAB_ARCH_BIN', '/opt/MATLAB/R2018a/bin/glnxa64');
% Run make
[status, result] = system('make -f ${modelName}.mk');
if status == 0
fprintf(' ✓ %s.so built successfully using Makefile\n', modelName);
cd(old_dir);
return;
else
fprintf(' Makefile failed, using manual build...\n');
end
catch
fprintf(' Makefile failed, using manual build...\n');
end
cd(old_dir);
end
% Manual build with correct paths
fprintf(' Manual build for %s.so...\n', modelName);
% Define include paths
matlab_root = '/opt/MATLAB/R2018a';
include_paths = {
fullfile(rtw_dir),
fullfile(matlab_root, 'extern', 'include'),
fullfile(matlab_root, 'simulink', 'include'),
fullfile(matlab_root, 'rtw', 'c', 'src'),
fullfile(matlab_root, 'rtw', 'c', 'src', 'ext_mode', 'common'),
fullfile(matlab_root, 'rtw', 'c', 'ert')
};
% Build include flags
include_flags = '';
for i = 1:length(include_paths)
include_flags = [include_flags, ' -I"', include_paths{i}, '"'];
end
% Define compilation flags
defines = {
['-DMODEL=', modelName],
'-DNUMST=1',
'-DNCSTATES=0',
'-DHAVESTDIO',
'-DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0',
'-DCLASSIC_INTERFACE=0',
'-DALLOCATIONFCN=0',
'-DTID01EQ=0',
'-DTERMFCN=1',
'-DONESTEPFCN=1',
'-DMAT_FILE=0',
'-DMULTI_INSTANCE_CODE=0',
'-DINTEGER_CODE=0',
'-DMT=0',
'-DUNIX'
};
% Build define flags
define_flags = '';
for i = 1:length(defines)
define_flags = [define_flags, ' ', defines{i}];
end
% Build command
gcc_cmd = sprintf('gcc -fPIC -shared -o "%s"%s%s "%s.c" ert_main.c -lm', ...
so_file, include_flags, define_flags, modelName);
% Change to RTW directory for compilation
old_dir = pwd;
cd(rtw_dir);
% Execute build
[status, result] = system(gcc_cmd);
% Return to original directory
cd(old_dir);
if status == 0
fprintf(' ✓ %s.so built successfully\n', modelName);
else
fprintf(' ✗ Failed to build %s.so: %s\n', modelName, result);
end
end
function build_static_lib(modelName, exampleDir)
% BUILD_STATIC_LIB Create a static library (.a) from RTW source
%
% This follows the same approach as Rosetta-Simulink build_static_lib.m
rtw_dir = fullfile(exampleDir, [modelName '_ert_shrlib_rtw']);
static_lib_name = fullfile(exampleDir, ['lib', modelName, '.a']);
combined_obj = fullfile(exampleDir, 'combined.o');
% Check if RTW directory exists
if ~isfolder(rtw_dir)
error('RTW directory "%s" does not exist.', rtw_dir);
end
% Find all .o files
d = dir(fullfile(rtw_dir, '*.o'));
if isempty(d)
fprintf(' No object (.o) files found in "%s", skipping static library\n', rtw_dir);
return;
end
fprintf(' Building static library %s...\n', static_lib_name);
% Build object file paths
object_files = '';
for i = 1:length(d)
object_files = [object_files, ' "', fullfile(rtw_dir, d(i).name), '"'];
end
% Change to example directory for build
old_dir = pwd;
cd(exampleDir);
try
% Combine object files using ld
ld_cmd = sprintf('ld -r -o %s%s', combined_obj, object_files);
[status, result] = system(ld_cmd);
if status ~= 0
error('ld command failed: %s', result);
end
% Create static library using ar
ar_cmd = sprintf('ar rcs %s %s', static_lib_name, combined_obj);
[status, result] = system(ar_cmd);
if status ~= 0
error('ar command failed: %s', result);
end
% Clean up combined object file
delete(combined_obj);
fprintf(' ✓ %s built successfully\n', static_lib_name);
catch ME
fprintf(' ✗ Failed to build static library: %s\n', ME.message);
cd(old_dir);
rethrow(ME);
end
cd(old_dir);
end