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.

1014 lines
40KB

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