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.

962 lines
29KB

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