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
This commit is contained in:
Matthias Koefferlein 2019-11-21 21:37:00 +01:00
parent 72a3528e55
commit 247bfa9ac5
4 changed files with 90 additions and 9 deletions

View File

@ -24,6 +24,7 @@
#include "dbTechnology.h"
#include "dbStream.h"
#include "tlFileUtils.h"
#include "tlExpression.h"
#include <stdio.h>
@ -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);
}
}

View File

@ -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.

View File

@ -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");
}

View File

@ -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