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.

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