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.

415 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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() + "LinuxMakefile";
  35. initialiseDependencyPathValues();
  36. }
  37. //==============================================================================
  38. bool canLaunchProject() override { return false; }
  39. bool launchProject() override { return false; }
  40. bool usesMMFiles() const override { return false; }
  41. bool canCopeWithDuplicateFiles() override { return false; }
  42. bool supportsUserDefinedConfigurations() const override { return true; }
  43. bool isXcode() const override { return false; }
  44. bool isVisualStudio() const override { return false; }
  45. bool isCodeBlocks() const override { return false; }
  46. bool isMakefile() const override { return true; }
  47. bool isAndroidStudio() const override { return false; }
  48. bool isAndroidAnt() const override { return false; }
  49. bool isAndroid() const override { return false; }
  50. bool isWindows() const override { return false; }
  51. bool isLinux() const override { return true; }
  52. bool isOSX() const override { return false; }
  53. bool isiOS() const override { return false; }
  54. bool supportsVST() const override { return true; }
  55. bool supportsVST3() const override { return false; }
  56. bool supportsAAX() const override { return false; }
  57. bool supportsRTAS() const override { return false; }
  58. bool supportsAU() const override { return false; }
  59. bool supportsAUv3() const override { return false; }
  60. bool supportsStandalone() const override { return false; }
  61. Value getCppStandardValue() { return getSetting (Ids::cppLanguageStandard); }
  62. String getCppStandardString() const { return settings[Ids::cppLanguageStandard]; }
  63. void createExporterProperties (PropertyListBuilder& properties) override
  64. {
  65. static const char* cppStandardNames[] = { "C++03", "C++11", "C++14", nullptr };
  66. static const char* cppStandardValues[] = { "-std=c++03", "-std=c++11", "-std=c++14", nullptr };
  67. properties.add (new ChoicePropertyComponent (getCppStandardValue(),
  68. "C++ standard to use",
  69. StringArray (cppStandardNames),
  70. Array<var> (cppStandardValues)),
  71. "The C++ standard to specify in the makefile");
  72. }
  73. //==============================================================================
  74. void create (const OwnedArray<LibraryModule>&) const override
  75. {
  76. Array<RelativePath> files;
  77. for (int i = 0; i < getAllGroups().size(); ++i)
  78. findAllFilesToCompile (getAllGroups().getReference(i), files);
  79. MemoryOutputStream mo;
  80. writeMakefile (mo, files);
  81. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  82. }
  83. //==============================================================================
  84. void addPlatformSpecificSettingsForProjectType (const ProjectType& type) override
  85. {
  86. if (type.isStaticLibrary())
  87. makefileTargetSuffix = ".a";
  88. else if (type.isDynamicLibrary())
  89. makefileTargetSuffix = ".so";
  90. else if (type.isAudioPlugin())
  91. makefileIsDLL = true;
  92. }
  93. protected:
  94. //==============================================================================
  95. class MakeBuildConfiguration : public BuildConfiguration
  96. {
  97. public:
  98. MakeBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  99. : BuildConfiguration (p, settings, e)
  100. {
  101. setValueIfVoid (getLibrarySearchPathValue(), "/usr/X11R6/lib/");
  102. }
  103. Value getArchitectureType() { return getValue (Ids::linuxArchitecture); }
  104. var getArchitectureTypeVar() const { return config [Ids::linuxArchitecture]; }
  105. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  106. void createConfigProperties (PropertyListBuilder& props) override
  107. {
  108. addGCCOptimisationProperty (props);
  109. static const char* const archNames[] = { "(Default)", "<None>", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" };
  110. const var archFlags[] = { var(), var (String()), "-m32", "-m64", "-march=armv6", "-march=armv7" };
  111. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  112. StringArray (archNames, numElementsInArray (archNames)),
  113. Array<var> (archFlags, numElementsInArray (archFlags))));
  114. }
  115. };
  116. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  117. {
  118. return new MakeBuildConfiguration (project, tree, *this);
  119. }
  120. private:
  121. //==============================================================================
  122. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  123. {
  124. if (projectItem.isGroup())
  125. {
  126. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  127. findAllFilesToCompile (projectItem.getChild(i), results);
  128. }
  129. else
  130. {
  131. if (projectItem.shouldBeCompiled())
  132. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  133. }
  134. }
  135. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  136. {
  137. StringPairArray defines;
  138. defines.set ("LINUX", "1");
  139. if (config.isDebug())
  140. {
  141. defines.set ("DEBUG", "1");
  142. defines.set ("_DEBUG", "1");
  143. }
  144. else
  145. {
  146. defines.set ("NDEBUG", "1");
  147. }
  148. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  149. }
  150. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  151. {
  152. StringArray searchPaths (extraSearchPaths);
  153. searchPaths.addArray (config.getHeaderSearchPaths());
  154. searchPaths.insert (0, "/usr/include/freetype2");
  155. searchPaths.insert (0, "/usr/include");
  156. searchPaths = getCleanedStringArray (searchPaths);
  157. for (int i = 0; i < searchPaths.size(); ++i)
  158. out << " -I " << escapeSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  159. }
  160. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  161. {
  162. out << " CPPFLAGS := $(DEPFLAGS)";
  163. writeDefineFlags (out, config);
  164. writeHeaderPathFlags (out, config);
  165. out << newLine;
  166. }
  167. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  168. {
  169. out << " LDFLAGS += $(TARGET_ARCH) -L$(BINDIR) -L$(LIBDIR)";
  170. {
  171. StringArray flags (makefileExtraLinkerFlags);
  172. if (makefileIsDLL)
  173. flags.add ("-shared");
  174. if (! config.isDebug())
  175. flags.add ("-fvisibility=hidden");
  176. if (flags.size() > 0)
  177. out << " " << getCleanedStringArray (flags).joinIntoString (" ");
  178. }
  179. out << config.getGCCLibraryPathFlags();
  180. for (int i = 0; i < linuxLibs.size(); ++i)
  181. out << " -l" << linuxLibs[i];
  182. if (getProject().isConfigFlagEnabled ("JUCE_USE_CURL"))
  183. out << " -lcurl";
  184. StringArray libraries;
  185. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  186. libraries.removeEmptyStrings();
  187. if (libraries.size() != 0)
  188. out << " -l" << replacePreprocessorTokens (config, libraries.joinIntoString (" -l")).trim();
  189. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  190. << newLine;
  191. }
  192. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  193. {
  194. const String buildDirName ("build");
  195. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  196. String outputDir (buildDirName);
  197. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  198. {
  199. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  200. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  201. }
  202. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  203. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  204. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  205. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  206. << " OUTDIR := " << escapeSpaces (outputDir) << newLine
  207. << newLine
  208. << " ifeq ($(TARGET_ARCH),)" << newLine
  209. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  210. << " endif" << newLine
  211. << newLine;
  212. writeCppFlags (out, config);
  213. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  214. if (config.isDebug())
  215. out << " -g -ggdb";
  216. if (makefileIsDLL)
  217. out << " -fPIC";
  218. out << " -O" << config.getGCCOptimisationFlag()
  219. << (" " + replacePreprocessorTokens (config, getExtraCompilerFlagsString())).trimEnd()
  220. << newLine;
  221. String cppStandardToUse (getCppStandardString());
  222. if (cppStandardToUse.isEmpty())
  223. cppStandardToUse = "-std=c++11";
  224. out << " CXXFLAGS += $(CFLAGS) "
  225. << cppStandardToUse
  226. << newLine;
  227. writeLinkerFlags (out, config);
  228. out << newLine;
  229. String targetName (replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  230. if (projectType.isStaticLibrary() || projectType.isDynamicLibrary())
  231. targetName = getLibbedFilename (targetName);
  232. else
  233. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  234. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  235. if (projectType.isStaticLibrary())
  236. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS)" << newLine;
  237. else
  238. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  239. out << " CLEANCMD = rm -rf $(OUTDIR)/$(TARGET) $(OBJDIR)" << newLine
  240. << "endif" << newLine
  241. << newLine;
  242. }
  243. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  244. {
  245. out << "OBJECTS := \\" << newLine;
  246. for (int i = 0; i < files.size(); ++i)
  247. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  248. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  249. out << newLine;
  250. }
  251. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  252. {
  253. out << "# Automatically generated makefile, created by the Projucer" << newLine
  254. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  255. << newLine;
  256. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  257. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  258. << newLine;
  259. out << "ifndef CONFIG" << newLine
  260. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  261. << "endif" << newLine
  262. << newLine;
  263. for (ConstConfigIterator config (*this); config.next();)
  264. writeConfig (out, *config);
  265. writeObjects (out, files);
  266. out << ".PHONY: clean" << newLine
  267. << newLine;
  268. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(RESOURCES)" << newLine
  269. << "\t@echo Linking " << projectName << newLine
  270. << "\t-@mkdir -p $(BINDIR)" << newLine
  271. << "\t-@mkdir -p $(LIBDIR)" << newLine
  272. << "\t-@mkdir -p $(OUTDIR)" << newLine
  273. << "\t@$(BLDCMD)" << newLine
  274. << newLine;
  275. out << "clean:" << newLine
  276. << "\t@echo Cleaning " << projectName << newLine
  277. << "\t@$(CLEANCMD)" << newLine
  278. << newLine;
  279. out << "strip:" << newLine
  280. << "\t@echo Stripping " << projectName << newLine
  281. << "\t-@strip --strip-unneeded $(OUTDIR)/$(TARGET)" << newLine
  282. << newLine;
  283. for (int i = 0; i < files.size(); ++i)
  284. {
  285. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  286. {
  287. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  288. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  289. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  290. << "\t-@mkdir -p $(OBJDIR)" << newLine
  291. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  292. << (files.getReference(i).hasFileExtension ("c;s;S") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  293. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  294. << newLine << newLine;
  295. }
  296. }
  297. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  298. }
  299. String getArchFlags (const BuildConfiguration& config) const
  300. {
  301. if (const MakeBuildConfiguration* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  302. if (! makeConfig->getArchitectureTypeVar().isVoid())
  303. return makeConfig->getArchitectureTypeVar();
  304. return "-march=native";
  305. }
  306. String getObjectFileFor (const RelativePath& file) const
  307. {
  308. return file.getFileNameWithoutExtension()
  309. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  310. }
  311. void initialiseDependencyPathValues()
  312. {
  313. vst2Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vstFolder),
  314. Ids::vst2Path,
  315. TargetOS::linux)));
  316. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder),
  317. Ids::vst3Path,
  318. TargetOS::linux)));
  319. }
  320. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  321. };