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.

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