From: Mikko Rasa Date: Fri, 1 Dec 2017 07:48:37 +0000 (+0200) Subject: Collapse the language-specific compilers into a single class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a4f02f02d4682c7f504e46ff2ce8f93035fd36dd;p=builder.git Collapse the language-specific compilers into a single class There's very little differences between languages of the C family, but on the other hand different platform SDKs need their own tweaks. It makes more sense to abstract by platform rather than by language here. --- diff --git a/source/androidccompiler.cpp b/source/androidccompiler.cpp deleted file mode 100644 index 8c9306e..0000000 --- a/source/androidccompiler.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "androidccompiler.h" -#include "androidtools.h" - -AndroidCCompiler::AndroidCCompiler(Builder &b, const Architecture &a, const AndroidNdk &ndk): - GnuCCompiler(b, a, ndk.get_platform_sysroot()) -{ - set_command((ndk.get_bin_dir()/command).str()); - build_info.incpath.insert(build_info.incpath.end(), system_path.begin(), system_path.end()); -} diff --git a/source/androidccompiler.h b/source/androidccompiler.h deleted file mode 100644 index f723a00..0000000 --- a/source/androidccompiler.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef ANDROIDCCOMPILER_H_ -#define ANDROIDCCOMPILER_H_ - -#include "gnuccompiler.h" - -class AndroidNdk; - -class AndroidCCompiler: public GnuCCompiler -{ -public: - AndroidCCompiler(Builder &, const Architecture &, const AndroidNdk &); -}; - -#endif diff --git a/source/androidcompiler.cpp b/source/androidcompiler.cpp new file mode 100644 index 0000000..490c198 --- /dev/null +++ b/source/androidcompiler.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include "androidcompiler.h" +#include "androidtools.h" +#include "builder.h" + +using namespace std; +using namespace Msp; + +AndroidCompiler::AndroidCompiler(Builder &b, const Architecture &a, const string &t, const AndroidNdk &n): + GnuCompiler(b, a, t, n.get_platform_sysroot()), + ndk(n) +{ + set_command((ndk.get_bin_dir()/command).str()); + build_info.incpath.insert(build_info.incpath.end(), system_path.begin(), system_path.end()); + if(tag=="CXX") + build_info.libs.push_back("gnustl_static"); +} + +void AndroidCompiler::do_prepare() +{ + GnuCompiler::do_prepare(); + if(executable && tag=="CXX") + { + FS::Path libstdcxx_path = ndk.get_root_dir()/"sources"/"cxx-stl"/"gnu-libstdc++"/version; + + FS::Path public_dir = libstdcxx_path/"include"; + system_path.push_back(public_dir); + build_info.incpath.push_back(public_dir); + + FS::Path arch_path = libstdcxx_path/"libs"; + builder.get_logger().log("files", format("Traversing %s", arch_path.str())); + string arch_dir = architecture->best_match(list_files(arch_path)); + if(!arch_dir.empty()) + { + build_info.incpath.push_back(libstdcxx_path/"libs"/arch_dir/"include"); + build_info.libpath.push_back(libstdcxx_path/"libs"/arch_dir); + } + } +} diff --git a/source/androidcompiler.h b/source/androidcompiler.h new file mode 100644 index 0000000..dbd2da6 --- /dev/null +++ b/source/androidcompiler.h @@ -0,0 +1,20 @@ +#ifndef ANDROIDCOMPILER_H_ +#define ANDROIDCOMPILER_H_ + +#include "gnucompiler.h" + +class AndroidNdk; + +class AndroidCompiler: public GnuCompiler +{ +private: + const AndroidNdk &ndk; + +public: + AndroidCompiler(Builder &, const Architecture &, const std::string &, const AndroidNdk &); + +protected: + virtual void do_prepare(); +}; + +#endif diff --git a/source/androidcxxcompiler.cpp b/source/androidcxxcompiler.cpp deleted file mode 100644 index 41f06ed..0000000 --- a/source/androidcxxcompiler.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include "androidcxxcompiler.h" -#include "androidtools.h" -#include "architecture.h" -#include "builder.h" - -using namespace std; -using namespace Msp; - -AndroidCxxCompiler::AndroidCxxCompiler(Builder &b, const Architecture &a, const AndroidNdk &n): - GnuCxxCompiler(b, a, n.get_platform_sysroot()), - ndk(n) -{ - set_command((ndk.get_bin_dir()/command).str()); - build_info.incpath.insert(build_info.incpath.end(), system_path.begin(), system_path.end()); - build_info.libs.push_back("gnustl_static"); -} - -void AndroidCxxCompiler::do_prepare() -{ - GnuCxxCompiler::do_prepare(); - if(executable) - { - FS::Path libstdcxx_path = ndk.get_root_dir()/"sources"/"cxx-stl"/"gnu-libstdc++"/version; - - FS::Path public_dir = libstdcxx_path/"include"; - system_path.push_back(public_dir); - build_info.incpath.push_back(public_dir); - - FS::Path arch_path = libstdcxx_path/"libs"; - builder.get_logger().log("files", format("Traversing %s", arch_path.str())); - string arch_dir = architecture->best_match(list_files(arch_path)); - if(!arch_dir.empty()) - { - build_info.incpath.push_back(libstdcxx_path/"libs"/arch_dir/"include"); - build_info.libpath.push_back(libstdcxx_path/"libs"/arch_dir); - } - } -} diff --git a/source/androidcxxcompiler.h b/source/androidcxxcompiler.h deleted file mode 100644 index e6c1478..0000000 --- a/source/androidcxxcompiler.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef ANDROIDCXXCOMPILER_H_ -#define ANDROIDCXXCOMPILER_H_ - -#include "gnucxxcompiler.h" - -class AndroidNdk; - -class AndroidCxxCompiler: public GnuCxxCompiler -{ -private: - const AndroidNdk &ndk; - -public: - AndroidCxxCompiler(Builder &, const Architecture &, const AndroidNdk &); - -protected: - virtual void do_prepare(); -}; - -#endif diff --git a/source/androidtools.cpp b/source/androidtools.cpp index 7fd0408..809da7d 100644 --- a/source/androidtools.cpp +++ b/source/androidtools.cpp @@ -5,8 +5,7 @@ #include #include "androidarchiver.h" #include "androidassetpackagingtool.h" -#include "androidccompiler.h" -#include "androidcxxcompiler.h" +#include "androidcompiler.h" #include "androidlinker.h" #include "androidmanifestgenerator.h" #include "androidtools.h" @@ -190,8 +189,8 @@ AndroidTools::AndroidTools(Builder &builder, const Architecture &arch): sdk.select_api_level(highest_common); ndk.select_api_level(highest_common); - add_tool(new AndroidCCompiler(builder, arch, ndk)); - add_tool(new AndroidCxxCompiler(builder, arch, ndk)); + add_tool(new AndroidCompiler(builder, arch, "CC", ndk)); + add_tool(new AndroidCompiler(builder, arch, "CXX", ndk)); add_tool(new AndroidLinker(builder, arch, ndk)); add_tool(new AndroidArchiver(builder, arch, ndk)); add_tool(new AndroidManifestGenerator(builder)); diff --git a/source/clangccompiler.cpp b/source/clangccompiler.cpp deleted file mode 100644 index c92e3d7..0000000 --- a/source/clangccompiler.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "clangccompiler.h" - -ClangCCompiler::ClangCCompiler(Builder &b, const Architecture &a): - GnuCCompiler(b, a) -{ - set_command("clang", true); -} diff --git a/source/clangccompiler.h b/source/clangccompiler.h deleted file mode 100644 index 2a4ec29..0000000 --- a/source/clangccompiler.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef CLANGCCOMPILER_H_ -#define CLANGCCOMPILER_H_ - -#include "gnuccompiler.h" - -class ClangCCompiler: public GnuCCompiler -{ -public: - ClangCCompiler(Builder &, const Architecture &); -}; - -#endif diff --git a/source/clangcompiler.cpp b/source/clangcompiler.cpp new file mode 100644 index 0000000..7ad47ed --- /dev/null +++ b/source/clangcompiler.cpp @@ -0,0 +1,10 @@ +#include "clangcompiler.h" + +using namespace std; +using namespace Msp; + +ClangCompiler::ClangCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &r): + GnuCompiler(b, a, t, r) +{ + set_command((tag=="CXX" ? "clang++" : "clang"), true); +} diff --git a/source/clangcompiler.h b/source/clangcompiler.h new file mode 100644 index 0000000..c848b75 --- /dev/null +++ b/source/clangcompiler.h @@ -0,0 +1,12 @@ +#ifndef CLANGCOMPILER_H_ +#define CLANGCOMPILER_H_ + +#include "gnucompiler.h" + +class ClangCompiler: public GnuCompiler +{ +public: + ClangCompiler(Builder &, const Architecture &, const std::string &, const Msp::FS::Path & = Msp::FS::Path()); +}; + +#endif diff --git a/source/clangcxxcompiler.cpp b/source/clangcxxcompiler.cpp deleted file mode 100644 index 4dcb9d3..0000000 --- a/source/clangcxxcompiler.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "clangcxxcompiler.h" - -ClangCxxCompiler::ClangCxxCompiler(Builder &b, const Architecture &a): - GnuCxxCompiler(b, a) -{ - set_command("clang++", true); -} diff --git a/source/clangcxxcompiler.h b/source/clangcxxcompiler.h deleted file mode 100644 index 95b901c..0000000 --- a/source/clangcxxcompiler.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef CLANGCXXCOMPILER_H_ -#define CLANGCXXCOMPILER_H_ - -#include "gnucxxcompiler.h" - -class ClangCxxCompiler: public GnuCxxCompiler -{ -public: - ClangCxxCompiler(Builder &, const Architecture &); -}; - -#endif diff --git a/source/clangobjccompiler.cpp b/source/clangobjccompiler.cpp deleted file mode 100644 index 557bf3e..0000000 --- a/source/clangobjccompiler.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "clangobjccompiler.h" - -ClangObjCCompiler::ClangObjCCompiler(Builder &b, const Architecture &a): - GnuObjCCompiler(b, a) -{ - set_command("clang", true); -} diff --git a/source/clangobjccompiler.h b/source/clangobjccompiler.h deleted file mode 100644 index 64b96d0..0000000 --- a/source/clangobjccompiler.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef CLANGOBJCCOMPILER_H_ -#define CLANGOBJCCOMPILER_H_ - -#include "gnuobjccompiler.h" - -class ClangObjCCompiler: public GnuObjCCompiler -{ -public: - ClangObjCCompiler(Builder &, const Architecture &); -}; - -#endif diff --git a/source/gnuccompiler.cpp b/source/gnuccompiler.cpp deleted file mode 100644 index 0210a6c..0000000 --- a/source/gnuccompiler.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "csourcefile.h" -#include "gnuccompiler.h" - -using namespace std; -using namespace Msp; - -GnuCCompiler::GnuCCompiler(Builder &b, const Architecture &a, const FS::Path &sysroot): - GnuCompiler(b, a, "CC", sysroot) -{ - set_command("gcc", true); - input_suffixes.push_back(".c"); - aux_suffixes.push_back(".h"); -} - -Target *GnuCCompiler::create_source(const Component &comp, const FS::Path &path) const -{ - return new CSourceFile(builder, comp, path); -} - -Target *GnuCCompiler::create_source(const FS::Path &path) const -{ - return new CSourceFile(builder, path); -} diff --git a/source/gnuccompiler.h b/source/gnuccompiler.h deleted file mode 100644 index c0b5701..0000000 --- a/source/gnuccompiler.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef GNUCCOMPILER_H_ -#define GNUCCOMPILER_H_ - -#include "gnucompiler.h" - -/** -The GNU C compiler, commonly known as gcc. -*/ -class GnuCCompiler: public GnuCompiler -{ -public: - GnuCCompiler(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); - - virtual Target *create_source(const Component &, const Msp::FS::Path &) const; - virtual Target *create_source(const Msp::FS::Path &) const; -}; - -#endif diff --git a/source/gnucompiler.cpp b/source/gnucompiler.cpp index 816a9a3..ea04fbe 100644 --- a/source/gnucompiler.cpp +++ b/source/gnucompiler.cpp @@ -1,13 +1,16 @@ #include #include +#include #include #include #include #include "architecture.h" #include "builder.h" #include "component.h" +#include "csourcefile.h" #include "externaltask.h" #include "gnucompiler.h" +#include "objcsourcefile.h" #include "objectfile.h" #include "sourcefile.h" #include "sourcepackage.h" @@ -29,6 +32,27 @@ const char *cpus[] = GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot): Tool(b, a, t) { + if(tag=="CC") + { + input_suffixes.push_back(".c"); + aux_suffixes.push_back(".h"); + } + else if(tag=="CXX") + { + input_suffixes.push_back(".cpp"); + input_suffixes.push_back(".cc"); + aux_suffixes.push_back(".hpp"); + } + else if(tag=="OBJC") + { + input_suffixes.push_back(".m"); + build_info.libs.push_back("objc"); + } + else + throw invalid_argument("GnuCompiler::GnuCompiler"); + + set_command((tag=="CXX" ? "g++" : "gcc"), true); + if(!sysroot.empty()) { build_info.sysroot = sysroot; @@ -40,6 +64,22 @@ GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, con system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include"); } +Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const +{ + if(tag=="OBJC") + return new ObjCSourceFile(builder, comp, path); + else + return new CSourceFile(builder, comp, path); +} + +Target *GnuCompiler::create_source(const FS::Path &path) const +{ + if(tag=="OBJC") + return new ObjCSourceFile(builder, path); + else + return new CSourceFile(builder, path); +} + Target *GnuCompiler::create_target(const list &sources, const string &) { if(sources.size()!=1) @@ -83,6 +123,14 @@ void GnuCompiler::do_prepare() { version = strip(ExternalTask::run_and_capture_output(argv)); builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version)); + + if(tag=="CXX") + { + const FS::Path &sysroot = build_info.sysroot; + FS::Path cxx_path = sysroot/"usr"/"include"/"c++"/version; + if(FS::is_dir(cxx_path)) + system_path.push_back(cxx_path); + } } catch(const runtime_error &) { } diff --git a/source/gnucompiler.h b/source/gnucompiler.h index cd6aa52..0eb25d0 100644 --- a/source/gnucompiler.h +++ b/source/gnucompiler.h @@ -15,9 +15,11 @@ class GnuCompiler: public Tool protected: std::string version; +public: GnuCompiler(Builder &, const Architecture &, const std::string &, const Msp::FS::Path & = Msp::FS::Path()); -public: + virtual Target *create_source(const Component &, const Msp::FS::Path &) const; + virtual Target *create_source(const Msp::FS::Path &) const; virtual Target *create_target(const std::list &, const std::string &); virtual std::string create_build_signature(const BuildInfo &) const; protected: diff --git a/source/gnucxxcompiler.cpp b/source/gnucxxcompiler.cpp deleted file mode 100644 index 87fe164..0000000 --- a/source/gnucxxcompiler.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include "csourcefile.h" -#include "gnucxxcompiler.h" - -using namespace std; -using namespace Msp; - -GnuCxxCompiler::GnuCxxCompiler(Builder &b, const Architecture &a, const FS::Path &sysroot): - GnuCompiler(b, a, "CXX", sysroot) -{ - set_command("g++", true); - input_suffixes.push_back(".cpp"); - input_suffixes.push_back(".cc"); - aux_suffixes.push_back(".hpp"); -} - -Target *GnuCxxCompiler::create_source(const Component &comp, const FS::Path &path) const -{ - return new CSourceFile(builder, comp, path); -} - -Target *GnuCxxCompiler::create_source(const FS::Path &path) const -{ - return new CSourceFile(builder, path); -} - -void GnuCxxCompiler::do_prepare() -{ - GnuCompiler::do_prepare(); - if(!version.empty()) - { - const FS::Path &sysroot = build_info.sysroot; - FS::Path cxx_path = sysroot/"usr"/"include"/"c++"/version; - if(FS::is_dir(cxx_path)) - system_path.push_back(cxx_path); - } -} diff --git a/source/gnucxxcompiler.h b/source/gnucxxcompiler.h deleted file mode 100644 index 491307e..0000000 --- a/source/gnucxxcompiler.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef GNUCXXCOMPILER_H_ -#define GNUCXXCOMPILER_H_ - -#include "gnucompiler.h" - -/** -The GNU C++ compiler, commonly known as g++. -*/ -class GnuCxxCompiler: public GnuCompiler -{ -public: - GnuCxxCompiler(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); - - virtual Target *create_source(const Component &, const Msp::FS::Path &) const; - virtual Target *create_source(const Msp::FS::Path &) const; - -protected: - virtual void do_prepare(); -}; - -#endif diff --git a/source/gnuobjccompiler.cpp b/source/gnuobjccompiler.cpp deleted file mode 100644 index 16e5215..0000000 --- a/source/gnuobjccompiler.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "gnuobjccompiler.h" -#include "objcsourcefile.h" - -using namespace Msp; - -GnuObjCCompiler::GnuObjCCompiler(Builder &b, const Architecture &a, const FS::Path &sysroot): - GnuCompiler(b, a, "OBJC", sysroot) -{ - set_command("gcc", true); - input_suffixes.push_back(".m"); - build_info.libs.push_back("objc"); -} - -Target *GnuObjCCompiler::create_source(const Component &comp, const FS::Path &path) const -{ - return new ObjCSourceFile(builder, comp, path); -} - -Target *GnuObjCCompiler::create_source(const FS::Path &path) const -{ - return new ObjCSourceFile(builder, path); -} diff --git a/source/gnuobjccompiler.h b/source/gnuobjccompiler.h deleted file mode 100644 index d82da03..0000000 --- a/source/gnuobjccompiler.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef GNUOBJCCOMPILER_H_ -#define GNUOBJCCOMPILER_H_ - -#include "gnucompiler.h" - -class GnuObjCCompiler: public GnuCompiler -{ -public: - GnuObjCCompiler(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); - - virtual Target *create_source(const Component &, const Msp::FS::Path &) const; - virtual Target *create_source(const Msp::FS::Path &) const; -}; - -#endif diff --git a/source/systemtools.cpp b/source/systemtools.cpp index bf90cf0..77fe2be 100644 --- a/source/systemtools.cpp +++ b/source/systemtools.cpp @@ -1,13 +1,9 @@ #include "architecture.h" #include "builder.h" -#include "clangccompiler.h" -#include "clangcxxcompiler.h" -#include "clangobjccompiler.h" +#include "clangcompiler.h" #include "gnuarchiver.h" -#include "gnuccompiler.h" -#include "gnucxxcompiler.h" +#include "gnucompiler.h" #include "gnulinker.h" -#include "gnuobjccompiler.h" #include "mingwdlltool.h" #include "systemtools.h" @@ -18,15 +14,15 @@ SystemTools::SystemTools(Builder &builder, const Architecture &arch) const string &sys = arch.get_system(); if((sys=="darwin" || sys=="freebsd") && builder.get_vfs().find_binary("clang")) { - add_tool(new ClangCCompiler(builder, arch)); - add_tool(new ClangCxxCompiler(builder, arch)); - add_tool(new ClangObjCCompiler(builder, arch)); + add_tool(new ClangCompiler(builder, arch, "CC")); + add_tool(new ClangCompiler(builder, arch, "CXX")); + add_tool(new ClangCompiler(builder, arch, "OBJC")); } else { - add_tool(new GnuCCompiler(builder, arch)); - add_tool(new GnuCxxCompiler(builder, arch)); - add_tool(new GnuObjCCompiler(builder, arch)); + add_tool(new GnuCompiler(builder, arch, "CC")); + add_tool(new GnuCompiler(builder, arch, "CXX")); + add_tool(new GnuCompiler(builder, arch, "OBJC")); } add_tool(new GnuLinker(builder, arch));