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.

1054 lines
41KB

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