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.

345 lines
14KB

  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_MAKE_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. //==============================================================================
  22. class MakefileProjectExporter : public ProjectExporter
  23. {
  24. public:
  25. //==============================================================================
  26. static const char* getNameLinux() { return "Linux Makefile"; }
  27. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  28. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  29. {
  30. if (settings.hasType (getValueTreeTypeName()))
  31. return new MakefileProjectExporter (project, settings);
  32. return nullptr;
  33. }
  34. //==============================================================================
  35. MakefileProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  36. {
  37. name = getNameLinux();
  38. if (getTargetLocationString().isEmpty())
  39. getTargetLocationValue() = getDefaultBuildsRootFolder() + "Linux";
  40. }
  41. //==============================================================================
  42. bool launchProject() { return false; }
  43. bool usesMMFiles() const { return false; }
  44. bool isLinux() const { return true; }
  45. bool canCopeWithDuplicateFiles() { return false; }
  46. void createExporterProperties (PropertyListBuilder&)
  47. {
  48. }
  49. //==============================================================================
  50. void create (const OwnedArray<LibraryModule>&) const
  51. {
  52. Array<RelativePath> files;
  53. for (int i = 0; i < getAllGroups().size(); ++i)
  54. findAllFilesToCompile (getAllGroups().getReference(i), files);
  55. MemoryOutputStream mo;
  56. writeMakefile (mo, files);
  57. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  58. }
  59. protected:
  60. //==============================================================================
  61. class MakeBuildConfiguration : public BuildConfiguration
  62. {
  63. public:
  64. MakeBuildConfiguration (Project& p, const ValueTree& settings)
  65. : BuildConfiguration (p, settings)
  66. {
  67. setValueIfVoid (getLibrarySearchPathValue(), "/usr/X11R6/lib/");
  68. }
  69. Value getArchitectureType() { return getValue (Ids::linuxArchitecture); }
  70. String getArchitectureTypeString() const { return config [Ids::linuxArchitecture]; }
  71. void createConfigProperties (PropertyListBuilder& props)
  72. {
  73. const char* const archNames[] = { "(Default)", "32-bit (-m32)", "64-bit (-m64)" };
  74. const var archFlags[] = { var(), "-m32", "-m64" };
  75. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  76. StringArray (archNames, numElementsInArray (archNames)),
  77. Array<var> (archFlags, numElementsInArray (archFlags))));
  78. }
  79. };
  80. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const
  81. {
  82. return new MakeBuildConfiguration (project, tree);
  83. }
  84. private:
  85. //==============================================================================
  86. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  87. {
  88. if (projectItem.isGroup())
  89. {
  90. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  91. findAllFilesToCompile (projectItem.getChild(i), results);
  92. }
  93. else
  94. {
  95. if (projectItem.shouldBeCompiled())
  96. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  97. }
  98. }
  99. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  100. {
  101. StringPairArray defines;
  102. defines.set ("LINUX", "1");
  103. if (config.isDebug())
  104. {
  105. defines.set ("DEBUG", "1");
  106. defines.set ("_DEBUG", "1");
  107. }
  108. else
  109. {
  110. defines.set ("NDEBUG", "1");
  111. }
  112. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  113. }
  114. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  115. {
  116. StringArray searchPaths (extraSearchPaths);
  117. searchPaths.addArray (config.getHeaderSearchPaths());
  118. searchPaths.insert (0, "/usr/include/freetype2");
  119. searchPaths.insert (0, "/usr/include");
  120. searchPaths.removeDuplicates (false);
  121. for (int i = 0; i < searchPaths.size(); ++i)
  122. out << " -I " << addQuotesIfContainsSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  123. }
  124. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  125. {
  126. out << " CPPFLAGS := $(DEPFLAGS)";
  127. writeDefineFlags (out, config);
  128. writeHeaderPathFlags (out, config);
  129. out << newLine;
  130. }
  131. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  132. {
  133. out << " LDFLAGS += " << getArchFlags (config) << "-L$(BINDIR) -L$(LIBDIR)";
  134. if (makefileIsDLL)
  135. out << " -shared";
  136. out << config.getGCCLibraryPathFlags();
  137. for (int i = 0; i < linuxLibs.size(); ++i)
  138. out << " -l" << linuxLibs[i];
  139. StringArray libraries;
  140. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  141. libraries.removeEmptyStrings();
  142. if (libraries.size() != 0)
  143. out << " -l" << replacePreprocessorTokens (config, libraries.joinIntoString (" -l")).trim();
  144. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  145. << newLine;
  146. }
  147. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  148. {
  149. const String buildDirName ("build");
  150. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  151. String outputDir (buildDirName);
  152. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  153. {
  154. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  155. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  156. }
  157. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  158. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  159. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  160. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  161. << " OUTDIR := " << escapeSpaces (outputDir) << newLine;
  162. writeCppFlags (out, config);
  163. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  164. if (config.isDebug())
  165. out << " -g -ggdb";
  166. if (makefileIsDLL)
  167. out << " -fPIC";
  168. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  169. out << " CXXFLAGS += $(CFLAGS) " << getArchFlags (config)
  170. << replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim() << newLine;
  171. writeLinkerFlags (out, config);
  172. out << " LDDEPS :=" << newLine
  173. << " RESFLAGS := ";
  174. writeDefineFlags (out, config);
  175. writeHeaderPathFlags (out, config);
  176. out << newLine;
  177. String targetName (config.getTargetBinaryNameString());
  178. if (projectType.isStaticLibrary() || projectType.isDynamicLibrary())
  179. targetName = getLibbedFilename (targetName);
  180. else
  181. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  182. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  183. if (projectType.isStaticLibrary())
  184. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  185. else
  186. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  187. out << "endif" << newLine << newLine;
  188. }
  189. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  190. {
  191. out << "OBJECTS := \\" << newLine;
  192. for (int i = 0; i < files.size(); ++i)
  193. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  194. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  195. out << newLine;
  196. }
  197. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  198. {
  199. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  200. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  201. << newLine;
  202. out << "ifndef CONFIG" << newLine
  203. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  204. << "endif" << newLine
  205. << newLine;
  206. if (! projectType.isStaticLibrary())
  207. out << "ifeq ($(TARGET_ARCH),)" << newLine
  208. << " TARGET_ARCH := -march=native" << newLine
  209. << "endif" << newLine << newLine;
  210. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  211. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  212. << newLine;
  213. for (ConstConfigIterator config (*this); config.next();)
  214. writeConfig (out, *config);
  215. writeObjects (out, files);
  216. out << ".PHONY: clean" << newLine
  217. << newLine;
  218. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  219. << "\t@echo Linking " << projectName << newLine
  220. << "\t-@mkdir -p $(BINDIR)" << newLine
  221. << "\t-@mkdir -p $(LIBDIR)" << newLine
  222. << "\t-@mkdir -p $(OUTDIR)" << newLine
  223. << "\t@$(BLDCMD)" << newLine
  224. << newLine;
  225. out << "clean:" << newLine
  226. << "\t@echo Cleaning " << projectName << newLine
  227. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  228. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  229. << "\t-@rm -rf $(OBJDIR)" << newLine
  230. << newLine;
  231. out << "strip:" << newLine
  232. << "\t@echo Stripping " << projectName << newLine
  233. << "\t-@strip --strip-unneeded $(OUTDIR)/$(TARGET)" << newLine
  234. << newLine;
  235. for (int i = 0; i < files.size(); ++i)
  236. {
  237. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  238. {
  239. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  240. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  241. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  242. << "\t-@mkdir -p $(OBJDIR)" << newLine
  243. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  244. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  245. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  246. << newLine << newLine;
  247. }
  248. }
  249. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  250. }
  251. String getArchFlags (const BuildConfiguration& config) const
  252. {
  253. if (const MakeBuildConfiguration* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  254. if (makeConfig->getArchitectureTypeString().isNotEmpty())
  255. return makeConfig->getArchitectureTypeString() + " ";
  256. return String::empty;
  257. }
  258. String getObjectFileFor (const RelativePath& file) const
  259. {
  260. return file.getFileNameWithoutExtension()
  261. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  262. }
  263. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  264. };
  265. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__