Added compiler arguments
This commit is contained in:
parent
d058f1f012
commit
7a5abc6595
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.2)
|
||||
project(cppscript
|
||||
VERSION 0
|
||||
VERSION 0.1.0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
|
|
31
README.md
31
README.md
|
@ -1,5 +1,36 @@
|
|||
**cppscript** allows you to execute C++ files as scripts.
|
||||
|
||||
It compiles the source file, stores the binary in `${XDG_CACHE_HOME}/cppscript/`
|
||||
and executes it.
|
||||
|
||||
It does compile the file every time at the moment.
|
||||
|
||||
## Usage
|
||||
|
||||
Use `#!/usr/bin/env cppscript` as shebang and write the compiler arguments in
|
||||
the line below with the prefix: `//cppscript:`
|
||||
|
||||
### Example
|
||||
|
||||
```C++
|
||||
#!/usr/bin/env cppscript
|
||||
//cppscript: -Wall -pedantic -Wextra
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using std::cout;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
cout << "Hello world!\n";
|
||||
cout << argv[1] << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
### From source
|
||||
|
|
18
src/main.cpp
18
src/main.cpp
|
@ -81,6 +81,7 @@ int main(int argc, char *argv[])
|
|||
const fs::path path(argv[1]);
|
||||
const fs::path binary = cache_dir / path.stem();
|
||||
const fs::path source = binary.string() + ".cpp";
|
||||
string compiler_arguments;
|
||||
|
||||
std::ifstream in(path);
|
||||
if (in.is_open())
|
||||
|
@ -90,6 +91,15 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
string buf;
|
||||
std::getline(in, buf);
|
||||
std::getline(in, buf);
|
||||
if (buf.substr(0, 12).compare("//cppscript:") == 0)
|
||||
{
|
||||
compiler_arguments = buf.substr(12);
|
||||
}
|
||||
else
|
||||
{
|
||||
out << buf << endl;
|
||||
}
|
||||
while (!in.eof())
|
||||
{
|
||||
std::getline(in, buf);
|
||||
|
@ -108,9 +118,13 @@ int main(int argc, char *argv[])
|
|||
cerr << "ERROR: Could not open file: " << path << endl;
|
||||
}
|
||||
|
||||
std::system((compiler + " " + source.string()
|
||||
std::system((compiler + " " + source.string() + " " + compiler_arguments
|
||||
+ " -o " + binary.string()).c_str());
|
||||
execv(binary.c_str(), argv);
|
||||
execv(binary.c_str(), &argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "usage: " << argv[0] << " file [arguments]\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Reference in New Issue