1 #ifndef CPPAD_CG_CLANG_COMPILER_INCLUDED 2 #define CPPAD_CG_CLANG_COMPILER_INCLUDED 30 std::set<std::string> _bcfiles;
34 ClangCompiler(
const std::string& clangPath =
"/usr/bin/clang") :
37 this->_compileFlags.push_back(
"-O2");
38 this->_compileLibFlags.push_back(
"-O2");
39 this->_compileLibFlags.push_back(
"-shared");
40 this->_compileLibFlags.push_back(
"-rdynamic");
46 const std::string& getVersion() {
47 if(_version.empty()) {
48 std::vector<std::string> args {
"--version"};
52 std::string vv =
"version ";
53 size_t is = output.find(vv);
54 if(is == std::string::npos) {
55 throw CGException(
"Failed to determine Clang version");
59 while (i < output.size() && output[i] !=
' ' && output[i] !=
'\n') {
63 _version = output.substr(is, i - is);
68 virtual const std::set<std::string>& getBitCodeFiles()
const {
72 virtual void generateLLVMBitCode(
const std::map<std::string, std::string>& sources,
74 bool posIndepCode =
false;
75 this->_compileFlags.push_back(
"-emit-llvm");
77 this->
compileSources(sources, posIndepCode, timer,
".bc", this->_bcfiles);
79 this->_compileFlags.pop_back();
90 JobTimer* timer =
nullptr)
override {
92 #if CPPAD_CG_SYSTEM_APPLE 93 std::string linkerName =
"-install_name";
94 #elif CPPAD_CG_SYSTEM_LINUX 95 std::string linkerName =
"-soname";
97 std::string linkerFlags =
"-Wl," + linkerName +
"," + system::filenameFromPath(library);
98 for (
size_t i = 0; i < this->_linkFlags.size(); i++)
99 linkerFlags +=
"," + this->_linkFlags[i];
101 std::vector<std::string> args;
102 args.insert(args.end(), this->_compileLibFlags.begin(), this->_compileLibFlags.end());
103 args.push_back(linkerFlags);
104 args.push_back(
"-o");
105 args.push_back(library);
107 for (
const std::string& it : this->_ofiles) {
111 if (timer !=
nullptr) {
112 timer->startingJob(
"'" + library +
"'", JobTimer::COMPILING_DYNAMIC_LIBRARY);
113 }
else if (this->_verbose) {
114 std::cout <<
"building library '" << library <<
"'" << std::endl;
119 if (timer !=
nullptr) {
120 timer->finishedJob();
126 for (
const std::string& it : _bcfiles) {
127 if (
remove(it.c_str()) != 0)
128 std::cerr <<
"Failed to delete temporary file '" << it <<
"'" << std::endl;
140 static std::vector<std::string> parseVersion(
const std::string& version) {
141 auto vv = explode(version,
".");
143 auto vv2 = explode(vv[2],
"-");
144 if (vv2.size() > 1) {
145 vv.erase(vv.begin() + 2);
146 vv.insert(vv.begin() + 2, vv2.begin(), vv2.end());
162 const std::string& output,
163 bool posIndepCode)
override {
164 std::vector<std::string> args;
165 args.push_back(
"-x");
167 args.insert(args.end(), this->_compileFlags.begin(), this->_compileFlags.end());
168 args.push_back(
"-c");
171 args.push_back(
"-fPIC");
173 args.push_back(
"-o");
174 args.push_back(output);
180 const std::string& output,
181 bool posIndepCode)
override {
182 std::vector<std::string> args;
183 args.push_back(
"-x");
185 args.insert(args.end(), this->_compileFlags.begin(), this->_compileFlags.end());
187 args.push_back(
"-fPIC");
189 args.push_back(
"-c");
190 args.push_back(path);
191 args.push_back(
"-o");
192 args.push_back(output);
void buildDynamic(const std::string &library, JobTimer *timer=nullptr) override
void callExecutable(const std::string &executable, const std::vector< std::string > &args, std::string *stdOutErrMessage=nullptr, const std::string *stdInMessage=nullptr)
void compileSources(const std::map< std::string, std::string > &sources, bool posIndepCode, JobTimer *timer=nullptr) override
void compileSource(const std::string &source, const std::string &output, bool posIndepCode) override
void compileFile(const std::string &path, const std::string &output, bool posIndepCode) override