added wasm32-unknown-wasi target support via the wasi-sdk

This commit is contained in:
Jorijn van der Graaf 2024-12-29 03:13:14 +01:00
commit 2f2ea03546
2 changed files with 38 additions and 8 deletions

View file

@ -160,31 +160,38 @@ void Project::Build(Configuration config, std::string outputDir) {
}else{ }else{
pcmDir = config.buildDir; pcmDir = config.buildDir;
} }
std::string clangDir;
if(config.target == "wasm32-unknown-wasi"){
clangDir = "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot";
name+=".wasm";
} else{
clangDir = "clang++";
}
for(const std::string& moduleFile : config.moduleFiles){ for(const std::string& moduleFile : config.moduleFiles){
system(std::format("clang++ -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}/{}.pcm", config.standard, moduleFile, pcmDir, pcmDir, moduleFile).c_str()); system(std::format("{} -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}/{}.pcm {}", clangDir, config.standard, moduleFile, pcmDir, pcmDir, moduleFile, target).c_str());
} }
std::vector<std::thread> threads = std::vector<std::thread>(config.moduleFiles.size() + config.sourceFiles.size()); std::vector<std::thread> threads = std::vector<std::thread>(config.moduleFiles.size() + config.sourceFiles.size());
std::string files; std::string files;
for(std::int_fast32_t i = 0; i < config.moduleFiles.size(); i++) { for(std::int_fast32_t i = 0; i < config.moduleFiles.size(); i++) {
files+=std::format("{}/{}.o ",config.buildDir, config.moduleFiles[i]); files+=std::format("{}/{}.o ",config.buildDir, config.moduleFiles[i]);
threads[i] = std::thread([i, config, pcmDir, target](){ threads[i] = std::thread([i, config, pcmDir, target, clangDir](){
system(std::format("clang++ -std={} {}/{}.pcm -fprebuilt-module-path={} -c -O{} -o {}/{}.o {}", config.standard, pcmDir, config.moduleFiles[i], pcmDir, config.optimizationLevel, config.buildDir, config.moduleFiles[i], target).c_str()); system(std::format("{} -std={} {}/{}.pcm -fprebuilt-module-path={} -c -O{} -o {}/{}.o {}", clangDir, config.standard, pcmDir, config.moduleFiles[i], pcmDir, config.optimizationLevel, config.buildDir, config.moduleFiles[i], target).c_str());
}); });
} }
for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) { for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) {
files+=std::format("{}/{}_source.o ",config.buildDir, config.sourceFiles[i]); files+=std::format("{}/{}_source.o ",config.buildDir, config.sourceFiles[i]);
threads[config.moduleFiles.size()+i] = std::thread([i, config, pcmDir, target](){ threads[config.moduleFiles.size()+i] = std::thread([i, config, pcmDir, target, clangDir](){
system(std::format("clang++ -std={} {}.cpp -fprebuilt-module-path={} -c -O{} -o {}/{}_source.o {}", config.standard, config.sourceFiles[i], pcmDir, config.optimizationLevel, config.buildDir, config.sourceFiles[i], target).c_str()); system(std::format("{} -std={} {}.cpp -fprebuilt-module-path={} -c -O{} -o {}/{}_source.o {}", clangDir, config.standard, config.sourceFiles[i], pcmDir, config.optimizationLevel, config.buildDir, config.sourceFiles[i], target).c_str());
}); });
} }
for(std::thread& thread : threads){ for(std::thread& thread : threads){
thread.join(); thread.join();
} }
if(config.type == "executable"){ if(config.type == "executable"){
system(std::format("clang++ {}-O{} -o {}/{} {}", files, config.optimizationLevel, outputDir, name, target).c_str()); system(std::format("{} {}-O{} -o {}/{} {}", clangDir, files, config.optimizationLevel, outputDir, name, target).c_str());
} else if(config.type == "library"){ } else if(config.type == "library"){
system(std::format("ar r {}/{}.a {}", outputDir, name, files, target).c_str()); system(std::format("ar r {}/{}.a {}", clangDir, outputDir, name, files, target).c_str());
} else if(config.type == "shared-library"){ } else if(config.type == "shared-library"){
system(std::format("ar r {}/{}.so {} -shared", outputDir, name, files, target).c_str()); system(std::format("ar r {}/{}.so {} -shared", outputDir, name, files, target).c_str());
} }

View file

@ -59,7 +59,7 @@ Save and close, then run ``crafter-build -c debug``. Now you can run the ``hello
## CLI arguments ## CLI arguments
``--help`` Displays this help message. ``--help`` Displays a help message.
``-c`` The name of the configuration to build. ``-c`` The name of the configuration to build.
@ -67,3 +67,26 @@ Save and close, then run ``crafter-build -c debug``. Now you can run the ``hello
``-o`` Overrides the output folder. ``-o`` Overrides the output folder.
## configuration properties
``name`` Name of the configuration.
``standard`` C++ standard that this configuration uses, please refer to the [relevant clang documentation](https://clang.llvm.org/cxx_status.html)
``source_files`` All source files of the project ``.cpp`` extension is assumed.
``module_files`` All C++ 20 module files of the project ``.cppm`` extension is assumed.
``build_dir`` The directory where intermediate files are stored.
``output_dir`` The directory where the output files will be placed.
``extends`` An array of configuration names that this configuration extends, later elements in the array take priority over previous ones.
``optimization_level`` Please refer to the [relevant clang documentation](https://clang.llvm.org/docs/CommandGuide/clang.html#code-generation-options).
``target`` Clang triplet this configuration uses, please refer to the [relevant clang documentation](https://clang.llvm.org/docs/CrossCompilation.html#target-triple).
Note: the WASI SDK needs to be installed to use the ``wasm32-unknown-wasi`` to compile to webassmbly, and ``$WASI_SDK_PATH`` needs to be set, please refer to the [wasi-sdk installation guide](https://github.com/WebAssembly/wasi-sdk?tab=readme-ov-file#install).