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
34namespace 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
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
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
169 {
170 return getVar("CMAKE_DIR");
171 }
172
173 std::string
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
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
std::string getBinaryDir() const
std::string getCMakeDir() const
Returns the directory containing CMake scripts.
static std::vector< std::string > FindPackageIncludePathList(const std::string &packageName)
Static function to find the include path with cmake.
std::vector< std::string > getDependencies() const
std::string getScenariosDir() const
std::vector< std::string > getComponentExecutableNames() const
const std::map< std::string, std::string > & getVars() const
std::string getConfigDir() const
Returns the path needed to find this package with cmake, i.e.
static std::string getArmarXCMakeTempDir()
return the path where the temporary cmake files are stored that are automically created by cmake.
std::map< std::string, std::string > getDependencyPaths() const
static std::string FindPackageLibs(const std::string &packageName)
std::string getVar(const std::string &varName) const
Returns the content of a CMake variable.
std::string getLibraryPaths() const
Returns the library paths seperated by semi-colons.
static std::string ExecCommand(std::string command, int &result, bool suppressStdErr=false)
std::string getName() const
Returns the name of the given package.
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.
std::string getBuildDir() const
std::vector< std::string > getIncludePathList() const
Return the include paths in a vector.
bool packageFound() const
Returns whether or not this package was found with cmake.
std::map< std::string, std::string > vars
std::string getStatechartsDir() const
static bool _ParseString(const std::string &input, std::string &varName, std::string &content)
std::string getPackageDir() const
Returns the top level path of a source package.
std::string getInterfacePaths() const
Returns the path to the slice (*.ice) files of a package.
static bool ReplaceCMakePackageFinderVars(std::string &string)
Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
std::vector< std::string > extractVariables(const std::string &cmakeVarString)
static std::string FindPackageIncludePaths(const std::string &packageName)
static std::vector< std::string > FindAllArmarXSourcePackages()
std::string getIncludePaths() const
Returns the include paths separated by semi-colons.
This file offers overloads of toIce() and fromIce() functions for STL container types.