From 20dba7464da39724b52b7c275b5bfc3754ba127b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Chmiel?= Date: Wed, 24 Jul 2024 12:40:39 +0200 Subject: [PATCH] Support`--compiler-include` headers in user-supplied cpp files (#5271) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bartłomiej Chmiel --- src/V3EmitMk.cpp | 11 ++++- test_regress/t/t_compiler_include.cpp | 41 +++++++------------ test_regress/t/t_compiler_include.h | 4 +- test_regress/t/t_compiler_include.pl | 5 ++- test_regress/t/t_compiler_include.v | 24 +---------- test_regress/t/t_compiler_include_dpi.cpp | 15 +++++++ test_regress/t/t_compiler_include_dpi.h | 15 +++++++ test_regress/t/t_compiler_include_dpi.pl | 24 +++++++++++ test_regress/t/t_compiler_include_dpi.v | 31 ++++++++++++++ .../t/t_compiler_include_dpi_split.pl | 24 +++++++++++ test_regress/t/t_compiler_include_split.pl | 25 +++++++++++ 11 files changed, 165 insertions(+), 54 deletions(-) create mode 100644 test_regress/t/t_compiler_include_dpi.cpp create mode 100644 test_regress/t/t_compiler_include_dpi.h create mode 100755 test_regress/t/t_compiler_include_dpi.pl create mode 100644 test_regress/t/t_compiler_include_dpi.v create mode 100755 test_regress/t/t_compiler_include_dpi_split.pl create mode 100755 test_regress/t/t_compiler_include_split.pl diff --git a/src/V3EmitMk.cpp b/src/V3EmitMk.cpp index 82c2b88a5..411d8f336 100644 --- a/src/V3EmitMk.cpp +++ b/src/V3EmitMk.cpp @@ -236,11 +236,18 @@ public: of.puts("\n"); } + const string compilerIncludePch + = v3Global.opt.compilerIncludes().empty() ? "" : "$(VK_PCH_H).fast.gch"; + const string compilerIncludeFlag + = v3Global.opt.compilerIncludes().empty() ? "" : "$(VK_PCH_I_FAST)"; for (const string& cppfile : cppFiles) { const string basename = V3Os::filenameNonDirExt(cppfile); // NOLINTNEXTLINE(performance-inefficient-string-concatenation) - of.puts(basename + ".o: " + cppfile + "\n"); - of.puts("\t$(OBJCACHE) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OPT_FAST) -c -o $@ $<\n"); + of.puts(basename + ".o: " + cppfile + " " + compilerIncludePch + "\n"); + + // NOLINTNEXTLINE(performance-inefficient-string-concatenation) + of.puts("\t$(OBJCACHE) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OPT_FAST) " + + compilerIncludeFlag + " -c -o $@ $<\n"); } if (v3Global.opt.exe()) { diff --git a/test_regress/t/t_compiler_include.cpp b/test_regress/t/t_compiler_include.cpp index 799a07812..7c5792f66 100644 --- a/test_regress/t/t_compiler_include.cpp +++ b/test_regress/t/t_compiler_include.cpp @@ -1,7 +1,7 @@ // -*- mode: C++; c-file-style: "cc-mode" -*- //************************************************************************* // -// Copyright 2009-2024 by Antmicro. This program is free software; you can +// 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. @@ -9,34 +9,23 @@ // //************************************************************************* -#include "svdpi.h" +// t_compiler_include.h is implicitly included by `--compiler-include` -// These require the above. Comment prevents clang-format moving them -#include "TestCheck.h" +#include +#include VM_PREFIX_INCLUDE -//====================================================================== +int main() { + Verilated::debug(0); -// 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 + VM_PREFIX* topp = new VM_PREFIX; + topp->in = 123; + topp->eval(); -#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. + if (ext_equal(topp->in, topp->out)) + VL_PRINTF("*-* All Finished *-*\n"); + else + VL_PRINTF("in (%d) != out (%d)\n", topp->in, topp->out); -extern void dpii_add(int a, int b, int* out); + topp->final(); + VL_DO_DANGLING(delete topp, topp); } -#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 index fb2de4f93..7a18a8501 100644 --- a/test_regress/t/t_compiler_include.h +++ b/test_regress/t/t_compiler_include.h @@ -1,7 +1,7 @@ // -*- mode: C++; c-file-style: "cc-mode" -*- //************************************************************************* // -// Copyright 2009-2024 by Antmicro. This program is free software; you can +// 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. @@ -11,4 +11,4 @@ // no header guards to check if included once in pch file -extern "C" int dpii_add_check(int actual, int expected) { return actual == expected; } +static inline int ext_equal(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 index 464c257b5..e03b9971f 100755 --- a/test_regress/t/t_compiler_include.pl +++ b/test_regress/t/t_compiler_include.pl @@ -11,8 +11,9 @@ if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); di 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"], + make_top_shell => 0, + make_main => 0, + verilator_flags2 => ["--exe", "$Self->{t_dir}/$Self->{name}.cpp", "--compiler-include $Self->{t_dir}/t_compiler_include.h", "--output-split 0"], ); execute( diff --git a/test_regress/t/t_compiler_include.v b/test_regress/t/t_compiler_include.v index 69e673621..a6d76d187 100644 --- a/test_regress/t/t_compiler_include.v +++ b/test_regress/t/t_compiler_include.v @@ -6,26 +6,6 @@ // 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 - +module t (input logic[31:0] in, output logic[31:0] out); + assign out = in; endmodule diff --git a/test_regress/t/t_compiler_include_dpi.cpp b/test_regress/t/t_compiler_include_dpi.cpp new file mode 100644 index 000000000..e3ed99be7 --- /dev/null +++ b/test_regress/t/t_compiler_include_dpi.cpp @@ -0,0 +1,15 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// +// 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 +// +//************************************************************************* + +// t_compiler_include.h is implicitly included by `--compiler-include` + +int dpii_add_check(int actual, int expected) { return actual == expected; } +void dpii_add(int a, int b, int* out) { *out = a + b; } diff --git a/test_regress/t/t_compiler_include_dpi.h b/test_regress/t/t_compiler_include_dpi.h new file mode 100644 index 000000000..a985d65a6 --- /dev/null +++ b/test_regress/t/t_compiler_include_dpi.h @@ -0,0 +1,15 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// +// 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 +// +//************************************************************************* + +// no header guards to check if included once in pch file + +extern "C" int dpii_add_check(int actual, int expected); +extern "C" void dpii_add(int a, int b, int* out); diff --git a/test_regress/t/t_compiler_include_dpi.pl b/test_regress/t/t_compiler_include_dpi.pl new file mode 100755 index 000000000..048b6fdea --- /dev/null +++ b/test_regress/t/t_compiler_include_dpi.pl @@ -0,0 +1,24 @@ +#!/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); +top_filename("t/t_compiler_include_dpi.v"); + +compile( + v_flags2 => ["t/t_compiler_include_dpi.cpp"], + verilator_flags2 => ["-Wall -Wno-DECLFILENAME --compiler-include $Self->{t_dir}/t_compiler_include_dpi.h --output-split 0"], + ); + +execute( + check_finished => 1, + ); + +ok(1); +1; diff --git a/test_regress/t/t_compiler_include_dpi.v b/test_regress/t/t_compiler_include_dpi.v new file mode 100644 index 000000000..69e673621 --- /dev/null +++ b/test_regress/t/t_compiler_include_dpi.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 diff --git a/test_regress/t/t_compiler_include_dpi_split.pl b/test_regress/t/t_compiler_include_dpi_split.pl new file mode 100755 index 000000000..35ac0a040 --- /dev/null +++ b/test_regress/t/t_compiler_include_dpi_split.pl @@ -0,0 +1,24 @@ +#!/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); +top_filename("t/t_compiler_include_dpi.v"); + +compile( + v_flags2 => ["t/t_compiler_include_dpi.cpp"], + verilator_flags2 => ["-Wall -Wno-DECLFILENAME --compiler-include $Self->{t_dir}/t_compiler_include_dpi.h --output-split 1"], + ); + +execute( + check_finished => 1, + ); + +ok(1); +1; diff --git a/test_regress/t/t_compiler_include_split.pl b/test_regress/t/t_compiler_include_split.pl new file mode 100755 index 000000000..cd4f8377b --- /dev/null +++ b/test_regress/t/t_compiler_include_split.pl @@ -0,0 +1,25 @@ +#!/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); +top_filename("t/t_compiler_include.v"); + +compile( + make_top_shell => 0, + make_main => 0, + verilator_flags2 => ["--exe", "$Self->{t_dir}/t_compiler_include.cpp", "--compiler-include $Self->{t_dir}/t_compiler_include.h", "--output-split 1"], + ); + +execute( + check_finished => 1, + ); + +ok(1); +1;