CppADCodeGen 2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
Loading...
Searching...
No Matches
gcc_compiler.hpp
1#ifndef CPPAD_CG_GCC_COMPILER_INCLUDED
2#define CPPAD_CG_GCC_COMPILER_INCLUDED
3/* --------------------------------------------------------------------------
4 * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5 * Copyright (C) 2012 Ciengis
6 * Copyright (C) 2018 Joao Leal
7 *
8 * CppADCodeGen is distributed under multiple licenses:
9 *
10 * - Eclipse Public License Version 1.0 (EPL1), and
11 * - GNU General Public License Version 3 (GPL3).
12 *
13 * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14 * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15 * ----------------------------------------------------------------------------
16 * Author: Joao Leal
17 */
18
19namespace CppAD {
20namespace cg {
21
27template<class Base>
28class GccCompiler : public AbstractCCompiler<Base> {
29public:
30
31 GccCompiler(const std::string& gccPath = "/usr/bin/gcc") :
33
34 this->_compileFlags.push_back("-O2"); // Optimization level
35 this->_compileLibFlags.push_back("-O2"); // Optimization level
36 this->_compileLibFlags.push_back("-shared"); // Make shared object
37 this->_compileLibFlags.push_back("-rdynamic"); // add all symbols to the dynamic symbol table
38 }
39
40 GccCompiler(const GccCompiler& orig) = delete;
41 GccCompiler& operator=(const GccCompiler& rhs) = delete;
42
48 void buildDynamic(const std::string& library,
49 JobTimer* timer = nullptr) override {
50
51#if CPPAD_CG_SYSTEM_APPLE
52 std::string linkerName = "-install_name";
53#elif CPPAD_CG_SYSTEM_LINUX
54 std::string linkerName = "-soname";
55#endif
56 std::string linkerFlags = "-Wl," + linkerName + "," + system::filenameFromPath(library);
57 for (size_t i = 0; i < this->_linkFlags.size(); i++)
58 linkerFlags += "," + this->_linkFlags[i];
59
60 std::vector<std::string> args;
61 args.insert(args.end(), this->_compileLibFlags.begin(), this->_compileLibFlags.end());
62 args.push_back(linkerFlags); // Pass suitable options to linker
63 args.push_back("-o"); // Output file name
64 args.push_back(library); // Output file name
65 for (const std::string& it : this->_ofiles) {
66 args.push_back(it);
67 }
68
69 if (timer != nullptr) {
70 timer->startingJob("'" + library + "'", JobTimer::COMPILING_DYNAMIC_LIBRARY);
71 } else if (this->_verbose) {
72 std::cout << "building library '" << library << "'" << std::endl;
73 }
74
75 system::callExecutable(this->_path, args);
76
77 if (timer != nullptr) {
78 timer->finishedJob();
79 }
80 }
81
82 virtual ~GccCompiler() = default;
83
84protected:
85
92 void compileSource(const std::string& source,
93 const std::string& output,
94 bool posIndepCode) override {
95 std::vector<std::string> args;
96 args.push_back("-x");
97 args.push_back("c"); // C source files
98 args.insert(args.end(), this->_compileFlags.begin(), this->_compileFlags.end());
99 args.push_back("-c");
100 args.push_back("-");
101 if (posIndepCode) {
102 args.push_back("-fPIC"); // position-independent code for dynamic linking
103 }
104 args.push_back("-o");
105 args.push_back(output);
106
107 system::callExecutable(this->_path, args, nullptr, &source);
108 }
109
110 void compileFile(const std::string& path,
111 const std::string& output,
112 bool posIndepCode) override {
113 std::vector<std::string> args;
114 args.push_back("-x");
115 args.push_back("c"); // C source files
116 args.insert(args.end(), this->_compileFlags.begin(), this->_compileFlags.end());
117 if (posIndepCode) {
118 args.push_back("-fPIC"); // position-independent code for dynamic linking
119 }
120 args.push_back("-c");
121 args.push_back(path);
122 args.push_back("-o");
123 args.push_back(output);
124
125 system::callExecutable(this->_path, args);
126 }
127
128};
129
130} // END cg namespace
131} // END CppAD namespace
132
133#endif
void compileFile(const std::string &path, const std::string &output, bool posIndepCode) override
void buildDynamic(const std::string &library, JobTimer *timer=nullptr) override
void compileSource(const std::string &source, const std::string &output, bool posIndepCode) override
void callExecutable(const std::string &executable, const std::vector< std::string > &args, std::string *stdOutErrMessage=nullptr, const std::string *stdInMessage=nullptr)