The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1007 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "jucer_Module.h"
  21. #include "../ProjectSaving/jucer_ProjectSaver.h"
  22. #include "../ProjectSaving/jucer_ProjectExport_Xcode.h"
  23. //==============================================================================
  24. static var parseModuleDesc (const StringArray& lines)
  25. {
  26. DynamicObject* o = new DynamicObject();
  27. var result (o);
  28. for (auto line : lines)
  29. {
  30. line = trimCommentCharsFromStartOfLine (line);
  31. auto colon = line.indexOfChar (':');
  32. if (colon >= 0)
  33. {
  34. auto key = line.substring (0, colon).trim();
  35. auto value = line.substring (colon + 1).trim();
  36. o->setProperty (key, value);
  37. }
  38. }
  39. return result;
  40. }
  41. static var parseModuleDesc (const File& header)
  42. {
  43. StringArray lines;
  44. header.readLines (lines);
  45. for (int i = 0; i < lines.size(); ++i)
  46. {
  47. if (trimCommentCharsFromStartOfLine (lines[i]).startsWith ("BEGIN_JUCE_MODULE_DECLARATION"))
  48. {
  49. StringArray desc;
  50. for (int j = i + 1; j < lines.size(); ++j)
  51. {
  52. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_MODULE_DECLARATION"))
  53. return parseModuleDesc (desc);
  54. desc.add (lines[j]);
  55. }
  56. break;
  57. }
  58. }
  59. return {};
  60. }
  61. ModuleDescription::ModuleDescription (const File& folder)
  62. : moduleFolder (folder),
  63. moduleInfo (parseModuleDesc (getHeader()))
  64. {
  65. }
  66. File ModuleDescription::getHeader() const
  67. {
  68. if (moduleFolder != File())
  69. {
  70. const char* extensions[] = { ".h", ".hpp", ".hxx" };
  71. for (auto e : extensions)
  72. {
  73. auto header = moduleFolder.getChildFile (moduleFolder.getFileName() + e);
  74. if (header.existsAsFile())
  75. return header;
  76. }
  77. }
  78. return {};
  79. }
  80. StringArray ModuleDescription::getDependencies() const
  81. {
  82. auto deps = StringArray::fromTokens (moduleInfo ["dependencies"].toString(), " \t;,", "\"'");
  83. deps.trim();
  84. deps.removeEmptyStrings();
  85. return deps;
  86. }
  87. //==============================================================================
  88. ModuleList::ModuleList()
  89. {
  90. }
  91. ModuleList::ModuleList (const ModuleList& other)
  92. {
  93. operator= (other);
  94. }
  95. ModuleList& ModuleList::operator= (const ModuleList& other)
  96. {
  97. modules.clear();
  98. modules.addCopiesOf (other.modules);
  99. return *this;
  100. }
  101. const ModuleDescription* ModuleList::getModuleWithID (const String& moduleID) const
  102. {
  103. for (auto* m : modules)
  104. if (m->getID() == moduleID)
  105. return m;
  106. return nullptr;
  107. }
  108. void ModuleList::sort()
  109. {
  110. std::sort (modules.begin(), modules.end(), [] (const ModuleDescription* m1, const ModuleDescription* m2)
  111. {
  112. return m1->getID().compareIgnoreCase (m2->getID()) < 0;
  113. });
  114. }
  115. StringArray ModuleList::getIDs() const
  116. {
  117. StringArray results;
  118. for (auto* m : modules)
  119. results.add (m->getID());
  120. results.sort (true);
  121. return results;
  122. }
  123. Result ModuleList::tryToAddModuleFromFolder (const File& path)
  124. {
  125. ModuleDescription m (path);
  126. if (m.isValid())
  127. {
  128. modules.add (new ModuleDescription (m));
  129. return Result::ok();
  130. }
  131. return Result::fail (path.getFullPathName() + " is not a valid module");
  132. }
  133. Result ModuleList::addAllModulesInFolder (const File& path)
  134. {
  135. if (! tryToAddModuleFromFolder (path))
  136. {
  137. int subfolders = 2;
  138. return addAllModulesInSubfoldersRecursively (path, subfolders);
  139. }
  140. return Result::ok();
  141. }
  142. Result ModuleList::addAllModulesInSubfoldersRecursively (const File& path, int depth)
  143. {
  144. if (depth > 0)
  145. {
  146. for (DirectoryIterator iter (path, false, "*", File::findDirectories); iter.next();)
  147. {
  148. auto childPath = iter.getFile().getLinkedTarget();
  149. if (! tryToAddModuleFromFolder (childPath))
  150. addAllModulesInSubfoldersRecursively (childPath, depth - 1);
  151. }
  152. }
  153. return Result::ok();
  154. }
  155. static File getModuleFolderFromPathIfItExists (const String& path, const String& moduleID, const Project& project)
  156. {
  157. if (path.isNotEmpty())
  158. {
  159. auto moduleFolder = project.resolveFilename (path);
  160. if (moduleFolder.exists())
  161. {
  162. if (ModuleDescription (moduleFolder).getID() == moduleID)
  163. return moduleFolder;
  164. auto f = moduleFolder.getChildFile (moduleID);
  165. if (ModuleDescription (f).getID() == moduleID)
  166. return f;
  167. }
  168. }
  169. return {};
  170. }
  171. static File getPathToSpecifiedModule (Project& project, StringRef moduleID)
  172. {
  173. auto& modules = project.getModules();
  174. if (! modules.shouldUseGlobalPath (moduleID))
  175. {
  176. for (Project::ExporterIterator exporter (project); exporter.next();)
  177. {
  178. if (! exporter->mayCompileOnCurrentOS())
  179. continue;
  180. auto path = getModuleFolderFromPathIfItExists (exporter->getPathForModuleString (moduleID), moduleID, project);
  181. if (path != File())
  182. return path;
  183. }
  184. }
  185. return {};
  186. }
  187. static Array<File> getAllPossibleModulePathsFromExporters (Project& project)
  188. {
  189. StringArray paths;
  190. for (Project::ExporterIterator exporter (project); exporter.next();)
  191. {
  192. auto& modules = project.getModules();
  193. auto n = modules.getNumModules();
  194. for (int i = 0; i < n; ++i)
  195. {
  196. auto id = modules.getModuleID (i);
  197. if (modules.shouldUseGlobalPath (id))
  198. continue;
  199. auto path = exporter->getPathForModuleString (id);
  200. if (path.isNotEmpty())
  201. paths.addIfNotAlreadyThere (path);
  202. }
  203. auto oldPath = exporter->getLegacyModulePath();
  204. if (oldPath.isNotEmpty())
  205. paths.addIfNotAlreadyThere (oldPath);
  206. }
  207. Array<File> files;
  208. for (auto& path : paths)
  209. {
  210. auto f = project.resolveFilename (path);
  211. if (f.isDirectory())
  212. {
  213. files.addIfNotAlreadyThere (f);
  214. if (f.getChildFile ("modules").isDirectory())
  215. files.addIfNotAlreadyThere (f.getChildFile ("modules"));
  216. }
  217. }
  218. return files;
  219. }
  220. Result ModuleList::scanProjectExporterModulePaths (Project& project)
  221. {
  222. modules.clear();
  223. Result result (Result::ok());
  224. for (auto& m : getAllPossibleModulePathsFromExporters (project))
  225. {
  226. result = addAllModulesInFolder (m);
  227. if (result.failed())
  228. break;
  229. }
  230. sort();
  231. return result;
  232. }
  233. void ModuleList::scanGlobalJuceModulePath()
  234. {
  235. modules.clear();
  236. auto& settings = getAppSettings();
  237. auto path = settings.getStoredPath (Ids::defaultJuceModulePath).toString();
  238. if (path.isNotEmpty())
  239. addAllModulesInFolder ({ path });
  240. sort();
  241. }
  242. void ModuleList::scanGlobalUserModulePath()
  243. {
  244. modules.clear();
  245. auto paths = StringArray::fromTokens (getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString(), ";", {});
  246. for (auto p : paths)
  247. {
  248. auto f = File::createFileWithoutCheckingPath (p.trim());
  249. if (f.exists())
  250. addAllModulesInFolder (f);
  251. }
  252. sort();
  253. }
  254. //==============================================================================
  255. LibraryModule::LibraryModule (const ModuleDescription& d)
  256. : moduleInfo (d)
  257. {
  258. }
  259. //==============================================================================
  260. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  261. {
  262. auto& project = projectSaver.project;
  263. auto& modules = project.getModules();
  264. auto id = getID();
  265. if (modules.shouldCopyModuleFilesLocally (id).getValue())
  266. {
  267. auto juceModuleFolder = moduleInfo.getFolder();
  268. auto localModuleFolder = project.getLocalModuleFolder (id);
  269. localModuleFolder.createDirectory();
  270. projectSaver.copyFolder (juceModuleFolder, localModuleFolder);
  271. }
  272. out << "#include <" << moduleInfo.moduleFolder.getFileName() << "/"
  273. << moduleInfo.getHeader().getFileName()
  274. << ">" << newLine;
  275. }
  276. //==============================================================================
  277. static void parseAndAddLibs (StringArray& libList, const String& libs)
  278. {
  279. libList.addTokens (libs, ", ", {});
  280. libList.trim();
  281. libList.sort (false);
  282. libList.removeDuplicates (false);
  283. }
  284. void LibraryModule::addSettingsForModuleToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  285. {
  286. auto& project = exporter.getProject();
  287. auto moduleRelativePath = exporter.getModuleFolderRelativeToProject (getID());
  288. exporter.addToExtraSearchPaths (moduleRelativePath.getParentDirectory());
  289. String libDirPlatform;
  290. if (exporter.isLinux())
  291. libDirPlatform = "Linux";
  292. else if (exporter.isCodeBlocks() && exporter.isWindows())
  293. libDirPlatform = "MinGW";
  294. else
  295. libDirPlatform = exporter.getTargetFolder().getFileName();
  296. auto libSubdirPath = moduleRelativePath.toUnixStyle() + "/libs/" + libDirPlatform;
  297. auto moduleLibDir = File (project.getProjectFolder().getFullPathName() + "/" + libSubdirPath);
  298. if (moduleLibDir.exists())
  299. exporter.addToModuleLibPaths ({ libSubdirPath, moduleRelativePath.getRoot() });
  300. auto extraInternalSearchPaths = moduleInfo.getExtraSearchPaths().trim();
  301. if (extraInternalSearchPaths.isNotEmpty())
  302. {
  303. auto paths = StringArray::fromTokens (extraInternalSearchPaths, true);
  304. for (auto& path : paths)
  305. exporter.addToExtraSearchPaths (moduleRelativePath.getChildFile (path.unquoted()));
  306. }
  307. {
  308. auto extraDefs = moduleInfo.getPreprocessorDefs().trim();
  309. if (extraDefs.isNotEmpty())
  310. exporter.getExporterPreprocessorDefsValue() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  311. }
  312. {
  313. Array<File> compiled;
  314. auto& modules = project.getModules();
  315. auto id = getID();
  316. auto localModuleFolder = modules.shouldCopyModuleFilesLocally (id).getValue() ? project.getLocalModuleFolder (id)
  317. : moduleInfo.getFolder();
  318. findAndAddCompiledUnits (exporter, &projectSaver, compiled);
  319. if (modules.shouldShowAllModuleFilesInProject (id).getValue())
  320. addBrowseableCode (exporter, compiled, localModuleFolder);
  321. }
  322. if (exporter.isXcode())
  323. {
  324. auto& xcodeExporter = dynamic_cast<XcodeProjectExporter&> (exporter);
  325. if (project.isAUPluginHost())
  326. xcodeExporter.xcodeFrameworks.addTokens (xcodeExporter.isOSX() ? "AudioUnit CoreAudioKit" : "CoreAudioKit", false);
  327. auto frameworks = moduleInfo.moduleInfo [xcodeExporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString();
  328. xcodeExporter.xcodeFrameworks.addTokens (frameworks, ", ", {});
  329. parseAndAddLibs (xcodeExporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  330. }
  331. else if (exporter.isLinux())
  332. {
  333. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["linuxLibs"].toString());
  334. parseAndAddLibs (exporter.linuxPackages, moduleInfo.moduleInfo ["linuxPackages"].toString());
  335. }
  336. else if (exporter.isWindows())
  337. {
  338. if (exporter.isCodeBlocks())
  339. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  340. else
  341. parseAndAddLibs (exporter.windowsLibs, moduleInfo.moduleInfo ["windowsLibs"].toString());
  342. }
  343. else if (exporter.isAndroid())
  344. {
  345. parseAndAddLibs (exporter.androidLibs, moduleInfo.moduleInfo ["androidLibs"].toString());
  346. }
  347. }
  348. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  349. {
  350. auto header = moduleInfo.getHeader();
  351. jassert (header.exists());
  352. StringArray lines;
  353. header.readLines (lines);
  354. for (int i = 0; i < lines.size(); ++i)
  355. {
  356. auto line = lines[i].trim();
  357. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  358. {
  359. ScopedPointer<Project::ConfigFlag> config (new Project::ConfigFlag());
  360. config->sourceModuleID = getID();
  361. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  362. if (config->symbol.length() > 2)
  363. {
  364. ++i;
  365. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  366. {
  367. if (lines[i].trim().isNotEmpty())
  368. config->description = config->description.trim() + " " + lines[i].trim();
  369. ++i;
  370. }
  371. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  372. config->value = project.getConfigFlag (config->symbol);
  373. i += 2;
  374. if (lines[i].contains ("#define " + config->symbol))
  375. {
  376. auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim();
  377. config->value.setDefault (value == "0" ? false : true);
  378. }
  379. auto currentValue = config->value.get().toString();
  380. if (currentValue == "enabled") config->value = true;
  381. else if (currentValue == "disabled") config->value = false;
  382. flags.add (config.release());
  383. }
  384. }
  385. }
  386. }
  387. //==============================================================================
  388. struct FileSorter
  389. {
  390. static int compareElements (const File& f1, const File& f2)
  391. {
  392. return f1.getFileName().compareNatural (f2.getFileName());
  393. }
  394. };
  395. bool LibraryModule::CompileUnit::hasSuffix (const File& f, const char* suffix)
  396. {
  397. auto fileWithoutSuffix = f.getFileNameWithoutExtension() + ".";
  398. return fileWithoutSuffix.containsIgnoreCase (suffix + String ("."))
  399. || fileWithoutSuffix.containsIgnoreCase (suffix + String ("_"));
  400. }
  401. void LibraryModule::CompileUnit::writeInclude (MemoryOutputStream&) const
  402. {
  403. }
  404. bool LibraryModule::CompileUnit::isNeededForExporter (ProjectExporter& exporter) const
  405. {
  406. if ((hasSuffix (file, "_OSX") && ! exporter.isOSX())
  407. || (hasSuffix (file, "_iOS") && ! exporter.isiOS())
  408. || (hasSuffix (file, "_Windows") && ! exporter.isWindows())
  409. || (hasSuffix (file, "_Linux") && ! exporter.isLinux())
  410. || (hasSuffix (file, "_Android") && ! exporter.isAndroid()))
  411. return false;
  412. auto targetType = Project::getTargetTypeFromFilePath (file, false);
  413. if (targetType != ProjectType::Target::unspecified && ! exporter.shouldBuildTargetType (targetType))
  414. return false;
  415. return exporter.usesMMFiles() ? isCompiledForObjC
  416. : isCompiledForNonObjC;
  417. }
  418. String LibraryModule::CompileUnit::getFilenameForProxyFile() const
  419. {
  420. return "include_" + file.getFileName();
  421. }
  422. Array<LibraryModule::CompileUnit> LibraryModule::getAllCompileUnits (ProjectType::Target::Type forTarget) const
  423. {
  424. auto files = getFolder().findChildFiles (File::findFiles, false);
  425. FileSorter sorter;
  426. files.sort (sorter);
  427. Array<LibraryModule::CompileUnit> units;
  428. for (auto& file : files)
  429. {
  430. if (file.getFileName().startsWithIgnoreCase (getID())
  431. && file.hasFileExtension (sourceFileExtensions))
  432. {
  433. if (forTarget == ProjectType::Target::unspecified
  434. || forTarget == Project::getTargetTypeFromFilePath (file, true))
  435. {
  436. CompileUnit cu;
  437. cu.file = file;
  438. units.add (cu);
  439. }
  440. }
  441. }
  442. for (auto& cu : units)
  443. {
  444. cu.isCompiledForObjC = true;
  445. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  446. if (cu.isCompiledForNonObjC)
  447. if (files.contains (cu.file.withFileExtension ("mm")))
  448. cu.isCompiledForObjC = false;
  449. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  450. }
  451. return units;
  452. }
  453. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  454. ProjectSaver* projectSaver,
  455. Array<File>& result,
  456. ProjectType::Target::Type forTarget) const
  457. {
  458. for (auto& cu : getAllCompileUnits (forTarget))
  459. {
  460. if (cu.isNeededForExporter (exporter))
  461. {
  462. auto localFile = exporter.getProject().getGeneratedCodeFolder()
  463. .getChildFile (cu.getFilenameForProxyFile());
  464. result.add (localFile);
  465. if (projectSaver != nullptr)
  466. projectSaver->addFileToGeneratedGroup (localFile);
  467. }
  468. }
  469. }
  470. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  471. {
  472. auto slash = path.indexOfChar (File::getSeparatorChar());
  473. if (slash >= 0)
  474. {
  475. auto topLevelGroup = path.substring (0, slash);
  476. auto remainingPath = path.substring (slash + 1);
  477. auto newGroup = group.getOrCreateSubGroup (topLevelGroup);
  478. addFileWithGroups (newGroup, file, remainingPath);
  479. }
  480. else
  481. {
  482. if (! group.containsChildForFile (file))
  483. group.addRelativeFile (file, -1, false);
  484. }
  485. }
  486. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  487. {
  488. Array<File> tempList;
  489. FileSorter sorter;
  490. DirectoryIterator iter (folder, true, "*", File::findFiles);
  491. bool isHiddenFile;
  492. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  493. if (! isHiddenFile && iter.getFile().hasFileExtension (browseableFileExtensions))
  494. tempList.addSorted (sorter, iter.getFile());
  495. filesFound.addArray (tempList);
  496. }
  497. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  498. {
  499. if (sourceFiles.isEmpty())
  500. findBrowseableFiles (localModuleFolder, sourceFiles);
  501. auto sourceGroup = Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false);
  502. auto moduleFromProject = exporter.getModuleFolderRelativeToProject (getID());
  503. auto moduleHeader = moduleInfo.getHeader();
  504. for (auto& sourceFile : sourceFiles)
  505. {
  506. auto pathWithinModule = FileHelpers::getRelativePathFrom (sourceFile, localModuleFolder);
  507. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  508. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  509. if ((exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFile)) && sourceFile != moduleHeader)
  510. addFileWithGroups (sourceGroup,
  511. moduleFromProject.getChildFile (pathWithinModule),
  512. pathWithinModule);
  513. }
  514. sourceGroup.sortAlphabetically (true, true);
  515. sourceGroup.addFileAtIndex (moduleHeader, -1, false);
  516. exporter.getModulesGroup().state.appendChild (sourceGroup.state.createCopy(), nullptr);
  517. }
  518. //==============================================================================
  519. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  520. : project (p), state (s)
  521. {
  522. }
  523. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  524. {
  525. return ModuleDescription (getModuleFolder (moduleID));
  526. }
  527. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  528. {
  529. return state.getChildWithProperty (Ids::ID, moduleID).isValid();
  530. }
  531. bool EnabledModuleList::isAudioPluginModuleMissing() const
  532. {
  533. return project.getProjectType().isAudioPlugin()
  534. && ! isModuleEnabled ("juce_audio_plugin_client");
  535. }
  536. bool EnabledModuleList::shouldUseGlobalPath (const String& moduleID) const
  537. {
  538. return static_cast<bool> (state.getChildWithProperty (Ids::ID, moduleID)
  539. .getProperty (Ids::useGlobalPath));
  540. }
  541. Value EnabledModuleList::getShouldUseGlobalPathValue (const String& moduleID) const
  542. {
  543. return state.getChildWithProperty (Ids::ID, moduleID)
  544. .getPropertyAsValue (Ids::useGlobalPath, getUndoManager());
  545. }
  546. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  547. {
  548. return state.getChildWithProperty (Ids::ID, moduleID)
  549. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  550. }
  551. File EnabledModuleList::findUserModuleFolder (const String& possiblePaths, const String& moduleID)
  552. {
  553. auto paths = StringArray::fromTokens (possiblePaths, ";", {});
  554. for (auto p : paths)
  555. {
  556. auto f = File::createFileWithoutCheckingPath (p.trim());
  557. if (f.exists())
  558. {
  559. auto moduleFolder = getModuleFolderFromPathIfItExists (f.getFullPathName(), moduleID, project);
  560. if (moduleFolder != File())
  561. return moduleFolder;
  562. }
  563. }
  564. return {};
  565. }
  566. File EnabledModuleList::getModuleFolder (const String& moduleID)
  567. {
  568. if (shouldUseGlobalPath (moduleID))
  569. {
  570. if (isJUCEModule (moduleID))
  571. return getModuleFolderFromPathIfItExists (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString(), moduleID, project);
  572. return findUserModuleFolder (getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString(), moduleID);
  573. }
  574. {
  575. auto path = getPathToSpecifiedModule (project, moduleID);
  576. if (path != File())
  577. return path;
  578. }
  579. auto paths = getAllPossibleModulePathsFromExporters (project);
  580. for (auto p : paths)
  581. {
  582. auto f = getModuleFolderFromPathIfItExists (p.getFullPathName(), moduleID, project);
  583. if (f != File())
  584. return f;
  585. }
  586. return {};
  587. }
  588. struct ModuleTreeSorter
  589. {
  590. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  591. {
  592. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  593. }
  594. };
  595. void EnabledModuleList::sortAlphabetically()
  596. {
  597. ModuleTreeSorter sorter;
  598. state.sort (sorter, getUndoManager(), false);
  599. }
  600. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  601. {
  602. return state.getChildWithProperty (Ids::ID, moduleID)
  603. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  604. }
  605. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally, bool useGlobalPath)
  606. {
  607. ModuleDescription info (moduleFolder);
  608. if (info.isValid())
  609. {
  610. auto moduleID = info.getID();
  611. if (! isModuleEnabled (moduleID))
  612. {
  613. ValueTree module (Ids::MODULE);
  614. module.setProperty (Ids::ID, moduleID, nullptr);
  615. state.appendChild (module, getUndoManager());
  616. sortAlphabetically();
  617. shouldShowAllModuleFilesInProject (moduleID) = true;
  618. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  619. getShouldUseGlobalPathValue (moduleID) = useGlobalPath;
  620. RelativePath path (moduleFolder.getParentDirectory(),
  621. project.getProjectFolder(), RelativePath::projectFolder);
  622. for (Project::ExporterIterator exporter (project); exporter.next();)
  623. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  624. }
  625. }
  626. }
  627. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  628. {
  629. for (auto i = state.getNumChildren(); --i >= 0;)
  630. if (state.getChild(i) [Ids::ID] == moduleID)
  631. state.removeChild (i, getUndoManager());
  632. for (Project::ExporterIterator exporter (project); exporter.next();)
  633. exporter->removePathForModule (moduleID);
  634. }
  635. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  636. {
  637. for (int i = 0; i < getNumModules(); ++i)
  638. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  639. }
  640. StringArray EnabledModuleList::getAllModules() const
  641. {
  642. StringArray moduleIDs;
  643. for (int i = 0; i < getNumModules(); ++i)
  644. moduleIDs.add (getModuleID (i));
  645. return moduleIDs;
  646. }
  647. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  648. {
  649. auto info = project.getModules().getModuleInfo (moduleID);
  650. for (auto uid : info.getDependencies())
  651. {
  652. if (! dependencies.contains (uid, true))
  653. {
  654. dependencies.add (uid);
  655. getDependencies (project, uid, dependencies);
  656. }
  657. }
  658. }
  659. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  660. {
  661. StringArray dependencies, extraDepsNeeded;
  662. getDependencies (project, moduleID, dependencies);
  663. for (auto dep : dependencies)
  664. if (dep != moduleID && ! isModuleEnabled (dep))
  665. extraDepsNeeded.add (dep);
  666. return extraDepsNeeded;
  667. }
  668. bool EnabledModuleList::doesModuleHaveHigherCppStandardThanProject (const String& moduleID)
  669. {
  670. auto projectCppStandard = project.getCppStandardString();
  671. if (projectCppStandard == "latest")
  672. return false;
  673. auto moduleCppStandard = getModuleInfo (moduleID).getMinimumCppStandard();
  674. return (moduleCppStandard.getIntValue() > projectCppStandard.getIntValue());
  675. }
  676. bool EnabledModuleList::areMostModulesUsingGlobalPath() const
  677. {
  678. int numYes = 0, numNo = 0;
  679. for (auto i = getNumModules(); --i >= 0;)
  680. {
  681. if (shouldUseGlobalPath (getModuleID (i)))
  682. ++numYes;
  683. else
  684. ++numNo;
  685. }
  686. return numYes > numNo;
  687. }
  688. bool EnabledModuleList::areMostModulesCopiedLocally() const
  689. {
  690. int numYes = 0, numNo = 0;
  691. for (auto i = getNumModules(); --i >= 0;)
  692. {
  693. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  694. ++numYes;
  695. else
  696. ++numNo;
  697. }
  698. return numYes > numNo;
  699. }
  700. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  701. {
  702. for (auto i = getNumModules(); --i >= 0;)
  703. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  704. }
  705. File EnabledModuleList::findGlobalModulesFolder()
  706. {
  707. auto& settings = getAppSettings();
  708. auto path = settings.getStoredPath (Ids::defaultJuceModulePath).toString();
  709. if (settings.isGlobalPathValid ({}, Ids::defaultJuceModulePath, path))
  710. return { path };
  711. return {};
  712. }
  713. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  714. {
  715. auto globalPath = findGlobalModulesFolder();
  716. if (globalPath != File())
  717. return globalPath;
  718. ModuleList available;
  719. available.scanProjectExporterModulePaths (project);
  720. for (auto i = available.modules.size(); --i >= 0;)
  721. {
  722. auto f = available.modules.getUnchecked(i)->getFolder();
  723. if (f.isDirectory())
  724. return f.getParentDirectory();
  725. }
  726. return File::getCurrentWorkingDirectory();
  727. }
  728. void EnabledModuleList::addModuleFromUserSelectedFile()
  729. {
  730. static auto lastLocation = findDefaultModulesFolder (project);
  731. FileChooser fc ("Select a module to add...", lastLocation, {});
  732. if (fc.browseForDirectory())
  733. {
  734. lastLocation = fc.getResult();
  735. addModuleOfferingToCopy (lastLocation, true);
  736. }
  737. }
  738. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  739. {
  740. ModuleList list;
  741. list.scanGlobalJuceModulePath();
  742. if (auto* info = list.getModuleWithID (moduleID))
  743. {
  744. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  745. return;
  746. }
  747. list.scanGlobalUserModulePath();
  748. if (auto* info = list.getModuleWithID (moduleID))
  749. {
  750. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  751. return;
  752. }
  753. list.scanProjectExporterModulePaths (project);
  754. if (auto* info = list.getModuleWithID (moduleID))
  755. addModule (info->moduleFolder, areMostModulesCopiedLocally(), false);
  756. else
  757. addModuleFromUserSelectedFile();
  758. }
  759. void EnabledModuleList::addModuleOfferingToCopy (const File& f, bool isFromUserSpecifiedFolder)
  760. {
  761. ModuleDescription m (f);
  762. if (! m.isValid())
  763. {
  764. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  765. "Add Module", "This wasn't a valid module folder!");
  766. return;
  767. }
  768. if (isModuleEnabled (m.getID()))
  769. {
  770. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  771. "Add Module", "The project already contains this module!");
  772. return;
  773. }
  774. addModule (m.moduleFolder, areMostModulesCopiedLocally(), isFromUserSpecifiedFolder ? false
  775. : areMostModulesUsingGlobalPath());
  776. }
  777. bool isJUCEFolder (const File& f)
  778. {
  779. return isJUCEModulesFolder (f.getChildFile ("modules"));
  780. }
  781. bool isJUCEModulesFolder (const File& f)
  782. {
  783. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  784. }