|
59 | 59 | #endif |
60 | 60 | #include <QtCore/QTextStream> |
61 | 61 | #include <QtCore/QVariant> |
| 62 | +#include <QtCore/QDir> |
62 | 63 |
|
63 | 64 | static QString strip_template_args(const QString &name) |
64 | 65 | { |
@@ -413,6 +414,39 @@ AbstractMetaClass* AbstractMetaBuilder::getGlobalNamespace(const TypeEntry* type |
413 | 414 | return global; |
414 | 415 | } |
415 | 416 |
|
| 417 | +Include AbstractMetaBuilder::getRelativeInclude(const QString& path) |
| 418 | +{ |
| 419 | + QString bestRelativePath; |
| 420 | + int bestNumDirectories = 0; |
| 421 | + // find the shortest relative path relative to all given include directories |
| 422 | + for (QString includePath : m_include_paths) { |
| 423 | + QDir includeDir(includePath); |
| 424 | + QString relativePath = includeDir.relativeFilePath(path); |
| 425 | + QFileInfo info(relativePath); |
| 426 | + if (!info.isRelative()) { |
| 427 | + // not relative - different drives? |
| 428 | + continue; |
| 429 | + } |
| 430 | + int numDirectories = relativePath.count('/'); |
| 431 | + if (bestRelativePath.isEmpty() || numDirectories < bestNumDirectories) { |
| 432 | + bestRelativePath = relativePath; |
| 433 | + bestNumDirectories = numDirectories; |
| 434 | + if (numDirectories == 0) { |
| 435 | + // optimal relative path |
| 436 | + break; |
| 437 | + } |
| 438 | + } |
| 439 | + } |
| 440 | + if (bestRelativePath.isEmpty()) { |
| 441 | + // This shouldn't happen |
| 442 | + QFileInfo info(path); |
| 443 | + return Include(Include::IncludePath, info.fileName()); |
| 444 | + } |
| 445 | + else { |
| 446 | + return Include(Include::IncludePath, bestRelativePath); |
| 447 | + } |
| 448 | +} |
| 449 | + |
416 | 450 | bool AbstractMetaBuilder::build() |
417 | 451 | { |
418 | 452 | Q_ASSERT(!m_file_name.isEmpty()); |
@@ -756,13 +790,11 @@ AbstractMetaClass *AbstractMetaBuilder::traverseNamespace(NamespaceModelItem nam |
756 | 790 | m_namespace_prefix = currentScope()->qualifiedName().join("::"); |
757 | 791 |
|
758 | 792 | if (!type->include().isValid()) { |
759 | | - QFileInfo info(namespace_item->fileName()); |
760 | | - type->setInclude(Include(Include::IncludePath, info.fileName())); |
| 793 | + type->setInclude(getRelativeInclude(namespace_item->fileName())); |
761 | 794 | } |
762 | 795 | // namespace items might come from different include files: |
763 | 796 | for (const QString& oneIncludeFile : includeFiles) { |
764 | | - QFileInfo info(oneIncludeFile); |
765 | | - type->addExtraInclude(Include(Include::IncludePath, info.fileName())); |
| 797 | + type->addExtraInclude(getRelativeInclude(oneIncludeFile)); |
766 | 798 | } |
767 | 799 |
|
768 | 800 | return meta_class; |
@@ -837,8 +869,7 @@ AbstractMetaEnum *AbstractMetaBuilder::traverseEnum(EnumModelItem enum_item, Abs |
837 | 869 | m_enum_values[meta_enum_value->name()] = meta_enum_value; |
838 | 870 | } |
839 | 871 |
|
840 | | - QFileInfo info(enum_item->fileName()); |
841 | | - meta_enum->typeEntry()->setInclude(Include(Include::IncludePath, info.fileName())); |
| 872 | + meta_enum->typeEntry()->setInclude(getRelativeInclude(enum_item->fileName())); |
842 | 873 |
|
843 | 874 | m_enums << meta_enum; |
844 | 875 |
|
@@ -872,8 +903,7 @@ AbstractMetaClass *AbstractMetaBuilder::traverseTypeAlias(TypeAliasModelItem typ |
872 | 903 |
|
873 | 904 | // Set the default include file name |
874 | 905 | if (!type->include().isValid()) { |
875 | | - QFileInfo info(typeAlias->fileName()); |
876 | | - type->setInclude(Include(Include::IncludePath, info.fileName())); |
| 906 | + type->setInclude(getRelativeInclude(typeAlias->fileName())); |
877 | 907 | } |
878 | 908 |
|
879 | 909 | return meta_class; |
@@ -986,8 +1016,7 @@ AbstractMetaClass *AbstractMetaBuilder::traverseClass(ClassModelItem class_item) |
986 | 1016 | // Set the default include file name. In case we saw an template instance earlier, |
987 | 1017 | // overwrite the include file when we see the actual declaration. |
988 | 1018 | if (!type->include().isValid() || class_item->hasActualDeclaration()) { |
989 | | - QFileInfo info(class_item->fileName()); |
990 | | - type->setInclude(Include(Include::IncludePath, info.fileName())); |
| 1019 | + type->setInclude(getRelativeInclude(class_item->fileName())); |
991 | 1020 | } |
992 | 1021 |
|
993 | 1022 | return meta_class; |
|
0 commit comments