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.

558 lines
22KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include <juce_build_tools/juce_build_tools.h>
  19. #include <fstream>
  20. #include <unordered_map>
  21. namespace
  22. {
  23. constexpr auto headerTemplate = R"(/*
  24. IMPORTANT! This file is auto-generated.
  25. If you alter its contents, your changes may be overwritten!
  26. This is the header file that your files should include in order to get all the
  27. JUCE library headers. You should avoid including the JUCE headers directly in
  28. your own source files, because that wouldn't pick up the correct configuration
  29. options for your app.
  30. */
  31. #pragma once
  32. ${JUCE_INCLUDES}
  33. #if JUCE_TARGET_HAS_BINARY_DATA
  34. #include "BinaryData.h"
  35. #endif
  36. #if ! DONT_SET_USING_JUCE_NAMESPACE
  37. // If your code uses a lot of JUCE classes, then this will obviously save you
  38. // a lot of typing, but can be disabled by setting DONT_SET_USING_JUCE_NAMESPACE.
  39. using namespace juce;
  40. #endif
  41. #if ! JUCE_DONT_DECLARE_PROJECTINFO
  42. namespace ProjectInfo
  43. {
  44. const char* const projectName = "${JUCE_EXECUTABLE_NAME}";
  45. const char* const companyName = "${JUCE_COMPANY_NAME}";
  46. const char* const versionString = "${JUCE_PROJECT_VERSION}";
  47. const int versionNumber = ${JUCE_PROJECT_VERSION_HEX};
  48. }
  49. #endif
  50. )";
  51. int writeBinaryData (juce::ArgumentList&& args)
  52. {
  53. args.checkMinNumArguments (4);
  54. const auto namespaceName = args.arguments.removeAndReturn (0);
  55. const auto headerName = args.arguments.removeAndReturn (0);
  56. const auto outFolder = args.arguments.removeAndReturn (0).resolveAsExistingFolder();
  57. const auto inputFileList = args.arguments.removeAndReturn (0).resolveAsExistingFile();
  58. juce::build_tools::ResourceFile resourceFile;
  59. resourceFile.setClassName (namespaceName.text);
  60. const auto lineEndings = args.removeOptionIfFound ("--windows") ? "\r\n" : "\n";
  61. const auto allLines = [&]
  62. {
  63. auto lines = juce::StringArray::fromLines (inputFileList.loadFileAsString());
  64. lines.removeEmptyStrings();
  65. return lines;
  66. }();
  67. for (const auto& arg : allLines)
  68. resourceFile.addFile (juce::File (arg));
  69. const auto result = resourceFile.write (0,
  70. lineEndings,
  71. outFolder.getChildFile (headerName.text),
  72. [&outFolder] (int index)
  73. {
  74. return outFolder.getChildFile ("./BinaryData" + juce::String { index + 1 } + ".cpp");
  75. });
  76. if (result.result.failed())
  77. juce::ConsoleApplication::fail (result.result.getErrorMessage(), 1);
  78. return 0;
  79. }
  80. struct IconParseResults final
  81. {
  82. juce::build_tools::Icons icons;
  83. juce::File output;
  84. };
  85. IconParseResults parseIconArguments (juce::ArgumentList&& args)
  86. {
  87. args.checkMinNumArguments (2);
  88. const auto output = args.arguments.removeAndReturn (0);
  89. const auto popDrawable = [&args]() -> std::unique_ptr<juce::Drawable>
  90. {
  91. if (args.size() == 0)
  92. return {};
  93. const auto firstArgText = args.arguments.removeAndReturn (0).text;
  94. return juce::Drawable::createFromImageFile (firstArgText);
  95. };
  96. auto smallIcon = popDrawable();
  97. auto bigIcon = popDrawable();
  98. return { { std::move (smallIcon), std::move (bigIcon) }, output.text };
  99. }
  100. int writeMacIcon (juce::ArgumentList&& argumentList)
  101. {
  102. const auto parsed = parseIconArguments (std::move (argumentList));
  103. juce::build_tools::writeMacIcon (parsed.icons, parsed.output);
  104. return 0;
  105. }
  106. int writeiOSAssets (juce::ArgumentList&& argumentList)
  107. {
  108. const auto parsed = parseIconArguments (std::move (argumentList));
  109. juce::build_tools::createXcassetsFolderFromIcons (parsed.icons,
  110. parsed.output.getParentDirectory(),
  111. parsed.output.getFileName());
  112. return 0;
  113. }
  114. int writeWinIcon (juce::ArgumentList&& argumentList)
  115. {
  116. const auto parsed = parseIconArguments (std::move (argumentList));
  117. juce::build_tools::writeWinIcon (parsed.icons, parsed.output);
  118. return 0;
  119. }
  120. std::unordered_map<juce::String, juce::String> parseProjectData (const juce::File& file)
  121. {
  122. constexpr auto recordSeparator = "\x1e";
  123. const auto contents = file.loadFileAsString();
  124. const auto lines = juce::StringArray::fromTokens (contents, recordSeparator, {});
  125. std::unordered_map<juce::String, juce::String> result;
  126. constexpr auto unitSeparator = "\x1f";
  127. for (const auto& line : lines)
  128. {
  129. if (line.isEmpty())
  130. continue;
  131. result.emplace (line.upToFirstOccurrenceOf (unitSeparator, false, false),
  132. line.fromFirstOccurrenceOf (unitSeparator, false, false));
  133. }
  134. return result;
  135. }
  136. juce::String getStringValue (const std::unordered_map<juce::String, juce::String>& dict,
  137. juce::StringRef key)
  138. {
  139. const auto it = dict.find (key);
  140. return it != dict.cend() ? it->second : juce::String{};
  141. }
  142. bool getBoolValue (const std::unordered_map<juce::String, juce::String>& dict, juce::StringRef key)
  143. {
  144. const auto str = getStringValue (dict, key);
  145. return str.equalsIgnoreCase ("yes")
  146. || str.equalsIgnoreCase ("true")
  147. || str.equalsIgnoreCase ("1")
  148. || str.equalsIgnoreCase ("on");
  149. }
  150. struct UpdateField final
  151. {
  152. const std::unordered_map<juce::String, juce::String>& dict;
  153. void operator() (juce::StringRef key, juce::String& value) const
  154. {
  155. value = getStringValue (dict, key);
  156. }
  157. void operator() (juce::StringRef key, juce::File& value) const
  158. {
  159. value = getStringValue (dict, key);
  160. }
  161. void operator() (juce::StringRef key, bool& value) const
  162. {
  163. value = getBoolValue (dict, key);
  164. }
  165. void operator() (juce::StringRef key, juce::StringArray& value) const
  166. {
  167. value = juce::StringArray::fromTokens (getStringValue (dict, key), ";", {});
  168. }
  169. };
  170. void setIfEmpty (juce::String& field, juce::StringRef fallback)
  171. {
  172. if (field.isEmpty())
  173. field = fallback;
  174. }
  175. juce::build_tools::PlistOptions parsePlistOptions (const juce::File& file,
  176. juce::build_tools::ProjectType::Target::Type type)
  177. {
  178. if (type == juce::build_tools::ProjectType::Target::ConsoleApp)
  179. juce::ConsoleApplication::fail ("Deduced project type does not require a plist", 1);
  180. const auto dict = parseProjectData (file);
  181. UpdateField updateField { dict };
  182. juce::build_tools::PlistOptions result;
  183. updateField ("EXECUTABLE_NAME", result.executableName);
  184. updateField ("PLIST_TO_MERGE", result.plistToMerge);
  185. updateField ("IS_IOS", result.iOS);
  186. updateField ("MICROPHONE_PERMISSION_ENABLED", result.microphonePermissionEnabled);
  187. updateField ("MICROPHONE_PERMISSION_TEXT", result.microphonePermissionText);
  188. updateField ("CAMERA_PERMISSION_ENABLED", result.cameraPermissionEnabled);
  189. updateField ("CAMERA_PERMISSION_TEXT", result.cameraPermissionText);
  190. updateField ("BLUETOOTH_PERMISSION_ENABLED", result.bluetoothPermissionEnabled);
  191. updateField ("BLUETOOTH_PERMISSION_TEXT", result.bluetoothPermissionText);
  192. updateField ("SEND_APPLE_EVENTS_PERMISSION_ENABLED", result.sendAppleEventsPermissionEnabled);
  193. updateField ("SEND_APPLE_EVENTS_PERMISSION_TEXT", result.sendAppleEventsPermissionText);
  194. updateField ("SHOULD_ADD_STORYBOARD", result.shouldAddStoryboardToProject);
  195. updateField ("LAUNCH_STORYBOARD_FILE", result.storyboardName);
  196. updateField ("PROJECT_NAME", result.projectName);
  197. updateField ("VERSION", result.marketingVersion);
  198. updateField ("BUILD_VERSION", result.currentProjectVersion);
  199. updateField ("COMPANY_COPYRIGHT", result.companyCopyright);
  200. updateField ("DOCUMENT_EXTENSIONS", result.documentExtensions);
  201. updateField ("FILE_SHARING_ENABLED", result.fileSharingEnabled);
  202. updateField ("DOCUMENT_BROWSER_ENABLED", result.documentBrowserEnabled);
  203. updateField ("STATUS_BAR_HIDDEN", result.statusBarHidden);
  204. updateField ("REQUIRES_FULL_SCREEN", result.requiresFullScreen);
  205. updateField ("BACKGROUND_AUDIO_ENABLED", result.backgroundAudioEnabled);
  206. updateField ("BACKGROUND_BLE_ENABLED", result.backgroundBleEnabled);
  207. updateField ("PUSH_NOTIFICATIONS_ENABLED", result.pushNotificationsEnabled);
  208. updateField ("PLUGIN_MANUFACTURER_CODE", result.pluginManufacturerCode);
  209. updateField ("PLUGIN_CODE", result.pluginCode);
  210. updateField ("IPHONE_SCREEN_ORIENTATIONS", result.iPhoneScreenOrientations);
  211. updateField ("IPAD_SCREEN_ORIENTATIONS", result.iPadScreenOrientations);
  212. updateField ("PLUGIN_NAME", result.pluginName);
  213. updateField ("PLUGIN_MANUFACTURER", result.pluginManufacturer);
  214. updateField ("PLUGIN_DESCRIPTION", result.pluginDescription);
  215. updateField ("PLUGIN_AU_EXPORT_PREFIX", result.pluginAUExportPrefix);
  216. updateField ("PLUGIN_AU_MAIN_TYPE", result.auMainType);
  217. updateField ("IS_AU_SANDBOX_SAFE", result.isAuSandboxSafe);
  218. updateField ("IS_PLUGIN_SYNTH", result.isPluginSynth);
  219. updateField ("SUPPRESS_AU_PLIST_RESOURCE_USAGE", result.suppressResourceUsage);
  220. updateField ("BUNDLE_ID", result.bundleIdentifier);
  221. updateField ("ICON_FILE", result.iconFile);
  222. result.type = type;
  223. if (result.storyboardName.isNotEmpty())
  224. result.storyboardName = result.storyboardName.fromLastOccurrenceOf ("/", false, false)
  225. .upToLastOccurrenceOf (".storyboard", false, false);
  226. setIfEmpty (result.microphonePermissionText,
  227. "This app requires audio input. If you do not have an audio interface connected it will use the built-in microphone.");
  228. setIfEmpty (result.cameraPermissionText,
  229. "This app requires access to the camera to function correctly.");
  230. setIfEmpty (result.bluetoothPermissionText,
  231. "This app requires access to Bluetooth to function correctly.");
  232. setIfEmpty (result.sendAppleEventsPermissionText,
  233. "This app requires the ability to send Apple events to function correctly.");
  234. result.documentExtensions = result.documentExtensions.replace (";", ",");
  235. // AUv3 needs a slightly different bundle ID
  236. if (type == juce::build_tools::ProjectType::Target::Type::AudioUnitv3PlugIn)
  237. {
  238. const auto bundleIdSegments = juce::StringArray::fromTokens (result.bundleIdentifier, ".", {});
  239. jassert (! bundleIdSegments.isEmpty());
  240. const auto last = bundleIdSegments.isEmpty() ? ""
  241. : bundleIdSegments[bundleIdSegments.size() - 1];
  242. result.bundleIdentifier += "." + last + "AUv3";
  243. }
  244. return result;
  245. }
  246. int writePlist (juce::ArgumentList&& args)
  247. {
  248. args.checkMinNumArguments (3);
  249. const auto kind = args.arguments.removeAndReturn (0);
  250. const auto input = args.arguments.removeAndReturn (0);
  251. const auto output = args.arguments.removeAndReturn (0);
  252. parsePlistOptions (input.resolveAsExistingFile(),
  253. juce::build_tools::ProjectType::Target::typeFromName (kind.text))
  254. .write (output.resolveAsFile());
  255. return 0;
  256. }
  257. juce::build_tools::EntitlementOptions parseEntitlementsOptions (const juce::File& file,
  258. juce::build_tools::ProjectType::Target::Type type)
  259. {
  260. if (type == juce::build_tools::ProjectType::Target::ConsoleApp)
  261. juce::ConsoleApplication::fail ("Deduced project type does not require entitlements", 1);
  262. const auto dict = parseProjectData (file);
  263. UpdateField updateField { dict };
  264. juce::build_tools::EntitlementOptions result;
  265. updateField ("IS_IOS", result.isiOS);
  266. updateField ("IS_PLUGIN", result.isAudioPluginProject);
  267. updateField ("ICLOUD_PERMISSIONS_ENABLED", result.isiCloudPermissionsEnabled);
  268. updateField ("PUSH_NOTIFICATIONS_ENABLED", result.isPushNotificationsEnabled);
  269. updateField ("APP_GROUPS_ENABLED", result.isAppGroupsEnabled);
  270. updateField ("APP_GROUP_IDS", result.appGroupIdString);
  271. updateField ("HARDENED_RUNTIME_ENABLED", result.isHardenedRuntimeEnabled);
  272. updateField ("HARDENED_RUNTIME_OPTIONS", result.hardenedRuntimeOptions);
  273. updateField ("APP_SANDBOX_ENABLED", result.isAppSandboxEnabled);
  274. updateField ("APP_SANDBOX_INHERIT", result.isAppSandboxInhertianceEnabled);
  275. updateField ("APP_SANDBOX_OPTIONS", result.appSandboxOptions);
  276. updateField ("NETWORK_MULTICAST_ENABLED", result.isNetworkingMulticastEnabled);
  277. struct SandboxTemporaryAccessKey
  278. {
  279. juce::String cMakeVar, key;
  280. };
  281. SandboxTemporaryAccessKey sandboxTemporaryAccessKeys[]
  282. {
  283. { "APP_SANDBOX_FILE_ACCESS_HOME_RO", "home-relative-path.read-only" },
  284. { "APP_SANDBOX_FILE_ACCESS_HOME_RW", "home-relative-path.read-write" },
  285. { "APP_SANDBOX_FILE_ACCESS_ABS_RO", "absolute-path.read-only" },
  286. { "APP_SANDBOX_FILE_ACCESS_ABS_RW", "absolute-path.read-write" }
  287. };
  288. for (const auto& entry : sandboxTemporaryAccessKeys)
  289. {
  290. juce::StringArray values;
  291. updateField (entry.cMakeVar, values);
  292. if (! values.isEmpty())
  293. result.appSandboxTemporaryPaths.push_back ({ "com.apple.security.temporary-exception.files." + entry.key,
  294. std::move (values) });
  295. }
  296. result.type = type;
  297. return result;
  298. }
  299. int writeEntitlements (juce::ArgumentList&& args)
  300. {
  301. args.checkMinNumArguments (3);
  302. const auto kind = args.arguments.removeAndReturn (0);
  303. const auto input = args.arguments.removeAndReturn (0);
  304. const auto output = args.arguments.removeAndReturn (0);
  305. const auto options = parseEntitlementsOptions (input.resolveAsExistingFile(),
  306. juce::build_tools::ProjectType::Target::typeFromName (kind.text));
  307. juce::build_tools::overwriteFileIfDifferentOrThrow (output.resolveAsFile(), options.getEntitlementsFileContent());
  308. return 0;
  309. }
  310. int createAndWrite (const juce::File& file, juce::StringRef text)
  311. {
  312. if (file.create())
  313. return file.replaceWithText (text) ? 0 : 1;
  314. return 1;
  315. }
  316. int writePkgInfo (juce::ArgumentList&& args)
  317. {
  318. args.checkMinNumArguments (2);
  319. const auto kind = args.arguments.removeAndReturn (0);
  320. const auto output = args.arguments.removeAndReturn (0);
  321. const auto projectType = juce::build_tools::ProjectType::Target::typeFromName (kind.text);
  322. return createAndWrite (output.resolveAsFile(),
  323. juce::build_tools::getXcodePackageType (projectType)
  324. + juce::build_tools::getXcodeBundleSignature (projectType));
  325. }
  326. juce::build_tools::ResourceRcOptions parseRcFileOptions (const juce::File& file)
  327. {
  328. const auto dict = parseProjectData (file);
  329. UpdateField updateField { dict };
  330. juce::build_tools::ResourceRcOptions result;
  331. updateField ("VERSION", result.version);
  332. updateField ("COMPANY_NAME", result.companyName);
  333. updateField ("COMPANY_COPYRIGHT", result.companyCopyright);
  334. updateField ("PROJECT_NAME", result.projectName);
  335. updateField ("ICON_FILE", result.icon);
  336. return result;
  337. }
  338. int writeRcFile (juce::ArgumentList&& args)
  339. {
  340. args.checkMinNumArguments (2);
  341. const auto input = args.arguments.removeAndReturn (0);
  342. const auto output = args.arguments.removeAndReturn (0);
  343. parseRcFileOptions (input.resolveAsExistingFile()).write (output.resolveAsFile());
  344. return 0;
  345. }
  346. juce::String createDefineStatements (juce::StringRef definitions)
  347. {
  348. const auto split = juce::StringArray::fromTokens (definitions, ";", "\"");
  349. juce::String defineStatements;
  350. for (const auto& def : split)
  351. {
  352. if (! def.startsWith ("JucePlugin_"))
  353. continue;
  354. const auto defineName = def.upToFirstOccurrenceOf ("=", false, false);
  355. const auto defineValue = def.fromFirstOccurrenceOf ("=", false, false);
  356. defineStatements += "#define " + defineName + " " + defineValue + '\n';
  357. }
  358. return defineStatements;
  359. }
  360. int writeAuPluginDefines (juce::ArgumentList&& args)
  361. {
  362. args.checkMinNumArguments (2);
  363. const auto input = args.arguments.removeAndReturn (0);
  364. const auto output = args.arguments.removeAndReturn (0);
  365. const auto dict = parseProjectData (input.resolveAsExistingFile());
  366. const auto getString = [&] (juce::StringRef key) { return getStringValue (dict, key); };
  367. const auto defines = "#pragma once\n" + createDefineStatements (getString ("MODULE_DEFINITIONS"));
  368. return createAndWrite (output.resolveAsFile(), defines);
  369. }
  370. juce::String createIncludeStatements (juce::StringRef definitions)
  371. {
  372. const auto split = juce::StringArray::fromTokens (definitions, ";", "\"");
  373. juce::String includeStatements;
  374. for (const auto& def : split)
  375. {
  376. constexpr auto moduleToken = "JUCE_MODULE_AVAILABLE_";
  377. if (def.startsWith (moduleToken))
  378. {
  379. const auto moduleName = def.fromFirstOccurrenceOf (moduleToken, false, false)
  380. .upToFirstOccurrenceOf ("=", false, false);
  381. includeStatements += "#include <" + moduleName + "/" + moduleName + ".h>\n";
  382. }
  383. }
  384. return includeStatements;
  385. }
  386. int writeHeader (juce::ArgumentList&& args)
  387. {
  388. args.checkMinNumArguments (2);
  389. const auto input = args.arguments.removeAndReturn (0);
  390. const auto output = args.arguments.removeAndReturn (0);
  391. const auto dict = parseProjectData (input.resolveAsExistingFile());
  392. const auto getString = [&] (juce::StringRef key) { return getStringValue (dict, key); };
  393. const auto includes = createIncludeStatements (getString ("MODULE_DEFINITIONS"));
  394. const auto projectName = getString ("PROJECT_NAME");
  395. const auto name = projectName.isEmpty() ? getString ("EXECUTABLE_NAME") : projectName;
  396. const auto versionString = getString ("VERSION");
  397. const auto headerText = juce::String (headerTemplate)
  398. .replace ("${JUCE_INCLUDES}", includes)
  399. .replace ("${JUCE_EXECUTABLE_NAME}", name)
  400. .replace ("${JUCE_COMPANY_NAME}", getString ("COMPANY_NAME"))
  401. .replace ("${JUCE_PROJECT_VERSION}", versionString)
  402. .replace ("${JUCE_PROJECT_VERSION_HEX}", juce::build_tools::getVersionAsHex (versionString));
  403. return createAndWrite (output.resolveAsFile(), headerText);
  404. }
  405. } // namespace
  406. int main (int argc, char** argv)
  407. {
  408. juce::ScopedJuceInitialiser_GUI libraryInitialiser;
  409. return juce::ConsoleApplication::invokeCatchingFailures ([argc, argv]
  410. {
  411. if (argc < 1)
  412. juce::ConsoleApplication::fail ("No arguments passed", 1);
  413. const auto getString = [&] (const char* text)
  414. {
  415. return juce::String (juce::CharPointer_UTF8 (text));
  416. };
  417. std::vector<juce::String> arguments;
  418. std::transform (argv, argv + argc, std::back_inserter (arguments), getString);
  419. juce::ArgumentList argumentList { arguments.front(),
  420. juce::StringArray (arguments.data() + 1, (int) arguments.size() - 1) };
  421. using Fn = std::add_lvalue_reference<decltype (writeBinaryData)>::type;
  422. const std::unordered_map<juce::String, Fn> commands
  423. {
  424. { "auplugindefines", writeAuPluginDefines },
  425. { "binarydata", writeBinaryData },
  426. { "entitlements", writeEntitlements },
  427. { "header", writeHeader },
  428. { "iosassets", writeiOSAssets },
  429. { "macicon", writeMacIcon },
  430. { "pkginfo", writePkgInfo },
  431. { "plist", writePlist },
  432. { "rcfile", writeRcFile },
  433. { "winicon", writeWinIcon }
  434. };
  435. argumentList.checkMinNumArguments (1);
  436. const auto mode = argumentList.arguments.removeAndReturn (0);
  437. const auto it = commands.find (mode.text);
  438. if (it == commands.cend())
  439. juce::ConsoleApplication::fail ("No matching mode", 1);
  440. return it->second (std::move (argumentList));
  441. });
  442. }