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.

1045 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. Array<File> files;
  431. getFolder().findChildFiles (files, File::findFiles, false);
  432. FileSorter sorter;
  433. files.sort (sorter);
  434. Array<LibraryModule::CompileUnit> units;
  435. for (auto& file : files)
  436. {
  437. if (file.getFileName().startsWithIgnoreCase (getID())
  438. && file.hasFileExtension (sourceFileExtensions))
  439. {
  440. if (forTarget == ProjectType::Target::unspecified
  441. || forTarget == Project::getTargetTypeFromFilePath (file, true))
  442. {
  443. CompileUnit cu;
  444. cu.file = file;
  445. units.add (cu);
  446. }
  447. }
  448. }
  449. for (auto& cu : units)
  450. {
  451. cu.isCompiledForObjC = true;
  452. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  453. if (cu.isCompiledForNonObjC)
  454. if (files.contains (cu.file.withFileExtension ("mm")))
  455. cu.isCompiledForObjC = false;
  456. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  457. }
  458. return units;
  459. }
  460. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  461. ProjectSaver* projectSaver,
  462. Array<File>& result,
  463. ProjectType::Target::Type forTarget) const
  464. {
  465. for (auto& cu : getAllCompileUnits (forTarget))
  466. {
  467. if (cu.isNeededForExporter (exporter))
  468. {
  469. auto localFile = exporter.getProject().getGeneratedCodeFolder()
  470. .getChildFile (cu.getFilenameForProxyFile());
  471. result.add (localFile);
  472. if (projectSaver != nullptr)
  473. projectSaver->addFileToGeneratedGroup (localFile);
  474. }
  475. }
  476. }
  477. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  478. {
  479. auto slash = path.indexOfChar (File::getSeparatorChar());
  480. if (slash >= 0)
  481. {
  482. auto topLevelGroup = path.substring (0, slash);
  483. auto remainingPath = path.substring (slash + 1);
  484. auto newGroup = group.getOrCreateSubGroup (topLevelGroup);
  485. addFileWithGroups (newGroup, file, remainingPath);
  486. }
  487. else
  488. {
  489. if (! group.containsChildForFile (file))
  490. group.addRelativeFile (file, -1, false);
  491. }
  492. }
  493. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  494. {
  495. Array<File> tempList;
  496. FileSorter sorter;
  497. DirectoryIterator iter (folder, true, "*", File::findFiles);
  498. bool isHiddenFile;
  499. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  500. if (! isHiddenFile && iter.getFile().hasFileExtension (browseableFileExtensions))
  501. tempList.addSorted (sorter, iter.getFile());
  502. filesFound.addArray (tempList);
  503. }
  504. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  505. {
  506. if (sourceFiles.isEmpty())
  507. findBrowseableFiles (localModuleFolder, sourceFiles);
  508. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false));
  509. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID()));
  510. auto moduleHeader = moduleInfo.getHeader();
  511. for (auto& sourceFile : sourceFiles)
  512. {
  513. auto pathWithinModule = FileHelpers::getRelativePathFrom (sourceFile, localModuleFolder);
  514. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  515. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  516. if ((exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFile)) && sourceFile != moduleHeader)
  517. addFileWithGroups (sourceGroup,
  518. moduleFromProject.getChildFile (pathWithinModule),
  519. pathWithinModule);
  520. }
  521. sourceGroup.sortAlphabetically (true, true);
  522. sourceGroup.addFileAtIndex (moduleHeader, -1, false);
  523. exporter.getModulesGroup().state.appendChild (sourceGroup.state.createCopy(), nullptr);
  524. }
  525. //==============================================================================
  526. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  527. : project (p), state (s)
  528. {
  529. }
  530. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  531. {
  532. return ModuleDescription (getModuleFolder (moduleID));
  533. }
  534. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  535. {
  536. return state.getChildWithProperty (Ids::ID, moduleID).isValid();
  537. }
  538. bool EnabledModuleList::isAudioPluginModuleMissing() const
  539. {
  540. return project.getProjectType().isAudioPlugin()
  541. && ! isModuleEnabled ("juce_audio_plugin_client");
  542. }
  543. bool EnabledModuleList::shouldUseGlobalPath (const String& moduleID) const
  544. {
  545. return static_cast<bool> (state.getChildWithProperty (Ids::ID, moduleID)
  546. .getProperty (Ids::useGlobalPath));
  547. }
  548. Value EnabledModuleList::getShouldUseGlobalPathValue (const String& moduleID) const
  549. {
  550. return state.getChildWithProperty (Ids::ID, moduleID)
  551. .getPropertyAsValue (Ids::useGlobalPath, getUndoManager());
  552. }
  553. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  554. {
  555. return state.getChildWithProperty (Ids::ID, moduleID)
  556. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  557. }
  558. File EnabledModuleList::findUserModuleFolder (const String& possiblePaths, const String& moduleID)
  559. {
  560. auto paths = StringArray::fromTokens (possiblePaths, ";", {});
  561. for (auto p : paths)
  562. {
  563. auto f = File::createFileWithoutCheckingPath (p.trim());
  564. if (f.exists())
  565. {
  566. auto moduleFolder = getModuleFolderFromPathIfItExists (f.getFullPathName(), moduleID, project);
  567. if (moduleFolder != File())
  568. return moduleFolder;
  569. }
  570. }
  571. return {};
  572. }
  573. File EnabledModuleList::getModuleFolder (const String& moduleID)
  574. {
  575. if (shouldUseGlobalPath (moduleID))
  576. {
  577. if (isJuceModule (moduleID))
  578. return getModuleFolderFromPathIfItExists (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString(), moduleID, project);
  579. return findUserModuleFolder (getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString(), moduleID);
  580. }
  581. {
  582. auto path = getPathToSpecifiedModule (project, moduleID);
  583. if (path != File())
  584. return path;
  585. }
  586. auto paths = getAllPossibleModulePathsFromExporters (project);
  587. for (auto p : paths)
  588. {
  589. auto f = getModuleFolderFromPathIfItExists (p.getFullPathName(), moduleID, project);
  590. if (f != File())
  591. return f;
  592. }
  593. return {};
  594. }
  595. struct ModuleTreeSorter
  596. {
  597. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  598. {
  599. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  600. }
  601. };
  602. void EnabledModuleList::sortAlphabetically()
  603. {
  604. ModuleTreeSorter sorter;
  605. state.sort (sorter, getUndoManager(), false);
  606. }
  607. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  608. {
  609. return state.getChildWithProperty (Ids::ID, moduleID)
  610. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  611. }
  612. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally, bool useGlobalPath)
  613. {
  614. ModuleDescription info (moduleFolder);
  615. if (info.isValid())
  616. {
  617. const String moduleID (info.getID());
  618. if (! isModuleEnabled (moduleID))
  619. {
  620. ValueTree module (Ids::MODULE);
  621. module.setProperty (Ids::ID, moduleID, nullptr);
  622. state.appendChild (module, getUndoManager());
  623. sortAlphabetically();
  624. shouldShowAllModuleFilesInProject (moduleID) = true;
  625. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  626. getShouldUseGlobalPathValue (moduleID) = useGlobalPath;
  627. RelativePath path (moduleFolder.getParentDirectory(),
  628. project.getProjectFolder(), RelativePath::projectFolder);
  629. for (Project::ExporterIterator exporter (project); exporter.next();)
  630. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  631. }
  632. }
  633. }
  634. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  635. {
  636. for (int i = state.getNumChildren(); --i >= 0;)
  637. if (state.getChild(i) [Ids::ID] == moduleID)
  638. state.removeChild (i, getUndoManager());
  639. for (Project::ExporterIterator exporter (project); exporter.next();)
  640. exporter->removePathForModule (moduleID);
  641. }
  642. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  643. {
  644. for (int i = 0; i < getNumModules(); ++i)
  645. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  646. }
  647. StringArray EnabledModuleList::getAllModules() const
  648. {
  649. StringArray moduleIDs;
  650. for (int i = 0; i < getNumModules(); ++i)
  651. moduleIDs.add (getModuleID (i));
  652. return moduleIDs;
  653. }
  654. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  655. {
  656. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  657. for (auto uid : info.getDependencies())
  658. {
  659. if (! dependencies.contains (uid, true))
  660. {
  661. dependencies.add (uid);
  662. getDependencies (project, uid, dependencies);
  663. }
  664. }
  665. }
  666. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  667. {
  668. StringArray dependencies, extraDepsNeeded;
  669. getDependencies (project, moduleID, dependencies);
  670. for (auto dep : dependencies)
  671. if (dep != moduleID && ! isModuleEnabled (dep))
  672. extraDepsNeeded.add (dep);
  673. return extraDepsNeeded;
  674. }
  675. bool EnabledModuleList::doesModuleHaveHigherCppStandardThanProject (const String& moduleID)
  676. {
  677. auto projectCppStandard = project.getCppStandardString();
  678. if (projectCppStandard == "latest")
  679. return false;
  680. auto moduleCppStandard = getModuleInfo (moduleID).getMinimumCppStandard();
  681. return (moduleCppStandard.getIntValue() > projectCppStandard.getIntValue());
  682. }
  683. bool EnabledModuleList::areMostModulesUsingGlobalPath() const
  684. {
  685. auto numYes = 0, numNo = 0;
  686. for (auto i = getNumModules(); --i >= 0;)
  687. {
  688. if (shouldUseGlobalPath (getModuleID (i)))
  689. ++numYes;
  690. else
  691. ++numNo;
  692. }
  693. return numYes > numNo;
  694. }
  695. bool EnabledModuleList::areMostModulesCopiedLocally() const
  696. {
  697. auto numYes = 0, numNo = 0;
  698. for (auto i = getNumModules(); --i >= 0;)
  699. {
  700. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  701. ++numYes;
  702. else
  703. ++numNo;
  704. }
  705. return numYes > numNo;
  706. }
  707. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  708. {
  709. for (int i = getNumModules(); --i >= 0;)
  710. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  711. }
  712. File EnabledModuleList::findGlobalModulesFolder()
  713. {
  714. auto& settings = getAppSettings();
  715. auto path = settings.getStoredPath (Ids::defaultJuceModulePath).toString();
  716. if (settings.isGlobalPathValid (File(), Ids::defaultJuceModulePath, path))
  717. return { path };
  718. return {};
  719. }
  720. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  721. {
  722. auto globalPath = findGlobalModulesFolder();
  723. if (globalPath != File())
  724. return globalPath;
  725. ModuleList available;
  726. available.scanProjectExporterModulePaths (project);
  727. for (int i = available.modules.size(); --i >= 0;)
  728. {
  729. File f (available.modules.getUnchecked(i)->getFolder());
  730. if (f.isDirectory())
  731. return f.getParentDirectory();
  732. }
  733. return File::getCurrentWorkingDirectory();
  734. }
  735. bool EnabledModuleList::isJuceModule (const String& moduleID)
  736. {
  737. static StringArray juceModuleIds =
  738. {
  739. "juce_analytics",
  740. "juce_audio_basics",
  741. "juce_audio_devices",
  742. "juce_audio_formats",
  743. "juce_audio_plugin_client",
  744. "juce_audio_processors",
  745. "juce_audio_utils",
  746. "juce_blocks_basics",
  747. "juce_box2d",
  748. "juce_core",
  749. "juce_cryptography",
  750. "juce_data_structures",
  751. "juce_dsp",
  752. "juce_events",
  753. "juce_graphics",
  754. "juce_gui_basics",
  755. "juce_gui_extra",
  756. "juce_opengl",
  757. "juce_osc",
  758. "juce_product_unlocking",
  759. "juce_video"
  760. };
  761. return juceModuleIds.contains (moduleID);
  762. }
  763. void EnabledModuleList::addModuleFromUserSelectedFile()
  764. {
  765. static File lastLocation (findDefaultModulesFolder (project));
  766. FileChooser fc ("Select a module to add...", lastLocation, String());
  767. if (fc.browseForDirectory())
  768. {
  769. lastLocation = fc.getResult();
  770. addModuleOfferingToCopy (lastLocation, true);
  771. }
  772. }
  773. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  774. {
  775. ModuleList list;
  776. list.scanGlobalJuceModulePath();
  777. if (auto* info = list.getModuleWithID (moduleID))
  778. {
  779. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  780. return;
  781. }
  782. list.scanGlobalUserModulePath();
  783. if (auto* info = list.getModuleWithID (moduleID))
  784. {
  785. addModule (info->moduleFolder, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  786. return;
  787. }
  788. list.scanProjectExporterModulePaths (project);
  789. if (auto* info = list.getModuleWithID (moduleID))
  790. addModule (info->moduleFolder, areMostModulesCopiedLocally(), false);
  791. else
  792. addModuleFromUserSelectedFile();
  793. }
  794. void EnabledModuleList::addModuleOfferingToCopy (const File& f, bool isFromUserSpecifiedFolder)
  795. {
  796. ModuleDescription m (f);
  797. if (! m.isValid())
  798. {
  799. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  800. "Add Module", "This wasn't a valid module folder!");
  801. return;
  802. }
  803. if (isModuleEnabled (m.getID()))
  804. {
  805. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  806. "Add Module", "The project already contains this module!");
  807. return;
  808. }
  809. addModule (m.moduleFolder, areMostModulesCopiedLocally(), isFromUserSpecifiedFolder ? false
  810. : areMostModulesUsingGlobalPath());
  811. }
  812. bool isJuceFolder (const File& f)
  813. {
  814. return isJuceModulesFolder (f.getChildFile ("modules"));
  815. }
  816. bool isJuceModulesFolder (const File& f)
  817. {
  818. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  819. }