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.

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