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.

1020 lines
40KB

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