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.

1023 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. bool canCopeWithDuplicateFiles() { return true; }
  71. void createPropertyEditors (Array <PropertyComponent*>& props)
  72. {
  73. ProjectExporter::createPropertyEditors (props);
  74. props.add (new TextPropertyComponent (getObjCSuffix(), "Objective-C class name suffix", 64, false));
  75. props.getLast()->setTooltip ("Because objective-C linkage is done by string-matching, you can get horrible linkage mix-ups when different modules containing the "
  76. "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.");
  77. if (projectType.isGUIApplication() && ! iOS)
  78. {
  79. props.add (new TextPropertyComponent (getSetting ("documentExtensions"), "Document file extensions", 128, false));
  80. props.getLast()->setTooltip ("A comma-separated list of file extensions for documents that your app can open.");
  81. }
  82. else if (iOS)
  83. {
  84. props.add (new BooleanPropertyComponent (getSetting ("UIFileSharingEnabled"), "File Sharing Enabled", "Enabled"));
  85. props.getLast()->setTooltip ("Enable this to expose your app's files to iTunes.");
  86. props.add (new BooleanPropertyComponent (getSetting ("UIStatusBarHidden"), "Status Bar Hidden", "Enabled"));
  87. props.getLast()->setTooltip ("Enable this to disable the status bar in your app.");
  88. }
  89. }
  90. void launchProject()
  91. {
  92. getProjectBundle().startAsProcess();
  93. }
  94. //==============================================================================
  95. void create()
  96. {
  97. infoPlistFile = getTargetFolder().getChildFile ("Info.plist");
  98. createIconFile();
  99. File projectBundle (getProjectBundle());
  100. createDirectoryOrThrow (projectBundle);
  101. createObjects();
  102. File projectFile (projectBundle.getChildFile ("project.pbxproj"));
  103. {
  104. MemoryOutputStream mo;
  105. writeProjectFile (mo);
  106. overwriteFileIfDifferentOrThrow (projectFile, mo);
  107. }
  108. writeInfoPlistFile();
  109. }
  110. private:
  111. OwnedArray<ValueTree> pbxBuildFiles, pbxFileReferences, pbxGroups, misc, projectConfigs, targetConfigs;
  112. StringArray buildPhaseIDs, resourceIDs, sourceIDs, frameworkIDs;
  113. StringArray frameworkFileIDs, rezFileIDs, resourceFileRefs;
  114. File infoPlistFile, iconFile;
  115. const bool iOS;
  116. static String sanitisePath (const String& path)
  117. {
  118. if (path.startsWithChar ('~'))
  119. return "$(HOME)" + path.substring (1);
  120. return path;
  121. }
  122. File getProjectBundle() const { return getTargetFolder().getChildFile (project.getProjectFilenameRoot()).withFileExtension (".xcodeproj"); }
  123. //==============================================================================
  124. void createObjects()
  125. {
  126. addFrameworks();
  127. addMainBuildProduct();
  128. if (xcodeCreatePList)
  129. {
  130. RelativePath plistPath (infoPlistFile, getTargetFolder(), RelativePath::buildTargetFolder);
  131. addFileReference (plistPath.toUnixStyle());
  132. resourceFileRefs.add (createFileRefID (plistPath));
  133. }
  134. if (iconFile.exists())
  135. {
  136. RelativePath iconPath (iconFile, getTargetFolder(), RelativePath::buildTargetFolder);
  137. addFileReference (iconPath.toUnixStyle());
  138. resourceIDs.add (addBuildFile (iconPath, false, false));
  139. resourceFileRefs.add (createFileRefID (iconPath));
  140. }
  141. {
  142. StringArray topLevelGroupIDs;
  143. for (int i = 0; i < groups.size(); ++i)
  144. if (groups.getReference(i).getNumChildren() > 0)
  145. topLevelGroupIDs.add (addProjectItem (groups.getReference(i)));
  146. { // Add 'resources' group
  147. String resourcesGroupID (createID ("__resources"));
  148. addGroup (resourcesGroupID, "Resources", resourceFileRefs);
  149. topLevelGroupIDs.add (resourcesGroupID);
  150. }
  151. { // Add 'frameworks' group
  152. String frameworksGroupID (createID ("__frameworks"));
  153. addGroup (frameworksGroupID, "Frameworks", frameworkFileIDs);
  154. topLevelGroupIDs.add (frameworksGroupID);
  155. }
  156. { // Add 'products' group
  157. String productsGroupID (createID ("__products"));
  158. StringArray products;
  159. products.add (createID ("__productFileID"));
  160. addGroup (productsGroupID, "Products", products);
  161. topLevelGroupIDs.add (productsGroupID);
  162. }
  163. addGroup (createID ("__mainsourcegroup"), "Source", topLevelGroupIDs);
  164. }
  165. for (int i = 0; i < configs.size(); ++i)
  166. {
  167. const Project::BuildConfiguration& config = configs.getReference(i);
  168. addProjectConfig (config.getName().getValue(), getProjectSettings (config));
  169. addTargetConfig (config.getName().getValue(), getTargetSettings (config));
  170. }
  171. addConfigList (projectConfigs, createID ("__projList"));
  172. addConfigList (targetConfigs, createID ("__configList"));
  173. if (! projectType.isLibrary())
  174. addBuildPhase ("PBXResourcesBuildPhase", resourceIDs);
  175. if (rezFileIDs.size() > 0)
  176. addBuildPhase ("PBXRezBuildPhase", rezFileIDs);
  177. addBuildPhase ("PBXSourcesBuildPhase", sourceIDs);
  178. if (! projectType.isLibrary())
  179. addBuildPhase ("PBXFrameworksBuildPhase", frameworkIDs);
  180. addShellScriptPhase();
  181. addTargetObject();
  182. addProjectObject();
  183. }
  184. static Image fixMacIconImageSize (Image& image)
  185. {
  186. const int w = image.getWidth();
  187. const int h = image.getHeight();
  188. if (w != h || (w != 16 && w != 32 && w != 48 && w != 64))
  189. {
  190. const int newSize = w >= 128 ? 128 : (w >= 64 ? 64 : (w >= 32 ? 32 : 16));
  191. Image newIm (Image::ARGB, newSize, newSize, true, SoftwareImageType());
  192. Graphics g (newIm);
  193. g.drawImageWithin (image, 0, 0, newSize, newSize,
  194. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  195. return newIm;
  196. }
  197. return image;
  198. }
  199. void writeIcnsFile (const Array<Image>& images, OutputStream& out)
  200. {
  201. MemoryOutputStream data;
  202. for (int i = 0; i < images.size(); ++i)
  203. {
  204. Image image (fixMacIconImageSize (images.getReference (i)));
  205. const int w = image.getWidth();
  206. const int h = image.getHeight();
  207. const char* type = nullptr;
  208. const char* maskType = nullptr;
  209. if (w == h)
  210. {
  211. if (w == 16) { type = "is32"; maskType = "s8mk"; }
  212. if (w == 32) { type = "il32"; maskType = "l8mk"; }
  213. if (w == 48) { type = "ih32"; maskType = "h8mk"; }
  214. if (w == 128) { type = "it32"; maskType = "t8mk"; }
  215. }
  216. if (type != nullptr)
  217. {
  218. data.write (type, 4);
  219. data.writeIntBigEndian (8 + 4 * w * h);
  220. const Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  221. int y;
  222. for (y = 0; y < h; ++y)
  223. {
  224. for (int x = 0; x < w; ++x)
  225. {
  226. const Colour pixel (bitmap.getPixelColour (x, y));
  227. data.writeByte ((char) pixel.getAlpha());
  228. data.writeByte ((char) pixel.getRed());
  229. data.writeByte ((char) pixel.getGreen());
  230. data.writeByte ((char) pixel.getBlue());
  231. }
  232. }
  233. data.write (maskType, 4);
  234. data.writeIntBigEndian (8 + w * h);
  235. for (y = 0; y < h; ++y)
  236. {
  237. for (int x = 0; x < w; ++x)
  238. {
  239. const Colour pixel (bitmap.getPixelColour (x, y));
  240. data.writeByte ((char) pixel.getAlpha());
  241. }
  242. }
  243. }
  244. }
  245. jassert (data.getDataSize() > 0); // no suitable sized images?
  246. out.write ("icns", 4);
  247. out.writeIntBigEndian (data.getDataSize() + 8);
  248. out << data;
  249. }
  250. void createIconFile()
  251. {
  252. Array<Image> images;
  253. Image bigIcon (project.getBigIcon());
  254. if (bigIcon.isValid())
  255. images.add (bigIcon);
  256. Image smallIcon (project.getSmallIcon());
  257. if (smallIcon.isValid())
  258. images.add (smallIcon);
  259. if (images.size() > 0)
  260. {
  261. MemoryOutputStream mo;
  262. writeIcnsFile (images, mo);
  263. iconFile = getTargetFolder().getChildFile ("Icon.icns");
  264. overwriteFileIfDifferentOrThrow (iconFile, mo);
  265. }
  266. }
  267. void writeInfoPlistFile()
  268. {
  269. if (! xcodeCreatePList)
  270. return;
  271. XmlElement plist ("plist");
  272. XmlElement* dict = plist.createNewChildElement ("dict");
  273. if (iOS)
  274. addPlistDictionaryKeyBool (dict, "LSRequiresIPhoneOS", true);
  275. addPlistDictionaryKey (dict, "CFBundleExecutable", "${EXECUTABLE_NAME}");
  276. addPlistDictionaryKey (dict, "CFBundleIconFile", iconFile.exists() ? iconFile.getFileName() : String::empty);
  277. addPlistDictionaryKey (dict, "CFBundleIdentifier", project.getBundleIdentifier().toString());
  278. addPlistDictionaryKey (dict, "CFBundleName", projectName);
  279. addPlistDictionaryKey (dict, "CFBundlePackageType", xcodePackageType);
  280. addPlistDictionaryKey (dict, "CFBundleSignature", xcodeBundleSignature);
  281. addPlistDictionaryKey (dict, "CFBundleShortVersionString", project.getVersion().toString());
  282. addPlistDictionaryKey (dict, "CFBundleVersion", project.getVersion().toString());
  283. StringArray documentExtensions;
  284. documentExtensions.addTokens (replacePreprocessorDefs (getAllPreprocessorDefs(), getSetting ("documentExtensions").toString()),
  285. ",", String::empty);
  286. documentExtensions.trim();
  287. documentExtensions.removeEmptyStrings (true);
  288. if (documentExtensions.size() > 0)
  289. {
  290. dict->createNewChildElement ("key")->addTextElement ("CFBundleDocumentTypes");
  291. XmlElement* dict2 = dict->createNewChildElement ("array")->createNewChildElement ("dict");
  292. for (int i = 0; i < documentExtensions.size(); ++i)
  293. {
  294. String ex (documentExtensions[i]);
  295. if (ex.startsWithChar ('.'))
  296. ex = ex.substring (1);
  297. dict2->createNewChildElement ("key")->addTextElement ("CFBundleTypeExtensions");
  298. dict2->createNewChildElement ("array")->createNewChildElement ("string")->addTextElement (ex);
  299. addPlistDictionaryKey (dict2, "CFBundleTypeName", ex);
  300. addPlistDictionaryKey (dict2, "CFBundleTypeRole", "Editor");
  301. addPlistDictionaryKey (dict2, "NSPersistentStoreTypeKey", "XML");
  302. }
  303. }
  304. if (getSetting ("UIFileSharingEnabled").getValue())
  305. addPlistDictionaryKeyBool (dict, "UIFileSharingEnabled", true);
  306. if (getSetting ("UIStatusBarHidden").getValue())
  307. addPlistDictionaryKeyBool (dict, "UIStatusBarHidden", true);
  308. for (int i = 0; i < xcodeExtraPListEntries.size(); ++i)
  309. dict->addChildElement (new XmlElement (xcodeExtraPListEntries.getReference(i)));
  310. MemoryOutputStream mo;
  311. plist.writeToStream (mo, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  312. overwriteFileIfDifferentOrThrow (infoPlistFile, mo);
  313. }
  314. StringArray getHeaderSearchPaths (const Project::BuildConfiguration& config)
  315. {
  316. StringArray searchPaths (extraSearchPaths);
  317. searchPaths.addArray (config.getHeaderSearchPaths());
  318. searchPaths.removeDuplicates (false);
  319. return searchPaths;
  320. }
  321. static void getLinkerFlagsForStaticLibrary (const RelativePath& library, StringArray& flags, StringArray& librarySearchPaths)
  322. {
  323. jassert (library.getFileNameWithoutExtension().substring (0, 3) == "lib");
  324. flags.add ("-l" + library.getFileNameWithoutExtension().substring (3));
  325. String searchPath (library.toUnixStyle().upToLastOccurrenceOf ("/", false, false));
  326. if (! library.isAbsolute())
  327. searchPath = "$(SRCROOT)/" + searchPath;
  328. librarySearchPaths.add (sanitisePath (searchPath));
  329. }
  330. void getLinkerFlags (const Project::BuildConfiguration& config, StringArray& flags, StringArray& librarySearchPaths)
  331. {
  332. if (xcodeIsBundle)
  333. flags.add ("-bundle");
  334. const Array<RelativePath>& extraLibs = config.isDebug().getValue() ? xcodeExtraLibrariesDebug
  335. : xcodeExtraLibrariesRelease;
  336. for (int i = 0; i < extraLibs.size(); ++i)
  337. getLinkerFlagsForStaticLibrary (extraLibs.getReference(i), flags, librarySearchPaths);
  338. flags.add (replacePreprocessorTokens (config, getExtraLinkerFlags().toString()));
  339. flags.removeEmptyStrings (true);
  340. }
  341. StringArray getProjectSettings (const Project::BuildConfiguration& config)
  342. {
  343. StringArray s;
  344. s.add ("ALWAYS_SEARCH_USER_PATHS = NO");
  345. s.add ("GCC_C_LANGUAGE_STANDARD = c99");
  346. s.add ("GCC_WARN_ABOUT_RETURN_TYPE = YES");
  347. s.add ("GCC_WARN_CHECK_SWITCH_STATEMENTS = YES");
  348. s.add ("GCC_WARN_UNUSED_VARIABLE = YES");
  349. s.add ("GCC_WARN_MISSING_PARENTHESES = YES");
  350. s.add ("GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES");
  351. s.add ("GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES");
  352. s.add ("WARNING_CFLAGS = -Wreorder");
  353. s.add ("GCC_MODEL_TUNING = G5");
  354. if (projectType.isLibrary())
  355. {
  356. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = NO");
  357. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = NO");
  358. }
  359. else
  360. {
  361. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = YES");
  362. }
  363. if (iOS)
  364. {
  365. s.add ("\"CODE_SIGN_IDENTITY[sdk=iphoneos*]\" = \"iPhone Developer\"");
  366. s.add ("SDKROOT = iphoneos");
  367. s.add ("TARGETED_DEVICE_FAMILY = \"1,2\"");
  368. }
  369. s.add ("ZERO_LINK = NO");
  370. if (xcodeCanUseDwarf)
  371. s.add ("DEBUG_INFORMATION_FORMAT = \"dwarf\"");
  372. s.add ("PRODUCT_NAME = \"" + config.getTargetBinaryName().toString() + "\"");
  373. return s;
  374. }
  375. StringArray getTargetSettings (const Project::BuildConfiguration& config)
  376. {
  377. StringArray s;
  378. const String arch (config.getMacArchitecture().toString());
  379. if (arch == Project::BuildConfiguration::osxArch_Native) s.add ("ARCHS = \"$(ARCHS_NATIVE)\"");
  380. else if (arch == Project::BuildConfiguration::osxArch_32BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_BIT)\"");
  381. else if (arch == Project::BuildConfiguration::osxArch_64BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_64_BIT)\"");
  382. else if (arch == Project::BuildConfiguration::osxArch_64Bit) s.add ("ARCHS = \"$(ARCHS_STANDARD_64_BIT)\"");
  383. s.add ("HEADER_SEARCH_PATHS = \"" + replacePreprocessorTokens (config, getHeaderSearchPaths (config).joinIntoString (" ")) + " $(inherited)\"");
  384. s.add ("GCC_OPTIMIZATION_LEVEL = " + config.getGCCOptimisationFlag());
  385. s.add ("INFOPLIST_FILE = " + infoPlistFile.getFileName());
  386. const String extraFlags (replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim());
  387. if (extraFlags.isNotEmpty())
  388. s.add ("OTHER_CPLUSPLUSFLAGS = \"" + extraFlags + "\"");
  389. if (xcodeProductInstallPath.isNotEmpty())
  390. s.add ("INSTALL_PATH = \"" + xcodeProductInstallPath + "\"");
  391. if (xcodeIsBundle)
  392. {
  393. s.add ("LIBRARY_STYLE = Bundle");
  394. s.add ("WRAPPER_EXTENSION = " + xcodeBundleExtension.substring (1));
  395. s.add ("GENERATE_PKGINFO_FILE = YES");
  396. }
  397. if (xcodeOtherRezFlags.isNotEmpty())
  398. s.add ("OTHER_REZFLAGS = \"" + xcodeOtherRezFlags + "\"");
  399. if (projectType.isLibrary())
  400. {
  401. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  402. {
  403. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  404. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  405. s.add ("DSTROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  406. s.add ("SYMROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  407. }
  408. s.add ("CONFIGURATION_BUILD_DIR = \"$(BUILD_DIR)\"");
  409. s.add ("DEPLOYMENT_LOCATION = YES");
  410. }
  411. String gccVersion ("com.apple.compilers.llvm.clang.1_0");
  412. if (! iOS)
  413. {
  414. const String sdk (config.getMacSDKVersion().toString());
  415. const String sdkCompat (config.getMacCompatibilityVersion().toString());
  416. if (sdk == Project::BuildConfiguration::osxVersion10_4)
  417. {
  418. s.add ("SDKROOT = macosx10.4");
  419. gccVersion = "4.0";
  420. }
  421. else if (sdk == Project::BuildConfiguration::osxVersion10_5)
  422. {
  423. s.add ("SDKROOT = macosx10.5");
  424. }
  425. else if (sdk == Project::BuildConfiguration::osxVersion10_6)
  426. {
  427. s.add ("SDKROOT = macosx10.6");
  428. }
  429. if (sdkCompat == Project::BuildConfiguration::osxVersion10_4) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.4");
  430. else if (sdkCompat == Project::BuildConfiguration::osxVersion10_5) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.5");
  431. else if (sdkCompat == Project::BuildConfiguration::osxVersion10_6) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.6");
  432. s.add ("MACOSX_DEPLOYMENT_TARGET_ppc = 10.4");
  433. }
  434. s.add ("GCC_VERSION = " + gccVersion);
  435. s.add ("CLANG_CXX_LANGUAGE_STANDARD = \"c++0x\"");
  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 = 46;\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 ("xml;zip;wav")) return "file" + file.getFileExtension();
  606. else if (file.hasFileExtension ("txt;rtf")) return "text" + file.getFileExtension();
  607. else if (file.hasFileExtension ("plist")) return "text.plist.xml";
  608. else if (file.hasFileExtension ("app")) return "wrapper.application";
  609. else if (file.hasFileExtension ("component;vst;plugin")) return "wrapper.cfbundle";
  610. else if (file.hasFileExtension ("xcodeproj")) return "wrapper.pb-project";
  611. else if (file.hasFileExtension ("a")) return "archive.ar";
  612. return "file" + file.getFileExtension();
  613. }
  614. String addFile (const RelativePath& path, bool shouldBeCompiled, bool inhibitWarnings)
  615. {
  616. const String pathAsString (path.toUnixStyle());
  617. const String refID (addFileReference (path.toUnixStyle()));
  618. if (shouldBeCompiled)
  619. {
  620. if (path.hasFileExtension (".r"))
  621. rezFileIDs.add (addBuildFile (pathAsString, refID, false, inhibitWarnings));
  622. else
  623. addBuildFile (pathAsString, refID, true, inhibitWarnings);
  624. }
  625. return refID;
  626. }
  627. String addProjectItem (const Project::Item& projectItem)
  628. {
  629. if (projectItem.isGroup())
  630. {
  631. StringArray childIDs;
  632. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  633. {
  634. const String childID (addProjectItem (projectItem.getChild(i)));
  635. if (childID.isNotEmpty())
  636. childIDs.add (childID);
  637. }
  638. return addGroup (projectItem, childIDs);
  639. }
  640. else
  641. {
  642. if (projectItem.shouldBeAddedToTargetProject())
  643. {
  644. String itemPath (projectItem.getFilePath());
  645. bool inhibitWarnings = projectItem.getShouldInhibitWarningsValue().getValue();
  646. if (itemPath.startsWith ("${"))
  647. {
  648. const RelativePath path (itemPath, RelativePath::unknown);
  649. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  650. }
  651. else
  652. {
  653. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  654. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  655. }
  656. }
  657. }
  658. return String::empty;
  659. }
  660. void addFramework (const String& frameworkName)
  661. {
  662. const String path ("System/Library/Frameworks/" + frameworkName + ".framework");
  663. const String fileRefID (createFileRefID (path));
  664. addFileReference ("${SDKROOT}/" + path);
  665. frameworkIDs.add (addBuildFile (path, fileRefID, false, false));
  666. frameworkFileIDs.add (fileRefID);
  667. }
  668. void addGroup (const String& groupID, const String& groupName, const StringArray& childIDs)
  669. {
  670. ValueTree* v = new ValueTree (groupID);
  671. v->setProperty ("isa", "PBXGroup", 0);
  672. v->setProperty ("children", "(" + indentList (childIDs, ",") + " )", 0);
  673. v->setProperty (Ids::name, groupName, 0);
  674. v->setProperty ("sourceTree", "<group>", 0);
  675. pbxGroups.add (v);
  676. }
  677. String addGroup (const Project::Item& item, StringArray& childIDs)
  678. {
  679. const String groupName (item.getName().toString());
  680. const String groupID (getIDForGroup (item));
  681. addGroup (groupID, groupName, childIDs);
  682. return groupID;
  683. }
  684. void addMainBuildProduct()
  685. {
  686. jassert (xcodeFileType.isNotEmpty());
  687. jassert (xcodeBundleExtension.isEmpty() || xcodeBundleExtension.startsWithChar('.'));
  688. String productName (configs.getReference(0).getTargetBinaryName().toString());
  689. if (xcodeFileType == "archive.ar")
  690. productName = getLibbedFilename (productName);
  691. else
  692. productName += xcodeBundleExtension;
  693. addBuildProduct (xcodeFileType, productName);
  694. }
  695. void addBuildProduct (const String& fileType, const String& binaryName)
  696. {
  697. ValueTree* v = new ValueTree (createID ("__productFileID"));
  698. v->setProperty ("isa", "PBXFileReference", 0);
  699. v->setProperty ("explicitFileType", fileType, 0);
  700. v->setProperty ("includeInIndex", (int) 0, 0);
  701. v->setProperty ("path", sanitisePath (binaryName), 0);
  702. v->setProperty ("sourceTree", "BUILT_PRODUCTS_DIR", 0);
  703. pbxFileReferences.add (v);
  704. }
  705. void addTargetConfig (const String& configName, const StringArray& buildSettings)
  706. {
  707. ValueTree* v = new ValueTree (createID ("targetconfigid_" + configName));
  708. v->setProperty ("isa", "XCBuildConfiguration", 0);
  709. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  710. v->setProperty (Ids::name, configName, 0);
  711. targetConfigs.add (v);
  712. }
  713. void addProjectConfig (const String& configName, const StringArray& buildSettings)
  714. {
  715. ValueTree* v = new ValueTree (createID ("projectconfigid_" + configName));
  716. v->setProperty ("isa", "XCBuildConfiguration", 0);
  717. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  718. v->setProperty (Ids::name, configName, 0);
  719. projectConfigs.add (v);
  720. }
  721. void addConfigList (const OwnedArray <ValueTree>& configsToUse, const String& listID)
  722. {
  723. StringArray configIDs;
  724. for (int i = 0; i < configsToUse.size(); ++i)
  725. configIDs.add (configsToUse[i]->getType().toString());
  726. ValueTree* v = new ValueTree (listID);
  727. v->setProperty ("isa", "XCConfigurationList", 0);
  728. v->setProperty ("buildConfigurations", "(" + indentList (configIDs, ",") + " )", 0);
  729. v->setProperty ("defaultConfigurationIsVisible", (int) 0, 0);
  730. if (configsToUse[0] != nullptr)
  731. v->setProperty ("defaultConfigurationName", configsToUse[0]->getProperty (Ids::name), 0);
  732. misc.add (v);
  733. }
  734. ValueTree* addBuildPhase (const String& phaseType, const StringArray& fileIds)
  735. {
  736. String phaseId (createID (phaseType + "resbuildphase"));
  737. buildPhaseIDs.add (phaseId);
  738. ValueTree* v = new ValueTree (phaseId);
  739. v->setProperty ("isa", phaseType, 0);
  740. v->setProperty ("buildActionMask", "2147483647", 0);
  741. v->setProperty ("files", "(" + indentList (fileIds, ",") + " )", 0);
  742. v->setProperty ("runOnlyForDeploymentPostprocessing", (int) 0, 0);
  743. misc.add (v);
  744. return v;
  745. }
  746. void addTargetObject()
  747. {
  748. ValueTree* const v = new ValueTree (createID ("__target"));
  749. v->setProperty ("isa", "PBXNativeTarget", 0);
  750. v->setProperty ("buildConfigurationList", createID ("__configList"), 0);
  751. v->setProperty ("buildPhases", "(" + indentList (buildPhaseIDs, ",") + " )", 0);
  752. v->setProperty ("buildRules", "( )", 0);
  753. v->setProperty ("dependencies", "( )", 0);
  754. v->setProperty (Ids::name, projectName, 0);
  755. v->setProperty ("productName", projectName, 0);
  756. v->setProperty ("productReference", createID ("__productFileID"), 0);
  757. if (xcodeProductInstallPath.isNotEmpty())
  758. v->setProperty ("productInstallPath", xcodeProductInstallPath, 0);
  759. jassert (xcodeProductType.isNotEmpty());
  760. v->setProperty ("productType", xcodeProductType, 0);
  761. misc.add (v);
  762. }
  763. void addProjectObject()
  764. {
  765. ValueTree* const v = new ValueTree (createID ("__root"));
  766. v->setProperty ("isa", "PBXProject", 0);
  767. v->setProperty ("buildConfigurationList", createID ("__projList"), 0);
  768. v->setProperty ("compatibilityVersion", "Xcode 3.2", 0);
  769. v->setProperty ("hasScannedForEncodings", (int) 0, 0);
  770. v->setProperty ("mainGroup", createID ("__mainsourcegroup"), 0);
  771. v->setProperty ("projectDirPath", "\"\"", 0);
  772. v->setProperty ("projectRoot", "\"\"", 0);
  773. v->setProperty ("targets", "( " + createID ("__target") + " )", 0);
  774. misc.add (v);
  775. }
  776. void addShellScriptPhase()
  777. {
  778. if (xcodeShellScript.isNotEmpty())
  779. {
  780. ValueTree* const v = addBuildPhase ("PBXShellScriptBuildPhase", StringArray());
  781. v->setProperty (Ids::name, xcodeShellScriptTitle, 0);
  782. v->setProperty ("shellPath", "/bin/sh", 0);
  783. v->setProperty ("shellScript", xcodeShellScript.replace ("\\", "\\\\")
  784. .replace ("\"", "\\\"")
  785. .replace ("\r\n", "\\n")
  786. .replace ("\n", "\\n"), 0);
  787. }
  788. }
  789. //==============================================================================
  790. static String indentList (const StringArray& list, const String& separator)
  791. {
  792. if (list.size() == 0)
  793. return " ";
  794. return "\n\t\t\t\t" + list.joinIntoString (separator + "\n\t\t\t\t")
  795. + (separator == ";" ? separator : String::empty);
  796. }
  797. String createID (String rootString) const
  798. {
  799. if (rootString.startsWith ("${"))
  800. rootString = rootString.fromFirstOccurrenceOf ("}/", false, false);
  801. rootString += project.getProjectUID();
  802. return MD5 (rootString.toUTF8()).toHexString().substring (0, 24).toUpperCase();
  803. }
  804. String createFileRefID (const RelativePath& path) const
  805. {
  806. return createFileRefID (path.toUnixStyle());
  807. }
  808. String createFileRefID (const String& path) const
  809. {
  810. return createID ("__fileref_" + path);
  811. }
  812. String getIDForGroup (const Project::Item& item) const
  813. {
  814. return createID (item.getID());
  815. }
  816. bool shouldFileBeCompiledByDefault (const RelativePath& file) const
  817. {
  818. return file.hasFileExtension (sourceFileExtensions);
  819. }
  820. };
  821. #endif // __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__