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.

1032 lines
31KB

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