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.

1044 lines
32KB

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