]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/rpp/rpp_make_rtw_hook.m
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / rpp / rpp_make_rtw_hook.m
1 % Copyright (C) 2013-2015 Czech Technical University in Prague
2 %
3 % Authors:
4 %     - Carlos Jenkins <carlos@jenkins.co.cr>
5 %
6 % Permission is hereby granted, free of charge, to any person
7 % obtaining a copy of this software and associated documentation
8 % files (the "Software"), to deal in the Software without
9 % restriction, including without limitation the rights to use,
10 % copy, modify, merge, publish, distribute, sublicense, and/or sell
11 % copies of the Software, and to permit persons to whom the
12 % Software is furnished to do so, subject to the following
13 % conditions:
14
15 % The above copyright notice and this permission notice shall be
16 % included in all copies or substantial portions of the Software.
17
18 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 % OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 % NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 % HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 % WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 % FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 % OTHER DEALINGS IN THE SOFTWARE.
26 %
27 % File : rpp_make_rtw_hook.m
28 % Abstract:
29 %     Build process hooks file.
30 %
31 %     This file is hook file that invoke target-specific functions or
32 %     executables at specified points in the build process. In particular, this
33 %     file handle the copying of required files before the compilation stage.
34 %
35 % References:
36 %     rtw_ug.pdf p. 1066-1072 and 1131
37
38 function rpp_make_rtw_hook(hookMethod, modelName, rtwRoot, templateMakefile, ...
39                            buildOpts, buildArgs)
40
41     switch hookMethod
42         case 'error'
43             % Called if an error occurs anywhere during the build. If no error
44             % occurs during the build, then this hook will not be called. Valid
45             % arguments at this stage are hookMethod and modelName. This enables
46             % cleaning up any static or global data used by this hook file.
47             disp(['### Build procedure for model: ''', modelName, ...
48                   ''' aborted due to an error.']);
49
50
51         case 'entry'
52             % Called at start of code generation process (before anything
53             % happens).
54             % Valid arguments at this stage are hookMethod, modelName, and
55             % buildArgs.
56
57             % FIXME: Implement step integrity verification?
58
59
60         case 'before_tlc'
61             % Called just prior to invoking TLC Compiler (actual code
62             % generation).
63             % Valid arguments at this stage are hookMethod, modelName, and
64             % buildArgs.
65
66
67         case 'after_tlc'
68             % Called just after to invoking TLC Compiler (actual code
69             % generation).
70             % Valid arguments at this stage are hookMethod, modelName, and
71             % buildArgs.
72
73             % FIXME: Implement model tasking mode check?
74
75
76         case 'before_make'
77             % Called after code generation is complete, and just prior to
78             % kicking off make process (assuming code generation only is not
79             % selected). All arguments are valid at this stage.
80
81             % Copy extra runtime header to code generation directory
82             copyfile(fullfile(getpref('rpp', 'TargetRoot'), 'rpp_simulink_runtime.h'));
83
84             % Copy extra makefiles and library files to code generation
85             % directory
86             rpp_write_makefiles(modelName);
87
88
89         case 'after_make'
90                         % Called after make process is complete. All arguments are valid at
91             % this stage.
92
93             % Download generated binary to the board
94             rpp_do_download(modelName);
95
96
97         case 'exit'
98             % Called at the end of the build process.  All arguments are valid
99             % at this stage.
100             disp(['### Successful completion of build ', ...
101                   'procedure for model: ', modelName]);
102
103     end
104 end
105
106
107 function rpp_write_makefiles(modelName)
108
109     modelRoot = Simulink.fileGenControl('getConfig').CodeGenFolder;
110     buildFolder = fullfile(modelRoot, 'slprj');
111
112     % Get configuration variables and create paths configuration makefile
113     CompilerRoot = getpref('rpp', 'CompilerRoot');
114     CCSRoot      = getpref('rpp', 'CCSRoot');
115     TargetRoot   = getpref('rpp', 'TargetRoot');
116     RppLibRoot   = getpref('rpp', 'RppLibRoot');
117
118     TargetPaths = fullfile(buildFolder, 'target_paths.mk');
119
120     fid = fopen(TargetPaths, 'w');
121     fwrite(fid, sprintf('%s\n\n', '# RPP paths'));
122     fwrite(fid, sprintf('COMPILER_ROOT = %s\n', CompilerRoot));
123     fwrite(fid, sprintf('CCS_ROOT      = %s\n', CCSRoot));
124     fwrite(fid, sprintf('TARGET_ROOT   = %s\n', TargetRoot));
125     fwrite(fid, sprintf('RPP_LIB_ROOT  = %s\n', RppLibRoot));
126     fclose(fid);
127
128     % Copy the RPP version of target_tools.mk into the build area
129     tgtToolsFile = 'target_tools.mk';
130     copyfile(fullfile(TargetRoot, tgtToolsFile), ...
131              fullfile(buildFolder, tgtToolsFile));
132
133 end
134
135
136 function rpp_do_download(modelName)
137     if verLessThan('matlab', '8.1')
138         makertwObj = get_param(gcs, 'MakeRTWSettingsObject');
139                 makertwBuildDirectory = makertwObj.BuildDirectory;
140                 makertwArgs = makertwObj.BuildInfo.BuildArgs;
141         else
142                 makertwObj = rtwprivate('get_makertwsettings', gcs, 'BuildInfo');
143                 makertwBuildDirectory = rtwprivate('get_makertwsettings', gcs, 'BuildDirectory');
144                 makertwArgs = makertwObj.BuildArgs;
145     end
146
147     % Check if user choose to Download to RPP board in Settings
148     download = 0;
149         use_openocd = 0;
150         use_sdram = 0;
151     for i=1:length(makertwArgs)
152         if strcmp(makertwArgs(i).DisplayLabel, 'RPP_DOWNLOAD')
153             download = str2double(makertwArgs(i).Value);
154         elseif strcmp(makertwArgs(i).DisplayLabel, 'RPP_USE_OPENOCD')
155             use_openocd = str2double(makertwArgs(i).Value);
156         elseif strcmp(makertwArgs(i).DisplayLabel, 'RPP_DOWNLOAD_TO_SDRAM')
157             use_sdram = str2double(makertwArgs(i).Value);
158         end
159     end
160
161     if download
162         if getenv('RPP_NO_DOWNLOAD')
163             disp(['RPP_NO_DOWNLOAD defined - skipping downloading to target.'])
164         else
165             rpp_download(modelName, makertwBuildDirectory, use_openocd, use_sdram);
166         end
167     end
168
169 end