23 #if defined (HAVE_CONFIG_H)
37 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
41 #define pclose _pclose
48 static map<string,string>
vars;
54 const string& new_prefix)
58 if (!prefix.empty () && new_prefix != prefix)
60 int len = prefix.length ();
61 if (retval.find (prefix) == 0)
62 retval.replace (0, len, new_prefix);
65 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
66 replace (retval.begin (), retval.end (),
'/',
'\\');
75 static vector<char> buf (100);
81 c =
static_cast<char> (fgetc (fp));
82 if (c ==
'\n' || c == EOF)
84 if (buf.size () <= idx)
85 buf.resize (buf.size () + 100);
91 return string (&buf[0], idx);
98 const char *val = getenv (name);
108 if (s.find (
' ') != string::npos && s[0] !=
'"')
109 return "\"" + s +
"\"";
124 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
127 string bin_dir (n,
'\0');
131 int status = GetModuleFileName (0, &bin_dir[0], n);
135 bin_dir.resize (status);
145 if (! bin_dir.empty ())
147 size_t pos = bin_dir.rfind (
"\\bin\\");
149 if (pos != string::npos)
150 vars[
"OCTAVE_HOME"] = bin_dir.substr (0, pos);
160 string DEFAULT_LIBDIR = %OCTAVE_CONF_LIBDIR%;
163 if (!
vars[
"OCTAVE_HOME"].empty ())
165 DEFAULT_OCTINCLUDEDIR
167 vars[
"OCTAVE_HOME"]);
171 vars[
"OCTAVE_HOME"]);
175 vars[
"OCTAVE_HOME"]);
179 vars[
"OCTAVE_HOME"]);
182 vars[
"OCTINCLUDEDIR"] =
get_variable (
"OCTINCLUDEDIR", DEFAULT_OCTINCLUDEDIR);
187 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
188 string DEFAULT_INCFLAGS
192 string DEFAULT_INCFLAGS
196 if (
vars[
"INCLUDEDIR"] !=
"/usr/include")
200 if (
vars[
"LIBDIR"] !=
"/usr/lib")
222 %OCTAVE_CONF_DEPEND_FLAGS%);
223 vars[
"DEPEND_EXTRA_SED_PATTERN"] =
get_variable (
"DEPEND_EXTRA_SED_PATTERN",
224 %OCTAVE_CONF_DEPEND_EXTRA_SED_PATTERN%);
232 vars[
"LIBOCTAVE"] =
"-loctave";
233 vars[
"LIBOCTINTERP"] =
"-loctinterp";
234 vars[
"READLINE_LIBS"] =
"-lreadline";
258 vars[
"ALL_FFLAGS"] =
vars[
"FFLAGS"] +
" " +
vars[
"F77_INTEGER8_FLAG"];
260 vars[
"ALL_CFLAGS"] = vars[
"INCFLAGS"] +
" " + vars[
"XTRA_CFLAGS"] +
" "
263 vars[
"ALL_CXXFLAGS"] = vars[
"INCFLAGS"] +
" " + vars[
"XTRA_CXXFLAGS"] +
" "
266 vars[
"ALL_LDFLAGS"] = vars[
"LD_STATIC_FLAG"] +
" " + vars[
"CPICFLAG"] +
" "
269 vars[
"OCTAVE_LIBS"] = vars[
"LIBOCTINTERP"] +
" " + vars[
"LIBOCTAVE"] +
" "
270 + vars[
"SPECIAL_MATH_LIB"];
272 vars[
"FFTW_LIBS"] = vars[
"FFTW3_LDFLAGS"] +
" " + vars[
"FFTW3_LIBS"] +
" "
273 + vars[
"FFTW3F_LDFLAGS"] +
" " + vars[
"FFTW3F_LIBS"];
276 static string usage_msg =
"usage: mkoctfile [options] file ...";
283 " -h, -?, --help Print this message.\n"
285 " -IDIR Add -IDIR to compile commands.\n"
287 " -idirafter DIR Add -idirafter DIR to compile commands.\n"
289 " -DDEF Add -DDEF to compile commands.\n"
291 " -lLIB Add library LIB to link command.\n"
293 " -LDIR Add -LDIR to link command.\n"
295 " -M, --depend Generate dependency files (.d) for C and C++\n"
298 " -RDIR Add -RDIR to link command.\n"
300 " -Wl,... Pass flags though the linker like -Wl,-rpath=...\n"
302 " -W... Pass flags though the compiler like -Wa,OPTION.\n"
304 " -c, --compile Compile, but do not link.\n"
306 " -o FILE, --output FILE Output file name. Default extension is .oct\n"
307 " (or .mex if --mex is specified) unless linking\n"
308 " a stand-alone executable.\n"
310 " -g Enable debugging options for compilers.\n"
312 " -p VAR, --print VAR Print configuration variable VAR. Recognized\n"
315 " ALL_CFLAGS FFTW3F_LDFLAGS\n"
316 " ALL_CXXFLAGS FFTW3F_LIBS\n"
317 " ALL_FFLAGS FLIBS\n"
318 " ALL_LDFLAGS FPICFLAG\n"
320 " BLAS_LIBS LAPACK_LIBS\n"
323 " CPICFLAG LD_STATIC_FLAG\n"
326 " CXXFLAGS LIBOCTINTERP\n"
328 " DEPEND_EXTRA_SED_PATTERN OCTAVE_LIBS\n"
329 " DEPEND_FLAGS OCTAVE_LINK_DEPS\n"
330 " DL_LD OCT_LINK_DEPS\n"
331 " DL_LDFLAGS RANLIB\n"
332 " EXEEXT RDYNAMIC_FLAG\n"
333 " F77 READLINE_LIBS\n"
334 " F77_INTEGER_8_FLAG SED\n"
335 " FFLAGS XTRA_CFLAGS\n"
336 " FFTW3_LDFLAGS XTRA_CXXFLAGS\n"
339 " --link-stand-alone Link a stand-alone executable file.\n"
341 " --mex Assume we are creating a MEX file. Set the\n"
342 " default output extension to \".mex\".\n"
344 " -s, --strip Strip output file.\n"
346 " -v, --verbose Echo commands as they are executed.\n"
348 " FILE Compile or link FILE. Recognized file types are:\n"
354 " .f Fortran source (fixed form)\n"
355 " .F Fortran source (fixed form)\n"
356 " .f90 Fortran source (free form)\n"
357 " .F90 Fortran source (free form)\n"
361 " .lib library file\n"
366 basename (
const string& s,
bool strip_path =
false)
369 size_t pos = s.rfind (
'.');
371 if (pos == string::npos)
374 retval = s.substr (0, pos);
378 size_t p1 = retval.rfind (
'/'), p2 = retval.rfind (
'\\');
379 pos = (p1 != string::npos && p2 != string::npos
380 ?
max (p1, p2) : (p2 != string::npos ? p2 : p1));
381 if (pos != string::npos)
382 retval = retval.substr (++pos, string::npos);
391 return (s.length () >= prefix.length () && s.find (prefix) == 0);
397 return (s.length () >= suffix.length ()
398 && s.rfind (suffix) == s.length () - suffix.length ());
406 return system (cmd.c_str ());
421 string file, output_option;
422 list<string> cfiles, ccfiles, f77files;
425 string objfiles =
"";
426 string libfiles =
"";
428 string outputfile =
"";
429 string incflags =
"";
432 string pass_on_options =
"";
434 bool no_oct_file_strip_on_this_platform =
is_true (
"%NO_OCT_FILE_STRIP%");
436 bool link_stand_alone =
false;
437 string output_ext =
".oct";
447 if (argc == 2 && (!strcmp (argv[1],
"-v")
448 || !strcmp (argv[1],
"-version")
449 || !strcmp (argv[1],
"--version")))
455 for (
int i = 1; i < argc; i++)
457 string arg = argv[i];
462 cfiles.push_back (file);
468 ccfiles.push_back (file);
474 f77files.push_back (file);
486 else if (arg ==
"-d" || arg ==
"-debug" || arg ==
"--debug"
487 || arg ==
"-v" || arg ==
"-verbose" || arg ==
"--verbose")
490 if (
vars[
"CC"] ==
"cc-msvc")
492 if (
vars[
"CXX"] ==
"cc-msvc")
493 vars[
"CXX"] +=
" -d";
494 if (
vars[
"DL_LD"] ==
"cc-msvc")
495 vars[
"DL_LD"] +=
" -d";
497 else if (arg ==
"-h" || arg ==
"-?" || arg ==
"-help" || arg ==
"--help")
507 else if (arg ==
"-idirafter")
512 incflags += (
" -idirafter " +
arg);
515 cerr <<
"mkoctfile: include directory name missing" << endl;
524 ldflags += (
" " +
arg);
526 else if (arg ==
"-M" || arg ==
"-depend" || arg ==
"--depend")
531 else if (arg ==
"-o" || arg ==
"-output" || arg ==
"--output")
539 cerr <<
"mkoctfile: output file name missing" << endl;
541 else if (arg ==
"-p" || arg ==
"-print" || arg ==
"--print")
550 cerr <<
"mkoctfile: --print requires argument" << endl;
552 else if (arg ==
"-s" || arg ==
"-strip" || arg ==
"--strip")
554 if (no_oct_file_strip_on_this_platform)
555 cerr <<
"mkoctfile: stripping disabled on this platform" << endl;
559 else if (arg ==
"-c" || arg ==
"-compile" || arg ==
"--compile")
563 else if (arg ==
"-g")
565 vars[
"ALL_CFLAGS"] +=
" -g";
566 vars[
"ALL_CXXFLAGS"] +=
" -g";
567 vars[
"ALL_FFLAGS"] +=
" -g";
569 else if (arg ==
"-link-stand-alone" || arg ==
"--link-stand-alone")
571 link_stand_alone =
true;
573 else if (arg ==
"-mex" || arg ==
"--mex")
577 ldflags +=
" -Wl,-export:mexFunction";
583 pass_on_options += (
" " +
arg);
587 cerr <<
"mkoctfile: unrecognized argument " <<
arg;
591 if (!file.empty () && octfile.empty ())
595 if (link_stand_alone)
597 if (!outputfile.empty ())
598 output_option =
"-o " + outputfile;
602 if (!outputfile.empty ())
604 octfile = outputfile;
605 size_t len = octfile.length ();
606 size_t len_ext = output_ext.length ();
607 if (octfile.substr (len-len_ext) != output_ext)
608 octfile += output_ext;
611 octfile =
basename (octfile,
true) + output_ext;
614 list<string>::const_iterator it;
618 for (it = cfiles.begin (); it != cfiles.end (); ++it)
622 unlink (dfile.c_str ());
623 string cmd =
vars[
"CC"] +
" " +
vars[
"DEPEND_FLAGS"] +
" "
624 +
vars[
"CPPFLAGS"] +
" " +
vars[
"ALL_CFLAGS"] +
" "
625 + incflags +
" " + defs +
" " +
quote_path (f);
627 FILE *fd = popen (cmd.c_str (),
"r");
628 ofstream fo (dfile.c_str ());
633 if ((pos =
line.rfind (
".o:")) != string::npos)
635 size_t spos =
line.rfind (
'/', pos);
637 (spos == string::npos ?
line.substr (0, pos+2)
638 :
line.substr (spos+1, pos-spos+1));
639 fo <<
"pic/" << ofile <<
" " << ofile <<
" "
640 << dfile <<
line.substr (pos) << endl;
649 for (it = ccfiles.begin (); it != ccfiles.end (); ++it)
653 unlink (dfile.c_str ());
654 string cmd =
vars[
"CC"] +
" " +
vars[
"DEPEND_FLAGS"] +
" "
655 +
vars[
"CPPFLAGS"] +
" " +
vars[
"ALL_CXXFLAGS"] +
" "
656 + incflags +
" " + defs +
" " +
quote_path (f);
658 FILE *fd = popen (cmd.c_str (),
"r");
659 ofstream fo (dfile.c_str ());
664 if ((pos =
line.rfind (
".o:")) != string::npos)
666 size_t spos =
line.rfind (
'/', pos);
668 (spos == string::npos ?
line.substr (0, pos+2)
669 :
line.substr (spos+1, pos-spos+1));
670 fo <<
"pic/" << ofile <<
" " << ofile <<
" "
671 << dfile <<
line.substr (pos+2) << endl;
683 for (it = f77files.begin (); it != f77files.end (); ++it)
686 if (!
vars[
"F77"].empty ())
689 if (!outputfile.empty ())
698 objfiles += (
" " + o);
699 string cmd =
vars[
"F77"] +
" -c " +
vars[
"FPICFLAG"] +
" "
700 +
vars[
"ALL_FFLAGS"] +
" " + incflags +
" " + defs +
" "
701 + pass_on_options +
" " + f +
" -o " + o;
706 cerr <<
"mkoctfile: no way to compile Fortran file " << f << endl;
711 for (it = cfiles.begin (); it != cfiles.end (); ++it)
714 if (!
vars[
"CC"].empty ())
717 if (!outputfile.empty ())
726 objfiles += (
" " + o);
727 string cmd =
vars[
"CC"] +
" -c " +
vars[
"CPPFLAGS"] +
" "
728 +
vars[
"CPICFLAG"] +
" " +
vars[
"ALL_CFLAGS"] +
" "
729 + pass_on_options +
" " + incflags +
" " + defs +
" "
735 cerr <<
"mkoctfile: no way to compile C file " << f << endl;
740 for (it = ccfiles.begin (); it != ccfiles.end (); ++it)
743 if (!
vars[
"CXX"].empty ())
746 if (!outputfile.empty ())
755 objfiles += (
" " + o);
756 string cmd =
vars[
"CXX"] +
" -c " +
vars[
"CPPFLAGS"] +
" "
757 +
vars[
"CXXPICFLAG"] +
" " +
vars[
"ALL_CXXFLAGS"] +
" "
758 + pass_on_options +
" " + incflags +
" " + defs +
" "
764 cerr <<
"mkoctfile: no way to compile C++ file " << f << endl;
769 if (link && !objfiles.empty ())
771 if (link_stand_alone)
773 if (!
vars[
"LD_CXX"].empty ())
775 string cmd =
vars[
"LD_CXX"] +
" " +
vars[
"CPPFLAGS"] +
" "
776 +
vars[
"ALL_CXXFLAGS"] +
" " +
vars[
"RDYNAMIC_FLAG"]
777 +
" " +
vars[
"ALL_LDFLAGS"] +
" "
778 + pass_on_options +
" " + output_option +
" "
779 + objfiles +
" " + libfiles +
" "
780 + ldflags +
" " +
vars[
"LFLAGS"]
781 +
" -loctinterp -loctave "
782 +
" " +
vars[
"OCT_LINK_OPTS"]
783 +
" " +
vars[
"OCTAVE_LINK_DEPS"];
788 cerr <<
"mkoctfile: no way to link stand-alone executable file"
795 string cmd =
vars[
"DL_LD"] +
" " +
vars[
"DL_LDFLAGS"] +
" "
796 + pass_on_options +
" -o " + octfile +
" "
797 + objfiles +
" " + libfiles +
" "
798 + ldflags +
" " +
vars[
"LFLAGS"]
799 +
" -loctinterp -loctave "
800 +
vars[
"OCT_LINK_OPTS"] +
" " +
vars[
"OCT_LINK_DEPS"];
806 string cmd =
"strip " + octfile;