Merge pull request #9 from hansiglaser/master

Added support for include directories with the new '-I' argument of the 'read_verilog' command
This commit is contained in:
Clifford Wolf 2013-08-20 09:38:31 -07:00
commit 459e8964fd
3 changed files with 24 additions and 4 deletions

View File

@ -38,7 +38,6 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <list>
static std::list<std::string> output_code;
static std::list<std::string> input_buffer;
@ -206,7 +205,7 @@ static std::string define_to_feature(std::string defname)
return std::string();
}
std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::map<std::string, std::string> pre_defines_map)
std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::map<std::string, std::string> pre_defines_map, const std::list<std::string> include_dirs)
{
std::map<std::string, std::string> defines_map(pre_defines_map);
int ifdef_fail_level = 0;
@ -273,9 +272,20 @@ std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::m
}
FILE *fp = fopen(fn.c_str(), "r");
if (fp == NULL && fn.size() > 0 && fn[0] != '/' && filename.find('/') != std::string::npos) {
// if the include file was not found, it is not given with an absolute path, and the
// currently read file is given with a path, then try again relative to its directory
std::string fn2 = filename.substr(0, filename.rfind('/')+1) + fn;
fp = fopen(fn2.c_str(), "r");
}
if (fp == NULL && fn.size() > 0 && fn[0] != '/') {
// if the include file was not found and it is not given with an absolute path, then
// search it in the include path
for (auto incdir : include_dirs) {
std::string fn2 = incdir + '/' + fn;
fp = fopen(fn2.c_str(), "r");
if (fp != NULL) break;
}
}
if (fp != NULL) {
input_file(fp, fn);
fclose(fp);

View File

@ -100,6 +100,10 @@ struct VerilogFrontend : public Frontend {
log(" define the preprocessor symbol 'name' and set its optional value\n");
log(" 'definition'\n");
log("\n");
log(" -Idir\n");
log(" add 'dir' to the directories which are used when searching include\n");
log(" files\n");
log("\n");
}
virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
{
@ -114,6 +118,7 @@ struct VerilogFrontend : public Frontend {
bool flag_lib = false;
bool flag_noopt = false;
std::map<std::string, std::string> defines_map;
std::list<std::string> include_dirs;
frontend_verilog_yydebug = false;
log_header("Executing Verilog-2005 frontend.\n");
@ -175,6 +180,10 @@ struct VerilogFrontend : public Frontend {
defines_map[name] = value;
continue;
}
if (arg.compare(0,2,"-I") == 0) {
include_dirs.push_back(arg.substr(2,std::string::npos));
continue;
}
break;
}
extra_args(f, filename, args, argidx);
@ -191,7 +200,7 @@ struct VerilogFrontend : public Frontend {
std::string code_after_preproc;
if (!flag_nopp) {
code_after_preproc = frontend_verilog_preproc(f, filename, defines_map);
code_after_preproc = frontend_verilog_preproc(f, filename, defines_map, include_dirs);
if (flag_ppdump)
log("-- Verilog code after preprocessor --\n%s-- END OF DUMP --\n", code_after_preproc.c_str());
fp = fmemopen((void*)code_after_preproc.c_str(), code_after_preproc.size(), "r");

View File

@ -33,6 +33,7 @@
#include "frontends/ast/ast.h"
#include <stdio.h>
#include <stdint.h>
#include <list>
namespace VERILOG_FRONTEND
{
@ -47,7 +48,7 @@ namespace VERILOG_FRONTEND
}
// the pre-processor
std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::map<std::string, std::string> pre_defines_map);
std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::map<std::string, std::string> pre_defines_map, const std::list<std::string> include_dirs);
// the usual bison/flex stuff
extern int frontend_verilog_yydebug;