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.

1022 lines
40KB

  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. String gccVersion ("com.apple.compilers.llvm.clang.1_0");
  412. if (! iOS)
  413. {
  414. const String sdk (config.getMacSDKVersion().toString());
  415. const String sdkCompat (config.getMacCompatibilityVersion().toString());
  416. if (sdk == Project::BuildConfiguration::osxVersion10_4)
  417. {
  418. s.add ("SDKROOT = macosx10.4");
  419. gccVersion = "4.0";
  420. }
  421. else if (sdk == Project::BuildConfiguration::osxVersion10_5)
  422. {
  423. s.add ("SDKROOT = macosx10.5");
  424. }
  425. else if (sdk == Project::BuildConfiguration::osxVersion10_6)
  426. {
  427. s.add ("SDKROOT = macosx10.6");
  428. }
  429. if (sdkCompat == Project::BuildConfiguration::osxVersion10_4) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.4");
  430. else if (sdkCompat == Project::BuildConfiguration::osxVersion10_5) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.5");
  431. else if (sdkCompat == Project::BuildConfiguration::osxVersion10_6) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.6");
  432. s.add ("MACOSX_DEPLOYMENT_TARGET_ppc = 10.4");
  433. }
  434. s.add ("GCC_VERSION = " + gccVersion);
  435. {
  436. StringArray linkerFlags, librarySearchPaths;
  437. getLinkerFlags (config, linkerFlags, librarySearchPaths);
  438. if (linkerFlags.size() > 0)
  439. s.add ("OTHER_LDFLAGS = \"" + linkerFlags.joinIntoString (" ") + "\"");
  440. if (librarySearchPaths.size() > 0)
  441. {
  442. String libPaths ("LIBRARY_SEARCH_PATHS = (\"$(inherited)\"");
  443. for (int i = 0; i < librarySearchPaths.size(); ++i)
  444. libPaths += ", \"\\\"" + librarySearchPaths[i] + "\\\"\"";
  445. s.add (libPaths + ")");
  446. }
  447. }
  448. StringPairArray defines;
  449. if (config.isDebug().getValue())
  450. {
  451. defines.set ("_DEBUG", "1");
  452. defines.set ("DEBUG", "1");
  453. s.add ("ONLY_ACTIVE_ARCH = YES");
  454. s.add ("COPY_PHASE_STRIP = NO");
  455. s.add ("GCC_DYNAMIC_NO_PIC = NO");
  456. }
  457. else
  458. {
  459. defines.set ("_NDEBUG", "1");
  460. defines.set ("NDEBUG", "1");
  461. s.add ("GCC_GENERATE_DEBUGGING_SYMBOLS = NO");
  462. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = YES");
  463. }
  464. {
  465. const String objCSuffix (getObjCSuffix().toString().trim());
  466. if (objCSuffix.isNotEmpty())
  467. defines.set ("JUCE_ObjCExtraSuffix", replacePreprocessorTokens (config, objCSuffix));
  468. }
  469. {
  470. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  471. StringArray defsList;
  472. for (int i = 0; i < defines.size(); ++i)
  473. {
  474. String def (defines.getAllKeys()[i]);
  475. const String value (defines.getAllValues()[i]);
  476. if (value.isNotEmpty())
  477. def << "=" << value;
  478. defsList.add (def.quoted());
  479. }
  480. s.add ("GCC_PREPROCESSOR_DEFINITIONS = (" + indentList (defsList, ",") + ")");
  481. }
  482. return s;
  483. }
  484. void addFrameworks()
  485. {
  486. if (! projectType.isLibrary())
  487. {
  488. StringArray s (xcodeFrameworks);
  489. s.trim();
  490. s.removeDuplicates (true);
  491. s.sort (true);
  492. for (int i = 0; i < s.size(); ++i)
  493. addFramework (s[i]);
  494. }
  495. }
  496. //==============================================================================
  497. void writeProjectFile (OutputStream& output)
  498. {
  499. output << "// !$*UTF8*$!\n{\n"
  500. "\tarchiveVersion = 1;\n"
  501. "\tclasses = {\n\t};\n"
  502. "\tobjectVersion = 46;\n"
  503. "\tobjects = {\n\n";
  504. Array <ValueTree*> objects;
  505. objects.addArray (pbxBuildFiles);
  506. objects.addArray (pbxFileReferences);
  507. objects.addArray (pbxGroups);
  508. objects.addArray (targetConfigs);
  509. objects.addArray (projectConfigs);
  510. objects.addArray (misc);
  511. for (int i = 0; i < objects.size(); ++i)
  512. {
  513. ValueTree& o = *objects.getUnchecked(i);
  514. output << "\t\t" << o.getType().toString() << " = { ";
  515. for (int j = 0; j < o.getNumProperties(); ++j)
  516. {
  517. const Identifier propertyName (o.getPropertyName(j));
  518. String val (o.getProperty (propertyName).toString());
  519. if (val.isEmpty() || (val.containsAnyOf (" \t;<>()=,&+-_\r\n")
  520. && ! (val.trimStart().startsWithChar ('(')
  521. || val.trimStart().startsWithChar ('{'))))
  522. val = val.quoted();
  523. output << propertyName.toString() << " = " << val << "; ";
  524. }
  525. output << "};\n";
  526. }
  527. output << "\t};\n\trootObject = " << createID ("__root") << ";\n}\n";
  528. }
  529. static void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  530. {
  531. xml->createNewChildElement ("key")->addTextElement (key);
  532. xml->createNewChildElement ("string")->addTextElement (value);
  533. }
  534. static void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  535. {
  536. xml->createNewChildElement ("key")->addTextElement (key);
  537. xml->createNewChildElement (value ? "true" : "false");
  538. }
  539. String addBuildFile (const String& path, const String& fileRefID, bool addToSourceBuildPhase, bool inhibitWarnings)
  540. {
  541. String fileID (createID (path + "buildref"));
  542. if (addToSourceBuildPhase)
  543. sourceIDs.add (fileID);
  544. ValueTree* v = new ValueTree (fileID);
  545. v->setProperty ("isa", "PBXBuildFile", 0);
  546. v->setProperty ("fileRef", fileRefID, 0);
  547. if (inhibitWarnings)
  548. v->setProperty ("settings", "{COMPILER_FLAGS = \"-w\"; }", 0);
  549. pbxBuildFiles.add (v);
  550. return fileID;
  551. }
  552. String addBuildFile (const RelativePath& path, bool addToSourceBuildPhase, bool inhibitWarnings)
  553. {
  554. return addBuildFile (path.toUnixStyle(), createFileRefID (path), addToSourceBuildPhase, inhibitWarnings);
  555. }
  556. String addFileReference (String pathString)
  557. {
  558. String sourceTree ("SOURCE_ROOT");
  559. RelativePath path (pathString, RelativePath::unknown);
  560. if (pathString.startsWith ("${"))
  561. {
  562. sourceTree = pathString.substring (2).upToFirstOccurrenceOf ("}", false, false);
  563. pathString = pathString.fromFirstOccurrenceOf ("}/", false, false);
  564. }
  565. else if (path.isAbsolute())
  566. {
  567. sourceTree = "<absolute>";
  568. }
  569. const String fileRefID (createFileRefID (pathString));
  570. ScopedPointer<ValueTree> v (new ValueTree (fileRefID));
  571. v->setProperty ("isa", "PBXFileReference", 0);
  572. v->setProperty ("lastKnownFileType", getFileType (path), 0);
  573. v->setProperty (Ids::name, pathString.fromLastOccurrenceOf ("/", false, false), 0);
  574. v->setProperty ("path", sanitisePath (pathString), 0);
  575. v->setProperty ("sourceTree", sourceTree, 0);
  576. const int existing = pbxFileReferences.indexOfSorted (*this, v);
  577. if (existing >= 0)
  578. {
  579. // If this fails, there's either a string hash collision, or the same file is being added twice (incorrectly)
  580. jassert (pbxFileReferences.getUnchecked (existing)->isEquivalentTo (*v));
  581. }
  582. else
  583. {
  584. pbxFileReferences.addSorted (*this, v.release());
  585. }
  586. return fileRefID;
  587. }
  588. public:
  589. static int compareElements (const ValueTree* first, const ValueTree* second)
  590. {
  591. return first->getType().toString().compare (second->getType().toString());
  592. }
  593. private:
  594. static String getFileType (const RelativePath& file)
  595. {
  596. if (file.hasFileExtension ("cpp;cc;cxx")) return "sourcecode.cpp.cpp";
  597. else if (file.hasFileExtension (".mm")) return "sourcecode.cpp.objcpp";
  598. else if (file.hasFileExtension (".m")) return "sourcecode.c.objc";
  599. else if (file.hasFileExtension (headerFileExtensions)) return "sourcecode.c.h";
  600. else if (file.hasFileExtension (".framework")) return "wrapper.framework";
  601. else if (file.hasFileExtension (".jpeg;.jpg")) return "image.jpeg";
  602. else if (file.hasFileExtension ("png;gif")) return "image" + file.getFileExtension();
  603. else if (file.hasFileExtension ("html;htm")) return "text.html";
  604. else if (file.hasFileExtension ("xml;zip;wav")) return "file" + file.getFileExtension();
  605. else if (file.hasFileExtension ("txt;rtf")) return "text" + file.getFileExtension();
  606. else if (file.hasFileExtension ("plist")) return "text.plist.xml";
  607. else if (file.hasFileExtension ("app")) return "wrapper.application";
  608. else if (file.hasFileExtension ("component;vst;plugin")) return "wrapper.cfbundle";
  609. else if (file.hasFileExtension ("xcodeproj")) return "wrapper.pb-project";
  610. else if (file.hasFileExtension ("a")) return "archive.ar";
  611. return "file" + file.getFileExtension();
  612. }
  613. String addFile (const RelativePath& path, bool shouldBeCompiled, bool inhibitWarnings)
  614. {
  615. const String pathAsString (path.toUnixStyle());
  616. const String refID (addFileReference (path.toUnixStyle()));
  617. if (shouldBeCompiled)
  618. {
  619. if (path.hasFileExtension (".r"))
  620. rezFileIDs.add (addBuildFile (pathAsString, refID, false, inhibitWarnings));
  621. else
  622. addBuildFile (pathAsString, refID, true, inhibitWarnings);
  623. }
  624. return refID;
  625. }
  626. String addProjectItem (const Project::Item& projectItem)
  627. {
  628. if (projectItem.isGroup())
  629. {
  630. StringArray childIDs;
  631. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  632. {
  633. const String childID (addProjectItem (projectItem.getChild(i)));
  634. if (childID.isNotEmpty())
  635. childIDs.add (childID);
  636. }
  637. return addGroup (projectItem, childIDs);
  638. }
  639. else
  640. {
  641. if (projectItem.shouldBeAddedToTargetProject())
  642. {
  643. String itemPath (projectItem.getFilePath());
  644. bool inhibitWarnings = projectItem.getShouldInhibitWarningsValue().getValue();
  645. if (itemPath.startsWith ("${"))
  646. {
  647. const RelativePath path (itemPath, RelativePath::unknown);
  648. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  649. }
  650. else
  651. {
  652. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  653. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  654. }
  655. }
  656. }
  657. return String::empty;
  658. }
  659. void addFramework (const String& frameworkName)
  660. {
  661. const String path ("System/Library/Frameworks/" + frameworkName + ".framework");
  662. const String fileRefID (createFileRefID (path));
  663. addFileReference ("${SDKROOT}/" + path);
  664. frameworkIDs.add (addBuildFile (path, fileRefID, false, false));
  665. frameworkFileIDs.add (fileRefID);
  666. }
  667. void addGroup (const String& groupID, const String& groupName, const StringArray& childIDs)
  668. {
  669. ValueTree* v = new ValueTree (groupID);
  670. v->setProperty ("isa", "PBXGroup", 0);
  671. v->setProperty ("children", "(" + indentList (childIDs, ",") + " )", 0);
  672. v->setProperty (Ids::name, groupName, 0);
  673. v->setProperty ("sourceTree", "<group>", 0);
  674. pbxGroups.add (v);
  675. }
  676. String addGroup (const Project::Item& item, StringArray& childIDs)
  677. {
  678. const String groupName (item.getName().toString());
  679. const String groupID (getIDForGroup (item));
  680. addGroup (groupID, groupName, childIDs);
  681. return groupID;
  682. }
  683. void addMainBuildProduct()
  684. {
  685. jassert (xcodeFileType.isNotEmpty());
  686. jassert (xcodeBundleExtension.isEmpty() || xcodeBundleExtension.startsWithChar('.'));
  687. String productName (configs.getReference(0).getTargetBinaryName().toString());
  688. if (xcodeFileType == "archive.ar")
  689. productName = getLibbedFilename (productName);
  690. else
  691. productName += xcodeBundleExtension;
  692. addBuildProduct (xcodeFileType, productName);
  693. }
  694. void addBuildProduct (const String& fileType, const String& binaryName)
  695. {
  696. ValueTree* v = new ValueTree (createID ("__productFileID"));
  697. v->setProperty ("isa", "PBXFileReference", 0);
  698. v->setProperty ("explicitFileType", fileType, 0);
  699. v->setProperty ("includeInIndex", (int) 0, 0);
  700. v->setProperty ("path", sanitisePath (binaryName), 0);
  701. v->setProperty ("sourceTree", "BUILT_PRODUCTS_DIR", 0);
  702. pbxFileReferences.add (v);
  703. }
  704. void addTargetConfig (const String& configName, const StringArray& buildSettings)
  705. {
  706. ValueTree* v = new ValueTree (createID ("targetconfigid_" + configName));
  707. v->setProperty ("isa", "XCBuildConfiguration", 0);
  708. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  709. v->setProperty (Ids::name, configName, 0);
  710. targetConfigs.add (v);
  711. }
  712. void addProjectConfig (const String& configName, const StringArray& buildSettings)
  713. {
  714. ValueTree* v = new ValueTree (createID ("projectconfigid_" + configName));
  715. v->setProperty ("isa", "XCBuildConfiguration", 0);
  716. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  717. v->setProperty (Ids::name, configName, 0);
  718. projectConfigs.add (v);
  719. }
  720. void addConfigList (const OwnedArray <ValueTree>& configsToUse, const String& listID)
  721. {
  722. StringArray configIDs;
  723. for (int i = 0; i < configsToUse.size(); ++i)
  724. configIDs.add (configsToUse[i]->getType().toString());
  725. ValueTree* v = new ValueTree (listID);
  726. v->setProperty ("isa", "XCConfigurationList", 0);
  727. v->setProperty ("buildConfigurations", "(" + indentList (configIDs, ",") + " )", 0);
  728. v->setProperty ("defaultConfigurationIsVisible", (int) 0, 0);
  729. if (configsToUse[0] != nullptr)
  730. v->setProperty ("defaultConfigurationName", configsToUse[0]->getProperty (Ids::name), 0);
  731. misc.add (v);
  732. }
  733. ValueTree* addBuildPhase (const String& phaseType, const StringArray& fileIds)
  734. {
  735. String phaseId (createID (phaseType + "resbuildphase"));
  736. buildPhaseIDs.add (phaseId);
  737. ValueTree* v = new ValueTree (phaseId);
  738. v->setProperty ("isa", phaseType, 0);
  739. v->setProperty ("buildActionMask", "2147483647", 0);
  740. v->setProperty ("files", "(" + indentList (fileIds, ",") + " )", 0);
  741. v->setProperty ("runOnlyForDeploymentPostprocessing", (int) 0, 0);
  742. misc.add (v);
  743. return v;
  744. }
  745. void addTargetObject()
  746. {
  747. ValueTree* const v = new ValueTree (createID ("__target"));
  748. v->setProperty ("isa", "PBXNativeTarget", 0);
  749. v->setProperty ("buildConfigurationList", createID ("__configList"), 0);
  750. v->setProperty ("buildPhases", "(" + indentList (buildPhaseIDs, ",") + " )", 0);
  751. v->setProperty ("buildRules", "( )", 0);
  752. v->setProperty ("dependencies", "( )", 0);
  753. v->setProperty (Ids::name, projectName, 0);
  754. v->setProperty ("productName", projectName, 0);
  755. v->setProperty ("productReference", createID ("__productFileID"), 0);
  756. if (xcodeProductInstallPath.isNotEmpty())
  757. v->setProperty ("productInstallPath", xcodeProductInstallPath, 0);
  758. jassert (xcodeProductType.isNotEmpty());
  759. v->setProperty ("productType", xcodeProductType, 0);
  760. misc.add (v);
  761. }
  762. void addProjectObject()
  763. {
  764. ValueTree* const v = new ValueTree (createID ("__root"));
  765. v->setProperty ("isa", "PBXProject", 0);
  766. v->setProperty ("buildConfigurationList", createID ("__projList"), 0);
  767. v->setProperty ("compatibilityVersion", "Xcode 3.2", 0);
  768. v->setProperty ("hasScannedForEncodings", (int) 0, 0);
  769. v->setProperty ("mainGroup", createID ("__mainsourcegroup"), 0);
  770. v->setProperty ("projectDirPath", "\"\"", 0);
  771. v->setProperty ("projectRoot", "\"\"", 0);
  772. v->setProperty ("targets", "( " + createID ("__target") + " )", 0);
  773. misc.add (v);
  774. }
  775. void addShellScriptPhase()
  776. {
  777. if (xcodeShellScript.isNotEmpty())
  778. {
  779. ValueTree* const v = addBuildPhase ("PBXShellScriptBuildPhase", StringArray());
  780. v->setProperty (Ids::name, xcodeShellScriptTitle, 0);
  781. v->setProperty ("shellPath", "/bin/sh", 0);
  782. v->setProperty ("shellScript", xcodeShellScript.replace ("\\", "\\\\")
  783. .replace ("\"", "\\\"")
  784. .replace ("\r\n", "\\n")
  785. .replace ("\n", "\\n"), 0);
  786. }
  787. }
  788. //==============================================================================
  789. static String indentList (const StringArray& list, const String& separator)
  790. {
  791. if (list.size() == 0)
  792. return " ";
  793. return "\n\t\t\t\t" + list.joinIntoString (separator + "\n\t\t\t\t")
  794. + (separator == ";" ? separator : String::empty);
  795. }
  796. String createID (String rootString) const
  797. {
  798. if (rootString.startsWith ("${"))
  799. rootString = rootString.fromFirstOccurrenceOf ("}/", false, false);
  800. rootString += project.getProjectUID();
  801. return MD5 (rootString.toUTF8()).toHexString().substring (0, 24).toUpperCase();
  802. }
  803. String createFileRefID (const RelativePath& path) const
  804. {
  805. return createFileRefID (path.toUnixStyle());
  806. }
  807. String createFileRefID (const String& path) const
  808. {
  809. return createID ("__fileref_" + path);
  810. }
  811. String getIDForGroup (const Project::Item& item) const
  812. {
  813. return createID (item.getID());
  814. }
  815. bool shouldFileBeCompiledByDefault (const RelativePath& file) const
  816. {
  817. return file.hasFileExtension (sourceFileExtensions);
  818. }
  819. };
  820. #endif // __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__