]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/rpp/rpp_make_rtw_hook.m
dd48be3e81391e1fded235d4dd1d91c99584f8b6
[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 % This document contains proprietary information belonging to Czech
7 % Technical University in Prague. Passing on and copying of this
8 % document, and communication of its contents is not permitted
9 % without prior written authorization.
10 %
11 % File : rpp_make_rtw_hook.m
12 % Abstract:
13 %     Build process hooks file.
14 %
15 %     This file is hook file that invoke target-specific functions or
16 %     executables at specified points in the build process. In particular, this
17 %     file handle the copying of required files before the compilation stage.
18 %
19 % References:
20 %     rtw_ug.pdf p. 1066-1072 and 1131
21
22 function rpp_make_rtw_hook(hookMethod, modelName, rtwRoot, templateMakefile, ...
23                            buildOpts, buildArgs)
24
25     switch hookMethod
26         case 'error'
27             % Called if an error occurs anywhere during the build. If no error
28             % occurs during the build, then this hook will not be called. Valid
29             % arguments at this stage are hookMethod and modelName. This enables
30             % cleaning up any static or global data used by this hook file.
31             disp(['### Build procedure for model: ''', modelName, ...
32                   ''' aborted due to an error.']);
33
34
35         case 'entry'
36             % Called at start of code generation process (before anything
37             % happens).
38             % Valid arguments at this stage are hookMethod, modelName, and
39             % buildArgs.
40
41             % FIXME: Implement step integrity verification?
42
43
44         case 'before_tlc'
45             % Called just prior to invoking TLC Compiler (actual code
46             % generation).
47             % Valid arguments at this stage are hookMethod, modelName, and
48             % buildArgs.
49
50
51         case 'after_tlc'
52             % Called just after to invoking TLC Compiler (actual code
53             % generation).
54             % Valid arguments at this stage are hookMethod, modelName, and
55             % buildArgs.
56
57             % FIXME: Implement model tasking mode check?
58
59
60         case 'before_make'
61             % Called after code generation is complete, and just prior to
62             % kicking off make process (assuming code generation only is not
63             % selected). All arguments are valid at this stage.
64
65             % Copy extra runtime header to code generation directory
66             copyfile(fullfile(getpref('rpp', 'TargetRoot'), 'rpp_simulink_runtime.h'));
67
68             % Copy extra makefiles and library files to code generation
69             % directory
70             rpp_write_makefiles(modelName);
71
72
73         case 'after_make'
74                         % Called after make process is complete. All arguments are valid at
75             % this stage.
76
77             % Download generated binary to the board
78             rpp_do_download(modelName);
79
80
81         case 'exit'
82             % Called at the end of the build process.  All arguments are valid
83             % at this stage.
84             disp(['### Successful completion of build ', ...
85                   'procedure for model: ', modelName]);
86
87     end
88 end
89
90
91 function rpp_write_makefiles(modelName)
92
93     modelRoot = Simulink.fileGenControl('getConfig').CodeGenFolder;
94     buildFolder = fullfile(modelRoot, 'slprj');
95
96     % Get configuration variables and create paths configuration makefile
97     CompilerRoot = getpref('rpp', 'CompilerRoot');
98     CCSRoot      = getpref('rpp', 'CCSRoot');
99     TargetRoot   = getpref('rpp', 'TargetRoot');
100     RppLibRoot   = getpref('rpp', 'RppLibRoot');
101
102     TargetPaths = fullfile(buildFolder, 'target_paths.mk');
103
104     fid = fopen(TargetPaths, 'w');
105     fwrite(fid, sprintf('%s\n\n', '# RPP paths'));
106     fwrite(fid, sprintf('COMPILER_ROOT = %s\n', CompilerRoot));
107     fwrite(fid, sprintf('CCS_ROOT      = %s\n', CCSRoot));
108     fwrite(fid, sprintf('TARGET_ROOT   = %s\n', TargetRoot));
109     fwrite(fid, sprintf('RPP_LIB_ROOT  = %s\n', RppLibRoot));
110     fclose(fid);
111
112     % Copy the RPP version of target_tools.mk into the build area
113     tgtToolsFile = 'target_tools.mk';
114     copyfile(fullfile(TargetRoot, tgtToolsFile), ...
115              fullfile(buildFolder, tgtToolsFile));
116
117 end
118
119
120 function rpp_do_download(modelName)
121     if verLessThan('matlab', '8.1')
122         makertwObj = get_param(gcs, 'MakeRTWSettingsObject');
123                 makertwBuildDirectory = makertwObj.BuildDirectory;
124                 makertwArgs = makertwObj.BuildInfo.BuildArgs;
125         else
126                 makertwObj = rtwprivate('get_makertwsettings', gcs, 'BuildInfo');
127                 makertwBuildDirectory = rtwprivate('get_makertwsettings', gcs, 'BuildDirectory');
128                 makertwArgs = makertwObj.BuildArgs;
129     end
130
131     % Check if user choose to Download to RPP board in Settings
132     download = 0;
133         use_openocd = 0;
134         use_sdram = 0;
135     for i=1:length(makertwArgs)
136         if strcmp(makertwArgs(i).DisplayLabel, 'RPP_DOWNLOAD')
137             download = str2double(makertwArgs(i).Value);
138         elseif strcmp(makertwArgs(i).DisplayLabel, 'RPP_USE_OPENOCD')
139             use_openocd = str2double(makertwArgs(i).Value);
140         elseif strcmp(makertwArgs(i).DisplayLabel, 'RPP_DOWNLOAD_TO_SDRAM')
141             use_sdram = str2double(makertwArgs(i).Value);
142         end
143     end
144
145     if download
146         if getenv('RPP_NO_DOWNLOAD')
147             disp(['RPP_NO_DOWNLOAD defined - skipping downloading to target.'])
148         else
149             rpp_download(modelName, makertwBuildDirectory, use_openocd, use_sdram);
150         end
151     end
152
153 end