image thumbnail
from Makefile like script by Andrea Tagliasacchi
Conditionally compile all "./util/*.cpp" MEX files

make( TYPE )
% [ Copyright  2007 Andrea Tagliasacchi - ata2 at cs dot sfu dot ca - All rights reserved ]
%
% SYNOPSIS
%	- make( TYPE )
%	
% DESCRIPTION
%	- MAKEFILE that compiles all the MEX C++ functions in the ./util folder
% 
% INPUT
%   - TYPE: 
%	  - 'all':    builds all the files in the folder
%	  - 'clean':  remove all mex compiled files from the folder
%
function make( TYPE )

% if nothing is specified make all
if nargin == 0
	TYPE = 'all';
end

% clear screen 
% clc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%          MAKE ALL            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp( TYPE, 'all' )
    % by default source files have .cpp extension
	cpp_files = dir('./util/*.cpp');
	for i=1:length( cpp_files )
		
		% extract name
		cpp_file = cpp_files(i);
		cpp_filename = cpp_file.name;
		
		% extract the mexfile name that would be generated by "mex ccp_filename"
        % NOTE: you can also use [pathstr, name, ext, versn] = fileparts(filename) 
		mex_filename = sprintf( './util/%s.%s', cpp_filename(1:end-4), mexext );
		mex_file     = dir( mex_filename );
		
		% file not already compiled OR file compiled is outdated
		if isempty( mex_file ) || ( cpp_file.datenum > mex_file.datenum )
			% compile
			disp(sprintf('compiling: %s', cpp_filename ) );
			mex( sprintf('./util/%s', cpp_filename), '-outdir', './util');
		else
			continue;
		end
	end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%          MAKE CLEAN          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp( TYPE, 'clean')
    % use mexext to determine for which architecture source have been built
    mex_files = dir( sprintf('./util/*.%s', mexext) );
    
    % delete those files one by one and notify deletion
	for i=1:length( mex_files )
        mex_filename = sprintf( './util/%s',mex_files(i).name );
        disp(sprintf('deleting %s', mex_filename) );
        delete( mex_filename );
    end
end
	
disp('Make done!');

Contact us at files@mathworks.com