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.

339 lines
13KB

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