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.

1117 lines
44KB

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