Added support for include directories with the new '-I' argument of the
authorJohann Glaser <Johann.Glaser@gmx.at>
Tue, 20 Aug 2013 13:48:16 +0000 (15:48 +0200)
committerJohann Glaser <Johann.Glaser@gmx.at>
Tue, 20 Aug 2013 13:48:16 +0000 (15:48 +0200)
'read_verilog' command

frontends/verilog/preproc.cc
frontends/verilog/verilog_frontend.cc
frontends/verilog/verilog_frontend.h

index 2dcc910434ba3eaea52e62f03f23e519f0e148a3..2033a290fda6105523fcaf69f43804afe034ac90 100644 (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);
index 9c6a5b64e2df18c4c6af2da5a15e279bf83aab9d..fb2b57ad002d48a9f02408bbc1a2325913eb3e8c 100644 (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");
index 244e2f58273bbea595d2524b582d7ce31a1b1287..222de7e7f16d750f15a52d8f2a110b8933d6a665 100644 (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;