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.

986 lines
29KB

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