Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Conditional Make Makefile-like in matlab

Subject: Conditional Make Makefile-like in matlab

From: Andrea Tagliasacchi

Date: 27 Feb, 2008 20:34:02

Message: 1 of 8

Hello there,

I am building a system that intensively uses MEX files to
optimize for speed. I have been storing all the MEX commands
in a Makefile.m so, when I need to recompile, I can just invoke

>> Makefile

In the command window.

A problem arises however, when the amount of MEX files to be
compiled is large. I was searching for a way of checking the
timestamp of the produced mex* (mexmaci in my case) against
the *.cpp file that generate it.

I searched the doc but the only element regarding timestamp
that I found was:

>> h = dir();
>> h.timestamp

This would allow me to create a .m based Makefile but with a
bit of scripting work to do. Do you have any other suggestion?

Thanks, Andrea

   

Subject: Conditional Make Makefile-like in matlab

From: Andrea Tagliasacchi

Date: 27 Feb, 2008 21:29:02

Message: 2 of 8

"Andrea Tagliasacchi" <ata2@nospam.cs.sfu.ca> wrote in
message <fq4hfq$ikj$1@fred.mathworks.com>...
> Hello there,
>
> I am building a system that intensively uses MEX files to
> optimize for speed. I have been storing all the MEX commands
> in a Makefile.m so, when I need to recompile, I can just
invoke
>
> >> Makefile
>
> In the command window.
>
> A problem arises however, when the amount of MEX files to be
> compiled is large. I was searching for a way of checking the
> timestamp of the produced mex* (mexmaci in my case) against
> the *.cpp file that generate it.
>
> I searched the doc but the only element regarding timestamp
> that I found was:
>
> >> h = dir();
> >> h.timestamp
>
> This would allow me to create a .m based Makefile but with a
> bit of scripting work to do. Do you have any other suggestion?
>
> Thanks, Andrea
>
>

I have built the following simple script that represents a
Makefile. It works perfectly on my machine, but, as I well
know is dependent on the architecture and folder used for
the make. Also instead of common commands like:

make
make all
make clean

it needs something like:

>> Makefile
>> Makefile('all')
>> Makefile('clean')

Any suggestion is welcome.

Cheers, Andrea.


p.s. they should really allow file attachments in here :)

================= CODE BEGINS HERE =====================
% [ Copyright ? 2007 Andrea Tagliasacchi - ata2 at cs dot
sfu dot ca - All rights reserved ]
%
% SYNOPSIS
% - PWD = mesh_compute_pair_wise_distances( M, selection_par )
%
% DESCRIPTION
% - MAKEFILE that compiles all the MEX function in the util
folder
%
% INPUT
% - TYPE:
% 'all': builds all the files in the folder
% 'clean': remove all mex compiled files from the folder
%
function Makefile( TYPE )

% if nothing is specified make all
if ~exist( 'TYPE','var' )
TYPE = 'all';
end

% clear screen
clc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAKE ALL %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp( TYPE, 'all' )
cpp_files = dir('./util/*.cpp');
for i=1:length( cpp_files )

% extract name
cpp_file = cpp_files(i);
cpp_filename = cpp_file.name;

% extract corresponding mexfile
mex_filename = sprintf( '%s%s', cpp_filename(1:end-4),
'.mexmaci' );
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', './');
else
continue;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAKE CLEAN %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp( TYPE, 'clean')
disp('removing all compiled mex files.');
delete('*.mexmaci');
end

disp('done!');




Subject: Conditional Make Makefile-like in matlab

From: Steven Lord

Date: 27 Feb, 2008 21:52:35

Message: 3 of 8


"Andrea Tagliasacchi" <ata2@nospam.cs.sfu.ca> wrote in message
news:fq4kmu$ai1$1@fred.mathworks.com...
> "Andrea Tagliasacchi" <ata2@nospam.cs.sfu.ca> wrote in
> message <fq4hfq$ikj$1@fred.mathworks.com>...
>> Hello there,
>>
>> I am building a system that intensively uses MEX files to
>> optimize for speed. I have been storing all the MEX commands
>> in a Makefile.m so, when I need to recompile, I can just
> invoke
>>
>> >> Makefile
>>
>> In the command window.
>>
>> A problem arises however, when the amount of MEX files to be
>> compiled is large. I was searching for a way of checking the
>> timestamp of the produced mex* (mexmaci in my case) against
>> the *.cpp file that generate it.
>>
>> I searched the doc but the only element regarding timestamp
>> that I found was:
>>
>> >> h = dir();
>> >> h.timestamp
>>
>> This would allow me to create a .m based Makefile but with a
>> bit of scripting work to do. Do you have any other suggestion?
>>
>> Thanks, Andrea
>>
>>
>
> I have built the following simple script that represents a
> Makefile. It works perfectly on my machine, but, as I well
> know is dependent on the architecture and folder used for
> the make. Also instead of common commands like:
>
> make
> make all
> make clean
>
> it needs something like:
>
>>> Makefile
>>> Makefile('all')
>>> Makefile('clean')

Through command-function duality, "make clean" is equivalent to
"make('clean')".

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-58170.html#f7-58289

> Any suggestion is welcome.
>
> Cheers, Andrea.
>
>
> p.s. they should really allow file attachments in here :)

The standard procedure for Usenet newsgroups that do not include the word
"binary" in their names, like comp.soft-sys.matlab, is text only -- and if
you're going to post a text attachment, why not just include it in your
message?

> ================= CODE BEGINS HERE =====================
> % [ Copyright ? 2007 Andrea Tagliasacchi - ata2 at cs dot
> sfu dot ca - All rights reserved ]
> %
> % SYNOPSIS
> % - PWD = mesh_compute_pair_wise_distances( M, selection_par )

Did you perhaps copy and paste the M-file help from another function and
forget to modify it? :)

> %
> % DESCRIPTION
> % - MAKEFILE that compiles all the MEX function in the util
> folder
> %
> % INPUT
> % - TYPE:
> % 'all': builds all the files in the folder
> % 'clean': remove all mex compiled files from the folder
> %
> function Makefile( TYPE )
>
> % if nothing is specified make all
> if ~exist( 'TYPE','var' )
> TYPE = 'all';
> end

For this, I'd use NARGIN:

function Makefile(type)

if nargin == 0
  type = 'all';
end

*snip*

--
Steve Lord
slord@mathworks.com


Subject: Conditional Make Makefile-like in matlab

From: Ralph Schleicher

Date: 28 Feb, 2008 00:28:51

Message: 4 of 8

"Andrea Tagliasacchi" <ata2@nospam.cs.sfu.ca> writes:

> This would allow me to create a .m based Makefile but with a
> bit of scripting work to do. Do you have any other suggestion?

Why do you want to reinvent the wheel? make(1) provides all the
facilities you want.

I utilize the GNU build system (Autoconf, Automake, Libtool) for
maintaining my projects. Together with a proper cross-compilation
environment I'm able to rebuild a complete toolbox (approximately
100 MEX-files) for various Matlab versions (6.1, 6.5.1, and 7.1)
and Matlab architectures (Windows, Linux, HP-UX, and Solaris) by
just typing

     $ make world

--
Ralph Schleicher http://ralph-schleicher.de

Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools

Subject: Conditional Make Makefile-like in matlab

From: Andrea Tagliasacchi

Date: 29 Feb, 2008 01:24:09

Message: 5 of 8

> Why do you want to reinvent the wheel? make(1) provides
all the
> facilities you want.
>
> I utilize the GNU build system (Autoconf, Automake,
Libtool) for
> maintaining my projects. Together with a proper
cross-compilation
> environment I'm able to rebuild a complete toolbox
(approximately
> 100 MEX-files) for various Matlab versions (6.1, 6.5.1,
and 7.1)
> and Matlab architectures (Windows, Linux, HP-UX, and
Solaris) by
> just typing
>
> $ make world
 
Hello Ralph,

make would have to be used outside Matlab though. What I
wanted was something integrated inside Matlab, for which
users wouldn't have to access an external shell. Consider
also that I am not distributing binaries, but source, and
users need to be able to easily compile it.

--
Andrea

Subject: Conditional Make Makefile-like in matlab

From: Tim Davis

Date: 29 Feb, 2008 02:35:11

Message: 6 of 8

"Andrea Tagliasacchi" <ata2@nospam.cs.sfu.ca> wrote in
message <fq7mrp$ekq$1@fred.mathworks.com>...
> > Why do you want to reinvent the wheel? make(1) provides
> all the
> > facilities you want.
> >
> > I utilize the GNU build system (Autoconf, Automake,
> Libtool) for
> > maintaining my projects. Together with a proper
> cross-compilation
> > environment I'm able to rebuild a complete toolbox
> (approximately
> > 100 MEX-files) for various Matlab versions (6.1, 6.5.1,
> and 7.1)
> > and Matlab architectures (Windows, Linux, HP-UX, and
> Solaris) by
> > just typing
> >
> > $ make world
>
> Hello Ralph,
>
> make would have to be used outside Matlab though. What I
> wanted was something integrated inside Matlab, for which
> users wouldn't have to access an external shell. Consider
> also that I am not distributing binaries, but source, and
> users need to be able to easily compile it.
>
> --
> Andrea

Try taking a look at the cs_make.m file in CSparse; it
checks the timestamps of the files and only compiles what's
needed, just like "make". It doesn't use the "make"
Unix/Linux/GNU/... command, so you can type "cs_make" on
Windows without requiring the user to install Cygwin.

Subject: Conditional Make Makefile-like in matlab

From: Andrea Tagliasacchi

Date: 1 Mar, 2008 20:15:03

Message: 7 of 8

> Try taking a look at the cs_make.m file in CSparse; it
> checks the timestamps of the files and only compiles what's
> needed, just like "make". It doesn't use the "make"
> Unix/Linux/GNU/... command, so you can type "cs_make" on
> Windows without requiring the user to install Cygwin.

Thanks! This is exactly what I meant :)
Unfortunately I tried to locate it in my system and I don't
have that file :(

--
Andrea

Subject: Conditional Make Makefile-like in matlab

From: Ralph Schleicher

Date: 2 Mar, 2008 00:51:15

Message: 8 of 8

"Andrea Tagliasacchi" <ata2@nospam.cs.sfu.ca> writes:

> Thanks! This is exactly what I meant :)
> Unfortunately I tried to locate it in my system and I don't
> have that file :(

Try these. I don't use them for coding up build scripts,
therefore I didn't think at it in the first place. Usage:

if make(['foo', mexext], 'foo.c')
  mex foo.c
end


% Copyright (C) 2007, 2008 Ralph Schleicher

% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as
% published by the Free Software Foundation; either version 2,
% or (at your option) any later version.

% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.

% You should have received a copy of the GNU General Public License
% along with this program; see the file COPYING. If not, write to
% the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
% Boston, MA 02111-1307, USA.

% As a special exception, Ralph Schleicher gives permission to link
% the code of this program with MATLAB from The Mathworks, Inc. (or
% with modified versions of MATLAB that use the same license as
% MATLAB), and distribute linked combinations including the two.
% You must obey the GNU General Public License in all respects for
% all of the code used other than with MATLAB. If you modify this
% file, you may extend this exception to your version of the file,
% but you are not obligated to do so. If you do not wish to do so,
% delete this exception statement from your version.

% make
%
% p = make(target, ...)
%
% Return non-zero if a file is out of date.
%
% First argument TARGET is the dependent file.
% Remaining arguments are the required files.
function p = make(target, varargin)

  % Dependencies.
  if nargin == 2
    dep = varargin{1};
    if ischar(dep)
      dep = cellstr(dep);
    end
  else
    dep = varargin;
  end

  % Required file modification times.
  t0 = mtime(dep);

  try
    % Dependent file modification times.
    t1 = mtime(target);
    % Rebuild if the newest required file is newer than
    % the oldest dependent file.
    if isempty(dep)
      p = 0;
    else
      p = max(t0) > min(t1);
    end
  catch
    p = 1;
  end

% mtime
%
% t = mtime(filename)
%
% Return the file modification time as a serial date number.
%
% Argument FILENAME is either a string, a character array, or a cell
% array of strings.
function t = mtime(filename)

  if ischar(filename)
    filename = cellstr(filename);
  end

  t = zeros(size(filename));
  for i = 1:numel(filename)
    dirent = dir(filename{i});
    if isempty(dirent)
      error(sprintf('%s: no such file or directory', filename{i}));
    end
    t(i) = datenum(dirent.date);
  end

--
Ralph Schleicher, Freelance Engineer http://ralph-schleicher.de

Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mex makefile timestamp Andrea Tagliasacchi 27 Feb, 2008 15:35:05
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics