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.

1017 lines
31KB

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