MATLAB: Is it possible to automatically modify the generated BAT-file from code generation so that it contains different options in Simulink Coder

hooksimulink coderstf_make_rtw_hook

I am using a 64-bit version of MATLAB R2011a and would like to generate code from my Simulink model for a 32-bit target.
By default, the generated code is for 64-bit platforms and the BAT-file contains the following line:
call "%VS100COMNTOOLS%..\..\VC\vcvarsall.bat" AMD64
If I edit this line to
call "%VS100COMNTOOLS%..\..\VC\vcvarsall.bat" x86
And then manually execute the BAT-file, I can get a compiled code for my 32-bit target.

Best Answer

Customizing the generated BAT-file is not possible in MATLAB 8.0 (R2012b) when building a model directly from Simulink.
To work around this issue, and build a model from a customized BAT-file, consider using "STF_make_rtw_hook.m" to modify the build process:
web([docroot '/rtw/ug/customizing-the-target-build-process-with-the-stf-make-rtw-hook-file.html']) %R2012b
The following outlines how to perform this using "rtwdemo_counter" to modify the BAT-file to point towards a different compiler for "grt.tlc":
1. Create a function from the following and place it in the current directory:
function grt_make_rtw_hook(hookMethod,modelName,rtwroot,templateMakefile,buildOpts,buildArgs)
switch hookMethod
case 'error'
msg = DAStudio.message('RTW:makertw:buildAborted', modelName);
disp(msg);
case 'entry'
msg = DAStudio.message('RTW:makertw:enterRTWBuild', modelName);
disp(msg);
option = LocalParseArgList(buildArgs);
if ~strcmp(option,'none')
ert_unspecified_hardware(modelName);
cs = getActiveConfigSet(modelName);
cscopy = cs.copy;
ert_auto_configuration(modelName,option);
locReportDifference(cscopy, cs);
end
case 'before_tlc'
case 'after_tlc'
case 'before_make'
case 'after_make'
filename = 'rtwdemo_counter.bat';
fid = fopen(filename,'r+');
fprintf(fid,'%s','call "%VS90COMNTOOLS%..\..\VC\bin\vcvars64" ');
fclose(fid);
!rtwdemo_counter.bat
case 'exit'
end
% Simple parse function to find:
% optimized_fixed_point=1
% optimized_floating_point=1
function option = LocalParseArgList(args)
if strfind(args,'optimized_fixed_point=1')
option = 'optimized_fixed_point';
elseif strfind(args,'optimized_floating_point=1')
option = 'optimized_floating_point';
else
option = 'none';
end
% local function: report difference between the configuration set settings
% before and after running auto-configuration script.
function locReportDifference(cs1, cs2)
[iseq, diffs] = slprivate('diff_config_sets', cs1, cs2, 'string');
if ~iseq
msg = DAStudio.message('RTW:makertw:incompatibleParamsUpdated', diffs);
summary = DAStudio.message('RTW:makertw:autoconfigSummary');
rtwprivate('rtw_disp_info',...
get_param(cs2.getModel, 'Name'),...
summary,...
msg);
end
This utilizes the 'after_make' case to modify the BAT-file. Here FPRINTF is used to modify the BAT-file and then the SYSTEM operation is used to execute the BAT-file.
2. Open "rtwdemo_counter".
3. Open Configuration Parameters and ensure that the TLC-file matches the "STF" part of the 'hook' file.
4. Select Generate Code only. This is to ensure that Simulink does not try to build the model. This is required because at 'before_make' the BAT-file is not generated, and 'after_make' is after Simulink has built the model. Selecting Generate Code only allows the use of 'after_make' to customize the BAT-file before the compiling phase.
5. Click Generate code. The will generate the code and when 'after_make' is called then the compiling and build occurs.