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.

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