cpoach.sh 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -9
- package/index.js +46 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
 -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://github.com/user-attachments/assets/ed3e5a8f-c8df-4644-b643-80620da189fb" alt="cpoach logo" width="60%"/>
|
|
5
|
+
</p>
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
**cpoach** is a support tool designed to simplify the use of single-file C/C++ libraries. These libraries can be effortlessly installed via `npm`, making it easier to integrate them into your projects without the hassle of *managing build systems* or including third-party libraries in your version control. Refer to the [website](https://nodef.github.io) for the list of available libraries.
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
### Features
|
|
8
10
|
|
|
9
11
|
- Install and use C/C++ libraries as easily as `#include <stdio.h>`.
|
|
10
12
|
- Libraries are available via NPM for seamless installation.
|
|
@@ -28,11 +30,13 @@ npm i -g cpoach.sh
|
|
|
28
30
|
After installation, you can include the desired libraries in your C/C++ projects. For example, if you want to use the [tigr.c] library for graphics, follow these steps:
|
|
29
31
|
|
|
30
32
|
Run:
|
|
33
|
+
|
|
31
34
|
```bash
|
|
32
35
|
$ npm i tigr.c
|
|
33
36
|
```
|
|
34
37
|
|
|
35
38
|
And then include `tigr.h` as follows:
|
|
39
|
+
|
|
36
40
|
```c
|
|
37
41
|
// main.c
|
|
38
42
|
#define TIGR_IMPLEMENTATION
|
|
@@ -41,14 +45,55 @@ And then include `tigr.h` as follows:
|
|
|
41
45
|
int main() { /* ... */ }
|
|
42
46
|
```
|
|
43
47
|
|
|
44
|
-
And then compile with
|
|
48
|
+
And then compile with **clang**, **gcc**, or **MSVC** as follows:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
$ cpoach clang main.c # or, use gcc
|
|
52
|
+
$ cpoach gcc main.c # or, use MSVC
|
|
53
|
+
$ cpoach cl main.c
|
|
54
|
+
# NOTE: clang++ and g++ can also be used
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can also manually retrieve the include flags and use them in your compilation command:
|
|
45
58
|
|
|
46
59
|
```bash
|
|
47
60
|
$ clang $(cpoach i) main.c # or, use gcc
|
|
48
|
-
$ gcc $(cpoach i) main.c
|
|
61
|
+
$ gcc $(cpoach i) main.c # or, use MSVC
|
|
62
|
+
$ cl $(cpoach i --msvc) main.c
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
On command prompt (Windows), use the following commands instead:
|
|
66
|
+
|
|
67
|
+
```batch
|
|
68
|
+
> FOR /F "tokens=*" %%i IN ('cpoach i') DO clang++ %%i main.cxx REM or, use g++
|
|
69
|
+
> FOR /F "tokens=*" %%i IN ('cpoach i') DO g++ %%i main.cxx REM or, use MSVC
|
|
70
|
+
> FOR /F "tokens=*" %%i IN ('cpoach i --msvc') DO cl %%i main.cxx
|
|
49
71
|
```
|
|
50
72
|
|
|
51
|
-
|
|
73
|
+
As mentioned earlier, the catalog of available libraries is available on the [website](https://nodef.github.io).
|
|
74
|
+
|
|
75
|
+
[tigr.c]: https://www.npmjs.com/package/tigr.c
|
|
76
|
+
|
|
77
|
+
<br>
|
|
78
|
+
|
|
79
|
+
## Documentation
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
$ cpoach [command] [options]
|
|
83
|
+
|
|
84
|
+
Usage: cpoach [command] [options]
|
|
85
|
+
|
|
86
|
+
Commands:
|
|
87
|
+
i | includes Generate compiler flags for include paths.
|
|
88
|
+
gcc | clang | cl Run compiler, passing the include flags.
|
|
89
|
+
|
|
90
|
+
Options:
|
|
91
|
+
--compiler [name] Specify the compiler (msvc, gcc, clang). Default is gcc.
|
|
92
|
+
--msvc Shortcut for --compiler msvc.
|
|
93
|
+
--gcc Shortcut for --compiler gcc.
|
|
94
|
+
--clang Shortcut for --compiler clang.
|
|
95
|
+
--help Display help information.
|
|
96
|
+
```
|
|
52
97
|
|
|
53
98
|
<br>
|
|
54
99
|
|
|
@@ -62,5 +107,3 @@ We welcome contributions! If you have suggestions, please open an issue on our [
|
|
|
62
107
|
|
|
63
108
|
[](https://nodef.github.io)
|
|
64
109
|

|
|
65
|
-
|
|
66
|
-
[tigr.c]: https://www.npmjs.com/package/tigr.c
|
package/index.js
CHANGED
|
@@ -1,18 +1,45 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
1
|
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const cp = require('child_process');
|
|
3
4
|
|
|
4
5
|
|
|
5
|
-
//
|
|
6
|
-
function
|
|
6
|
+
// Get compiler type from its name.
|
|
7
|
+
function getCompilerType(name) {
|
|
8
|
+
name = name.toLowerCase();
|
|
9
|
+
if (name.includes('msvc') || name.includes('cl')) return 'msvc';
|
|
10
|
+
if (name.includes('gcc') || name.includes('g++')) return 'gcc';
|
|
11
|
+
if (name.includes('clang')) return 'clang';
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// Get include paths from the current directory's node_modules.
|
|
17
|
+
function getIncludes(opt) {
|
|
7
18
|
const includes = [];
|
|
8
19
|
const cwd = process.cwd();
|
|
9
20
|
const pth = path.basename(cwd) === 'node_modules'? cwd : path.join(cwd, 'node_modules');
|
|
21
|
+
if (!fs.existsSync(pth) || !fs.statSync(pth).isDirectory()) return includes;
|
|
10
22
|
for (const ent of fs.readdirSync(pth)) {
|
|
11
23
|
if (!ent.endsWith('.c') && !ent.endsWith('.cxx')) continue;
|
|
12
24
|
includes.push(path.join(pth, ent));
|
|
13
25
|
}
|
|
14
26
|
const FLAG = opt.compiler==='msvc'? '/I' : '-I';
|
|
15
|
-
|
|
27
|
+
return includes.map(pth => `${FLAG}"${pth}"`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// Run the `includes` command, which generates compiler flags for include paths.
|
|
32
|
+
function runIncludes(opt) {
|
|
33
|
+
console.log(getIncludes(opt).join(' ').trim());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// Run the `gcc`/`clang`/`cl` command, which runs the compiler with include flags.
|
|
38
|
+
function runCompile(opt) {
|
|
39
|
+
const includes = getIncludes(opt);
|
|
40
|
+
const args = [...includes, ...opt.args];
|
|
41
|
+
try { cp.execFileSync(opt.compiler, args, {encoding: 'utf8', stdio: 'inherit'}); }
|
|
42
|
+
catch (err) { process.exit(err.status || 1); }
|
|
16
43
|
}
|
|
17
44
|
|
|
18
45
|
|
|
@@ -23,6 +50,7 @@ function runHelp() {
|
|
|
23
50
|
`\n` +
|
|
24
51
|
`Commands:\n` +
|
|
25
52
|
` i | includes Generate compiler flags for include paths.\n` +
|
|
53
|
+
` gcc | clang | cl Run compiler, passing the include flags.\n` +
|
|
26
54
|
`\n` +
|
|
27
55
|
`Options:\n` +
|
|
28
56
|
` --compiler [name] Specify the compiler (msvc, gcc, clang). Default is gcc.\n` +
|
|
@@ -36,6 +64,7 @@ function runHelp() {
|
|
|
36
64
|
// Parse the command name.
|
|
37
65
|
function parseCommand(opt, cmd) {
|
|
38
66
|
if (/i|includes/.test(cmd)) opt.command = 'includes';
|
|
67
|
+
else if (getCompilerType(cmd)) opt.command = 'compile';
|
|
39
68
|
else opt.error = `Unknown option: ${cmd}`;
|
|
40
69
|
}
|
|
41
70
|
|
|
@@ -47,7 +76,7 @@ function parseArg(opt, argv, k, i) {
|
|
|
47
76
|
else if (k==='--gcc') opt.compiler = 'gcc';
|
|
48
77
|
else if (k==='--clang') opt.compiler = 'clang';
|
|
49
78
|
else if (!opt.command) parseCommand(opt, k);
|
|
50
|
-
else opt.error = `Unknown option: ${k}`;
|
|
79
|
+
else if (opt.command !== 'compile') opt.error = `Unknown option: ${k}`;
|
|
51
80
|
return i;
|
|
52
81
|
}
|
|
53
82
|
|
|
@@ -56,11 +85,20 @@ function parseArgs(argv) {
|
|
|
56
85
|
const opt = {
|
|
57
86
|
error: null,
|
|
58
87
|
help: false,
|
|
59
|
-
command:
|
|
60
|
-
compiler:
|
|
88
|
+
command: null,
|
|
89
|
+
compiler: null,
|
|
90
|
+
args: [],
|
|
61
91
|
};
|
|
62
92
|
for (let i=2; i<argv.length; i++)
|
|
63
93
|
i = parseArg(opt, argv, argv[i], i);
|
|
94
|
+
const compiler = getCompilerType(argv[2]);
|
|
95
|
+
if (compiler) {
|
|
96
|
+
opt.command = 'compile';
|
|
97
|
+
opt.compiler = compiler;
|
|
98
|
+
opt.args = argv.slice(3);
|
|
99
|
+
}
|
|
100
|
+
opt.command = opt.command || 'includes';
|
|
101
|
+
opt.compiler = opt.compiler || 'gcc';
|
|
64
102
|
return opt;
|
|
65
103
|
}
|
|
66
104
|
|
|
@@ -79,6 +117,7 @@ function main(argv) {
|
|
|
79
117
|
}
|
|
80
118
|
switch (opt.command) {
|
|
81
119
|
case 'includes': runIncludes(opt); break;
|
|
120
|
+
case 'compile': runCompile(opt); break;
|
|
82
121
|
default:
|
|
83
122
|
console.error(`Unknown command: ${opt.command}`);
|
|
84
123
|
process.exit(1);
|