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.

1164 lines
47KB

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