From 247bfa9ac5ad8c9eee5e785a5fb4ea0b8dc63849 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Thu, 21 Nov 2019 21:37:00 +0100 Subject: [PATCH] Implemented #407 (variables in technology base path) The implementation uses extrapolation of strings in the "Expressions" framework. There is how: * $(tech_name) -> substituted by the technology name * $(tech_dir) -> substituted by the directory the technology file is stored in * $(tech_file) -> substituted by the absolute path to the tech file * $(appdata_path) -> substituted by KLayout's home directory (e.g. ~/.klayout) * $(env('X')) -> substituted by the environment variable $X --- src/db/db/dbTechnology.cc | 21 ++++++-- src/db/db/dbTechnology.h | 5 +- src/db/unit_tests/dbTechnologyTests.cc | 70 ++++++++++++++++++++++++++ src/db/unit_tests/unit_tests.pro | 3 +- 4 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 src/db/unit_tests/dbTechnologyTests.cc diff --git a/src/db/db/dbTechnology.cc b/src/db/db/dbTechnology.cc index a4acd4ce0..967758adc 100644 --- a/src/db/db/dbTechnology.cc +++ b/src/db/db/dbTechnology.cc @@ -24,6 +24,7 @@ #include "dbTechnology.h" #include "dbStream.h" #include "tlFileUtils.h" +#include "tlExpression.h" #include @@ -408,13 +409,24 @@ Technology::set_component (TechnologyComponent *component) } } +std::string +Technology::base_path () const +{ + tl::Eval expr; + expr.set_var ("tech_dir", m_default_base_path); + expr.set_var ("tech_file", m_lyt_file); + expr.set_var ("tech_name", name ()); + return expr.interpolate (m_explicit_base_path.empty () ? m_default_base_path : m_explicit_base_path); +} + std::string Technology::correct_path (const std::string &fp) const { - if (base_path ().empty ()) { + std::string bp = base_path (); + if (bp.empty ()) { return fp; } else { - return tl::relative_path (base_path (), fp); + return tl::relative_path (bp, fp); } } @@ -442,14 +454,15 @@ Technology::save (const std::string &fn) const std::string Technology::build_effective_path (const std::string &p) const { - if (p.empty () || base_path ().empty ()) { + std::string bp = base_path (); + if (p.empty () || bp.empty ()) { return p; } if (tl::is_absolute (p)) { return p; } else { - return tl::combine_path (base_path (), p); + return tl::combine_path (bp, p); } } diff --git a/src/db/db/dbTechnology.h b/src/db/db/dbTechnology.h index f2acec09e..e832abc2b 100644 --- a/src/db/db/dbTechnology.h +++ b/src/db/db/dbTechnology.h @@ -327,10 +327,7 @@ public: * a technology file was imported. The explicit one is the one that is specified * explicitly. */ - const std::string &base_path () const - { - return m_explicit_base_path.empty () ? m_default_base_path : m_explicit_base_path; - } + std::string base_path () const; /** * @brief Makes a file path relative to the base path if one is specified. diff --git a/src/db/unit_tests/dbTechnologyTests.cc b/src/db/unit_tests/dbTechnologyTests.cc new file mode 100644 index 000000000..b854e1c70 --- /dev/null +++ b/src/db/unit_tests/dbTechnologyTests.cc @@ -0,0 +1,70 @@ + +/* + + KLayout Layout Viewer + Copyright (C) 2006-2019 Matthias Koefferlein + + This program 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 2 of the License, or + (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + + +#include "dbTechnology.h" +#include "tlUnitTest.h" +#include "tlFileUtils.h" + +TEST(1_Basic) +{ + db::Technology tech ("name", "description"); + + EXPECT_EQ (tech.name (), "name"); + EXPECT_EQ (tech.description (), "description"); + + tech.set_name ("x"); + EXPECT_EQ (tech.name (), "x"); + + tech.set_description ("y"); + EXPECT_EQ (tech.description (), "y"); + + tech.set_grain_name ("a"); + EXPECT_EQ (tech.grain_name (), "a"); + + tech.set_grain_name ("a"); + EXPECT_EQ (tech.grain_name (), "a"); + + tech.set_dbu (2.5); + EXPECT_EQ (tech.dbu (), 2.5); +} + +TEST(2_BasePath) +{ + db::Technology tech ("x", "description"); + + tech.set_default_base_path ("def"); + EXPECT_EQ (tech.default_base_path (), "def"); + + tech.set_explicit_base_path ("$(tech_name)_plus"); + EXPECT_EQ (tech.explicit_base_path (), "$(tech_name)_plus"); + + EXPECT_EQ (tech.base_path (), "x_plus"); + EXPECT_EQ (tech.correct_path (tl::combine_path ("x_plus", "z")), "z"); + + tech.set_tech_file_path ("lyt"); + tech.set_explicit_base_path ("$(tech_file)_plus"); + EXPECT_EQ (tech.base_path (), "lyt_plus"); + + tech.set_explicit_base_path ("$(tech_dir)_plus"); + EXPECT_EQ (tech.base_path (), "def_plus"); +} diff --git a/src/db/unit_tests/unit_tests.pro b/src/db/unit_tests/unit_tests.pro index 112491536..aee620228 100644 --- a/src/db/unit_tests/unit_tests.pro +++ b/src/db/unit_tests/unit_tests.pro @@ -72,7 +72,8 @@ SOURCES = \ dbNetlistReaderTests.cc \ dbLayoutVsSchematicTests.cc \ dbLayoutQueryTests.cc \ - dbPolygonToolsTests.cc + dbPolygonToolsTests.cc \ + dbTechnologyTests.cc INCLUDEPATH += $$TL_INC $$DB_INC $$GSI_INC DEPENDPATH += $$TL_INC $$DB_INC $$GSI_INC