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
39KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. //==============================================================================
  22. class XCodeProjectExporter : public ProjectExporter
  23. {
  24. public:
  25. //==============================================================================
  26. static const char* getNameMac() { return "XCode (MacOSX)"; }
  27. static const char* getNameiOS() { return "XCode (iOS)"; }
  28. static const char* getValueTreeTypeName (bool iOS) { return iOS ? "XCODE_IPHONE" : "XCODE_MAC"; }
  29. //==============================================================================
  30. XCodeProjectExporter (Project& project_, const ValueTree& settings_, const bool iOS_)
  31. : ProjectExporter (project_, settings_),
  32. iOS (iOS_)
  33. {
  34. name = iOS ? getNameiOS() : getNameMac();
  35. if (getTargetLocation().toString().isEmpty())
  36. getTargetLocation() = getDefaultBuildsRootFolder() + (iOS ? "iOS" : "MacOSX");
  37. if (getSettings() ["objCExtraSuffix"].isVoid())
  38. getObjCSuffix() = createAlphaNumericUID();
  39. }
  40. static XCodeProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  41. {
  42. if (settings.hasType (getValueTreeTypeName (false)))
  43. return new XCodeProjectExporter (project, settings, false);
  44. else if (settings.hasType (getValueTreeTypeName (true)))
  45. return new XCodeProjectExporter (project, settings, true);
  46. return 0;
  47. }
  48. //==============================================================================
  49. Value getObjCSuffix() { return getSetting ("objCExtraSuffix"); }
  50. int getLaunchPreferenceOrderForCurrentOS()
  51. {
  52. #if JUCE_MAC
  53. return iOS ? 1 : 2;
  54. #else
  55. return 0;
  56. #endif
  57. }
  58. bool isAvailableOnCurrentOS()
  59. {
  60. #if JUCE_MAC
  61. return true;
  62. #else
  63. return false;
  64. #endif
  65. }
  66. bool isPossibleForCurrentProject() { return projectType.isGUIApplication() || ! iOS; }
  67. bool usesMMFiles() const { return true; }
  68. bool isXcode() const { return true; }
  69. bool isOSX() const { return ! iOS; }
  70. bool canCopeWithDuplicateFiles() { return true; }
  71. void createPropertyEditors (Array <PropertyComponent*>& props)
  72. {
  73. ProjectExporter::createPropertyEditors (props);
  74. props.add (new TextPropertyComponent (getObjCSuffix(), "Objective-C class name suffix", 64, false));
  75. props.getLast()->setTooltip ("Because objective-C linkage is done by string-matching, you can get horrible linkage mix-ups when different modules containing the "
  76. "same class-names are loaded simultaneously. This setting lets you provide a unique string that will be used in naming the obj-C classes in your executable to avoid this.");
  77. if (projectType.isGUIApplication() && ! iOS)
  78. {
  79. props.add (new TextPropertyComponent (getSetting ("documentExtensions"), "Document file extensions", 128, false));
  80. props.getLast()->setTooltip ("A comma-separated list of file extensions for documents that your app can open.");
  81. }
  82. else if (iOS)
  83. {
  84. props.add (new BooleanPropertyComponent (getSetting ("UIFileSharingEnabled"), "File Sharing Enabled", "Enabled"));
  85. props.getLast()->setTooltip ("Enable this to expose your app's files to iTunes.");
  86. props.add (new BooleanPropertyComponent (getSetting ("UIStatusBarHidden"), "Status Bar Hidden", "Enabled"));
  87. props.getLast()->setTooltip ("Enable this to disable the status bar in your app.");
  88. }
  89. }
  90. void launchProject()
  91. {
  92. getProjectBundle().startAsProcess();
  93. }
  94. //==============================================================================
  95. void create()
  96. {
  97. infoPlistFile = getTargetFolder().getChildFile ("Info.plist");
  98. createIconFile();
  99. File projectBundle (getProjectBundle());
  100. createDirectoryOrThrow (projectBundle);
  101. createObjects();
  102. File projectFile (projectBundle.getChildFile ("project.pbxproj"));
  103. {
  104. MemoryOutputStream mo;
  105. writeProjectFile (mo);
  106. overwriteFileIfDifferentOrThrow (projectFile, mo);
  107. }
  108. writeInfoPlistFile();
  109. }
  110. private:
  111. OwnedArray<ValueTree> pbxBuildFiles, pbxFileReferences, pbxGroups, misc, projectConfigs, targetConfigs;
  112. StringArray buildPhaseIDs, resourceIDs, sourceIDs, frameworkIDs;
  113. StringArray frameworkFileIDs, rezFileIDs, resourceFileRefs;
  114. File infoPlistFile, iconFile;
  115. const bool iOS;
  116. static String sanitisePath (const String& path)
  117. {
  118. if (path.startsWithChar ('~'))
  119. return "$(HOME)" + path.substring (1);
  120. return path;
  121. }
  122. File getProjectBundle() const { return getTargetFolder().getChildFile (project.getProjectFilenameRoot()).withFileExtension (".xcodeproj"); }
  123. //==============================================================================
  124. void createObjects()
  125. {
  126. addFrameworks();
  127. addMainBuildProduct();
  128. if (xcodeCreatePList)
  129. {
  130. RelativePath plistPath (infoPlistFile, getTargetFolder(), RelativePath::buildTargetFolder);
  131. addFileReference (plistPath.toUnixStyle());
  132. resourceFileRefs.add (createFileRefID (plistPath));
  133. }
  134. if (iconFile.exists())
  135. {
  136. RelativePath iconPath (iconFile, getTargetFolder(), RelativePath::buildTargetFolder);
  137. addFileReference (iconPath.toUnixStyle());
  138. resourceIDs.add (addBuildFile (iconPath, false, false));
  139. resourceFileRefs.add (createFileRefID (iconPath));
  140. }
  141. {
  142. StringArray topLevelGroupIDs;
  143. for (int i = 0; i < groups.size(); ++i)
  144. if (groups.getReference(i).getNumChildren() > 0)
  145. topLevelGroupIDs.add (addProjectItem (groups.getReference(i)));
  146. { // Add 'resources' group
  147. String resourcesGroupID (createID ("__resources"));
  148. addGroup (resourcesGroupID, "Resources", resourceFileRefs);
  149. topLevelGroupIDs.add (resourcesGroupID);
  150. }
  151. { // Add 'frameworks' group
  152. String frameworksGroupID (createID ("__frameworks"));
  153. addGroup (frameworksGroupID, "Frameworks", frameworkFileIDs);
  154. topLevelGroupIDs.add (frameworksGroupID);
  155. }
  156. { // Add 'products' group
  157. String productsGroupID (createID ("__products"));
  158. StringArray products;
  159. products.add (createID ("__productFileID"));
  160. addGroup (productsGroupID, "Products", products);
  161. topLevelGroupIDs.add (productsGroupID);
  162. }
  163. addGroup (createID ("__mainsourcegroup"), "Source", topLevelGroupIDs);
  164. }
  165. for (int i = 0; i < configs.size(); ++i)
  166. {
  167. const Project::BuildConfiguration& config = configs.getReference(i);
  168. addProjectConfig (config.getName().getValue(), getProjectSettings (config));
  169. addTargetConfig (config.getName().getValue(), getTargetSettings (config));
  170. }
  171. addConfigList (projectConfigs, createID ("__projList"));
  172. addConfigList (targetConfigs, createID ("__configList"));
  173. if (! projectType.isLibrary())
  174. addBuildPhase ("PBXResourcesBuildPhase", resourceIDs);
  175. if (rezFileIDs.size() > 0)
  176. addBuildPhase ("PBXRezBuildPhase", rezFileIDs);
  177. addBuildPhase ("PBXSourcesBuildPhase", sourceIDs);
  178. if (! projectType.isLibrary())
  179. addBuildPhase ("PBXFrameworksBuildPhase", frameworkIDs);
  180. addShellScriptPhase();
  181. addTargetObject();
  182. addProjectObject();
  183. }
  184. static Image fixMacIconImageSize (Image& image)
  185. {
  186. const int w = image.getWidth();
  187. const int h = image.getHeight();
  188. if (w != h || (w != 16 && w != 32 && w != 48 && w != 64))
  189. {
  190. const int newSize = w >= 128 ? 128 : (w >= 64 ? 64 : (w >= 32 ? 32 : 16));
  191. Image newIm (Image::ARGB, newSize, newSize, true, SoftwareImageType());
  192. Graphics g (newIm);
  193. g.drawImageWithin (image, 0, 0, newSize, newSize,
  194. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  195. return newIm;
  196. }
  197. return image;
  198. }
  199. void writeIcnsFile (const Array<Image>& images, OutputStream& out)
  200. {
  201. MemoryOutputStream data;
  202. for (int i = 0; i < images.size(); ++i)
  203. {
  204. Image image (fixMacIconImageSize (images.getReference (i)));
  205. const int w = image.getWidth();
  206. const int h = image.getHeight();
  207. const char* type = nullptr;
  208. const char* maskType = nullptr;
  209. if (w == h)
  210. {
  211. if (w == 16) { type = "is32"; maskType = "s8mk"; }
  212. if (w == 32) { type = "il32"; maskType = "l8mk"; }
  213. if (w == 48) { type = "ih32"; maskType = "h8mk"; }
  214. if (w == 128) { type = "it32"; maskType = "t8mk"; }
  215. }
  216. if (type != nullptr)
  217. {
  218. data.write (type, 4);
  219. data.writeIntBigEndian (8 + 4 * w * h);
  220. const Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  221. int y;
  222. for (y = 0; y < h; ++y)
  223. {
  224. for (int x = 0; x < w; ++x)
  225. {
  226. const Colour pixel (bitmap.getPixelColour (x, y));
  227. data.writeByte ((char) pixel.getAlpha());
  228. data.writeByte ((char) pixel.getRed());
  229. data.writeByte ((char) pixel.getGreen());
  230. data.writeByte ((char) pixel.getBlue());
  231. }
  232. }
  233. data.write (maskType, 4);
  234. data.writeIntBigEndian (8 + w * h);
  235. for (y = 0; y < h; ++y)
  236. {
  237. for (int x = 0; x < w; ++x)
  238. {
  239. const Colour pixel (bitmap.getPixelColour (x, y));
  240. data.writeByte ((char) pixel.getAlpha());
  241. }
  242. }
  243. }
  244. }
  245. jassert (data.getDataSize() > 0); // no suitable sized images?
  246. out.write ("icns", 4);
  247. out.writeIntBigEndian (data.getDataSize() + 8);
  248. out << data;
  249. }
  250. void createIconFile()
  251. {
  252. Array<Image> images;
  253. Image bigIcon (project.getBigIcon());
  254. if (bigIcon.isValid())
  255. images.add (bigIcon);
  256. Image smallIcon (project.getSmallIcon());
  257. if (smallIcon.isValid())
  258. images.add (smallIcon);
  259. if (images.size() > 0)
  260. {
  261. MemoryOutputStream mo;
  262. writeIcnsFile (images, mo);
  263. iconFile = getTargetFolder().getChildFile ("Icon.icns");
  264. overwriteFileIfDifferentOrThrow (iconFile, mo);
  265. }
  266. }
  267. void writeInfoPlistFile()
  268. {
  269. if (! xcodeCreatePList)
  270. return;
  271. XmlElement plist ("plist");
  272. XmlElement* dict = plist.createNewChildElement ("dict");
  273. if (iOS)
  274. addPlistDictionaryKeyBool (dict, "LSRequiresIPhoneOS", true);
  275. addPlistDictionaryKey (dict, "CFBundleExecutable", "${EXECUTABLE_NAME}");
  276. addPlistDictionaryKey (dict, "CFBundleIconFile", iconFile.exists() ? iconFile.getFileName() : String::empty);
  277. addPlistDictionaryKey (dict, "CFBundleIdentifier", project.getBundleIdentifier().toString());
  278. addPlistDictionaryKey (dict, "CFBundleName", projectName);
  279. addPlistDictionaryKey (dict, "CFBundlePackageType", xcodePackageType);
  280. addPlistDictionaryKey (dict, "CFBundleSignature", xcodeBundleSignature);
  281. addPlistDictionaryKey (dict, "CFBundleShortVersionString", project.getVersion().toString());
  282. addPlistDictionaryKey (dict, "CFBundleVersion", project.getVersion().toString());
  283. StringArray documentExtensions;
  284. documentExtensions.addTokens (replacePreprocessorDefs (getAllPreprocessorDefs(), getSetting ("documentExtensions").toString()),
  285. ",", String::empty);
  286. documentExtensions.trim();
  287. documentExtensions.removeEmptyStrings (true);
  288. if (documentExtensions.size() > 0)
  289. {
  290. dict->createNewChildElement ("key")->addTextElement ("CFBundleDocumentTypes");
  291. XmlElement* dict2 = dict->createNewChildElement ("array")->createNewChildElement ("dict");
  292. for (int i = 0; i < documentExtensions.size(); ++i)
  293. {
  294. String ex (documentExtensions[i]);
  295. if (ex.startsWithChar ('.'))
  296. ex = ex.substring (1);
  297. dict2->createNewChildElement ("key")->addTextElement ("CFBundleTypeExtensions");
  298. dict2->createNewChildElement ("array")->createNewChildElement ("string")->addTextElement (ex);
  299. addPlistDictionaryKey (dict2, "CFBundleTypeName", ex);
  300. addPlistDictionaryKey (dict2, "CFBundleTypeRole", "Editor");
  301. addPlistDictionaryKey (dict2, "NSPersistentStoreTypeKey", "XML");
  302. }
  303. }
  304. if (getSetting ("UIFileSharingEnabled").getValue())
  305. addPlistDictionaryKeyBool (dict, "UIFileSharingEnabled", true);
  306. if (getSetting ("UIStatusBarHidden").getValue())
  307. addPlistDictionaryKeyBool (dict, "UIStatusBarHidden", true);
  308. for (int i = 0; i < xcodeExtraPListEntries.size(); ++i)
  309. dict->addChildElement (new XmlElement (xcodeExtraPListEntries.getReference(i)));
  310. MemoryOutputStream mo;
  311. plist.writeToStream (mo, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  312. overwriteFileIfDifferentOrThrow (infoPlistFile, mo);
  313. }
  314. StringArray getHeaderSearchPaths (const Project::BuildConfiguration& config)
  315. {
  316. StringArray searchPaths (extraSearchPaths);
  317. searchPaths.addArray (config.getHeaderSearchPaths());
  318. searchPaths.removeDuplicates (false);
  319. return searchPaths;
  320. }
  321. static void getLinkerFlagsForStaticLibrary (const RelativePath& library, StringArray& flags, StringArray& librarySearchPaths)
  322. {
  323. jassert (library.getFileNameWithoutExtension().substring (0, 3) == "lib");
  324. flags.add ("-l" + library.getFileNameWithoutExtension().substring (3));
  325. String searchPath (library.toUnixStyle().upToLastOccurrenceOf ("/", false, false));
  326. if (! library.isAbsolute())
  327. searchPath = "$(SRCROOT)/" + searchPath;
  328. librarySearchPaths.add (sanitisePath (searchPath));
  329. }
  330. void getLinkerFlags (const Project::BuildConfiguration& config, StringArray& flags, StringArray& librarySearchPaths)
  331. {
  332. if (xcodeIsBundle)
  333. flags.add ("-bundle");
  334. const Array<RelativePath>& extraLibs = config.isDebug().getValue() ? xcodeExtraLibrariesDebug
  335. : xcodeExtraLibrariesRelease;
  336. for (int i = 0; i < extraLibs.size(); ++i)
  337. getLinkerFlagsForStaticLibrary (extraLibs.getReference(i), flags, librarySearchPaths);
  338. flags.add (replacePreprocessorTokens (config, getExtraLinkerFlags().toString()));
  339. flags.removeEmptyStrings (true);
  340. }
  341. StringArray getProjectSettings (const Project::BuildConfiguration& config)
  342. {
  343. StringArray s;
  344. s.add ("ALWAYS_SEARCH_USER_PATHS = NO");
  345. s.add ("GCC_C_LANGUAGE_STANDARD = c99");
  346. s.add ("GCC_WARN_ABOUT_RETURN_TYPE = YES");
  347. s.add ("GCC_WARN_CHECK_SWITCH_STATEMENTS = YES");
  348. s.add ("GCC_WARN_UNUSED_VARIABLE = YES");
  349. s.add ("GCC_WARN_MISSING_PARENTHESES = YES");
  350. s.add ("GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES");
  351. s.add ("GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES");
  352. s.add ("WARNING_CFLAGS = -Wreorder");
  353. s.add ("GCC_MODEL_TUNING = G5");
  354. if (projectType.isLibrary())
  355. {
  356. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = NO");
  357. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = NO");
  358. }
  359. else
  360. {
  361. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = YES");
  362. }
  363. if (iOS)
  364. {
  365. s.add ("\"CODE_SIGN_IDENTITY[sdk=iphoneos*]\" = \"iPhone Developer\"");
  366. s.add ("SDKROOT = iphoneos");
  367. s.add ("TARGETED_DEVICE_FAMILY = \"1,2\"");
  368. }
  369. s.add ("ZERO_LINK = NO");
  370. if (xcodeCanUseDwarf)
  371. s.add ("DEBUG_INFORMATION_FORMAT = \"dwarf\"");
  372. s.add ("PRODUCT_NAME = \"" + config.getTargetBinaryName().toString() + "\"");
  373. return s;
  374. }
  375. StringArray getTargetSettings (const Project::BuildConfiguration& config)
  376. {
  377. StringArray s;
  378. const String arch (config.getMacArchitecture().toString());
  379. if (arch == Project::BuildConfiguration::osxArch_Native) s.add ("ARCHS = \"$(ARCHS_NATIVE)\"");
  380. else if (arch == Project::BuildConfiguration::osxArch_32BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_BIT)\"");
  381. else if (arch == Project::BuildConfiguration::osxArch_64BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_64_BIT)\"");
  382. else if (arch == Project::BuildConfiguration::osxArch_64Bit) s.add ("ARCHS = \"$(ARCHS_STANDARD_64_BIT)\"");
  383. s.add ("HEADER_SEARCH_PATHS = \"" + replacePreprocessorTokens (config, getHeaderSearchPaths (config).joinIntoString (" ")) + " $(inherited)\"");
  384. s.add ("GCC_OPTIMIZATION_LEVEL = " + config.getGCCOptimisationFlag());
  385. s.add ("INFOPLIST_FILE = " + infoPlistFile.getFileName());
  386. const String extraFlags (replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim());
  387. if (extraFlags.isNotEmpty())
  388. s.add ("OTHER_CPLUSPLUSFLAGS = " + extraFlags);
  389. if (xcodeProductInstallPath.isNotEmpty())
  390. s.add ("INSTALL_PATH = \"" + xcodeProductInstallPath + "\"");
  391. if (xcodeIsBundle)
  392. {
  393. s.add ("LIBRARY_STYLE = Bundle");
  394. s.add ("WRAPPER_EXTENSION = " + xcodeBundleExtension.substring (1));
  395. s.add ("GENERATE_PKGINFO_FILE = YES");
  396. }
  397. if (xcodeOtherRezFlags.isNotEmpty())
  398. s.add ("OTHER_REZFLAGS = \"" + xcodeOtherRezFlags + "\"");
  399. if (projectType.isLibrary())
  400. {
  401. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  402. {
  403. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  404. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  405. s.add ("DSTROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  406. s.add ("SYMROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  407. }
  408. s.add ("CONFIGURATION_BUILD_DIR = \"$(BUILD_DIR)\"");
  409. s.add ("DEPLOYMENT_LOCATION = YES");
  410. }
  411. if (! iOS)
  412. {
  413. const String sdk (config.getMacSDKVersion().toString());
  414. const String sdkCompat (config.getMacCompatibilityVersion().toString());
  415. if (sdk == Project::BuildConfiguration::osxVersion10_4)
  416. {
  417. s.add ("SDKROOT = macosx10.4");
  418. s.add ("GCC_VERSION = 4.0");
  419. }
  420. else if (sdk == Project::BuildConfiguration::osxVersion10_5)
  421. {
  422. s.add ("SDKROOT = macosx10.5");
  423. }
  424. else if (sdk == Project::BuildConfiguration::osxVersion10_6)
  425. {
  426. s.add ("SDKROOT = macosx10.6");
  427. }
  428. if (sdkCompat == Project::BuildConfiguration::osxVersion10_4) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.4");
  429. else if (sdkCompat == Project::BuildConfiguration::osxVersion10_5) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.5");
  430. else if (sdkCompat == Project::BuildConfiguration::osxVersion10_6) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.6");
  431. s.add ("MACOSX_DEPLOYMENT_TARGET_ppc = 10.4");
  432. }
  433. {
  434. StringArray linkerFlags, librarySearchPaths;
  435. getLinkerFlags (config, linkerFlags, librarySearchPaths);
  436. if (linkerFlags.size() > 0)
  437. s.add ("OTHER_LDFLAGS = \"" + linkerFlags.joinIntoString (" ") + "\"");
  438. if (librarySearchPaths.size() > 0)
  439. {
  440. String libPaths ("LIBRARY_SEARCH_PATHS = (\"$(inherited)\"");
  441. for (int i = 0; i < librarySearchPaths.size(); ++i)
  442. libPaths += ", \"\\\"" + librarySearchPaths[i] + "\\\"\"";
  443. s.add (libPaths + ")");
  444. }
  445. }
  446. StringPairArray defines;
  447. if (config.isDebug().getValue())
  448. {
  449. defines.set ("_DEBUG", "1");
  450. defines.set ("DEBUG", "1");
  451. s.add ("ONLY_ACTIVE_ARCH = YES");
  452. s.add ("COPY_PHASE_STRIP = NO");
  453. s.add ("GCC_DYNAMIC_NO_PIC = NO");
  454. }
  455. else
  456. {
  457. defines.set ("_NDEBUG", "1");
  458. defines.set ("NDEBUG", "1");
  459. s.add ("GCC_GENERATE_DEBUGGING_SYMBOLS = NO");
  460. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = YES");
  461. }
  462. {
  463. const String objCSuffix (getObjCSuffix().toString().trim());
  464. if (objCSuffix.isNotEmpty())
  465. defines.set ("JUCE_ObjCExtraSuffix", replacePreprocessorTokens (config, objCSuffix));
  466. }
  467. {
  468. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  469. StringArray defsList;
  470. for (int i = 0; i < defines.size(); ++i)
  471. {
  472. String def (defines.getAllKeys()[i]);
  473. const String value (defines.getAllValues()[i]);
  474. if (value.isNotEmpty())
  475. def << "=" << value;
  476. defsList.add (def.quoted());
  477. }
  478. s.add ("GCC_PREPROCESSOR_DEFINITIONS = (" + indentList (defsList, ",") + ")");
  479. }
  480. return s;
  481. }
  482. void addFrameworks()
  483. {
  484. if (! projectType.isLibrary())
  485. {
  486. StringArray s (xcodeFrameworks);
  487. s.trim();
  488. s.removeDuplicates (true);
  489. s.sort (true);
  490. for (int i = 0; i < s.size(); ++i)
  491. addFramework (s[i]);
  492. }
  493. }
  494. //==============================================================================
  495. void writeProjectFile (OutputStream& output)
  496. {
  497. output << "// !$*UTF8*$!\n{\n"
  498. "\tarchiveVersion = 1;\n"
  499. "\tclasses = {\n\t};\n"
  500. "\tobjectVersion = 45;\n"
  501. "\tobjects = {\n\n";
  502. Array <ValueTree*> objects;
  503. objects.addArray (pbxBuildFiles);
  504. objects.addArray (pbxFileReferences);
  505. objects.addArray (pbxGroups);
  506. objects.addArray (targetConfigs);
  507. objects.addArray (projectConfigs);
  508. objects.addArray (misc);
  509. for (int i = 0; i < objects.size(); ++i)
  510. {
  511. ValueTree& o = *objects.getUnchecked(i);
  512. output << "\t\t" << o.getType().toString() << " = { ";
  513. for (int j = 0; j < o.getNumProperties(); ++j)
  514. {
  515. const Identifier propertyName (o.getPropertyName(j));
  516. String val (o.getProperty (propertyName).toString());
  517. if (val.isEmpty() || (val.containsAnyOf (" \t;<>()=,&+-_\r\n")
  518. && ! (val.trimStart().startsWithChar ('(')
  519. || val.trimStart().startsWithChar ('{'))))
  520. val = val.quoted();
  521. output << propertyName.toString() << " = " << val << "; ";
  522. }
  523. output << "};\n";
  524. }
  525. output << "\t};\n\trootObject = " << createID ("__root") << ";\n}\n";
  526. }
  527. static void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  528. {
  529. xml->createNewChildElement ("key")->addTextElement (key);
  530. xml->createNewChildElement ("string")->addTextElement (value);
  531. }
  532. static void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  533. {
  534. xml->createNewChildElement ("key")->addTextElement (key);
  535. xml->createNewChildElement (value ? "true" : "false");
  536. }
  537. String addBuildFile (const String& path, const String& fileRefID, bool addToSourceBuildPhase, bool inhibitWarnings)
  538. {
  539. String fileID (createID (path + "buildref"));
  540. if (addToSourceBuildPhase)
  541. sourceIDs.add (fileID);
  542. ValueTree* v = new ValueTree (fileID);
  543. v->setProperty ("isa", "PBXBuildFile", 0);
  544. v->setProperty ("fileRef", fileRefID, 0);
  545. if (inhibitWarnings)
  546. v->setProperty ("settings", "{COMPILER_FLAGS = \"-w\"; }", 0);
  547. pbxBuildFiles.add (v);
  548. return fileID;
  549. }
  550. String addBuildFile (const RelativePath& path, bool addToSourceBuildPhase, bool inhibitWarnings)
  551. {
  552. return addBuildFile (path.toUnixStyle(), createFileRefID (path), addToSourceBuildPhase, inhibitWarnings);
  553. }
  554. String addFileReference (String pathString)
  555. {
  556. String sourceTree ("SOURCE_ROOT");
  557. RelativePath path (pathString, RelativePath::unknown);
  558. if (pathString.startsWith ("${"))
  559. {
  560. sourceTree = pathString.substring (2).upToFirstOccurrenceOf ("}", false, false);
  561. pathString = pathString.fromFirstOccurrenceOf ("}/", false, false);
  562. }
  563. else if (path.isAbsolute())
  564. {
  565. sourceTree = "<absolute>";
  566. }
  567. const String fileRefID (createFileRefID (pathString));
  568. ScopedPointer<ValueTree> v (new ValueTree (fileRefID));
  569. v->setProperty ("isa", "PBXFileReference", 0);
  570. v->setProperty ("lastKnownFileType", getFileType (path), 0);
  571. v->setProperty (Ids::name, pathString.fromLastOccurrenceOf ("/", false, false), 0);
  572. v->setProperty ("path", sanitisePath (pathString), 0);
  573. v->setProperty ("sourceTree", sourceTree, 0);
  574. const int existing = pbxFileReferences.indexOfSorted (*this, v);
  575. if (existing >= 0)
  576. {
  577. // If this fails, there's either a string hash collision, or the same file is being added twice (incorrectly)
  578. jassert (pbxFileReferences.getUnchecked (existing)->isEquivalentTo (*v));
  579. }
  580. else
  581. {
  582. pbxFileReferences.addSorted (*this, v.release());
  583. }
  584. return fileRefID;
  585. }
  586. public:
  587. static int compareElements (const ValueTree* first, const ValueTree* second)
  588. {
  589. return first->getType().toString().compare (second->getType().toString());
  590. }
  591. private:
  592. static String getFileType (const RelativePath& file)
  593. {
  594. if (file.hasFileExtension ("cpp;cc;cxx")) return "sourcecode.cpp.cpp";
  595. else if (file.hasFileExtension (".mm")) return "sourcecode.cpp.objcpp";
  596. else if (file.hasFileExtension (".m")) return "sourcecode.c.objc";
  597. else if (file.hasFileExtension (headerFileExtensions)) return "sourcecode.c.h";
  598. else if (file.hasFileExtension (".framework")) return "wrapper.framework";
  599. else if (file.hasFileExtension (".jpeg;.jpg")) return "image.jpeg";
  600. else if (file.hasFileExtension ("png;gif")) return "image" + file.getFileExtension();
  601. else if (file.hasFileExtension ("html;htm")) return "text.html";
  602. else if (file.hasFileExtension ("txt;rtf")) return "text" + file.getFileExtension();
  603. else if (file.hasFileExtension ("plist")) return "text.plist.xml";
  604. else if (file.hasFileExtension ("app")) return "wrapper.application";
  605. else if (file.hasFileExtension ("component;vst;plugin")) return "wrapper.cfbundle";
  606. else if (file.hasFileExtension ("xcodeproj")) return "wrapper.pb-project";
  607. else if (file.hasFileExtension ("a")) return "archive.ar";
  608. return "file" + file.getFileExtension();
  609. }
  610. String addFile (const RelativePath& path, bool shouldBeCompiled, bool inhibitWarnings)
  611. {
  612. const String pathAsString (path.toUnixStyle());
  613. const String refID (addFileReference (path.toUnixStyle()));
  614. if (shouldBeCompiled)
  615. {
  616. if (path.hasFileExtension (".r"))
  617. rezFileIDs.add (addBuildFile (pathAsString, refID, false, inhibitWarnings));
  618. else
  619. addBuildFile (pathAsString, refID, true, inhibitWarnings);
  620. }
  621. return refID;
  622. }
  623. String addProjectItem (const Project::Item& projectItem)
  624. {
  625. if (projectItem.isGroup())
  626. {
  627. StringArray childIDs;
  628. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  629. {
  630. const String childID (addProjectItem (projectItem.getChild(i)));
  631. if (childID.isNotEmpty())
  632. childIDs.add (childID);
  633. }
  634. return addGroup (projectItem, childIDs);
  635. }
  636. else
  637. {
  638. if (projectItem.shouldBeAddedToTargetProject())
  639. {
  640. String itemPath (projectItem.getFilePath());
  641. bool inhibitWarnings = projectItem.getShouldInhibitWarningsValue().getValue();
  642. if (itemPath.startsWith ("${"))
  643. {
  644. const RelativePath path (itemPath, RelativePath::unknown);
  645. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  646. }
  647. else
  648. {
  649. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  650. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  651. }
  652. }
  653. }
  654. return String::empty;
  655. }
  656. void addFramework (const String& frameworkName)
  657. {
  658. const String path ("System/Library/Frameworks/" + frameworkName + ".framework");
  659. const String fileRefID (createFileRefID (path));
  660. addFileReference ("${SDKROOT}/" + path);
  661. frameworkIDs.add (addBuildFile (path, fileRefID, false, false));
  662. frameworkFileIDs.add (fileRefID);
  663. }
  664. void addGroup (const String& groupID, const String& groupName, const StringArray& childIDs)
  665. {
  666. ValueTree* v = new ValueTree (groupID);
  667. v->setProperty ("isa", "PBXGroup", 0);
  668. v->setProperty ("children", "(" + indentList (childIDs, ",") + " )", 0);
  669. v->setProperty (Ids::name, groupName, 0);
  670. v->setProperty ("sourceTree", "<group>", 0);
  671. pbxGroups.add (v);
  672. }
  673. String addGroup (const Project::Item& item, StringArray& childIDs)
  674. {
  675. const String groupName (item.getName().toString());
  676. const String groupID (getIDForGroup (item));
  677. addGroup (groupID, groupName, childIDs);
  678. return groupID;
  679. }
  680. void addMainBuildProduct()
  681. {
  682. jassert (xcodeFileType.isNotEmpty());
  683. jassert (xcodeBundleExtension.isEmpty() || xcodeBundleExtension.startsWithChar('.'));
  684. String productName (configs.getReference(0).getTargetBinaryName().toString());
  685. if (xcodeFileType == "archive.ar")
  686. productName = getLibbedFilename (productName);
  687. else
  688. productName += xcodeBundleExtension;
  689. addBuildProduct (xcodeFileType, productName);
  690. }
  691. void addBuildProduct (const String& fileType, const String& binaryName)
  692. {
  693. ValueTree* v = new ValueTree (createID ("__productFileID"));
  694. v->setProperty ("isa", "PBXFileReference", 0);
  695. v->setProperty ("explicitFileType", fileType, 0);
  696. v->setProperty ("includeInIndex", (int) 0, 0);
  697. v->setProperty ("path", sanitisePath (binaryName), 0);
  698. v->setProperty ("sourceTree", "BUILT_PRODUCTS_DIR", 0);
  699. pbxFileReferences.add (v);
  700. }
  701. void addTargetConfig (const String& configName, const StringArray& buildSettings)
  702. {
  703. ValueTree* v = new ValueTree (createID ("targetconfigid_" + configName));
  704. v->setProperty ("isa", "XCBuildConfiguration", 0);
  705. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  706. v->setProperty (Ids::name, configName, 0);
  707. targetConfigs.add (v);
  708. }
  709. void addProjectConfig (const String& configName, const StringArray& buildSettings)
  710. {
  711. ValueTree* v = new ValueTree (createID ("projectconfigid_" + configName));
  712. v->setProperty ("isa", "XCBuildConfiguration", 0);
  713. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  714. v->setProperty (Ids::name, configName, 0);
  715. projectConfigs.add (v);
  716. }
  717. void addConfigList (const OwnedArray <ValueTree>& configsToUse, const String& listID)
  718. {
  719. StringArray configIDs;
  720. for (int i = 0; i < configsToUse.size(); ++i)
  721. configIDs.add (configsToUse[i]->getType().toString());
  722. ValueTree* v = new ValueTree (listID);
  723. v->setProperty ("isa", "XCConfigurationList", 0);
  724. v->setProperty ("buildConfigurations", "(" + indentList (configIDs, ",") + " )", 0);
  725. v->setProperty ("defaultConfigurationIsVisible", (int) 0, 0);
  726. if (configsToUse[0] != nullptr)
  727. v->setProperty ("defaultConfigurationName", configsToUse[0]->getProperty (Ids::name), 0);
  728. misc.add (v);
  729. }
  730. ValueTree* addBuildPhase (const String& phaseType, const StringArray& fileIds)
  731. {
  732. String phaseId (createID (phaseType + "resbuildphase"));
  733. buildPhaseIDs.add (phaseId);
  734. ValueTree* v = new ValueTree (phaseId);
  735. v->setProperty ("isa", phaseType, 0);
  736. v->setProperty ("buildActionMask", "2147483647", 0);
  737. v->setProperty ("files", "(" + indentList (fileIds, ",") + " )", 0);
  738. v->setProperty ("runOnlyForDeploymentPostprocessing", (int) 0, 0);
  739. misc.add (v);
  740. return v;
  741. }
  742. void addTargetObject()
  743. {
  744. ValueTree* const v = new ValueTree (createID ("__target"));
  745. v->setProperty ("isa", "PBXNativeTarget", 0);
  746. v->setProperty ("buildConfigurationList", createID ("__configList"), 0);
  747. v->setProperty ("buildPhases", "(" + indentList (buildPhaseIDs, ",") + " )", 0);
  748. v->setProperty ("buildRules", "( )", 0);
  749. v->setProperty ("dependencies", "( )", 0);
  750. v->setProperty (Ids::name, projectName, 0);
  751. v->setProperty ("productName", projectName, 0);
  752. v->setProperty ("productReference", createID ("__productFileID"), 0);
  753. if (xcodeProductInstallPath.isNotEmpty())
  754. v->setProperty ("productInstallPath", xcodeProductInstallPath, 0);
  755. jassert (xcodeProductType.isNotEmpty());
  756. v->setProperty ("productType", xcodeProductType, 0);
  757. misc.add (v);
  758. }
  759. void addProjectObject()
  760. {
  761. ValueTree* const v = new ValueTree (createID ("__root"));
  762. v->setProperty ("isa", "PBXProject", 0);
  763. v->setProperty ("buildConfigurationList", createID ("__projList"), 0);
  764. v->setProperty ("compatibilityVersion", "Xcode 3.1", 0);
  765. v->setProperty ("hasScannedForEncodings", (int) 0, 0);
  766. v->setProperty ("mainGroup", createID ("__mainsourcegroup"), 0);
  767. v->setProperty ("projectDirPath", "\"\"", 0);
  768. v->setProperty ("projectRoot", "\"\"", 0);
  769. v->setProperty ("targets", "( " + createID ("__target") + " )", 0);
  770. misc.add (v);
  771. }
  772. void addShellScriptPhase()
  773. {
  774. if (xcodeShellScript.isNotEmpty())
  775. {
  776. ValueTree* const v = addBuildPhase ("PBXShellScriptBuildPhase", StringArray());
  777. v->setProperty (Ids::name, xcodeShellScriptTitle, 0);
  778. v->setProperty ("shellPath", "/bin/sh", 0);
  779. v->setProperty ("shellScript", xcodeShellScript.replace ("\\", "\\\\")
  780. .replace ("\"", "\\\"")
  781. .replace ("\r\n", "\\n")
  782. .replace ("\n", "\\n"), 0);
  783. }
  784. }
  785. //==============================================================================
  786. static String indentList (const StringArray& list, const String& separator)
  787. {
  788. if (list.size() == 0)
  789. return " ";
  790. return "\n\t\t\t\t" + list.joinIntoString (separator + "\n\t\t\t\t")
  791. + (separator == ";" ? separator : String::empty);
  792. }
  793. String createID (String rootString) const
  794. {
  795. if (rootString.startsWith ("${"))
  796. rootString = rootString.fromFirstOccurrenceOf ("}/", false, false);
  797. rootString += project.getProjectUID();
  798. return MD5 (rootString.toUTF8()).toHexString().substring (0, 24).toUpperCase();
  799. }
  800. String createFileRefID (const RelativePath& path) const
  801. {
  802. return createFileRefID (path.toUnixStyle());
  803. }
  804. String createFileRefID (const String& path) const
  805. {
  806. return createID ("__fileref_" + path);
  807. }
  808. String getIDForGroup (const Project::Item& item) const
  809. {
  810. return createID (item.getID());
  811. }
  812. bool shouldFileBeCompiledByDefault (const RelativePath& file) const
  813. {
  814. return file.hasFileExtension (sourceFileExtensions);
  815. }
  816. };
  817. #endif // __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__