From: Mikko Rasa Date: Tue, 28 May 2013 10:07:46 +0000 (+0300) Subject: Add target and tools for compiling Objective-C sources X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7bcd4fef81679dedcabc5a58b12511a6b52c4fe4;p=builder.git Add target and tools for compiling Objective-C sources --- diff --git a/source/clangobjccompiler.cpp b/source/clangobjccompiler.cpp new file mode 100644 index 0000000..557bf3e --- /dev/null +++ b/source/clangobjccompiler.cpp @@ -0,0 +1,7 @@ +#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 new file mode 100644 index 0000000..64b96d0 --- /dev/null +++ b/source/clangobjccompiler.h @@ -0,0 +1,12 @@ +#ifndef CLANGOBJCCOMPILER_H_ +#define CLANGOBJCCOMPILER_H_ + +#include "gnuobjccompiler.h" + +class ClangObjCCompiler: public GnuObjCCompiler +{ +public: + ClangObjCCompiler(Builder &, const Architecture &); +}; + +#endif diff --git a/source/clangtools.cpp b/source/clangtools.cpp index 1c7c8d6..a4f005c 100644 --- a/source/clangtools.cpp +++ b/source/clangtools.cpp @@ -1,9 +1,11 @@ #include "clangccompiler.h" #include "clangcxxcompiler.h" +#include "clangobjccompiler.h" #include "clangtools.h" ClangTools::ClangTools(Builder &builder, const Architecture &arch) { add_tool(new ClangCCompiler(builder, arch)); add_tool(new ClangCxxCompiler(builder, arch)); + add_tool(new ClangObjCCompiler(builder, arch)); } diff --git a/source/csourcefile.cpp b/source/csourcefile.cpp index 13c74b9..e34ff27 100644 --- a/source/csourcefile.cpp +++ b/source/csourcefile.cpp @@ -22,6 +22,16 @@ CSourceFile::CSourceFile(Builder &b, const Component &c, const FS::Path &p): install_location = FS::Path("include")/package->get_name(); } +void CSourceFile::parse_includes(IO::Base &in) +{ + static Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]"); + + string line; + while(in.getline(line)) + if(RegMatch match = r_include.match(line)) + includes.push_back(match[1].str); +} + void CSourceFile::find_dependencies() { if(!component || !mtime) @@ -38,13 +48,7 @@ void CSourceFile::find_dependencies() builder.get_logger().log("files", format("Reading includes from %s", path.str())); - Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]"); - - string line; - while(in.getline(line)) - if(RegMatch match = r_include.match(line)) - includes.push_back(match[1].str); - + parse_includes(in); cache.set_values(this, "includes", includes); } diff --git a/source/csourcefile.h b/source/csourcefile.h index 9ed736a..dce6e19 100644 --- a/source/csourcefile.h +++ b/source/csourcefile.h @@ -1,6 +1,7 @@ #ifndef CSOURCEFILE_H_ #define CSOURCEFILE_H_ +#include #include "sourcefile.h" /** @@ -11,7 +12,7 @@ class CSourceFile: public SourceFile public: typedef std::list IncludeList; -private: +protected: IncludeList includes; public: @@ -20,7 +21,8 @@ public: virtual const char *get_type() const { return "CSourceFile"; } const IncludeList &get_includes() const { return includes; } -private: +protected: + virtual void parse_includes(Msp::IO::Base &); virtual void find_dependencies(); virtual void modified(); }; diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index b2df14c..b2516d6 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -212,6 +212,7 @@ Task *GnuLinker::Linker::run(const Target &target) const argv.push_back(relative(bin.get_path(), work_dir).str()); bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC); + bool need_l_objc = false; const Target::Dependencies &depends = target.get_dependencies(); for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i) @@ -220,7 +221,13 @@ Task *GnuLinker::Linker::run(const Target &target) const Target *tgt = (*i)->get_real_target(); if(ObjectFile *obj = dynamic_cast(tgt)) + { argv.push_back(relative(obj->get_path(), work_dir).str()); + /* XXX This is a hack. A more generic way is needed for tools to pass + information down the chain. */ + if(obj->get_tool()->get_tag()=="OBJC") + need_l_objc = true; + } else if(StaticLibrary *stlib = dynamic_cast(tgt)) argv.push_back((file?file:stlib)->get_path().str()); else if(SharedLibrary *shlib = dynamic_cast(tgt)) @@ -239,6 +246,8 @@ Task *GnuLinker::Linker::run(const Target &target) const } } + if(need_l_objc) + argv.push_back("-lobjc"); if(static_link_ok) argv.push_back("-static"); else if(architecture->get_system()=="windows") diff --git a/source/gnuobjccompiler.cpp b/source/gnuobjccompiler.cpp new file mode 100644 index 0000000..eddca5f --- /dev/null +++ b/source/gnuobjccompiler.cpp @@ -0,0 +1,21 @@ +#include "gnuobjccompiler.h" +#include "objcsourcefile.h" + +using namespace Msp; + +GnuObjCCompiler::GnuObjCCompiler(Builder &b, const Architecture &a): + GnuCompiler(b, a, "OBJC") +{ + set_command("gcc", true); + input_suffixes.push_back(".m"); +} + +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 new file mode 100644 index 0000000..11fbd91 --- /dev/null +++ b/source/gnuobjccompiler.h @@ -0,0 +1,15 @@ +#ifndef GNUOBJCCOMPILER_H_ +#define GNUOBJCCOMPILER_H_ + +#include "gnucompiler.h" + +class GnuObjCCompiler: public GnuCompiler +{ +public: + GnuObjCCompiler(Builder &, const Architecture &); + + 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/gnutools.cpp b/source/gnutools.cpp index 20463e6..0dc2749 100644 --- a/source/gnutools.cpp +++ b/source/gnutools.cpp @@ -3,6 +3,7 @@ #include "gnuccompiler.h" #include "gnucxxcompiler.h" #include "gnulinker.h" +#include "gnuobjccompiler.h" #include "gnutools.h" #include "mingwdlltool.h" @@ -10,6 +11,7 @@ GnuTools::GnuTools(Builder &builder, const Architecture &arch) { add_tool(new GnuCCompiler(builder, arch)); add_tool(new GnuCxxCompiler(builder, arch)); + add_tool(new GnuObjCCompiler(builder, arch)); add_tool(new GnuLinker(builder, arch)); add_tool(new GnuArchiver(builder, arch)); if(arch.get_system()=="windows") diff --git a/source/objcsourcefile.cpp b/source/objcsourcefile.cpp new file mode 100644 index 0000000..ed334fc --- /dev/null +++ b/source/objcsourcefile.cpp @@ -0,0 +1,23 @@ +#include +#include "objcsourcefile.h" + +using namespace std; +using namespace Msp; + +ObjCSourceFile::ObjCSourceFile(Builder &b, const FS::Path &p): + CSourceFile(b, p) +{ } + +ObjCSourceFile::ObjCSourceFile(Builder &b, const Component &c, const FS::Path &p): + CSourceFile(b, c, p) +{ } + +void ObjCSourceFile::parse_includes(IO::Base &in) +{ + static Regex r_include("^[ \t]*#(include|import)[ \t]+([\"<].*)[\">]"); + + string line; + while(in.getline(line)) + if(RegMatch match = r_include.match(line)) + includes.push_back(match[2].str); +} diff --git a/source/objcsourcefile.h b/source/objcsourcefile.h new file mode 100644 index 0000000..2069615 --- /dev/null +++ b/source/objcsourcefile.h @@ -0,0 +1,21 @@ +#ifndef OBJCSOURCEFILE_H_ +#define OBJCSOURCEFILE_H_ + +#include "csourcefile.h" + +/** +Represents an Objective-C source file. +*/ +class ObjCSourceFile: public CSourceFile +{ +public: + ObjCSourceFile(Builder &, const Msp::FS::Path &); + ObjCSourceFile(Builder &, const Component &, const Msp::FS::Path &); + + virtual const char *get_type() const { return "ObjCSourceFile"; } + +protected: + virtual void parse_includes(Msp::IO::Base &); +}; + +#endif