2008-02-03 11:28:36 +01:00
|
|
|
/*
|
2016-02-23 13:55:02 +01:00
|
|
|
* Copyright (C) 2003-2016 Dynare Team
|
2008-02-03 11:28:36 +01:00
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare 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 Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
|
2009-01-21 13:08:11 +01:00
|
|
|
#include <cstdlib>
|
2008-12-15 15:39:36 +01:00
|
|
|
#include <cstring>
|
2009-05-16 00:41:51 +02:00
|
|
|
#ifndef PACKAGE_VERSION
|
2009-12-16 18:13:23 +01:00
|
|
|
# define PACKAGE_VERSION 4.
|
2009-05-16 00:41:51 +02:00
|
|
|
#endif
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2013-03-18 13:44:04 +01:00
|
|
|
#include <unistd.h>
|
2015-08-31 14:15:29 +02:00
|
|
|
#include "ParsingDriver.hh"
|
2014-04-20 11:12:12 +02:00
|
|
|
#include "ExtendedPreprocessorTypes.hh"
|
2015-08-28 16:09:43 +02:00
|
|
|
#include "ConfigFile.hh"
|
2013-03-18 13:44:04 +01:00
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
/* Prototype for second part of main function
|
|
|
|
Splitting main() in two parts was necessary because ParsingDriver.h and MacroDriver.h can't be
|
|
|
|
included simultaneously (because of Bison limitations).
|
|
|
|
*/
|
2015-08-28 16:09:43 +02:00
|
|
|
void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear_global,
|
|
|
|
bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console,
|
|
|
|
bool nograph, bool nointeractive, bool parallel, ConfigFile &config_file,
|
|
|
|
WarningConsolidation &warnings_arg, bool nostrict, bool check_model_changes,
|
2016-02-23 13:55:02 +01:00
|
|
|
bool minimal_workspace, bool compute_xrefs, FileOutputType output_mode,
|
2016-05-18 12:26:19 +02:00
|
|
|
LanguageOutputType lang, int params_derivs_order
|
2009-12-11 15:46:21 +01:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__)
|
2009-12-16 18:13:23 +01:00
|
|
|
, bool cygwin, bool msvc
|
2009-12-11 15:46:21 +01:00
|
|
|
#endif
|
2009-12-16 18:13:23 +01:00
|
|
|
);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2015-08-31 14:15:29 +02:00
|
|
|
void main1(char *modfile, string &basename, bool debug, bool save_macro, string &save_macro_file,
|
|
|
|
bool no_line_macro,
|
|
|
|
map<string, string> &defines, vector<string> &path, stringstream ¯o_output);
|
|
|
|
|
2008-10-29 16:19:56 +01:00
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2014-12-30 21:51:33 +01:00
|
|
|
cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [onlyclearglobals] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]"
|
2015-05-28 11:42:12 +02:00
|
|
|
<< " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test]"
|
2016-02-23 13:55:02 +01:00
|
|
|
<< " [-D<variable>[=<value>]] [-I/path] [nostrict] [fast] [minimal_workspace] [compute_xrefs] [output=dynamic|first|second|third] [language=C|C++|julia]"
|
2016-05-18 12:26:19 +02:00
|
|
|
<< " [params_derivs_order=0|1|2]"
|
2009-12-11 15:46:21 +01:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__)
|
|
|
|
<< " [cygwin] [msvc]"
|
|
|
|
#endif
|
|
|
|
<< endl;
|
2008-10-29 16:19:56 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
int
|
2009-12-16 18:13:23 +01:00
|
|
|
main(int argc, char **argv)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2013-03-18 13:44:04 +01:00
|
|
|
/*
|
|
|
|
Redirect stderr to stdout.
|
|
|
|
Made necessary because MATLAB/Octave can only capture stdout (but not
|
|
|
|
stderr), in order to put it in the logfile (see issue #306)
|
|
|
|
*/
|
|
|
|
dup2(STDOUT_FILENO, STDERR_FILENO);
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
cerr << "Missing model file!" << endl;
|
2008-10-29 16:19:56 +01:00
|
|
|
usage();
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool clear_all = true;
|
2014-12-30 21:51:33 +01:00
|
|
|
bool clear_global = false;
|
2008-02-03 11:28:36 +01:00
|
|
|
bool save_macro = false;
|
2008-12-15 15:39:36 +01:00
|
|
|
string save_macro_file;
|
2008-03-28 18:21:45 +01:00
|
|
|
bool debug = false;
|
2008-10-29 16:10:51 +01:00
|
|
|
bool no_tmp_terms = false;
|
2008-12-15 15:39:36 +01:00
|
|
|
bool only_macro = false;
|
2009-01-21 14:45:44 +01:00
|
|
|
bool no_line_macro = false;
|
2012-04-20 18:15:02 +02:00
|
|
|
bool no_log = false;
|
2013-02-26 16:50:05 +01:00
|
|
|
bool no_warn = false;
|
2016-05-18 12:26:19 +02:00
|
|
|
int params_derivs_order = 2;
|
2009-11-06 19:31:03 +01:00
|
|
|
bool warn_uninit = false;
|
2010-10-28 11:19:11 +02:00
|
|
|
bool console = false;
|
2013-07-10 11:03:48 +02:00
|
|
|
bool nograph = false;
|
2013-07-10 12:02:12 +02:00
|
|
|
bool nointeractive = false;
|
2009-12-11 15:46:21 +01:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__)
|
|
|
|
bool cygwin = false;
|
|
|
|
bool msvc = false;
|
|
|
|
#endif
|
2010-10-25 18:20:58 +02:00
|
|
|
string parallel_config_file;
|
|
|
|
bool parallel = false;
|
|
|
|
string cluster_name;
|
|
|
|
bool parallel_slave_open_mode = false;
|
|
|
|
bool parallel_test = false;
|
2013-09-13 20:37:31 +02:00
|
|
|
bool nostrict = false;
|
2015-05-10 20:29:03 +02:00
|
|
|
bool check_model_changes = false;
|
2015-05-28 11:42:12 +02:00
|
|
|
bool minimal_workspace = false;
|
2016-02-23 13:55:02 +01:00
|
|
|
bool compute_xrefs = false;
|
2011-03-29 15:53:10 +02:00
|
|
|
map<string, string> defines;
|
2015-08-27 16:49:00 +02:00
|
|
|
vector<string> path;
|
2014-02-25 15:53:14 +01:00
|
|
|
FileOutputType output_mode = none;
|
2014-04-20 11:12:12 +02:00
|
|
|
LanguageOutputType language = matlab;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
// Parse options
|
|
|
|
for (int arg = 2; arg < argc; arg++)
|
|
|
|
{
|
2008-12-15 15:39:36 +01:00
|
|
|
if (!strcmp(argv[arg], "debug"))
|
2008-03-28 18:21:45 +01:00
|
|
|
debug = true;
|
2008-12-15 15:39:36 +01:00
|
|
|
else if (!strcmp(argv[arg], "noclearall"))
|
2008-02-03 11:28:36 +01:00
|
|
|
clear_all = false;
|
2016-05-18 12:26:19 +02:00
|
|
|
else if (strlen(argv[arg]) >= 19 && !strncmp(argv[arg], "params_derivs_order", 19))
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) >= 22 || argv[arg][19] != '=' ||
|
|
|
|
!(argv[arg][20] == '0' || argv[arg][20] == '1' || argv[arg][20] == '2'))
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for params_derivs_order option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
2016-05-19 12:09:56 +02:00
|
|
|
params_derivs_order = atoi(argv[arg] + 20);
|
2016-05-18 12:26:19 +02:00
|
|
|
}
|
2014-12-30 21:51:33 +01:00
|
|
|
else if (!strcmp(argv[arg], "onlyclearglobals"))
|
|
|
|
{
|
|
|
|
clear_all = false;
|
|
|
|
clear_global = true;
|
|
|
|
}
|
2008-12-15 15:39:36 +01:00
|
|
|
else if (!strcmp(argv[arg], "onlymacro"))
|
|
|
|
only_macro = true;
|
|
|
|
else if (strlen(argv[arg]) >= 9 && !strncmp(argv[arg], "savemacro", 9))
|
|
|
|
{
|
|
|
|
save_macro = true;
|
|
|
|
if (strlen(argv[arg]) > 9)
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) == 10 || argv[arg][9] != '=')
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for savemacro option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
save_macro_file = string(argv[arg] + 10);
|
|
|
|
}
|
|
|
|
}
|
2009-01-21 14:45:44 +01:00
|
|
|
else if (!strcmp(argv[arg], "nolinemacro"))
|
|
|
|
no_line_macro = true;
|
2008-12-15 15:39:36 +01:00
|
|
|
else if (!strcmp(argv[arg], "notmpterms"))
|
2008-10-29 16:10:51 +01:00
|
|
|
no_tmp_terms = true;
|
2012-04-20 18:15:02 +02:00
|
|
|
else if (!strcmp(argv[arg], "nolog"))
|
|
|
|
no_log = true;
|
2013-02-26 16:50:05 +01:00
|
|
|
else if (!strcmp(argv[arg], "nowarn"))
|
|
|
|
no_warn = true;
|
2009-11-06 19:31:03 +01:00
|
|
|
else if (!strcmp(argv[arg], "warn_uninit"))
|
|
|
|
warn_uninit = true;
|
2010-10-28 11:19:11 +02:00
|
|
|
else if (!strcmp(argv[arg], "console"))
|
|
|
|
console = true;
|
2013-07-10 11:03:48 +02:00
|
|
|
else if (!strcmp(argv[arg], "nograph"))
|
|
|
|
nograph = true;
|
2013-07-10 12:02:12 +02:00
|
|
|
else if (!strcmp(argv[arg], "nointeractive"))
|
|
|
|
nointeractive = true;
|
2009-12-11 15:46:21 +01:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__)
|
|
|
|
else if (!strcmp(argv[arg], "cygwin"))
|
|
|
|
cygwin = true;
|
|
|
|
else if (!strcmp(argv[arg], "msvc"))
|
|
|
|
msvc = true;
|
|
|
|
#endif
|
2010-10-25 18:20:58 +02:00
|
|
|
else if (strlen(argv[arg]) >= 8 && !strncmp(argv[arg], "conffile", 8))
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) <= 9 || argv[arg][8] != '=')
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for conffile option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
parallel_config_file = string(argv[arg] + 9);
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[arg], "parallel_slave_open_mode"))
|
|
|
|
parallel_slave_open_mode = true;
|
|
|
|
else if (!strcmp(argv[arg], "parallel_test"))
|
|
|
|
parallel_test = true;
|
2013-09-13 20:37:31 +02:00
|
|
|
else if (!strcmp(argv[arg], "nostrict"))
|
|
|
|
nostrict = true;
|
2015-05-10 20:29:03 +02:00
|
|
|
else if (!strcmp(argv[arg], "fast"))
|
|
|
|
check_model_changes = true;
|
2015-05-28 11:42:12 +02:00
|
|
|
else if (!strcmp(argv[arg], "minimal_workspace"))
|
|
|
|
minimal_workspace = true;
|
2016-02-23 13:55:02 +01:00
|
|
|
else if (!strcmp(argv[arg], "compute_xrefs"))
|
|
|
|
compute_xrefs = true;
|
2010-10-25 18:20:58 +02:00
|
|
|
else if (strlen(argv[arg]) >= 8 && !strncmp(argv[arg], "parallel", 8))
|
|
|
|
{
|
|
|
|
parallel = true;
|
|
|
|
if (strlen(argv[arg]) > 8)
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) == 9 || argv[arg][8] != '=')
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for parallel option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
cluster_name = string(argv[arg] + 9);
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 15:53:10 +02:00
|
|
|
else if (strlen(argv[arg]) >= 2 && !strncmp(argv[arg], "-D", 2))
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) == 2)
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for command line define: the defined variable "
|
|
|
|
<< "must not be separated from -D by whitespace." << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t equal_index = string(argv[arg]).find('=');
|
|
|
|
if (equal_index != string::npos)
|
|
|
|
{
|
|
|
|
string key = string(argv[arg]).erase(equal_index).erase(0,2);
|
|
|
|
defines[key] = string(argv[arg]).erase(0, equal_index+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string key = string(argv[arg]).erase(0,2);
|
|
|
|
defines[key] = "1";
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 16:49:00 +02:00
|
|
|
else if (strlen(argv[arg]) >= 2 && !strncmp(argv[arg], "-I", 2))
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) == 2)
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for command line define: the defined variable "
|
|
|
|
<< "must not be separated from -I by whitespace." << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
path.push_back(string(argv[arg]).erase(0,2));
|
|
|
|
}
|
2013-08-13 21:16:02 +02:00
|
|
|
else if (strlen(argv[arg]) >= 6 && !strncmp(argv[arg], "output", 6))
|
|
|
|
{
|
|
|
|
if (strlen(argv[arg]) <= 7 || argv[arg][6] != '=')
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for ouput option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (strlen(argv[arg]) == 14 && !strncmp(argv[arg] + 7, "dynamic", 7))
|
|
|
|
output_mode = dynamic;
|
2013-09-17 15:11:57 +02:00
|
|
|
else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "first", 5))
|
2013-08-13 21:16:02 +02:00
|
|
|
output_mode = first;
|
2013-09-17 15:11:57 +02:00
|
|
|
else if (strlen(argv[arg]) == 13 && !strncmp(argv[arg] + 7, "second", 6))
|
2013-08-13 21:16:02 +02:00
|
|
|
output_mode = second;
|
2013-09-17 15:11:57 +02:00
|
|
|
else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 7, "third", 5))
|
2013-08-13 21:16:02 +02:00
|
|
|
output_mode = third;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for ouput option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 11:12:12 +02:00
|
|
|
else if (strlen(argv[arg]) >= 8 && !strncmp(argv[arg], "language", 8))
|
|
|
|
{
|
2014-04-20 21:23:32 +02:00
|
|
|
if (strlen(argv[arg]) <= 9 || argv[arg][8] != '=')
|
2014-04-20 11:12:12 +02:00
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for language option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
2015-09-01 14:11:38 +02:00
|
|
|
|
|
|
|
if (strlen(argv[arg]) == 14 && !strncmp(argv[arg] + 9, "julia", 5))
|
|
|
|
language = julia;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we don't want temp terms in external functions (except Julia)
|
|
|
|
no_tmp_terms = true;
|
|
|
|
if (strlen(argv[arg]) == 10 && !strncmp(argv[arg] + 9, "C", 1))
|
|
|
|
language = c;
|
|
|
|
else if (strlen(argv[arg]) == 12 && !strncmp(argv[arg] + 9, "C++", 3))
|
|
|
|
language = cpp;
|
|
|
|
else if (strlen(argv[arg]) == 13 && !strncmp(argv[arg] + 9, "cuda", 4))
|
|
|
|
language = cuda;
|
|
|
|
else if (strlen(argv[arg]) == 15 && !strncmp(argv[arg] + 9, "python", 6))
|
|
|
|
language = python;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Incorrect syntax for language option" << endl;
|
|
|
|
usage();
|
|
|
|
}
|
2014-04-20 11:12:12 +02:00
|
|
|
}
|
|
|
|
}
|
2008-10-29 16:19:56 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Unknown option: " << argv[arg] << endl;
|
|
|
|
usage();
|
|
|
|
}
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
|
2009-04-23 17:52:00 +02:00
|
|
|
cout << "Starting Dynare (version " << PACKAGE_VERSION << ")." << endl
|
2008-04-02 17:01:52 +02:00
|
|
|
<< "Starting preprocessing of the model file ..." << endl;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2009-01-21 13:08:11 +01:00
|
|
|
// Construct basename (i.e. remove file extension if there is one)
|
2008-02-03 11:28:36 +01:00
|
|
|
string basename = argv[1];
|
2009-01-21 13:08:11 +01:00
|
|
|
size_t pos = basename.find_last_of('.');
|
|
|
|
if (pos != string::npos)
|
|
|
|
basename.erase(pos);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2015-08-28 16:09:43 +02:00
|
|
|
WarningConsolidation warnings(no_warn);
|
|
|
|
|
|
|
|
// Process config file
|
|
|
|
ConfigFile config_file(parallel, parallel_test, parallel_slave_open_mode, cluster_name);
|
|
|
|
config_file.getConfigFileInfo(parallel_config_file);
|
|
|
|
config_file.checkPass(warnings);
|
|
|
|
config_file.transformPass();
|
|
|
|
|
2015-08-28 17:58:43 +02:00
|
|
|
// If Include option was passed to the [paths] block of the config file, add
|
|
|
|
// it to paths before macroprocessing
|
|
|
|
vector<string> config_include_paths = config_file.getIncludePaths();
|
|
|
|
for (vector<string>::const_iterator it = config_include_paths.begin();
|
|
|
|
it != config_include_paths.end(); it++)
|
|
|
|
path.push_back(*it);
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
// Do macro processing
|
|
|
|
stringstream macro_output;
|
2015-08-31 14:15:29 +02:00
|
|
|
main1(argv[1], basename, debug, save_macro, save_macro_file, no_line_macro, defines, path, macro_output);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2008-12-15 15:39:36 +01:00
|
|
|
if (only_macro)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
// Do the rest
|
2015-08-28 16:09:43 +02:00
|
|
|
main2(macro_output, basename, debug, clear_all, clear_global,
|
|
|
|
no_tmp_terms, no_log, no_warn, warn_uninit, console, nograph, nointeractive,
|
|
|
|
parallel, config_file, warnings, nostrict, check_model_changes, minimal_workspace,
|
2016-05-18 12:26:19 +02:00
|
|
|
compute_xrefs, output_mode, language, params_derivs_order
|
2009-12-11 15:46:21 +01:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__)
|
2009-12-11 15:52:04 +01:00
|
|
|
, cygwin, msvc
|
2009-12-11 15:46:21 +01:00
|
|
|
#endif
|
2009-12-16 18:13:23 +01:00
|
|
|
);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2008-10-29 16:33:16 +01:00
|
|
|
return EXIT_SUCCESS;
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|