1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 Denis Shienkov <denis.shienkov@gmail.com>
4 ** Contact: http://www.qt.io/licensing
5 **
6 ** This file is part of Qbs.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms and
13 ** conditions see http://www.qt.io/terms-conditions. For further information
14 ** use the contact form at http://www.qt.io/contact-us.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 or version 3 as published by the Free
19 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
20 ** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
21 ** following information to ensure the GNU Lesser General Public License
22 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
23 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, The Qt Company gives you certain additional
26 ** rights.  These rights are described in The Qt Company LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ****************************************************************************/
30 
31 #include "iarewutils.h"
32 
33 #include <generators/generatorutils.h>
34 
35 namespace qbs {
36 namespace IarewUtils {
37 
toolkitRootPath(const ProductData & qbsProduct)38 QString toolkitRootPath(const ProductData &qbsProduct)
39 {
40     QDir dir(qbsProduct.moduleProperties()
41              .getModuleProperty(Internal::StringConstants::cppModule(),
42                                 QStringLiteral("toolchainInstallPath"))
43              .toString());
44     dir.cdUp();
45     return dir.absolutePath();
46 }
47 
dlibToolkitRootPath(const ProductData & qbsProduct)48 QString dlibToolkitRootPath(const ProductData &qbsProduct)
49 {
50     return toolkitRootPath(qbsProduct) + QLatin1String("/lib/dlib");
51 }
52 
clibToolkitRootPath(const ProductData & qbsProduct)53 QString clibToolkitRootPath(const ProductData &qbsProduct)
54 {
55     return toolkitRootPath(qbsProduct) + QLatin1String("/lib/clib");
56 }
57 
libToolkitRootPath(const ProductData & qbsProduct)58 QString libToolkitRootPath(const ProductData &qbsProduct)
59 {
60     return toolkitRootPath(qbsProduct) + QLatin1String("/lib");
61 }
62 
toolkitRelativeFilePath(const QString & basePath,const QString & fullFilePath)63 QString toolkitRelativeFilePath(const QString &basePath,
64                                 const QString &fullFilePath)
65 {
66     return QLatin1String("$TOOLKIT_DIR$/")
67             + gen::utils::relativeFilePath(basePath, fullFilePath);
68 }
69 
projectRelativeFilePath(const QString & basePath,const QString & fullFilePath)70 QString projectRelativeFilePath(const QString &basePath,
71                                 const QString &fullFilePath)
72 {
73     return QLatin1String("$PROJ_DIR$/")
74             + gen::utils::relativeFilePath(basePath, fullFilePath);
75 }
76 
outputBinaryType(const ProductData & qbsProduct)77 OutputBinaryType outputBinaryType(const ProductData &qbsProduct)
78 {
79     const auto &qbsProductType = qbsProduct.type();
80     if (qbsProductType.contains(QLatin1String("application")))
81         return ApplicationOutputType;
82     if (qbsProductType.contains(QLatin1String("staticlibrary")))
83         return LibraryOutputType;
84     return ApplicationOutputType;
85 }
86 
flagValue(const QStringList & flags,const QString & flagKey)87 QString flagValue(const QStringList &flags, const QString &flagKey)
88 {
89     // Seach for full 'flagKey' option matching.
90     const auto flagBegin = flags.cbegin();
91     const auto flagEnd = flags.cend();
92     auto flagIt = std::find_if(flagBegin, flagEnd, [flagKey](const QString &flag) {
93         return flag == flagKey;
94     });
95     if (flagIt == flagEnd) {
96         // Search for start/end of 'flagKey' matching.
97         flagIt = std::find_if(flagBegin, flagEnd, [flagKey](const QString &flag) {
98             return flag.startsWith(flagKey) || flag.endsWith(flagKey);
99         });
100         if (flagIt == flagEnd)
101             return {};
102     }
103 
104     QString value;
105     // Check that option is in form of 'flagKey=<flagValue>'.
106     if (flagIt->contains(QLatin1Char('='))) {
107         value = flagIt->split(QLatin1Char('=')).at(1).trimmed();
108     } else if (flagKey.count() < flagIt->count()) {
109         // In this case an option is in form of 'flagKey<flagValue>'.
110         value = flagIt->mid(flagKey.count()).trimmed();
111     } else {
112         // In this case an option is in form of 'flagKey <flagValue>'.
113         ++flagIt;
114         if (flagIt < flagEnd)
115             value = (*flagIt).trimmed();
116         else
117             return {};
118     }
119     return value;
120 }
121 
flagValues(const QStringList & flags,const QString & flagKey)122 QVariantList flagValues(const QStringList &flags, const QString &flagKey)
123 {
124     QVariantList values;
125     for (auto flagIt = flags.cbegin(); flagIt < flags.cend(); ++flagIt) {
126         if (*flagIt != flagKey)
127             continue;
128         ++flagIt;
129         values.push_back(*flagIt);
130     }
131     return values;
132 }
133 
cppModuleCompilerFlags(const PropertyMap & qbsProps)134 QStringList cppModuleCompilerFlags(const PropertyMap &qbsProps)
135 {
136     return gen::utils::cppStringModuleProperties(
137                 qbsProps, {QStringLiteral("driverFlags"), QStringLiteral("cFlags"),
138                            QStringLiteral("cppFlags"), QStringLiteral("cxxFlags"),
139                            QStringLiteral("commonCompilerFlags")});
140 }
141 
cppModuleAssemblerFlags(const PropertyMap & qbsProps)142 QStringList cppModuleAssemblerFlags(const PropertyMap &qbsProps)
143 {
144     return gen::utils::cppStringModuleProperties(
145                 qbsProps, {QStringLiteral("assemblerFlags")});
146 }
147 
cppModuleLinkerFlags(const PropertyMap & qbsProps)148 QStringList cppModuleLinkerFlags(const PropertyMap &qbsProps)
149 {
150     return gen::utils::cppStringModuleProperties(
151                 qbsProps, {QStringLiteral("driverFlags"),
152                            QStringLiteral("driverLinkerFlags")});
153 }
154 
155 } // namespace IarewUtils
156 } // namespace qbs
157