cpp.js 0.5.0 → 0.6.1
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/package.json +1 -1
- package/src/assets/CMakeLists.txt +28 -19
- package/src/assets/dist.cmake +19 -0
- package/src/bin.js +7 -4
- package/src/functions/createWasm.js +126 -25
- package/src/index.js +7 -3
- package/src/utils/createTempDir.js +1 -1
- package/src/utils/findCMakeListsFile.js +4 -0
- package/src/utils/getConfig.js +28 -29
- package/src/utils/getDirName.js +7 -0
package/package.json
CHANGED
|
@@ -1,37 +1,46 @@
|
|
|
1
1
|
cmake_minimum_required(VERSION 3.25)
|
|
2
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
2
3
|
project("${PROJECT_NAME}")
|
|
4
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
|
3
5
|
|
|
4
6
|
option(BUILD_BRIDGE "Build Bridge" OFF)
|
|
5
7
|
option(BUILD_SOURCE "Build Source" OFF)
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
foreach(depend IN ZIP_LISTS DEPENDS_CMAKE_PATHS DEPENDS_CMAKE_NAMES)
|
|
10
|
+
add_subdirectory("${depend_0}" "${CMAKE_CURRENT_BINARY_DIR}/dependencies/${depend_1}")
|
|
11
|
+
endforeach()
|
|
12
|
+
|
|
13
|
+
if (BUILD_SOURCE)
|
|
14
|
+
file(GLOB_RECURSE BUILD_SRC_FILES ${NATIVE_GLOB})
|
|
9
15
|
endif(BUILD_SOURCE)
|
|
10
16
|
|
|
11
|
-
if(BUILD_BRIDGE)
|
|
12
|
-
file(GLOB BRIDGE_SRC_FILES "${BRIDGE_DIR}/*.i.cpp")
|
|
17
|
+
if (BUILD_BRIDGE)
|
|
18
|
+
file(GLOB BRIDGE_SRC_FILES "${BRIDGE_DIR}/*.i.cpp")
|
|
13
19
|
endif(BUILD_BRIDGE)
|
|
14
20
|
|
|
15
21
|
set(SRC_FILES "${BUILD_SRC_FILES}" "${BRIDGE_SRC_FILES}")
|
|
16
|
-
add_library("${PROJECT_NAME}"
|
|
17
|
-
|
|
18
|
-
file(GLOB LIB_HEADERS_DIR "${BASE_DIR}/node_modules/cppjs-lib-*/include" "${BASE_DIR}/node_modules/cppjs-lib-*/node_modules/cppjs-lib-*/include")
|
|
19
|
-
file(GLOB_RECURSE HEADER_FILES ${HEADER_GLOB})
|
|
20
|
-
target_include_directories("${PROJECT_NAME}" PUBLIC "${LIB_HEADERS_DIR}" "${HEADER_DIR}")
|
|
22
|
+
add_library("${PROJECT_NAME}" "${BUILD_TYPE}" ${SRC_FILES})
|
|
21
23
|
|
|
22
|
-
if(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
BASE_DIRS "${HEADER_DIR}"
|
|
26
|
-
FILES "${HEADER_FILES}")
|
|
24
|
+
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "")
|
|
25
|
+
target_link_libraries("${PROJECT_NAME}" "${DEPENDS_CMAKE_NAMES}")
|
|
26
|
+
endif()
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
file(GLOB_RECURSE HEADER_FILES ${HEADER_GLOB})
|
|
29
|
+
target_include_directories("${PROJECT_NAME}" PUBLIC "${HEADER_DIR}")
|
|
30
|
+
|
|
31
|
+
if (BUILD_SOURCE)
|
|
32
|
+
target_sources("${PROJECT_NAME}"
|
|
33
|
+
PUBLIC FILE_SET HEADERS
|
|
34
|
+
BASE_DIRS "${HEADER_DIR}"
|
|
35
|
+
FILES "${HEADER_FILES}")
|
|
36
|
+
string(REPLACE "-" ";" HEADER_FOLDER ${PROJECT_NAME})
|
|
37
|
+
list(GET HEADER_FOLDER 2 HEADER_FOLDER)
|
|
38
|
+
install(TARGETS "${PROJECT_NAME}" DESTINATION "prebuilt/${PACKAGE_HOST}/lib")
|
|
39
|
+
install(TARGETS "${PROJECT_NAME}" FILE_SET HEADERS DESTINATION "prebuilt/${PACKAGE_HOST}/include/cppjs-lib-${HEADER_FOLDER}")
|
|
31
40
|
endif(BUILD_SOURCE)
|
|
32
41
|
unset(BUILD_SOURCE CACHE)
|
|
33
42
|
|
|
34
|
-
if(BUILD_BRIDGE)
|
|
35
|
-
install(TARGETS "${PROJECT_NAME}")
|
|
43
|
+
if (BUILD_BRIDGE)
|
|
44
|
+
install(TARGETS "${PROJECT_NAME}")
|
|
36
45
|
endif(BUILD_BRIDGE)
|
|
37
46
|
unset(BUILD_BRIDGE CACHE)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.25)
|
|
2
|
+
set(CMAKE_CXX_STANDARD 11)
|
|
3
|
+
set(PROJECT_NAME "___PROJECT_NAME___")
|
|
4
|
+
project("${PROJECT_NAME}")
|
|
5
|
+
|
|
6
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
|
7
|
+
|
|
8
|
+
find_library(PROJECT_LIBRARY
|
|
9
|
+
NAMES "${PROJECT_NAME}"
|
|
10
|
+
PATHS "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib"
|
|
11
|
+
NO_CACHE
|
|
12
|
+
NO_DEFAULT_PATH
|
|
13
|
+
NO_CMAKE_FIND_ROOT_PATH
|
|
14
|
+
REQUIRED
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
add_library("${PROJECT_NAME}" INTERFACE)
|
|
18
|
+
target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_LIBRARY}")
|
|
19
|
+
target_include_directories("${PROJECT_NAME}" INTERFACE "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/include")
|
package/src/bin.js
CHANGED
|
@@ -55,11 +55,14 @@ function generateLib(platform, output, base) {
|
|
|
55
55
|
|
|
56
56
|
compiler.createBridge();
|
|
57
57
|
compiler.createWasm({ cc: ['-O3'] });
|
|
58
|
-
createDir('
|
|
58
|
+
createDir('prebuilt', compiler.config.paths.output);
|
|
59
59
|
fs.copyFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.js`, `${compiler.config.paths.output}/${compiler.config.general.name}.js`);
|
|
60
60
|
fs.copyFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.wasm`, `${compiler.config.paths.output}/${compiler.config.general.name}.wasm`);
|
|
61
|
-
fs.
|
|
62
|
-
fs.
|
|
63
|
-
|
|
61
|
+
fs.rmSync(`${compiler.config.paths.output}/prebuilt`, { recursive: true, force: true });
|
|
62
|
+
fs.renameSync(`${compiler.config.paths.temp}/prebuilt`, `${compiler.config.paths.output}/prebuilt`);
|
|
63
|
+
|
|
64
|
+
const distCmakeContent = fs.readFileSync(`${compiler.config.paths.cli}/assets/dist.cmake`, { encoding: 'utf8', flag: 'r' })
|
|
65
|
+
.replace('___PROJECT_NAME___', compiler.config.general.name);
|
|
66
|
+
fs.writeFileSync(`${compiler.config.paths.output}/prebuilt/CMakeLists.txt`, distCmakeContent);
|
|
64
67
|
fs.rmSync(compiler.config.paths.temp, { recursive: true, force: true });
|
|
65
68
|
}
|
|
@@ -4,63 +4,165 @@ import pullDockerImage, { getDockerImage } from '../utils/pullDockerImage.js';
|
|
|
4
4
|
import getBaseInfo from '../utils/getBaseInfo.js';
|
|
5
5
|
import getPathInfo from '../utils/getPathInfo.js';
|
|
6
6
|
import getOsUserAndGroupId from '../utils/getOsUserAndGroupId.js';
|
|
7
|
+
import { getCliCMakeListsFile } from '../utils/findCMakeListsFile.js'
|
|
7
8
|
|
|
8
9
|
export default function createWasm(compiler, options = {}) {
|
|
9
10
|
const compiler2 = new CppjsCompiler(
|
|
10
11
|
compiler.config,
|
|
11
12
|
options,
|
|
12
13
|
);
|
|
13
|
-
return compiler2.
|
|
14
|
+
return compiler2.compileToWasm();
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
export function getCmakeParams(compiler, options = {}) {
|
|
18
|
+
const compiler2 = new CppjsCompiler(
|
|
19
|
+
compiler.config,
|
|
20
|
+
options,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
compiler2.prepare();
|
|
24
|
+
return compiler2.getCmakeParams(compiler.config.general.name, true, true);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function setPath(arr, dependency, type, filter = () => {}) {
|
|
28
|
+
if (filter(dependency)) {
|
|
29
|
+
if (type === 'this') {
|
|
30
|
+
arr.push(dependency);
|
|
31
|
+
} else if (Array.isArray(dependency.paths[type])) {
|
|
32
|
+
arr.push(...dependency.paths[type]);
|
|
33
|
+
} else {
|
|
34
|
+
arr.push(dependency.paths[type]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
dependency.dependencies.forEach(dep => {
|
|
39
|
+
setPath(arr, dep, type, filter);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getParentPath(path) {
|
|
44
|
+
const pathArray = path.split('/');
|
|
45
|
+
pathArray.pop();
|
|
46
|
+
return pathArray.join('/');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
16
50
|
class CppjsCompiler {
|
|
17
51
|
constructor(config, options) {
|
|
18
52
|
this.config = config;
|
|
19
53
|
this.options = options;
|
|
54
|
+
this.pathType = 'absolute';
|
|
55
|
+
this.pathPrefix = '';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getPath(path) {
|
|
59
|
+
if (this.pathType === 'absolute') {
|
|
60
|
+
return getPathInfo(path, this.config.paths.base).absolute;
|
|
61
|
+
} else if (this.pathType === 'relative') {
|
|
62
|
+
return `${this.pathPrefix}${getPathInfo(path, this.config.paths.base).relative}`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return '';
|
|
20
66
|
}
|
|
21
67
|
|
|
22
|
-
|
|
68
|
+
compileToWasm() {
|
|
23
69
|
pullDockerImage();
|
|
24
70
|
|
|
71
|
+
this.pathType = 'relative';
|
|
72
|
+
this.pathPrefix = '/live/';
|
|
73
|
+
|
|
74
|
+
this.prepare();
|
|
25
75
|
this.cmake(this.config.general.name, true, false);
|
|
26
76
|
this.make();
|
|
27
77
|
this.cmake(this.config.general.name+'bridge', false, true);
|
|
28
78
|
this.make();
|
|
29
79
|
|
|
80
|
+
const dependLibs = [
|
|
81
|
+
...glob.sync(`${this.config.paths.temp}/dependencies/**/*.a`, { absolute: true, cwd: this.config.paths.project }),
|
|
82
|
+
];
|
|
83
|
+
this.cmakeDepends.forEach((d) => {
|
|
84
|
+
dependLibs.push(...glob.sync(`${d.paths.output}/prebuilt/Emscripten-x86_64/**/*.a`, { absolute: true, cwd: d.paths.project }));
|
|
85
|
+
});
|
|
86
|
+
|
|
30
87
|
this.libs = [
|
|
31
88
|
`${this.config.paths.temp}/lib${this.config.general.name}.a`,
|
|
32
89
|
`${this.config.paths.temp}/lib${this.config.general.name}bridge.a`,
|
|
33
|
-
...
|
|
34
|
-
|
|
35
|
-
].filter(path => !!path.toString()).map(path => `/live/${getPathInfo(path, this.config.paths.base).relative}`);
|
|
90
|
+
...dependLibs,
|
|
91
|
+
].filter(path => !!path.toString()).map(path => this.getPath(path));
|
|
36
92
|
|
|
37
93
|
this.cc();
|
|
38
94
|
|
|
39
95
|
return this.config.paths.temp;
|
|
40
96
|
}
|
|
41
97
|
|
|
42
|
-
|
|
98
|
+
prepare() {
|
|
99
|
+
const sourceFilter = (d) => d === this.config || d.export.type === 'source';
|
|
100
|
+
this.headerPathWithDepends = [];
|
|
101
|
+
setPath(this.headerPathWithDepends, this.config, 'header', sourceFilter);
|
|
102
|
+
this.headerPathWithDepends = this.headerPathWithDepends.map(p => this.getPath(p)).join(';');
|
|
103
|
+
|
|
104
|
+
this.headerGlob = [];
|
|
105
|
+
this.headerPathWithDepends.split(';').forEach(h => {
|
|
106
|
+
this.config.ext.header.forEach(ext => {
|
|
107
|
+
this.headerGlob.push(`${h}/*.${ext}`);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
this.nativePathWithDepends = [];
|
|
113
|
+
setPath(this.nativePathWithDepends, this.config, 'native', sourceFilter);
|
|
114
|
+
this.nativePathWithDepends = this.nativePathWithDepends.map(p => this.getPath(p)).join(';');
|
|
115
|
+
|
|
116
|
+
this.nativeGlob = [];
|
|
117
|
+
this.nativePathWithDepends.split(';').forEach(h => {
|
|
118
|
+
this.config.ext.source.forEach(ext => {
|
|
119
|
+
this.nativeGlob.push(`${h}/*.${ext}`);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const cliCMakeListsFile = getCliCMakeListsFile();
|
|
124
|
+
const cmakeFilter = (d) => d !== this.config && d.export.type === 'cmake' && d.paths.cmake !== cliCMakeListsFile;
|
|
125
|
+
this.cmakeDepends = [];
|
|
126
|
+
setPath(this.cmakeDepends, this.config, 'this', cmakeFilter);
|
|
127
|
+
|
|
128
|
+
this.pathsOfCmakeDepends = this.cmakeDepends
|
|
129
|
+
.map(d => getParentPath(d.paths.cmake))
|
|
130
|
+
.map(p => this.getPath(p)).join(';');
|
|
131
|
+
this.nameOfCmakeDepends = this.cmakeDepends.map(d => d.general.name).join(';');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
getCmakeParams(name, isBuildSource, isBuildBridge) {
|
|
43
135
|
const params = [];
|
|
44
136
|
if (isBuildSource) params.push('-DBUILD_SOURCE=TRUE');
|
|
45
137
|
if (isBuildBridge) params.push('-DBUILD_BRIDGE=TRUE');
|
|
46
138
|
|
|
47
|
-
const output =
|
|
48
|
-
const projectPath =
|
|
49
|
-
|
|
50
|
-
|
|
139
|
+
const output = this.getPath(this.config.paths.temp);
|
|
140
|
+
const projectPath = this.getPath(process.cwd());
|
|
141
|
+
|
|
142
|
+
params.push(...[
|
|
143
|
+
`-DBASE_DIR=${projectPath}`,
|
|
144
|
+
`-DNATIVE_GLOB=${this.nativeGlob.join(';')}`,
|
|
145
|
+
`-DHEADER_GLOB=${this.headerGlob.join(';')}`,
|
|
146
|
+
`-DHEADER_DIR=${this.headerPathWithDepends}`,
|
|
147
|
+
`-DDEPENDS_CMAKE_PATHS=${this.pathsOfCmakeDepends}`,
|
|
148
|
+
`-DDEPENDS_CMAKE_NAMES=${this.nameOfCmakeDepends}`,
|
|
149
|
+
`-DBRIDGE_DIR=${output}/bridge`,
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
return params;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
cmake(name, isBuildSource, isBuildBridge) {
|
|
156
|
+
const params = this.getCmakeParams(name, isBuildSource, isBuildBridge);
|
|
157
|
+
|
|
158
|
+
const output = this.getPath(this.config.paths.temp);
|
|
51
159
|
const base = getBaseInfo(this.config.paths.base);
|
|
52
160
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
cMakeParentPath = cMakeParentPath.join('/');
|
|
161
|
+
const cMakeParentPath = getParentPath(this.config.paths.cmake);
|
|
162
|
+
|
|
56
163
|
const args = [
|
|
57
|
-
"run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${cMakeParentPath}:/cmake`, "--workdir",
|
|
58
|
-
"emcmake", "cmake", "/cmake", '-DCMAKE_BUILD_TYPE=Release',
|
|
59
|
-
`-DBASE_DIR=/live/${projectPath.relative}`,
|
|
60
|
-
`-DNATIVE_GLOB=${this.config.ext.source.map(ext => `${native}/*.${ext}`).join(';')}`,
|
|
61
|
-
`-DHEADER_GLOB=${this.config.ext.header.map(ext => `${header}/*.${ext}`).join(';')}`,
|
|
62
|
-
`-DHEADER_DIR=${header}`,
|
|
63
|
-
`-DCMAKE_INSTALL_PREFIX=/live/${output.relative}`, `-DBRIDGE_DIR=/live/${output.relative}/bridge`, `-DPROJECT_NAME=${name}`, ...params,
|
|
164
|
+
"run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${cMakeParentPath}:/cmake`, "--workdir", `${output}`, getDockerImage(),
|
|
165
|
+
"emcmake", "cmake", "/cmake", '-DCMAKE_BUILD_TYPE=Release', '-DBUILD_TYPE=STATIC', `-DCMAKE_INSTALL_PREFIX=${output}`, `-DPROJECT_NAME=${name}`, ...params,
|
|
64
166
|
];
|
|
65
167
|
const options = { cwd: this.config.paths.temp, stdio : 'pipe' };
|
|
66
168
|
execFileSync("docker", args, options);
|
|
@@ -68,14 +170,14 @@ class CppjsCompiler {
|
|
|
68
170
|
}
|
|
69
171
|
|
|
70
172
|
make() {
|
|
71
|
-
const output =
|
|
173
|
+
const output = this.getPath(this.config.paths.temp);
|
|
72
174
|
const base = getBaseInfo(this.config.paths.base);
|
|
73
175
|
|
|
74
176
|
let cMakeParentPath = this.config.paths.cmake.split('/');
|
|
75
177
|
cMakeParentPath.pop();
|
|
76
178
|
cMakeParentPath = cMakeParentPath.join('/');
|
|
77
179
|
const args = [
|
|
78
|
-
"run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${cMakeParentPath}:/cmake`, "--workdir",
|
|
180
|
+
"run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${cMakeParentPath}:/cmake`, "--workdir", `${output}`, getDockerImage(),
|
|
79
181
|
"emmake", "make", "install"
|
|
80
182
|
];
|
|
81
183
|
const options = { cwd: this.config.paths.temp, stdio : 'pipe' };
|
|
@@ -84,12 +186,11 @@ class CppjsCompiler {
|
|
|
84
186
|
}
|
|
85
187
|
|
|
86
188
|
cc() {
|
|
87
|
-
const
|
|
88
|
-
const output = getPathInfo(this.config.paths.temp, this.config.paths.base);
|
|
189
|
+
const output = this.getPath(this.config.paths.temp);
|
|
89
190
|
const base = getBaseInfo(this.config.paths.base);
|
|
90
191
|
const args = [
|
|
91
192
|
"run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${this.config.paths.cli}:/cli`, getDockerImage(),
|
|
92
|
-
"emcc", "-lembind", "-Wl,--whole-archive", ...this.libs, ...(this.options.cc || []), "-s", "WASM=1", "-s", "MODULARIZE=1", '-o',
|
|
193
|
+
"emcc", "-lembind", "-Wl,--whole-archive", ...this.libs, ...(this.options.cc || []), "-s", "WASM=1", "-s", "MODULARIZE=1", '-o', `${output}/${this.config.general.name}.js`, '--extern-post-js', '/cli/assets/extern-post.js'
|
|
93
194
|
];
|
|
94
195
|
const options = { cwd: this.config.paths.temp, stdio : 'pipe' };
|
|
95
196
|
execFileSync("docker", args, options);
|
package/src/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import createBridge from './functions/createBridge.js';
|
|
2
2
|
import findOrCreateInterfaceFile from './functions/findOrCreateInterfaceFile.js';
|
|
3
|
-
import createWasm from './functions/createWasm.js';
|
|
3
|
+
import createWasm, { getCmakeParams } from './functions/createWasm.js';
|
|
4
4
|
import getConfig from './utils/getConfig.js';
|
|
5
5
|
|
|
6
6
|
export default class CppjsCompiler {
|
|
7
|
-
constructor(
|
|
8
|
-
this.config = getConfig(
|
|
7
|
+
constructor() {
|
|
8
|
+
this.config = getConfig();
|
|
9
9
|
this.interfaces = [];
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -20,4 +20,8 @@ export default class CppjsCompiler {
|
|
|
20
20
|
createWasm(options) {
|
|
21
21
|
return createWasm(this, options);
|
|
22
22
|
}
|
|
23
|
+
|
|
24
|
+
getCmakeParams() {
|
|
25
|
+
return getCmakeParams(this);
|
|
26
|
+
}
|
|
23
27
|
}
|
|
@@ -2,7 +2,7 @@ import fs from 'fs';
|
|
|
2
2
|
import p from 'path';
|
|
3
3
|
|
|
4
4
|
export default function createTempDir(folder = 'a'+Math.random(), base = process.cwd()) {
|
|
5
|
-
const path = p.join(base,
|
|
5
|
+
const path = p.join(base, ".cppjs");
|
|
6
6
|
return createDir(folder, path);
|
|
7
7
|
}
|
|
8
8
|
|
package/src/utils/getConfig.js
CHANGED
|
@@ -3,16 +3,15 @@ import createTempDir, { createDir } from './createTempDir.js';
|
|
|
3
3
|
import findCMakeListsFile from './findCMakeListsFile.js';
|
|
4
4
|
import p from 'path';
|
|
5
5
|
import * as url from 'node:url';
|
|
6
|
-
import { createRequire } from 'module';
|
|
7
6
|
|
|
8
7
|
const __filename = url.fileURLToPath(import.meta.url);
|
|
9
8
|
const temp = __filename.split('/'); temp.pop(); temp.pop();
|
|
10
9
|
const __dirname = temp.join('/');
|
|
11
|
-
const require = createRequire(import.meta.url);
|
|
12
10
|
|
|
13
11
|
/**
|
|
14
12
|
* @typedef {Object} Config
|
|
15
13
|
* @property {string} general General
|
|
14
|
+
* @property {any[]} dependencies Dependencies
|
|
16
15
|
* @property {ConfigPaths} paths Paths
|
|
17
16
|
* @property {ConfigExtensions} ext Extensions
|
|
18
17
|
*/
|
|
@@ -42,44 +41,37 @@ const require = createRequire(import.meta.url);
|
|
|
42
41
|
* @property {string} name Project name
|
|
43
42
|
*/
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
let tempConfig = { general: {}, dependencies: [], paths: {}, ext: {}, export: {} };
|
|
45
|
+
await initDefaultConfigFile();
|
|
47
46
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
async function initDefaultConfigFile() {
|
|
48
|
+
let filePath;
|
|
49
|
+
['json', 'js', 'mjs', 'cjs', 'ts'].some(e => {
|
|
50
|
+
filePath = `${process.cwd()}/cppjs.config.${e}`;
|
|
51
|
+
if (!fs.existsSync(filePath)) filePath = null;
|
|
52
|
+
else return true;
|
|
53
|
+
});
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
} else {
|
|
56
|
-
filePath = `${process.cwd()}/.cppjs.config.json`;
|
|
57
|
-
if (!fs.existsSync(filePath)) filePath = null;
|
|
55
|
+
if (filePath) {
|
|
56
|
+
let file = await import(filePath);
|
|
57
|
+
if (file.default) file = file.default;
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (filePath) {
|
|
64
|
-
const file = require(filePath);
|
|
65
|
-
if (file.default) file = file.default;
|
|
66
|
-
if (typeof file === 'function') tempConfig = file();
|
|
67
|
-
else if (typeof file === "object") tempConfig = file;
|
|
68
|
-
}
|
|
69
|
-
} else if (typeof param === "object") {
|
|
70
|
-
tempConfig = param;
|
|
71
|
-
} else {
|
|
72
|
-
console.error('Error');
|
|
59
|
+
if (typeof file === 'function') tempConfig = file();
|
|
60
|
+
else if (typeof file === "object") tempConfig = file;
|
|
73
61
|
}
|
|
62
|
+
}
|
|
74
63
|
|
|
64
|
+
export default function getConfig() {
|
|
75
65
|
return fillConfig(forceToConfigSchema(tempConfig));
|
|
76
66
|
}
|
|
77
67
|
|
|
78
68
|
function forceToConfigSchema(tempConfig) {
|
|
79
69
|
const config = {
|
|
80
70
|
general: tempConfig && tempConfig.general ? tempConfig.general : {},
|
|
71
|
+
dependencies: tempConfig && tempConfig.dependencies ? tempConfig.dependencies : [],
|
|
81
72
|
paths: tempConfig && tempConfig.paths ? tempConfig.paths : {},
|
|
82
73
|
ext: tempConfig && tempConfig.ext ? tempConfig.ext : {},
|
|
74
|
+
export: tempConfig && tempConfig.export ? tempConfig.export : {},
|
|
83
75
|
};
|
|
84
76
|
return config;
|
|
85
77
|
}
|
|
@@ -97,13 +89,15 @@ function getAbsolutePath(projectPath, path) {
|
|
|
97
89
|
return p.resolve(path);
|
|
98
90
|
}
|
|
99
91
|
|
|
100
|
-
function fillConfig(tempConfig) {
|
|
92
|
+
function fillConfig(tempConfig, options = {}) {
|
|
101
93
|
const config = {
|
|
102
94
|
general: {},
|
|
95
|
+
dependencies: tempConfig.dependencies ? tempConfig.dependencies.map(d => fillConfig(forceToConfigSchema(d), { depend: true })) : [],
|
|
103
96
|
paths: {
|
|
104
97
|
project: getAbsolutePath(null, tempConfig.paths.project) || process.cwd(),
|
|
105
98
|
},
|
|
106
99
|
ext: {},
|
|
100
|
+
export: {},
|
|
107
101
|
};
|
|
108
102
|
|
|
109
103
|
if (!config.general.name) {
|
|
@@ -126,13 +120,18 @@ function fillConfig(tempConfig) {
|
|
|
126
120
|
config.paths.header = (tempConfig.paths.header || config.paths.native).map(p => getPath(p));
|
|
127
121
|
config.paths.bridge = (tempConfig.paths.bridge || [...config.paths.native, config.paths.temp]).map(p => getPath(p));
|
|
128
122
|
config.paths.output = getPath(tempConfig.paths.output) || config.paths.temp;
|
|
129
|
-
config.paths.cmake = getPath(tempConfig.paths.cmake || findCMakeListsFile(config.paths.project));
|
|
123
|
+
config.paths.cmake = options.depend ? findCMakeListsFile(config.paths.output) : getPath(tempConfig.paths.cmake || findCMakeListsFile(config.paths.project));
|
|
130
124
|
config.paths.cli = __dirname;
|
|
131
125
|
|
|
132
126
|
config.ext.header = tempConfig.ext.header || ['h', 'hpp', 'hxx', 'hh'];
|
|
133
127
|
config.ext.source = tempConfig.ext.source || ['c', 'cpp', 'cxx', 'cc'];
|
|
134
128
|
config.ext.module = tempConfig.ext.module || ['i'];
|
|
135
129
|
|
|
130
|
+
config.export.type = tempConfig.export.type || 'cmake';
|
|
131
|
+
config.export.header = tempConfig.export.header || 'include';
|
|
132
|
+
config.export.libPath = getPath(tempConfig.export.libPath || 'lib');
|
|
133
|
+
config.export.libName = tempConfig.export.libName || [`lib${config.general.name}.a`];
|
|
134
|
+
|
|
136
135
|
createDir('interface', config.paths.temp);
|
|
137
136
|
createDir('bridge', config.paths.temp);
|
|
138
137
|
|