% Copyright (C) 2013-2014 Czech Technical University in Prague
%
% Authors:
%     - Carlos Jenkins <carlos@jenkins.co.cr>
%
% Permission is hereby granted, free of charge, to any person
% obtaining a copy of this software and associated documentation
% files (the "Software"), to deal in the Software without
% restriction, including without limitation the rights to use,
% copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the
% Software is furnished to do so, subject to the following
% conditions:

% The above copyright notice and this permission notice shall be
% included in all copies or substantial portions of the Software.

% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
% OTHER DEALINGS IN THE SOFTWARE.
%
% File : compile_blocks.m
% Abstract:
%     Compile all the C-MEX S-Function in current folder.
%
% References:
%     http://www.mathworks.com/help/matlab/ref/mex.html
%     http://www.mathworks.com/help/matlab/matlab_external/custom-building-mex-files.html


function compile_blocks()

    % Options:
    % -g        Create a binary MEX-file containing additional symbolic
    %           information for use in debugging. This option disables the mex
    %           default behavior of optimizing built object code
    %           (see the -O option).
    % -v        Verbose mode. Print the values for important internal variables
    %           after the options file is processed and all command-line
    %           arguments are considered. Prints each compile step and final
    %           link step fully evaluated.
    % options = '-v -g';
    options = '';
    matlabRootPath=strcat('"',matlabroot(),'"');
    mex = fullfile(matlabRootPath, 'bin', 'mex');

    sources = dir('sfunction_*.c');

    for i = sources'
        local_opts = options;
        if strcmp(i.name, 'sfunction_canreceive.c') || ...
           strcmp(i.name, 'sfunction_cantransmit.c'),
            local_opts = [local_opts ' ' ...
                '-I' matlabRootPath '/toolbox/shared/can/src/scanutil ' ...
		'-I' matlabRootPath '/toolbox/rtw/targets/common/can/datatypes ' ...
                matlabRootPath '/toolbox/rtw/targets/common/can/datatypes/sfun_can_util.c '...
                matlabRootPath '/toolbox/rtw/targets/common/can/datatypes/can_msg.c'];
        end
        command = [mex ' ' i.name ' ' local_opts];
        disp(command);
        [status, result] = system(command);
        if status ~= 0,
            disp(result)
            throw(MException('rpp:compile_block_failure', [i.name, ' failed to compile']))
        end
    end

end
