CMakePackageFinder.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2014
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include <filesystem>
31 
32 #include <ArmarXCore/interface/core/BasicTypes.h>
33 
34 
35 namespace armarx
36 {
37 
38  /**
39  * @class CMakePackageFinder
40  * @ingroup core-utility
41  * @brief The CMakePackageFinder class provides an interface to the CMake Package
42  * finder capabilities.
43  *
44  * This class calls the CMake script FindPackageX.cmake to retrieve information
45  * about a specific ArmarX package.
46  * The CMake configuration file of a package is located in the getConfigDir() directory (source packages and installations).
47  *
48  * The available variables and their values are written to stdout and parsed by
49  * CMakePackageFinder::_ParseString().
50  * The values of the variables can be retieved via CMakePackageFinder::getVar() or
51  * their specific convenience functions like getBuildDir().
52  */
54  {
55  public:
56 
57  /**
58  * @brief The package with name packageName is searched with cmake during construction of this
59  * class. Query the values with the class functions.
60  * @param packageName Name of the ArmarX package to find.
61  * @param packagePath Optional path to the package. Use if a specific instance of the package should be used.
62  * This needs to be the path where the ${packageName}Config.cmake is located, so for source packages the build dir.
63  * @see getConfigDir()
64  */
65  CMakePackageFinder(const std::string& packageName, const std::filesystem::path& packagePath = "", bool suppressStdErr = false, bool usePackagePathOnlyAsHint = false);
66 
67  /**
68  * @brief Static function to find the include path with cmake. Does use cmake built in functions and
69  * not the ArmarX cmake find script.
70  * @param packageName
71  * @return
72  */
73  static std::vector<std::string> FindPackageIncludePathList(const std::string& packageName);
74  static std::string FindPackageIncludePaths(const std::string& packageName);
75  static std::string FindPackageLibs(const std::string& packageName);
76 
77  static std::string ExecCommand(std::string command, int& result, bool suppressStdErr = false);
78 
79  static std::vector<std::string> FindAllArmarXSourcePackages();
80 
81  /**
82  * @brief Returns the name of the given package. Same name as given in constructor.
83  */
84  std::string getName() const;
85 
86  /**
87  * @brief Returns whether or not this package was found with cmake.
88  */
89  bool packageFound() const;
90  const std::map<std::string, std::string>& getVars() const;
91 
92  /**
93  * @brief Returns the content of a CMake variable. Usually there exists a dedicated function for all variables e.g. getIncludePaths()
94  * @param varName Name of the CMake Variable
95  * @return Content of the variable.
96  */
97  std::string getVar(const std::string& varName) const;
98 
99  /**
100  * @brief Returns the path needed to find this package with cmake, i.e. the dir where ${PACKAGENAME}Config.cmake resides.
101  */
102  std::string getConfigDir() const
103  {
104  return getVar("CONFIG_DIR");
105  }
106 
107  /**
108  * @brief Returns the include paths separated by semi-colons.
109  */
110  std::string getIncludePaths() const
111  {
112  return getVar("INCLUDE_DIRS");
113  }
114 
115  std::string getStatechartsDir() const
116  {
117  return getVar("STATECHARTS_DIR");
118  }
119 
120  /**
121  * @brief Return the include paths in a vector. Same as getIncludePaths() but already splitted.
122  */
123  std::vector<std::string> getIncludePathList() const;
124 
125  /**
126  * @brief Returns the library paths seperated by semi-colons.
127  */
128  std::string getLibraryPaths() const
129  {
130  return getVar("LIBRARY_DIRS");
131  }
132  std::string getLibs() const
133  {
134  return getVar("LIBRARIES");
135  }
136  std::string getBuildDir() const
137  {
138  return getVar("BUILD_DIR");
139  }
140  /**
141  * @brief Returns the top level path of a source package. Does not work for installed packages, so use with caution.
142  * To get the package config dir, e.g. for cmake, use getConfigDir().
143  */
144  std::string getPackageDir() const
145  {
146  return getVar("PROJECT_BASE_DIR");
147  }
148 
149  /**
150  * @brief Returns the directory containing CMake scripts.
151  * @note This is not the CMake Config Dir!
152  * @see getConfigDir()
153  *
154  */
155  std::string getCMakeDir() const
156  {
157  return getVar("CMAKE_DIR");
158  }
159  std::string getBinaryDir() const
160  {
161  return getVar("BINARY_DIR");
162  }
163 
164  std::string getExecutables() const;
165 
166  std::vector<std::string> getComponentExecutableNames() const;
167 
168  /**
169  * @brief Returns the path to the slice (*.ice) files of a package.
170  * @note The generated slice files are relative getIncludePaths() with the same directory structure.
171  */
172  std::string getInterfacePaths() const
173  {
174  return getVar("INTERFACE_DIRS");
175  }
176  std::string getDataDir() const
177  {
178  return getVar("DATA_DIR");
179  }
180  std::string getScenariosDir() const
181  {
182  return getVar("SCENARIOS_DIR");
183  }
184  std::vector<std::string> getDependencies() const;
185  std::map<std::string, std::string> getDependencyPaths() const;
186 
187  /**
188  * @brief return the path where the temporary cmake files are stored that are automically created by cmake.
189  * If username available returns /tmp/armarxcmake-$username, otherwise /tmp.
190  */
191  static std::string getArmarXCMakeTempDir();
192 
193  /**
194  * @brief Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
195  * E.g. $C{ArmarXCore:BINARY_DIR} could be replaced with /home/user/armarx/ArmarXCore/build/bin.
196  * @param string Contents in this string will be replaced.
197  * @return True if something was replaced.
198  */
199  static bool ReplaceCMakePackageFinderVars(std::string& string);
200  protected:
201  std::vector<std::string> extractVariables(const std::string& cmakeVarString);
202  static bool _ParseString(const std::string& input, std::string& varName, std::string& content);
203  static void _CreateSharedMutex();
204 
205  bool found;
206  std::map<std::string, std::string> vars;
207  std::string packageName;
208  };
209 
210 }
armarx::CMakePackageFinder::vars
std::map< std::string, std::string > vars
Definition: CMakePackageFinder.h:206
armarx::CMakePackageFinder::packageFound
bool packageFound() const
Returns whether or not this package was found with cmake.
Definition: CMakePackageFinder.cpp:485
armarx::CMakePackageFinder::getScenariosDir
std::string getScenariosDir() const
Definition: CMakePackageFinder.h:180
armarx::CMakePackageFinder::getVar
std::string getVar(const std::string &varName) const
Returns the content of a CMake variable.
Definition: CMakePackageFinder.cpp:495
armarx::CMakePackageFinder::getExecutables
std::string getExecutables() const
Definition: CMakePackageFinder.cpp:661
armarx::CMakePackageFinder::FindAllArmarXSourcePackages
static std::vector< std::string > FindAllArmarXSourcePackages()
Definition: CMakePackageFinder.cpp:438
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:53
armarx::CMakePackageFinder::packageName
std::string packageName
Definition: CMakePackageFinder.h:207
armarx::CMakePackageFinder::getLibraryPaths
std::string getLibraryPaths() const
Returns the library paths seperated by semi-colons.
Definition: CMakePackageFinder.h:128
armarx::CMakePackageFinder::getLibs
std::string getLibs() const
Definition: CMakePackageFinder.h:132
armarx::CMakePackageFinder::getArmarXCMakeTempDir
static std::string getArmarXCMakeTempDir()
return the path where the temporary cmake files are stored that are automically created by cmake.
Definition: CMakePackageFinder.cpp:609
armarx::CMakePackageFinder::getInterfacePaths
std::string getInterfacePaths() const
Returns the path to the slice (*.ice) files of a package.
Definition: CMakePackageFinder.h:172
armarx::CMakePackageFinder::getIncludePathList
std::vector< std::string > getIncludePathList() const
Return the include paths in a vector.
Definition: CMakePackageFinder.cpp:601
armarx::CMakePackageFinder::getPackageDir
std::string getPackageDir() const
Returns the top level path of a source package.
Definition: CMakePackageFinder.h:144
armarx::CMakePackageFinder::getName
std::string getName() const
Returns the name of the given package.
Definition: CMakePackageFinder.cpp:480
armarx::CMakePackageFinder::ExecCommand
static std::string ExecCommand(std::string command, int &result, bool suppressStdErr=false)
Definition: CMakePackageFinder.cpp:412
armarx::CMakePackageFinder::getComponentExecutableNames
std::vector< std::string > getComponentExecutableNames() const
Definition: CMakePackageFinder.cpp:666
armarx::CMakePackageFinder::getBinaryDir
std::string getBinaryDir() const
Definition: CMakePackageFinder.h:159
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::CMakePackageFinder::found
bool found
Definition: CMakePackageFinder.h:205
armarx::CMakePackageFinder::getDataDir
std::string getDataDir() const
Definition: CMakePackageFinder.h:176
armarx::CMakePackageFinder::_ParseString
static bool _ParseString(const std::string &input, std::string &varName, std::string &content)
Definition: CMakePackageFinder.cpp:537
armarx::CMakePackageFinder::getStatechartsDir
std::string getStatechartsDir() const
Definition: CMakePackageFinder.h:115
armarx::CMakePackageFinder::getVars
const std::map< std::string, std::string > & getVars() const
Definition: CMakePackageFinder.cpp:490
armarx::CMakePackageFinder::FindPackageIncludePathList
static std::vector< std::string > FindPackageIncludePathList(const std::string &packageName)
Static function to find the include path with cmake.
Definition: CMakePackageFinder.cpp:312
armarx::CMakePackageFinder::_CreateSharedMutex
static void _CreateSharedMutex()
Definition: CMakePackageFinder.cpp:566
armarx::CMakePackageFinder::getConfigDir
std::string getConfigDir() const
Returns the path needed to find this package with cmake, i.e.
Definition: CMakePackageFinder.h:102
armarx::CMakePackageFinder::getDependencies
std::vector< std::string > getDependencies() const
Definition: CMakePackageFinder.cpp:507
armarx::CMakePackageFinder::FindPackageLibs
static std::string FindPackageLibs(const std::string &packageName)
Definition: CMakePackageFinder.cpp:337
armarx::CMakePackageFinder::getIncludePaths
std::string getIncludePaths() const
Returns the include paths separated by semi-colons.
Definition: CMakePackageFinder.h:110
armarx::CMakePackageFinder::ReplaceCMakePackageFinderVars
static bool ReplaceCMakePackageFinderVars(std::string &string)
Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
Definition: CMakePackageFinder.cpp:630
armarx::CMakePackageFinder::getBuildDir
std::string getBuildDir() const
Definition: CMakePackageFinder.h:136
armarx::CMakePackageFinder::FindPackageIncludePaths
static std::string FindPackageIncludePaths(const std::string &packageName)
Definition: CMakePackageFinder.cpp:372
armarx::CMakePackageFinder::getDependencyPaths
std::map< std::string, std::string > getDependencyPaths() const
Definition: CMakePackageFinder.cpp:515
armarx::CMakePackageFinder::extractVariables
std::vector< std::string > extractVariables(const std::string &cmakeVarString)
Definition: CMakePackageFinder.cpp:572
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::CMakePackageFinder::getCMakeDir
std::string getCMakeDir() const
Returns the directory containing CMake scripts.
Definition: CMakePackageFinder.h:155
armarx::CMakePackageFinder::CMakePackageFinder
CMakePackageFinder(const std::string &packageName, const std::filesystem::path &packagePath="", bool suppressStdErr=false, bool usePackagePathOnlyAsHint=false)
The package with name packageName is searched with cmake during construction of this class.
Definition: CMakePackageFinder.cpp:236