Hi

matlab supports a useful mechanism to override its default compiler
settings, see <http://www.mathworks.co.uk/help/techdoc/ref/mex.html> for
details. You can say things like

mex CFLAGS="-O2 -Wall" myfile.c

this is useful to override any defaults but it can also be used to add
compiler flags that aren't supported by mex, as in

mex CFLAGS="\$CFLAGS -Wall" myfile.c

(note the "escaping" of the $ that is necessary for unix shells, see also
below).

I had a look at how to implement this in mkoctfile, as I thought it'd be a
useful feature to have (aside from the fact it'd would make my scripts to
compile mex files also work under octave!). 

Before you read further: it gets very messy and I didn't find a good
solution, essentially because of how octave and the shell handle double
quotes. I'm not sure if the level of (apparently necessary) complication
isn't too high (and dangerous), but I thought I'd record my attempts here
for posterity anyway :-?

Doing this in the mkoctfile shell script isn't too hard. I can add the
following in the option processing:

    CFLAGS=*|LDFLAGS=*)
       # find the variable name by using the string before the =
       var=${1%%=*}
       # find value of the variable by using the string after the =
       val=${1#*=}
         # set the variable in the current environment
       # quotes are necessary in case $val has spaces
       eval ${var}=\"${val}\"
       #echo CFLAGS=$CFLAGS
       #echo LDFLAGS=$LDFLAGS
    ;;

obviously, this would work for any environment flag you'd want to change as
long as you add it to the pattern.

Before getting this to work, I also had to move the setting of the
ALL_CFLAGS etc, e.g.

: ${ALL_CFLAGS="$INCFLAGS $XTRA_CFLAGS $CFLAGS"}

from *before* the option processing to *after* the option processing
(otherwise, the above modification only sets CFLAGS, but the rest of the
script uses ALL_CFLAGS).

With this change, I can do things like matlab from the Unix/linux shell.
Great!

Unfortunately, this matlab compatibility breaks down when using mex (or in
effect mkoctfile) from the octave prompt. The reason is that mkoctfile.m
adds double quotes around its arguments to construct the shell command.
Those then conflict with any you happen to have in the value of the CFLAGS
variable, making the mkoctfile see multiple arguments because of the spaces
(see below). Here's what works for instance in matlab

 mexOptions={'-g', 'CFLAGS="\$CFLAGS someotheroption"'};
 mex(mexOptions{:}, 'myfile.c')

in Octave, this'd get passed to the mkoctfile script as

  mkoctfile --mex -g "CFLAGS="\$CFLAGS someotheroption""

which is obviously wrong. 

The following does work (on the octave prompt)

  mex 'CFLAGS=\$CFLAGS someotherflag' myfile.c

but I have no idea if this is matlab compatible.

It seems to me that to get this to work, mkoctfile.m would need to be
modified as it's not sufficient to simply wrap the arguments with double
quotes to construct the cmd line, as is currently done in L145 in
mkoctfile.m:

 cmd = cstrcat (cmd, " \"", varargin{i}, "\"");

At this point, I gave up...

Opinions welcome of course

Kris


------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management 
Up to 160% more powerful than alternatives and 25% more efficient. 
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to