From 864a852bca16461b2ff7d8542da8443b2e9e3b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Chmiel?= Date: Fri, 28 Jun 2024 00:53:44 +0200 Subject: [PATCH] Add `--compiler-include` for additional C++ includes (#5139) (#5202) --- bin/verilator | 1 + docs/guide/exe_verilator.rst | 7 +++++ src/V3EmitCPch.cpp | 5 ++++ src/V3Options.cpp | 4 +++ src/V3Options.h | 3 ++ test_regress/t/t_compiler_include.cpp | 42 +++++++++++++++++++++++++++ test_regress/t/t_compiler_include.h | 14 +++++++++ test_regress/t/t_compiler_include.pl | 23 +++++++++++++++ test_regress/t/t_compiler_include.v | 31 ++++++++++++++++++++ 9 files changed, 130 insertions(+) create mode 100644 test_regress/t/t_compiler_include.cpp create mode 100644 test_regress/t/t_compiler_include.h create mode 100755 test_regress/t/t_compiler_include.pl create mode 100644 test_regress/t/t_compiler_include.v diff --git a/bin/verilator b/bin/verilator index f9670ef8b..7b0d6d5d0 100755 --- a/bin/verilator +++ b/bin/verilator @@ -324,6 +324,7 @@ detailed descriptions of these arguments. --clk Mark specified signal as clock --no-clk Prevent marking specified signal as clock --compiler Tune for specified C++ compiler + --compiler-include Include additional header in the precompiled one --converge-limit Tune convergence settle time --coverage Enable all coverage --coverage-line Enable line coverage diff --git a/docs/guide/exe_verilator.rst b/docs/guide/exe_verilator.rst index 52ed6946e..a632b4c97 100644 --- a/docs/guide/exe_verilator.rst +++ b/docs/guide/exe_verilator.rst @@ -222,6 +222,13 @@ Summary: expressions into sub-expressions to avoid error C1009, and breaking deep blocks into functions to avoid error C1061. +.. option:: --compiler-include + + Specifies additional headers to be included in the final PCH header. + It is required to add them to this header, due to compilers' + limitation that allow only one precompiled header per compilation. + Use this instead of ::vlopt:`-CFLAGS` with `-include `. + .. option:: --converge-limit Rarely needed. Specifies the maximum number of runtime iterations diff --git a/src/V3EmitCPch.cpp b/src/V3EmitCPch.cpp index 0bcbcb8f7..b5c9ff4dc 100644 --- a/src/V3EmitCPch.cpp +++ b/src/V3EmitCPch.cpp @@ -57,6 +57,11 @@ public: of.puts("#include \"" + symClassName() + ".h\"\n"); of.puts("#include \"" + topClassName() + ".h\"\n"); + of.puts("\n// Additional include files added using '--compiler-include'\n"); + for (const string& filename : v3Global.opt.compilerIncludes()) { + of.puts("#include \"" + filename + "\"\n"); + } + of.putsEndGuard(); } diff --git a/src/V3Options.cpp b/src/V3Options.cpp index 89875ee01..c21639472 100644 --- a/src/V3Options.cpp +++ b/src/V3Options.cpp @@ -361,6 +361,9 @@ void V3Options::checkParameters() { void V3Options::addCppFile(const string& filename) { m_cppFiles.insert(filename); } void V3Options::addCFlags(const string& filename) { m_cFlags.push_back(filename); } +void V3Options::addCompilerIncludes(const string& filename) { + m_compilerIncludes.insert(filename); +} void V3Options::addLdLibs(const string& filename) { m_ldLibs.push_back(filename); } void V3Options::addMakeFlags(const string& filename) { m_makeFlags.push_back(filename); } void V3Options::addFuture(const string& flag) { m_futures.insert(flag); } @@ -1186,6 +1189,7 @@ void V3Options::parseOptsList(FileLine* fl, const string& optdir, int argc, << fl->warnMore() << "... Suggest 'clang', 'gcc', or 'msvc'"); } }); + DECL_OPTION("-compiler-include", CbVal, callStrSetter(&V3Options::addCompilerIncludes)); DECL_OPTION("-coverage", CbOnOff, [this](bool flag) { coverage(flag); }); DECL_OPTION("-converge-limit", Set, &m_convergeLimit); DECL_OPTION("-coverage-line", OnOff, &m_coverageLine); diff --git a/src/V3Options.h b/src/V3Options.h index a7b9f2268..cd822c692 100644 --- a/src/V3Options.h +++ b/src/V3Options.h @@ -203,6 +203,7 @@ private: V3StringList m_cFlags; // argument: user CFLAGS V3StringList m_ldLibs; // argument: user LDFLAGS V3StringList m_makeFlags; // argument: user MAKEFLAGS + V3StringSet m_compilerIncludes; // argument: user --compiler-include V3StringSet m_futures; // argument: -Wfuture- list V3StringSet m_future0s; // argument: -future list V3StringSet m_future1s; // argument: -future1 list @@ -435,6 +436,7 @@ public: // METHODS void addCppFile(const string& filename); void addCFlags(const string& filename); + void addCompilerIncludes(const string& filename); void addLdLibs(const string& filename); void addMakeFlags(const string& filename); void addLibraryFile(const string& filename); @@ -628,6 +630,7 @@ public: const V3StringSet& cppFiles() const { return m_cppFiles; } const V3StringList& cFlags() const { return m_cFlags; } + const V3StringSet& compilerIncludes() const { return m_compilerIncludes; } const V3StringList& ldLibs() const { return m_ldLibs; } const V3StringList& makeFlags() const { return m_makeFlags; } const V3StringSet& libraryFiles() const { return m_libraryFiles; } diff --git a/test_regress/t/t_compiler_include.cpp b/test_regress/t/t_compiler_include.cpp new file mode 100644 index 000000000..799a07812 --- /dev/null +++ b/test_regress/t/t_compiler_include.cpp @@ -0,0 +1,42 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// +// Copyright 2009-2024 by Antmicro. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License +// Version 2.0. +// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 +// +//************************************************************************* + +#include "svdpi.h" + +// These require the above. Comment prevents clang-format moving them +#include "TestCheck.h" + +//====================================================================== + +// clang-format off +#if defined(VERILATOR) +# include "Vt_compiler_include__Dpi.h" +#elif defined(VCS) +# include "../vc_hdrs.h" +#elif defined(NC) +# define NEED_EXTERNS +// #elif defined(MS) +// # define NEED_EXTERNS +#else +# error "Unknown simulator for DPI test" +#endif +// clang-format on + +#ifdef NEED_EXTERNS +extern "C" { +// If get ncsim: *F,NOFDPI: Function {foo} not found in default libdpi. +// Then probably forgot to list a function here. + +extern void dpii_add(int a, int b, int* out); +} +#endif + +void dpii_add(int a, int b, int* out) { *out = a + b; } diff --git a/test_regress/t/t_compiler_include.h b/test_regress/t/t_compiler_include.h new file mode 100644 index 000000000..fb2de4f93 --- /dev/null +++ b/test_regress/t/t_compiler_include.h @@ -0,0 +1,14 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// +// Copyright 2009-2024 by Antmicro. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License +// Version 2.0. +// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 +// +//************************************************************************* + +// no header guards to check if included once in pch file + +extern "C" int dpii_add_check(int actual, int expected) { return actual == expected; } diff --git a/test_regress/t/t_compiler_include.pl b/test_regress/t/t_compiler_include.pl new file mode 100755 index 000000000..464c257b5 --- /dev/null +++ b/test_regress/t/t_compiler_include.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; } +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2024 by Wilson Snyder. This program is free software; you +# can redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +scenarios(simulator => 1); + +compile( + v_flags2 => ["t/t_compiler_include.cpp"], + verilator_flags2 => ["-Wall -Wno-DECLFILENAME --compiler-include $Self->{t_dir}/t_compiler_include.h"], + ); + +execute( + check_finished => 1, + ); + +ok(1); +1; diff --git a/test_regress/t/t_compiler_include.v b/test_regress/t/t_compiler_include.v new file mode 100644 index 000000000..69e673621 --- /dev/null +++ b/test_regress/t/t_compiler_include.v @@ -0,0 +1,31 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// Copyright 2024 by Antmicro. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License +// Version 2.0. +// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +module t (/*AUTOARG*/); + int a = 123; + int b = 321; + int out; + + import "DPI-C" function void dpii_add + (int a, int b, ref int out); + import "DPI-C" function int dpii_add_check + (int actual, int expected); + + initial begin + dpii_add(a, b, out); + if (dpii_add_check(out, (a + b)) != 1) begin + $write("%%Error: Failure in DPI tests\n"); + $stop; + end + else begin + $write("*-* All Finished *-*\n"); + $finish; + end + end + +endmodule