OpenASIP  2.0
Environment.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2022 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file Environment.cc
26  *
27  * Definition of Environment class.
28  *
29  * @author Atte Oksman 2003 (oksman-no.spam-cs.tut.fi)
30  * @author Viljami Korhonen 2007 (viljami.korhonen-no.spam-tut.fi)
31  * @author Pekka Jääskeläinen 2007-2020
32  *
33  * @note reviewed 19 May 2004 by ao, jn, ml, am
34  * @note rating: red
35  */
36 
37 #include <vector>
38 #include <string>
39 
40 #include "Environment.hh"
41 #include "tce_config.h"
42 #include "FileSystem.hh"
43 #include "StringTools.hh"
44 #include "VectorTools.hh"
45 #include "MapTools.hh"
46 #include "TCEString.hh"
47 #include "Application.hh"
48 
49 using std::vector;
50 using std::string;
51 
52 // initializing the static class member variables
53 const string Environment::SCHEMA_DIR_NAME_ = "data";
54 const string Environment::DATA_DIR_NAME_ = "data";
55 const string Environment::BITMAPS_DIR_NAME_ = "bitmaps";
56 const string Environment::MAN_DIR_NAME_ = "man";
57 const string Environment::CONF_DIR_NAME_ = "conf";
58 const string Environment::ICON_DIR_NAME_ = "icons";
59 const string Environment::ERROR_LOG_FILE_NAME_ = "error_log.log";
60 bool Environment::initialized_ = false;
61 bool Environment::developerMode_ = false;
62 vector<string> Environment::schemaPaths_;
63 vector<string> Environment::dataPaths_;
64 vector<string> Environment::bitmapsPaths_;
65 vector<string> Environment::manPaths_;
66 vector<string> Environment::iconPaths_;
67 vector<string> Environment::errorPaths_;
68 vector<string> Environment::pathsToUse_;
73 string Environment::newManFileDir_ = "";
74 string Environment::newConfFile_ = "";
76 string Environment::simTraceDir_ = "";
77 
78 const string Environment::PDF_MANUAL_INSTALLED = "manual/OpenASIP_manual.pdf";
79 const string Environment::PDF_MANUAL_SRC = "manual/OpenASIP_manual.pdf";
80 
81 const string Environment::MINIMAL_ADF_INSTALLED = "data/mach/minimal.adf";
82 const string Environment::MINIMAL_ADF_SRC = "data/mach/minimal.adf";
83 
84 // macro definitions (maybe relocated later to config.h)
85 #define INSTALLATION_DIR "/share/openasip/"
86 #define DS TCEString(FileSystem::DIRECTORY_SEPARATOR)
87 
88 
89 /**
90  * Initializes the program environment.
91  *
92  * Initializes all of the source directories the program needs.
93  */
94 void
96 
97  if (initialized_) {
98  return;
99  }
100 
102  Environment::environmentVariable("TCE_DEVEL_MODE") == "1";
103 
104  errorPaths_.push_back(
106  FileSystem::DIRECTORY_SEPARATOR + string(".openasip"));
107 
108  if (developerMode_) {
109  schemaPaths_.push_back(string(TCE_SRC_ROOT));
110  iconPaths_.push_back(string(TCE_SRC_ROOT));
111  dataPaths_.push_back(string(TCE_SRC_ROOT));
112  bitmapsPaths_.push_back(string(TCE_SRC_ROOT));
113  manPaths_.push_back(
114  string(TCE_SRC_ROOT) +
115  FileSystem::DIRECTORY_SEPARATOR + string("doc"));
116  } else {
117  string instDir =
119  schemaPaths_.push_back(instDir);
120  iconPaths_.push_back(instDir);
121  dataPaths_.push_back(instDir);
122  bitmapsPaths_.push_back(instDir);
123  manPaths_.push_back(instDir);
124  }
125 
126  string traceEnv = environmentVariable("TTASIM_TRACE_DIR");
127  if (traceEnv != "") {
128  if (!FileSystem::fileExists(traceEnv)) {
129  if (!FileSystem::createDirectory(traceEnv)) {
130  traceEnv = "";
131  }
132  }
133  }
134  simTraceDir_ = traceEnv;
135 
136  initialized_ = true;
137 }
138 
139 
140 /**
141  * Tries to locate the directory where the XML Schema file is stored.
142  *
143  * Returns the first absolute path of the directory where the file is found.
144  * If directory for new data file is given, it is created and returned.
145  * If the directory can not be created, an empty string is returned.
146  *
147  * @param prog The program whose Schema is searched.
148  * @return The path of the Schema directory.
149  */
150 string
151 Environment::schemaDirPath(const std::string& prog) {
152  initialize();
153  string path = SCHEMA_DIR_NAME_ + FileSystem::DIRECTORY_SEPARATOR + prog;
154  pathsToUse_.assign(schemaPaths_.begin(), schemaPaths_.end());
155  string schemaPath = pathTo(path);
156  if (schemaPath == "" && newSchemaFileDir_ != "") {
158  return newSchemaFileDir_;
159  }
160  }
161  return schemaPath;
162 }
163 
164 
165 /**
166  * Tries to locate the directory where the application data is stored.
167  *
168  * Returns the first absolute path of the directory where the data is found.
169  * If directory is not found and directory for new data file is given, it
170  * is created. If creation fails, an empty string is returned.
171  *
172  * @param prog The program whose data directory is searched.
173  * @return The path of data directory or an empty string.
174  */
175 string
176 Environment::dataDirPath(const std::string& prog) {
177  initialize();
178  string path = DATA_DIR_NAME_ + FileSystem::DIRECTORY_SEPARATOR + prog;
179  pathsToUse_.assign(dataPaths_.begin(), dataPaths_.end());
180  string dataPath = pathTo(path);
181  if (dataPath == "" && newDataFileDir_ != "") {
183  return newDataFileDir_;
184  }
185  }
186  return dataPath;
187 }
188 
189 
190 /**
191  * Tries to locate the directory where the application bitmaps are stored.
192  *
193  * Returns the first absolute path of the directory where the file is found.
194  * If bitmaps directory is not found and a new directory is given, it is
195  * created. If creation fails, an empty string is returned.
196  *
197  * @param prog The program whose bitmaps directory are searched.
198  * @return The path of the bitmaps directory.
199  */
200 string
201 Environment::bitmapsDirPath(const std::string& prog) {
202  initialize();
205  pathsToUse_.assign(bitmapsPaths_.begin(), bitmapsPaths_.end());
206  string bitmapsPath = pathTo(path);
207  if (bitmapsPath == "" && newBitmapsFileDir_ != "") {
209  return newBitmapsFileDir_;
210  }
211  }
212  return bitmapsPath;
213 }
214 
215 /**
216  * Tries to locate the directory where common toolbar icon files are stored.
217  *
218  * Returns the first absolute path of the directory where the file is found.
219  * If icons directory is not found and a new directory is given, it is
220  * created. If creation fails, an empty string is returned.
221  *
222  * @return The path of the icons directory.
223  */
224 string
226  initialize();
229  pathsToUse_.assign(bitmapsPaths_.begin(), bitmapsPaths_.end());
230  string iconPath = pathTo(path);
231  if (iconPath == "" && newIconFileDir_ != "") {
233  return newIconFileDir_;
234  }
235  }
236  return iconPath;
237 }
238 
239 /**
240  * Tries to locate the directory where the system's manual is stored.
241  *
242  * Returns the first absolute path of the directory where the file is found.
243  * If no directory is found and a new directory for manual is given, it is
244  * created. If creation fails, an empty string is returned.
245  *
246  * @param prog The program whose manual directory is searched.
247  * @return The path of the manual directory.
248  */
249 string
250 Environment::manDirPath(const std::string& prog) {
251  initialize();
252  string path = MAN_DIR_NAME_ + FileSystem::DIRECTORY_SEPARATOR + prog;
253  pathsToUse_.assign(manPaths_.begin(), manPaths_.end());
254  string manPath = pathTo(path);
255  if (manPath == "" && newManFileDir_ != "") {
257  return newManFileDir_;
258  }
259  }
260  return manPath;
261 }
262 
263 
264 /**
265  * Tries to locate the application's configuration file for
266  * reading.
267  *
268  * First tries to find one in the user's homedir, if not found,
269  * uses the installed one (with defaults). Note: when writing
270  * (overriding) configuration files, one should call userConfDirPath()
271  * instead!
272  *
273  * @param fileName The name of the configuration file.
274  * @return The path of the configuration file.
275  */
276 TCEString
277 Environment::confPath(const std::string& fileName) {
278  initialize();
279 
280  TCEString userConf =
281  FileSystem::homeDirectory() + DS + ".openasip" + DS + fileName;
282  TCEString srcDirConf =
283  TCEString(TCE_SRC_ROOT) + DS + CONF_DIR_NAME_ + DS + fileName;
284 
285  TCEString installedConf =
287  DS + CONF_DIR_NAME_ + DS + fileName;
288  if (FileSystem::fileExists(userConf)) {
289  return userConf;
290  }
291 
292  if (Environment::developerMode() && FileSystem::fileExists(srcDirConf)) {
293  return srcDirConf;
294  } else if (FileSystem::fileExists(installedConf)) {
295  return installedConf;
296  }
297 
298  FileSystem::createFile(userConf);
299  return userConf;
300 }
301 
302 TCEString
303 Environment::userConfPath(const std::string& fileName) {
304  TCEString userConf =
305  FileSystem::homeDirectory() + DS + ".openasip" + DS + fileName;
306  if (!FileSystem::fileExists(userConf)) {
307  FileSystem::createFile(userConf);
308  }
309  return userConf;
310 }
311 
312 /**
313  * Tries to locate the error log file on system.
314  *
315  * Returns the first absolute path where the file is found. If the file is
316  * not found and directory for the error log file is given, the directory
317  * and the error log file is created. If creation fails, an empty string is
318  * returned.
319  *
320  * @return The path of the error log file.
321  */
322 string
324  initialize();
325  pathsToUse_.assign(errorPaths_.begin(), errorPaths_.end());
326  string errorPath = pathTo(ERROR_LOG_FILE_NAME_);
327  if (errorPath == "" && newErrorLogFileDir_ != "") {
329  string filePath = newErrorLogFileDir_ +
331  if (FileSystem::createFile(filePath)) {
332  return filePath;
333  }
334  }
335  }
336  return errorPath;
337 }
338 
339 /**
340  * Returns path where installable tce include headers reside.
341  *
342  * The list returned contains both post-installation paths and the paths to
343  * the original header files found in the source tree.
344  *
345  * @return Paths where tce installable headers files reside.
346  */
347 std::vector<std::string>
349  vector<string> includes;
350  const std::string ROOT = string(TCE_SRC_ROOT);
351  const std::string BASE = ROOT + DS + "src" + DS + "base";
352  const std::string APPLIBS = ROOT + DS + "src" + DS + "applibs";
353  const std::string BLD = string(TCE_BLD_ROOT);
354 
356  includes.push_back(ROOT);
357  includes.push_back(ROOT + DS + "src" + DS + "tools");
358  includes.push_back(BASE + DS + "osal");
359  includes.push_back(BASE + DS + "memory");
360  includes.push_back(BASE + DS + "bem");
361  includes.push_back(BASE + DS + "Graph");
362  includes.push_back(BASE + DS + "idf");
363  includes.push_back(BASE + DS + "mach");
364  includes.push_back(BASE + DS + "program");
365  includes.push_back(BASE + DS + "tpef");
366  includes.push_back(BASE + DS + "umach");
367  includes.push_back(APPLIBS + DS + "Simulator");
368  includes.push_back(APPLIBS + DS + "mach");
369  includes.push_back(APPLIBS + DS + "FSA");
370  includes.push_back(TCE_BLD_ROOT);
371  } else {
372  includes.push_back(Application::installationDir() + DS + "include");
373  }
374 
375  return includes;
376 }
377 
378 /**
379  * Returns the include dir needed to build base opset in compiling the
380  * distributed version of TCE
381  *
382  */
383 vector<string>
385  string BASE = TCE_SRC_ROOT + DS + "src" + DS + "base";
386 
387  vector<string> includePaths;
388  includePaths.push_back(BASE + DS + "osal ");
389  includePaths.push_back(BASE + DS + "tpef ");
390  includePaths.push_back(BASE + DS + "memory ");
391  includePaths.push_back(TCE_SRC_ROOT + DS + "src" + DS + "tools");
392  includePaths.push_back(TCE_SRC_ROOT);
393 
394  return includePaths;
395 }
396 
397 /**
398  * Returns the value of the given environment variable.
399  *
400  * An empty string is returned if the variable does not exist.
401  *
402  * @param variable The environment variable whose value is required.
403  * @return The value of the environment variable.
404  */
405 string
406 Environment::environmentVariable(const std::string& variable) {
407 
408  char* varValue = getenv(variable.c_str());
409  if (varValue == NULL) {
410  return "";
411  }
412 
413  return varValue;
414 }
415 
416 
417 /**
418  * Constructor.
419  */
421 }
422 
423 
424 /**
425  * Destructor.
426  */
428 }
429 
430 
431 /**
432  * Tries to locate the given directory or file on system.
433  *
434  * Returns the first absolute path of the directory if the directory/file
435  * is found. In case the directory/file is not found in any of the search
436  * paths, returns an empty string.
437  *
438  * @fixme this is not thread safe due to the use of pathsToUse_ member
439  * variable for data passing!
440  * @param name The name of the searched directory/file.
441  * @return The path of the directory/file.
442  */
443 string
444 Environment::pathTo(const std::string& name) {
445 
446  // test the stored paths and try to open the file
447  for (unsigned int i = 0; i < pathsToUse_.size(); ++i) {
448  string path =
450 
451  if (FileSystem::fileExists(path)) {
452  return path;
453  }
454  }
455 
456  return "";
457 }
458 
459 /**
460  * Sets the value for new schema file directory.
461  *
462  * @param path The path of new schema file directory.
463  */
464 void
465 Environment::setNewSchemaFileDir(const std::string& path) {
466  newSchemaFileDir_ = path;
467 }
468 
469 /**
470  * Sets the value for new data file directory
471  *
472  * @param path The path of new data file directory.
473  */
474 void
475 Environment::setNewDataFileDir(const std::string& path) {
476  newDataFileDir_ = path;
477 }
478 
479 /**
480  * Sets the value for new bitmaps file directory.
481  *
482  * @param path The path for new bitmaps directory.
483  */
484 void
485 Environment::setNewBitmapsFileDir(const std::string& path) {
486  newBitmapsFileDir_ = path;
487 }
488 
489 /**
490  * Sets the path for new manual file directory.
491  *
492  * @param path The path of new manual file directory.
493  */
494 void
495 Environment::setNewManFileDir(const std::string& path) {
496  newManFileDir_ = path;
497 }
498 
499 /**
500  * Sets the path for new error log file directory.
501  *
502  * @param path The path of new error log file directory.
503  */
504 void
505 Environment::setNewErrorLogFileDir(const std::string& path) {
506  newErrorLogFileDir_ = path;
507 }
508 
509 /**
510  * Returns the paths in which operation set files are searched.
511  *
512  * The paths are returned in the order of preference. The user
513  * can override global operation definitions by redefining them in
514  * a local search path.
515  *
516  * @return The paths in which operations are searched.
517  */
518 vector<string>
520 
521  vector<string> paths;
522 
523  // ./data
524  string data = FileSystem::currentWorkingDir() + DS + "data";
525 
526  // tce/opset/base -- these are needed during newlib build,
527  // specifically STDOUT is needed to build puts() correctly.
528  string src = string(TCE_BLD_ROOT) + DS + "opset" + DS + "base";
529 
530  // default path for predefined and "standard" operations
531  string basePath =
532  Application::installationDir() + string(INSTALLATION_DIR) + "opset" +
533  DS + "base";
534  // path for user-local custom operations
535  string userPath =
536  FileSystem::homeDirectory() + DS + ".openasip" + DS + "opset" +
537  DS + "custom";
538  // path for system-wide, shared custom operations
539  string customPath =
540  Application::installationDir() + string(INSTALLATION_DIR) + "opset" +
541  DS + "custom";
542  // search path taken from environment
543  string envPath = Environment::environmentVariable("TCE_OSAL_PATH");
544  envPath = FileSystem::absolutePathOf(envPath);
545  // as a last resort fall-back, operations are always searched in
546  // in current working directory
547  string cwd = FileSystem::currentWorkingDir();
548 
549  if (envPath != "")
550  VectorTools::insertUnique(paths, envPath);
551  VectorTools::insertUnique(paths, cwd);
552  VectorTools::insertUnique(paths, data);
554  VectorTools::insertUnique(paths, src);
555  } else {
556  VectorTools::insertUnique(paths, userPath);
557  VectorTools::insertUnique(paths, customPath);
558  VectorTools::insertUnique(paths, basePath);
559  }
560 
561  return paths;
562 }
563 
564 /**
565  * Returns the paths in which code compressor plugins are searched.
566  *
567  * @param libraryPathsOnly When set to true, excludes CWD from search paths.
568  * @return The paths in which code compressor plugins are searched.
569  */
570 vector<string>
571 Environment::codeCompressorPaths(bool libraryPathsOnly) {
572 
573  vector<string> paths;
574 
576  string path = string(TCE_SRC_ROOT) + DS + "compressors";
577  paths.push_back(path);
578  } else {
579  string base = Application::installationDir() + string(INSTALLATION_DIR) +
580  "codecompressors" + DS + "base";
581  string custom = Application::installationDir() + string(INSTALLATION_DIR)+
582  "codecompressors" + DS + "custom";
583  string personal = FileSystem::homeDirectory() + DS + ".openasip" + DS +
584  "codecompressors" + DS + "custom";
585 
586  paths.push_back(base);
587  paths.push_back(personal);
588  paths.push_back(custom);
589  }
590  string cwd = FileSystem::currentWorkingDir();
591  if (!libraryPathsOnly) paths.push_back(cwd);
592  return paths;
593 }
594 
595 /**
596  * Returns the paths in which scheduler plugins are searched.
597  *
598  * @return The paths in which scheduler plugins are searched.
599  */
600 vector<string>
602 
603  vector<string> paths;
604 
605  string base = Application::installationDir() + string(INSTALLATION_DIR) +
606  "scheduler" + DS + "passes";
607 
608  string personal = FileSystem::homeDirectory() + DS + ".openasip" + DS +
609  "scheduler" + DS + "passes";
610 
611  string srcBase = string(TCE_SRC_ROOT) + DS + "scheduler" + DS + "passes";
612 
613  string cwd = FileSystem::currentWorkingDir();
614 
616  // first find from the src tree to make the system tests etc.
617  // to test the plugins in source base, not the installed plugins
618  paths.push_back(srcBase);
619  } else {
620  paths.push_back(base);
621  paths.push_back(personal);
622  }
623  paths.push_back(cwd);
624 
625  return paths;
626 }
627 
628 /**
629  * Returns the paths in which IC/decoder plugins are searched.
630  *
631  * @param libraryPathsOnly Set to true excludes CWD from search paths.
632  * @return The paths in which IC/decoder plugins are searched.
633  */
634 vector<string>
635 Environment::icDecoderPluginPaths(bool libraryPathsOnly) {
636 
637  vector<string> paths;
638 
639  string base = Application::installationDir() + string(INSTALLATION_DIR) +
640  "icdecoder_plugins" + DS + "base";
641  string custom = Application::installationDir() + string(INSTALLATION_DIR)+
642  "icdecoder_plugins" + DS + "custom";
643  string personal = FileSystem::homeDirectory() + DS + ".openasip" + DS +
644  "icdecoder_plugins";
645  string cwd = FileSystem::currentWorkingDir();
646  string data = cwd + DS + "data";
648  // first find from the src tree to make the system tests etc.
649  // to test the plugins in source base, not the installed plugins
650  string path = string(TCE_SRC_ROOT) + DS + "icdecoder_plugins";
651  paths.push_back(path);
652  } else {
653  paths.push_back(base);
654  paths.push_back(personal);
655  paths.push_back(custom);
656  }
657  if (!libraryPathsOnly) paths.push_back(cwd);
658  if (!libraryPathsOnly) paths.push_back(data);
659  return paths;
660 }
661 
662 
663 /**
664  * Returns the paths in which hardware modules are searched.
665  *
666  * @return The paths in which hardware modules are searched.
667  */
668 std::vector<std::string>
670  vector<string> paths;
671  paths.push_back(FileSystem::currentWorkingDir());
672  return paths;
673 }
674 
675 
676 /**
677  * Returns the paths in which hardware databases are searched.
678  *
679  * @param libraryPathsOnly When set to true, excludes CWD from search paths.
680  * @return The paths in which hardware databases are searched.
681  */
682 std::vector<std::string>
683 Environment::hdbPaths(bool libraryPathsOnly) {
684 
685  vector<string> paths;
687  string srcBase = string(TCE_SRC_ROOT) + DS + "hdb";
688  paths.push_back(srcBase);
689  } else {
690  string instBase = Application::installationDir() +
691  string(INSTALLATION_DIR) + "hdb";
692  paths.push_back(instBase);
693  }
694  if (!libraryPathsOnly) {
695  string cwd = FileSystem::currentWorkingDir();
696  paths.push_back(cwd);
697  paths.push_back(cwd + DS + "data");
698  }
699  return paths;
700 }
701 
702 /**
703  * Shortens the given HDB path by replacing source tree, install directory or
704  * current working directory paths with human-readable aliases.
705  */
706 TCEString
708  string sourceRoot = string(TCE_SRC_ROOT) + DS;
709  string installRoot = Application::installationDir() + DS;
710  string cwd = FileSystem::currentWorkingDir() + DS;
711 
712  TCEString shortPath = hdbPath;
713  if (hdbPath.startsWith(sourceRoot)) {
714  int rootPathLength = sourceRoot.length();
715  shortPath = "<source tree>" + DS + hdbPath.substr(rootPathLength);
716  } else if (hdbPath.startsWith(installRoot)) {
717  int installDirLength = installRoot.length();
718  shortPath = "<install directory>" + DS +
719  hdbPath.substr(installDirLength);
720  } else if (hdbPath.startsWith(cwd)) {
721  int cwdLength = cwd.length();
722  shortPath = "." + DS + hdbPath.substr(cwdLength);
723  }
724  return shortPath;
725 }
726 
727 /**
728  * Undoes the shortening done by shortHDBPath
729  */
730 TCEString
732  string sourceRoot = string("<source tree>") + DS;
733  string installRoot = string("<install directory>") + DS;
734  string cwd = string(".") + DS;
735 
736  TCEString longPath = hdbPath;
737  if (hdbPath.startsWith(sourceRoot)) {
738  int rootPathLength = sourceRoot.length();
739  longPath = string(TCE_SRC_ROOT) + DS + hdbPath.substr(rootPathLength);
740  } else if (hdbPath.startsWith(installRoot)) {
741  int installDirLength = installRoot.length();
742  longPath = Application::installationDir() + DS +
743  hdbPath.substr(installDirLength);
744  } else if (hdbPath.startsWith(cwd)) {
745  int cwdLength = cwd.length();
746  longPath = FileSystem::currentWorkingDir() + DS +
747  hdbPath.substr(cwdLength);
748  }
749  return longPath;
750 }
751 
752 /**
753  * Returns the paths in which VHDL files are searched for.
754  *
755  * @param hdbPath The path where the HDB file is, to be added to the
756  * search paths.
757  * @return The paths in which VHDL files are searched for.
758  */
759 std::vector<std::string>
760 Environment::vhdlPaths(const std::string& hdbPath) {
761 
762  vector<string> paths;
763 
764  // Drop the name of hdb file from hdb path
765  string hdbDir = FileSystem::directoryOfPath(hdbPath);
766  paths.push_back(hdbDir);
767  paths.push_back(hdbDir + DS + "vhdl");
768 
770  string srcBase = string(TCE_SRC_ROOT) + DS + "hdb";
771  paths.push_back(srcBase);
772  paths.push_back(srcBase + DS + "vhdl");
773  } else {
774  string srcBase = Application::installationDir() +
775  string(INSTALLATION_DIR) + "hdb";
776  paths.push_back(srcBase);
777  paths.push_back(srcBase + DS + "vhdl");
778  }
779  string cwd = FileSystem::currentWorkingDir();
780  paths.push_back(cwd);
781  paths.push_back(cwd + DS + "data");
782  return paths;
783 }
784 
785 
786 /**
787  * Returns the paths in which decompressor module definitions are searhed.
788  *
789  * @param libraryPathsOnly When set to true, excludes CWD from search paths.
790  * @return The paths in which decompressor module definitions are searched.
791  */
792 std::vector<std::string>
793 Environment::decompressorPaths(bool libraryPathsOnly) {
794  vector<string> paths;
795  if (!libraryPathsOnly) {
796  paths.push_back(FileSystem::currentWorkingDir());
797  paths.push_back(FileSystem::currentWorkingDir() + DS + "data");
798  }
799  return paths;
800 }
801 
802 
803 /**
804  * Returns the paths in which explorer plugins are searched.
805  *
806  * @return The paths in which explorer plugins are searched.
807  */
808 vector<string>
810 
811  vector<string> paths;
812 
814  string srcBase = string(TCE_SRC_ROOT) + DS + "explorer";
815  paths.push_back(srcBase);
816  } else {
817  string base = Application::installationDir() + string(INSTALLATION_DIR) +
818  "explorer" + DS + "base";
819  paths.push_back(base);
820 
821  string personal = FileSystem::homeDirectory() + DS + ".openasip" + DS +
822  "explorer";
823  paths.push_back(personal);
824  }
825 
826  string cwd = FileSystem::currentWorkingDir();
827  paths.push_back(cwd);
828 
829  string data = FileSystem::currentWorkingDir() + DS + "data";
830  paths.push_back(data);
831 
832  return paths;
833 }
834 
835 
836 /**
837  * Returns the paths in which estimator plugins are searched.
838  *
839  * @return The paths in which estimator plugins are searched.
840  */
841 vector<string>
843 
844  vector<string> paths;
845  string base = Application::installationDir() + string(INSTALLATION_DIR) +
846  "cost_estimator_plugins";
847  string baseRF = Application::installationDir() + string(INSTALLATION_DIR) +
848  "cost_estimator_plugins/rf";
849  string baseFU = Application::installationDir() + string(INSTALLATION_DIR) +
850  "cost_estimator_plugins/fu";
851  string personal = FileSystem::homeDirectory() + DS + ".openasip" + DS +
852  "cost_estimator_plugins";
853  string custom = string(TCE_SRC_ROOT) + DS + "cost_estimator_plugins";
854  string cwd = FileSystem::currentWorkingDir();
855  string data = FileSystem::currentWorkingDir() + DS + "data";
856 
858  paths.push_back(custom);
859  } else {
860  paths.push_back(base);
861  paths.push_back(baseRF);
862  paths.push_back(baseFU);
863  }
864  paths.push_back(personal);
865  paths.push_back(cwd);
866  paths.push_back(data);
867  return paths;
868 }
869 
870 
871 /**
872  * Returns full path to the TCE .pdf manual.
873  *
874  * @return Full path to the TCE manual pdf.
875  */
876 string
878 
880  std::string srcPath =
881  string(TCE_SRC_ROOT) + FileSystem::DIRECTORY_SEPARATOR +
883  assert(
884  FileSystem::fileExists(srcPath) &&
885  "Installation broken, minimal.adf not found");
886  return srcPath;
887  } else {
888  std::string path = Application::installationDir() +
890 
891  if (!FileSystem::fileExists(path))
892  debugLog("Installation broken, manual not found");
893  return path;
894  }
895 }
896 
897 /**
898  * Returns the full path to the minimal.adf.
899  */
900 string
902 
904  std::string srcPath =
905  string(TCE_SRC_ROOT) + FileSystem::DIRECTORY_SEPARATOR +
907 
908  assert(
909  FileSystem::fileExists(srcPath) &&
910  "Installation broken, minimal.adf not found");
911  return srcPath;
912  } else {
913  std::string path = Application::installationDir() +
915 
916  assert(
917  FileSystem::fileExists(path) &&
918  "Installation broken, minimal.adf not found");
919  return path;
920  }
921 }
922 
923 
924 /**
925  * @return Full path to the tce compiler tcecc.
926  */
927 string
929 
931  std::string srcPath =
932  string(TCE_SRC_ROOT) + "/src/bintools/Compiler/oacc";
933 
934  std::string llvmTceSrcPath =
935  string(TCE_SRC_ROOT) + "/src/bintools/Compiler/llvm-tce/llvm-tce";
936 
937  assert(FileSystem::fileExists(srcPath) &&
938  FileSystem::fileExists(llvmTceSrcPath) &&
939  "Build broken, compiler paths not found.");
940  return srcPath;
941  } else {
942  std::string path = Application::installationDir() + "/bin/oacc";
943  assert(
944  FileSystem::fileExists(path) &&
945  "Installation broken: oacc not found.");
946  return path;
947  }
948 }
949 
950 
951 /**
952  * Returns full path to the default scheduler pass configuration file.
953  *
954  * @return Full path to the default_scheduler.conf
955  */
956 string
958 
960  std::string srcPath =
961  string(TCE_SRC_ROOT) + "/scheduler/passes/default_scheduler.conf";
962  assert(
963  FileSystem::fileExists(srcPath) &&
964  "Build broken, default_scheduler.conf not found.");
965  return srcPath;
966  } else {
967  std::string path = Application::installationDir() +
968  string(INSTALLATION_DIR) + "/scheduler/passes/default_scheduler.conf";
969  assert(
970  FileSystem::fileExists(path) &&
971  "Installation broken, default_scheduler.conf not found.");
972  return path;
973  }
974 }
975 
976 
977 /**
978  * Returns full path to the old gcc scheduler pass configuration file.
979  *
980  * This is the scheduler conf that is used to schedule code from the
981  * old gcc compiler.
982  *
983  * @return Full path to the old_gcc.conf
984  */
985 string
987 
989  // first find from the src tree
990  std::string srcPath =
991  string(TCE_SRC_ROOT) + "/scheduler/passes/old_gcc.conf";
992  assert(
993  FileSystem::fileExists(srcPath) &&
994  "Build broken, old_gcc.conf not found.");
995  return srcPath;
996  } else {
997  std::string path = Application::installationDir() +
998  string(INSTALLATION_DIR) + "/scheduler/passes/old_gcc.conf";
999  assert(
1000  FileSystem::fileExists(path) &&
1001  "Installation broken, old_gcc.conf not found.");
1002  return path;
1003  }
1004 }
1005 
1006 /**
1007  * Returns full path to the default IC/Decoder plugin file.
1008  *
1009  * @return Full path to the defaultICDecoderPlugin.so
1010  */
1011 string
1013 
1014  try {
1015  std::string file = "DefaultICDecoderPlugin.so";
1016  vector<std::string> paths = icDecoderPluginPaths();
1017  std::string path = FileSystem::findFileInSearchPaths(paths, file);
1018 
1019  return path;
1020  } catch(FileNotFound e) {
1022  "Installation broken, DefaultICDecoderPlugin.so not found.");
1023  return ""; // just to remove a warning
1024  }
1025 }
1026 
1027 /**
1028  * Returns default GUI text editor to be used, atm. for osed
1029  *
1030  * @return Full path of default text editor, first from VISUAL and, failing
1031  * that, from a list of popular GUI editors
1032  */
1033 string
1035 
1036  std::string editor = environmentVariable("VISUAL");
1037 
1038  if (editor.empty()) {
1039  // If env variable is empty, check a list of known GUI editors
1040  vector<std::string> editors;
1041  editors.push_back("gedit"); // Gnome
1042  editors.push_back("leafpad"); // LXDE
1043  editors.push_back("mousepad"); // XFCE (old)
1044  editors.push_back("kate"); // KDE
1045 
1046  // returns empty string if none found
1047  return findExecutableFromPATH(editors);
1048  }
1049 
1050  // test if env variable contained full path to text editor executable
1051  if (FileSystem::fileIsExecutable(editor)) {
1052  return editor;
1053  }
1054 
1055  // EDITOR and VISUAL doesn't have to contain full path
1056  // so let's search PATH enviroment variable to get the full path
1057  std::string testEditor = "";
1058  vector<std::string> paths;
1059  parsePathEnvVariable(paths);
1060  for (unsigned int i = 0; i < paths.size(); ++i) {
1061  testEditor = paths.at(i) + DS + editor;
1062  if (FileSystem::fileIsExecutable(testEditor)) {
1063  return testEditor;
1064  }
1065  }
1066 
1067  // no editor were found
1068  return "";
1069 }
1070 
1071 /**
1072  * Returns Paths in PATH environment variable.
1073  *
1074  * @return Paths in PATH environment variable.
1075  */
1076 inline void
1077 Environment::parsePathEnvVariable(std::vector<std::string>& paths) {
1078 
1079  std::string pathsEnv = environmentVariable("PATH");
1080  StringTools::chopString(pathsEnv, ":", paths);
1081 }
1082 
1083 /**
1084  * Returns full path to the default LLVM backend plugin cache directory.
1085  */
1086 string
1088 
1089  std::string path =
1091  FileSystem::DIRECTORY_SEPARATOR + string(".openasip") +
1092  FileSystem::DIRECTORY_SEPARATOR + string("tcecc") +
1093  FileSystem::DIRECTORY_SEPARATOR + string("cache");
1094 
1095  return path;
1096 }
1097 
1098 /**
1099  * Finds a first match of a given list of files from PATH env variable.
1100  *
1101  * Finds a first executable match of a given list of files from paths in the
1102  * PATH environment variable.
1103  *
1104  * @param execs A list of file names, that are to be searched from PATH.
1105  * @return Full path of the executable found or a zero length string, in case
1106  * nothing was found.
1107  */
1108 std::string
1109 Environment::findExecutableFromPATH(const std::vector<std::string>& execs) {
1110  vector<std::string> paths;
1111  parsePathEnvVariable(paths);
1112  for (unsigned int i = 0; i < paths.size(); ++i) {
1113  for (unsigned int j = 0; j < execs.size(); ++j) {
1114  std::string exec = paths.at(i) + DS + execs.at(j);
1115  if (FileSystem::fileIsExecutable(exec)) {
1116  return exec;
1117  }
1118  }
1119  }
1120  return "";
1121 }
1122 
1123 /**
1124  * Returns full paths to implementation tester vhdl testbench template
1125  * directory
1126  *
1127  * @return Paths to testbench templates
1128  */
1130  std::vector<std::string> paths;
1132  string path = string(TCE_SRC_ROOT) + DS + "data" + DS + "hdb";
1133  paths.push_back(path);
1134  }
1135  string path = Application::installationDir() + string(INSTALLATION_DIR) +
1136  "data" + DS + "hdb";
1137  paths.push_back(path);
1138  return paths;
1139 }
1140 
1141 /**
1142  * Returns full path to ttasim trace dir if set using TTASIM_TRACE_DIR
1143  * environment variable
1144  *
1145  * Return full path to ttasim trace dir location if environment variable
1146  * TTASIM_TRACE_DIR is pointing to valid dir. Empty string otherwise.
1147  */
1148 string
1150  initialize();
1151  return simTraceDir_;
1152 }
1153 
1154 /**
1155  * Returns true in case TCE is running in developer mode.
1156  *
1157  * The developer mode can be enabled by setting environment variable
1158  * TCE_DEVEL_MODE to 1 and causes the following differences at TCE
1159  * runtime:
1160  *
1161  * - build directories are used as various search paths instead of
1162  * installation prefix paths
1163  * - debug output printed to stderr
1164  */
1165 bool
1168  return developerMode_;
1169 }
Environment::simTraceDir_
static std::string simTraceDir_
Directory to store ttasim traces.
Definition: Environment.hh:161
FileSystem.hh
Environment::oldGccSchedulerConf
static std::string oldGccSchedulerConf()
Definition: Environment.cc:986
Environment::newBitmapsFileDir_
static std::string newBitmapsFileDir_
Directory for new bitmaps file.
Definition: Environment.hh:151
FileNotFound
Definition: Exception.hh:224
Environment::CONF_DIR_NAME_
static const std::string CONF_DIR_NAME_
Name of the configuration directory.
Definition: Environment.hh:121
FileSystem::createDirectory
static bool createDirectory(const std::string &path)
Definition: FileSystem.cc:400
TCEString::startsWith
bool startsWith(const std::string &str) const
DS
#define DS
Definition: Environment.cc:86
Environment::manDirPath
static std::string manDirPath(const std::string &prog)
Definition: Environment.cc:250
Environment::PDF_MANUAL_INSTALLED
static const std::string PDF_MANUAL_INSTALLED
Relative path to the installed TCE .pdf manual.
Definition: Environment.hh:164
Environment::setNewDataFileDir
static void setNewDataFileDir(const std::string &path)
Definition: Environment.cc:475
Environment::developerMode_
static bool developerMode_
Flag indicating whether TCE is executing in developer mode.
Definition: Environment.hh:129
MapTools.hh
Environment::schemaPaths_
static std::vector< std::string > schemaPaths_
Schema directory search paths.
Definition: Environment.hh:132
Environment::initialize
static void initialize()
Definition: Environment.cc:95
Environment::minimalADF
static std::string minimalADF()
Definition: Environment.cc:901
VectorTools::insertUnique
static bool insertUnique(ContainerType &aVec, const ValueType &aValue)
Environment::newSchemaFileDir_
static std::string newSchemaFileDir_
Directory for new schema file.
Definition: Environment.hh:147
Environment::icDecoderPluginPaths
static std::vector< std::string > icDecoderPluginPaths(bool libraryPathsOnly=false)
Definition: Environment.cc:635
Environment::schedulerPluginPaths
static std::vector< std::string > schedulerPluginPaths()
Definition: Environment.cc:601
Environment::iconPaths_
static std::vector< std::string > iconPaths_
Toolbar icon search paths.
Definition: Environment.hh:140
Environment::decompressorPaths
static std::vector< std::string > decompressorPaths(bool libraryPathsOnly=false)
Definition: Environment.cc:793
FileSystem::absolutePathOf
static std::string absolutePathOf(const std::string &pathName)
Definition: FileSystem.cc:303
Environment::pathTo
static std::string pathTo(const std::string &name)
Definition: Environment.cc:444
Environment::PDF_MANUAL_SRC
static const std::string PDF_MANUAL_SRC
Relative path to the TCE .pdf manual in source tree.
Definition: Environment.hh:166
Environment::errorLogFilePath
static std::string errorLogFilePath()
Definition: Environment.cc:323
Environment::implementationTesterTemplatePaths
static std::vector< std::string > implementationTesterTemplatePaths()
Definition: Environment.cc:1129
Environment::newDataFileDir_
static std::string newDataFileDir_
Directory for new data file.
Definition: Environment.hh:149
Environment::setNewErrorLogFileDir
static void setNewErrorLogFileDir(const std::string &path)
Definition: Environment.cc:505
Environment::tceCompiler
static std::string tceCompiler()
Definition: Environment.cc:928
Environment::bitmapsDirPath
static std::string bitmapsDirPath(const std::string &prog)
Definition: Environment.cc:201
Environment::defaultICDecoderPlugin
static std::string defaultICDecoderPlugin()
Definition: Environment.cc:1012
Environment::userConfPath
static TCEString userConfPath(const std::string &fileName)
Definition: Environment.cc:303
Environment::bitmapsPaths_
static std::vector< std::string > bitmapsPaths_
Bitmaps directory search paths.
Definition: Environment.hh:136
Environment::MAN_DIR_NAME_
static const std::string MAN_DIR_NAME_
Name of the manual directory.
Definition: Environment.hh:119
Environment::DATA_DIR_NAME_
static const std::string DATA_DIR_NAME_
Name of the data directory.
Definition: Environment.hh:115
TCEString.hh
StringTools.hh
assert
#define assert(condition)
Definition: Application.hh:86
Environment::codeCompressorPaths
static std::vector< std::string > codeCompressorPaths(bool libraryPathsOnly=false)
Definition: Environment.cc:571
Environment::defaultTextEditorPath
static std::string defaultTextEditorPath()
Definition: Environment.cc:1034
Environment::setNewManFileDir
static void setNewManFileDir(const std::string &path)
Definition: Environment.cc:495
FileSystem::homeDirectory
static std::string homeDirectory()
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
Environment::opsetIncludeDir
static std::vector< std::string > opsetIncludeDir()
Definition: Environment.cc:384
Environment::setNewSchemaFileDir
static void setNewSchemaFileDir(const std::string &path)
Definition: Environment.cc:465
Environment::ERROR_LOG_FILE_NAME_
static const std::string ERROR_LOG_FILE_NAME_
Name of the system error log file.
Definition: Environment.hh:125
Environment::hdbPaths
static std::vector< std::string > hdbPaths(bool libraryPathsOnly=false)
Definition: Environment.cc:683
Environment::includeDirPaths
static std::vector< std::string > includeDirPaths()
Definition: Environment.cc:348
Environment::MINIMAL_ADF_INSTALLED
static const std::string MINIMAL_ADF_INSTALLED
Relative path to the installed minimal.adf.
Definition: Environment.hh:168
Environment::shortHDBPath
static TCEString shortHDBPath(const TCEString &hdbPath)
Definition: Environment.cc:707
Application.hh
Environment::dataPaths_
static std::vector< std::string > dataPaths_
Data directory search paths.
Definition: Environment.hh:134
FileSystem::directoryOfPath
static std::string directoryOfPath(const std::string fileName)
Definition: FileSystem.cc:79
VectorTools.hh
Environment::defaultSchedulerConf
static std::string defaultSchedulerConf()
Definition: Environment.cc:957
Environment::parsePathEnvVariable
static void parsePathEnvVariable(std::vector< std::string > &paths)
Definition: Environment.cc:1077
Environment::schemaDirPath
static std::string schemaDirPath(const std::string &prog)
Definition: Environment.cc:151
Environment.hh
Environment::newIconFileDir_
static std::string newIconFileDir_
Directory for new icon file.
Definition: Environment.hh:153
Environment::newManFileDir_
static std::string newManFileDir_
Directory for new manual file.
Definition: Environment.hh:155
Environment::Environment
Environment()
Definition: Environment.cc:420
Environment::newErrorLogFileDir_
static std::string newErrorLogFileDir_
Directory for new error log file.
Definition: Environment.hh:159
FileSystem::createFile
static bool createFile(const std::string &file)
Definition: FileSystem.cc:468
Environment::osalPaths
static std::vector< std::string > osalPaths()
Definition: Environment.cc:519
Environment::MINIMAL_ADF_SRC
static const std::string MINIMAL_ADF_SRC
Relative path to the minimal.adf in source tree.
Definition: Environment.hh:170
Environment::pathsToUse_
static std::vector< std::string > pathsToUse_
Paths used in a particular search.
Definition: Environment.hh:144
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
Environment::longHDBPath
static TCEString longHDBPath(const TCEString &hdbPath)
Definition: Environment.cc:731
Environment::confPath
static TCEString confPath(const std::string &fileName)
Definition: Environment.cc:277
Environment::manPaths_
static std::vector< std::string > manPaths_
Man directory search paths.
Definition: Environment.hh:138
Environment::iconDirPath
static std::string iconDirPath()
Definition: Environment.cc:225
Environment::simTraceDirPath
static std::string simTraceDirPath()
Definition: Environment.cc:1149
Environment::vhdlPaths
static std::vector< std::string > vhdlPaths(const std::string &hdbPath)
Definition: Environment.cc:760
Environment::llvmtceCachePath
static std::string llvmtceCachePath()
Definition: Environment.cc:1087
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Environment::findExecutableFromPATH
static std::string findExecutableFromPATH(const std::vector< std::string > &file)
Definition: Environment.cc:1109
Application::installationDir
static std::string installationDir()
Definition: Application.cc:537
Environment::setNewBitmapsFileDir
static void setNewBitmapsFileDir(const std::string &path)
Definition: Environment.cc:485
TCEString
Definition: TCEString.hh:53
StringTools::chopString
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
Definition: StringTools.cc:181
Environment::SCHEMA_DIR_NAME_
static const std::string SCHEMA_DIR_NAME_
Name of the XML Schema directory.
Definition: Environment.hh:113
FileSystem::fileIsExecutable
static bool fileIsExecutable(const std::string fileName)
Environment::explorerPluginPaths
static std::vector< std::string > explorerPluginPaths()
Definition: Environment.cc:809
Environment::ICON_DIR_NAME_
static const std::string ICON_DIR_NAME_
Name of the toolbar icon directory.
Definition: Environment.hh:123
Environment::initialized_
static bool initialized_
Flag indicating whether class has been initialized.
Definition: Environment.hh:127
Environment::hwModulePaths
static std::vector< std::string > hwModulePaths()
Definition: Environment.cc:669
INSTALLATION_DIR
#define INSTALLATION_DIR
Definition: Environment.cc:85
FileSystem::currentWorkingDir
static std::string currentWorkingDir()
Definition: FileSystem.cc:142
Environment::newConfFile_
static std::string newConfFile_
Directory for new configure file.
Definition: Environment.hh:157
Environment::developerMode
static bool developerMode()
Definition: Environment.cc:1166
Environment::environmentVariable
static std::string environmentVariable(const std::string &variable)
Definition: Environment.cc:406
Environment::errorPaths_
static std::vector< std::string > errorPaths_
Error file search paths.
Definition: Environment.hh:142
Environment::estimatorPluginPaths
static std::vector< std::string > estimatorPluginPaths()
Definition: Environment.cc:842
debugLog
#define debugLog(text)
Definition: Application.hh:95
FileSystem::findFileInSearchPaths
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
Definition: FileSystem.cc:562
Environment::BITMAPS_DIR_NAME_
static const std::string BITMAPS_DIR_NAME_
Name of the bitmaps directory.
Definition: Environment.hh:117
Environment::~Environment
~Environment()
Definition: Environment.cc:427
Environment::pdfManual
static std::string pdfManual()
Definition: Environment.cc:877
Environment::dataDirPath
static std::string dataDirPath(const std::string &prog)
Definition: Environment.cc:176