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 <filesystem>
28 #include <map>
29 #include <string>
30 #include <vector>
31 
32 #include <ArmarXCore/interface/core/BasicTypes.h>
33 
34 namespace armarx
35 {
36 
37  /**
38  * @class CMakePackageFinder
39  * @ingroup core-utility
40  * @brief The CMakePackageFinder class provides an interface to the CMake Package
41  * finder capabilities.
42  *
43  * This class calls the CMake script FindPackageX.cmake to retrieve information
44  * about a specific ArmarX package.
45  * The CMake configuration file of a package is located in the getConfigDir() directory (source packages and installations).
46  *
47  * The available variables and their values are written to stdout and parsed by
48  * CMakePackageFinder::_ParseString().
49  * The values of the variables can be retieved via CMakePackageFinder::getVar() or
50  * their specific convenience functions like getBuildDir().
51  */
53  {
54  public:
55  /**
56  * @brief The package with name packageName is searched with cmake during construction of this
57  * class. Query the values with the class functions.
58  * @param packageName Name of the ArmarX package to find.
59  * @param packagePath Optional path to the package. Use if a specific instance of the package should be used.
60  * This needs to be the path where the ${packageName}Config.cmake is located, so for source packages the build dir.
61  * @see getConfigDir()
62  */
63  CMakePackageFinder(const std::string& packageName,
64  const std::filesystem::path& packagePath = "",
65  bool suppressStdErr = false,
66  bool usePackagePathOnlyAsHint = false);
67 
68  /**
69  * @brief Static function to find the include path with cmake. Does use cmake built in functions and
70  * not the ArmarX cmake find script.
71  * @param packageName
72  * @return
73  */
74  static std::vector<std::string> FindPackageIncludePathList(const std::string& packageName);
75  static std::string FindPackageIncludePaths(const std::string& packageName);
76  static std::string FindPackageLibs(const std::string& packageName);
77 
78  static std::string
79  ExecCommand(std::string command, int& result, bool suppressStdErr = false);
80 
81  static std::vector<std::string> FindAllArmarXSourcePackages();
82 
83  /**
84  * @brief Returns the name of the given package. Same name as given in constructor.
85  */
86  std::string getName() const;
87 
88  /**
89  * @brief Returns whether or not this package was found with cmake.
90  */
91  bool packageFound() const;
92  const std::map<std::string, std::string>& getVars() const;
93 
94  /**
95  * @brief Returns the content of a CMake variable. Usually there exists a dedicated function for all variables e.g. getIncludePaths()
96  * @param varName Name of the CMake Variable
97  * @return Content of the variable.
98  */
99  std::string getVar(const std::string& varName) const;
100 
101  /**
102  * @brief Returns the path needed to find this package with cmake, i.e. the dir where ${PACKAGENAME}Config.cmake resides.
103  */
104  std::string
105  getConfigDir() const
106  {
107  return getVar("CONFIG_DIR");
108  }
109 
110  /**
111  * @brief Returns the include paths separated by semi-colons.
112  */
113  std::string
115  {
116  return getVar("INCLUDE_DIRS");
117  }
118 
119  std::string
121  {
122  return getVar("STATECHARTS_DIR");
123  }
124 
125  /**
126  * @brief Return the include paths in a vector. Same as getIncludePaths() but already splitted.
127  */
128  std::vector<std::string> getIncludePathList() const;
129 
130  /**
131  * @brief Returns the library paths seperated by semi-colons.
132  */
133  std::string
135  {
136  return getVar("LIBRARY_DIRS");
137  }
138 
139  std::string
140  getLibs() const
141  {
142  return getVar("LIBRARIES");
143  }
144 
145  std::string
146  getBuildDir() const
147  {
148  return getVar("BUILD_DIR");
149  }
150 
151  /**
152  * @brief Returns the top level path of a source package. Does not work for installed packages, so use with caution.
153  * To get the package config dir, e.g. for cmake, use getConfigDir().
154  */
155  std::string
157  {
158  return getVar("PROJECT_BASE_DIR");
159  }
160 
161  /**
162  * @brief Returns the directory containing CMake scripts.
163  * @note This is not the CMake Config Dir!
164  * @see getConfigDir()
165  *
166  */
167  std::string
168  getCMakeDir() const
169  {
170  return getVar("CMAKE_DIR");
171  }
172 
173  std::string
174  getBinaryDir() const
175  {
176  return getVar("BINARY_DIR");
177  }
178 
179  std::string getExecutables() const;
180 
181  std::vector<std::string> getComponentExecutableNames() const;
182 
183  /**
184  * @brief Returns the path to the slice (*.ice) files of a package.
185  * @note The generated slice files are relative getIncludePaths() with the same directory structure.
186  */
187  std::string
189  {
190  return getVar("INTERFACE_DIRS");
191  }
192 
193  std::string
194  getDataDir() const
195  {
196  return getVar("DATA_DIR");
197  }
198 
199  std::string
201  {
202  return getVar("SCENARIOS_DIR");
203  }
204 
205  std::vector<std::string> getDependencies() const;
206  std::map<std::string, std::string> getDependencyPaths() const;
207 
208  /**
209  * @brief return the path where the temporary cmake files are stored that are automically created by cmake.
210  * If username available returns /tmp/armarxcmake-$username, otherwise /tmp.
211  */
212  static std::string getArmarXCMakeTempDir();
213 
214  /**
215  * @brief Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
216  * E.g. $C{ArmarXCore:BINARY_DIR} could be replaced with /home/user/armarx/ArmarXCore/build/bin.
217  * @param string Contents in this string will be replaced.
218  * @return True if something was replaced.
219  */
220  static bool ReplaceCMakePackageFinderVars(std::string& string);
221 
222  protected:
223  std::vector<std::string> extractVariables(const std::string& cmakeVarString);
224  static bool
225  _ParseString(const std::string& input, std::string& varName, std::string& content);
226  static void _CreateSharedMutex();
227 
228  bool found;
229  std::map<std::string, std::string> vars;
230  std::string packageName;
231  };
232 
233 } // namespace armarx
armarx::CMakePackageFinder::vars
std::map< std::string, std::string > vars
Definition: CMakePackageFinder.h:229
armarx::CMakePackageFinder::packageFound
bool packageFound() const
Returns whether or not this package was found with cmake.
Definition: CMakePackageFinder.cpp:511
armarx::CMakePackageFinder::getScenariosDir
std::string getScenariosDir() const
Definition: CMakePackageFinder.h:200
armarx::CMakePackageFinder::getVar
std::string getVar(const std::string &varName) const
Returns the content of a CMake variable.
Definition: CMakePackageFinder.cpp:523
armarx::CMakePackageFinder::getExecutables
std::string getExecutables() const
Definition: CMakePackageFinder.cpp:697
armarx::CMakePackageFinder::FindAllArmarXSourcePackages
static std::vector< std::string > FindAllArmarXSourcePackages()
Definition: CMakePackageFinder.cpp:461
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:52
armarx::CMakePackageFinder::packageName
std::string packageName
Definition: CMakePackageFinder.h:230
armarx::CMakePackageFinder::getLibraryPaths
std::string getLibraryPaths() const
Returns the library paths seperated by semi-colons.
Definition: CMakePackageFinder.h:134
armarx::CMakePackageFinder::getLibs
std::string getLibs() const
Definition: CMakePackageFinder.h:140
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:642
armarx::CMakePackageFinder::getInterfacePaths
std::string getInterfacePaths() const
Returns the path to the slice (*.ice) files of a package.
Definition: CMakePackageFinder.h:188
armarx::CMakePackageFinder::getIncludePathList
std::vector< std::string > getIncludePathList() const
Return the include paths in a vector.
Definition: CMakePackageFinder.cpp:634
armarx::CMakePackageFinder::getPackageDir
std::string getPackageDir() const
Returns the top level path of a source package.
Definition: CMakePackageFinder.h:156
armarx::CMakePackageFinder::getName
std::string getName() const
Returns the name of the given package.
Definition: CMakePackageFinder.cpp:505
armarx::CMakePackageFinder::ExecCommand
static std::string ExecCommand(std::string command, int &result, bool suppressStdErr=false)
Definition: CMakePackageFinder.cpp:434
armarx::CMakePackageFinder::getComponentExecutableNames
std::vector< std::string > getComponentExecutableNames() const
Definition: CMakePackageFinder.cpp:703
armarx::CMakePackageFinder::getBinaryDir
std::string getBinaryDir() const
Definition: CMakePackageFinder.h:174
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
armarx::CMakePackageFinder::found
bool found
Definition: CMakePackageFinder.h:228
armarx::CMakePackageFinder::getDataDir
std::string getDataDir() const
Definition: CMakePackageFinder.h:194
armarx::CMakePackageFinder::_ParseString
static bool _ParseString(const std::string &input, std::string &varName, std::string &content)
Definition: CMakePackageFinder.cpp:568
armarx::CMakePackageFinder::getStatechartsDir
std::string getStatechartsDir() const
Definition: CMakePackageFinder.h:120
armarx::CMakePackageFinder::getVars
const std::map< std::string, std::string > & getVars() const
Definition: CMakePackageFinder.cpp:517
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:336
armarx::CMakePackageFinder::_CreateSharedMutex
static void _CreateSharedMutex()
Definition: CMakePackageFinder.cpp:599
armarx::CMakePackageFinder::getConfigDir
std::string getConfigDir() const
Returns the path needed to find this package with cmake, i.e.
Definition: CMakePackageFinder.h:105
armarx::CMakePackageFinder::getDependencies
std::vector< std::string > getDependencies() const
Definition: CMakePackageFinder.cpp:536
armarx::CMakePackageFinder::FindPackageLibs
static std::string FindPackageLibs(const std::string &packageName)
Definition: CMakePackageFinder.cpp:359
armarx::CMakePackageFinder::getIncludePaths
std::string getIncludePaths() const
Returns the include paths separated by semi-colons.
Definition: CMakePackageFinder.h:114
armarx::CMakePackageFinder::ReplaceCMakePackageFinderVars
static bool ReplaceCMakePackageFinderVars(std::string &string)
Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
Definition: CMakePackageFinder.cpp:664
armarx::CMakePackageFinder::getBuildDir
std::string getBuildDir() const
Definition: CMakePackageFinder.h:146
armarx::CMakePackageFinder::FindPackageIncludePaths
static std::string FindPackageIncludePaths(const std::string &packageName)
Definition: CMakePackageFinder.cpp:393
armarx::CMakePackageFinder::getDependencyPaths
std::map< std::string, std::string > getDependencyPaths() const
Definition: CMakePackageFinder.cpp:545
armarx::CMakePackageFinder::extractVariables
std::vector< std::string > extractVariables(const std::string &cmakeVarString)
Definition: CMakePackageFinder.cpp:604
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::CMakePackageFinder::getCMakeDir
std::string getCMakeDir() const
Returns the directory containing CMake scripts.
Definition: CMakePackageFinder.h:168
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:248