Skip to content

Commit 9c2231c

Browse files
committed
Target POSIX API by default when building with Emscripten.
This allows to enable more POSIX APIs when building with Emscripten by default. Users may switch to WASI API by defining BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI config macro. Originally implemented in and based on #233.
2 parents 15249ba + 931cc9f commit 9c2231c

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(BOOST_FILESYSTEM_DISABLE_STATX OFF CACHE BOOL "Disable usage of statx API in
1919
set(BOOST_FILESYSTEM_DISABLE_GETRANDOM OFF CACHE BOOL "Disable usage of getrandom API in Boost.Filesystem")
2020
set(BOOST_FILESYSTEM_DISABLE_ARC4RANDOM OFF CACHE BOOL "Disable usage of arc4random API in Boost.Filesystem")
2121
set(BOOST_FILESYSTEM_DISABLE_BCRYPT OFF CACHE BOOL "Disable usage of BCrypt API in Boost.Filesystem")
22+
set(BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI OFF CACHE BOOL "Use WASI under Emscripten in Boost.Filesystem")
2223

2324
# Note: We can't use the Boost::library targets in the configure checks as they may not yet be included
2425
# by the superproject when this CMakeLists.txt is included. We also don't want to hardcode include paths
@@ -131,6 +132,9 @@ endif()
131132
if(BOOST_FILESYSTEM_DISABLE_BCRYPT)
132133
target_compile_definitions(boost_filesystem PRIVATE BOOST_FILESYSTEM_DISABLE_BCRYPT)
133134
endif()
135+
if(BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI)
136+
target_compile_definitions(boost_filesystem PRIVATE BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI)
137+
endif()
134138

135139
if(BOOST_FILESYSTEM_HAS_STAT_ST_BLKSIZE)
136140
target_compile_definitions(boost_filesystem PRIVATE BOOST_FILESYSTEM_HAS_STAT_ST_BLKSIZE)

doc/index.htm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,13 @@ <h2><a name="Macros">Macros</a></h2>
312312
<td valign="top">Not defined. BCrypt API presence detected at library build time.</td>
313313
<td valign="top">Boost.Filesystem library does not use the BCrypt API on Windows. Has no effect on other platforms.</td>
314314
</tr>
315+
<tr>
316+
<td valign="top"><code>BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI</code></td>
317+
<td valign="top">Not defined. When building with <a href="https://emscripten.org/">Emscripten</a>, target POSIX API.</td>
318+
<td valign="top">Boost.Filesystem library targets WASI API instead of POSIX when building with <a href="https://emscripten.org/">Emscripten</a>. Has no effect on other platforms.</td>
319+
</tr>
315320
</table>
316-
<p>User-defined BOOST_POSIX_API and BOOST_WINDOWS_API macros are no longer
321+
<p>User-defined <code>BOOST_POSIX_API</code> and <code>BOOST_WINDOWS_API</code> macros are no longer
317322
supported.</p>
318323
<h2><a name="Building">Building</a> the object-library</h2>
319324
<p>The object-library will be built automatically if you are using the Boost
@@ -330,7 +335,7 @@ <h2><a name="Building">Building</a> the object-library</h2>
330335
operating systems or embedded operating systems, support POSIX compatible file
331336
systems and so will work with the Filesystem Library.</p>
332337
<p>The object-library can be built for static or dynamic (shared/dll) linking.
333-
This is controlled by the BOOST_ALL_DYN_LINK or BOOST_FILESYSTEM_DYN_LINK
338+
This is controlled by the <code>BOOST_ALL_DYN_LINK</code> or <code>BOOST_FILESYSTEM_DYN_LINK</code>
334339
macros. See the <a href="http://www.boost.org/development/separate_compilation.html">Separate
335340
Compilation</a> page for a description of the techniques used.</p>
336341
<h3>Note for <a name="Cygwin">Cygwin</a> users</h3>
@@ -488,7 +493,7 @@ <h3>Version 1</h3>
488493
<hr>
489494

490495
<p>&copy; Copyright Beman Dawes, 2002-2005</p>
491-
<p>&copy; Copyright Andrey Semashev, 2021</p>
496+
<p>&copy; Copyright Andrey Semashev, 2021-2022</p>
492497
<p> Use, modification, and distribution are subject to the Boost Software
493498
License, Version 1.0. See <a href="http://www.boost.org/LICENSE_1_0.txt">
494499
www.boost.org/LICENSE_1_0.txt</a></p>

doc/release_history.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
<h2>1.80.0</h2>
4343
<ul>
44+
<li>When building with <a href="https://emscripten.org/">Emscripten</a>, target POSIX API by default. Users can define <code>BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI</code> to target WASI API instead. (<a href="https://github.com/boostorg/filesystem/pull/233">PR#233</a>)</li>
4445
<li>On Windows, added a fallback implementation for querying file attributes in case if the file cannot be opened with <code>ERROR_ACCESS_DENIED</code> error. This may allow <code>status</code> and <code>symlink_status</code> to succeed for system files and directories that are not reparse points or symlinks. (<a href="https://github.com/boostorg/filesystem/issues/234">#234</a>)</li>
4546
<li>On Windows, added a workaround for <code>status</code> and <code>symlink_status</code> reporting that files do not exist on FAT/exFAT filesystems. (<a href="https://github.com/boostorg/filesystem/issues/236">#236</a>)</li>
4647
</ul>

src/operations.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@
3838
#endif
3939
#include <cerrno>
4040

41+
// Default to POSIX under Emscripten. If BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI is set, use WASI instead.
42+
#if defined(__wasm) && (!defined(__EMSCRIPTEN__) || defined(BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI))
43+
#define BOOST_FILESYSTEM_USE_WASI
44+
#endif
45+
4146
#ifdef BOOST_POSIX_API
4247

4348
#include <sys/types.h>
4449
#include <sys/stat.h>
4550

46-
#if defined(__wasm)
51+
#if defined(BOOST_FILESYSTEM_USE_WASI)
4752
// WASI does not have statfs or statvfs.
4853
#elif !defined(__APPLE__) && \
4954
(!defined(__OpenBSD__) || BOOST_OS_BSD_OPEN >= BOOST_VERSION_NUMBER(4, 4, 0)) && \
@@ -2220,7 +2225,7 @@ bool copy_file(path const& from, path const& to, unsigned int options, error_cod
22202225
}
22212226

22222227
mode_t to_mode = from_mode;
2223-
#if !defined(__wasm)
2228+
#if !defined(BOOST_FILESYSTEM_USE_WASI)
22242229
// Enable writing for the newly created files. Having write permission set is important e.g. for NFS,
22252230
// which checks the file permission on the server, even if the client's file descriptor supports writing.
22262231
to_mode |= S_IWUSR;
@@ -2339,7 +2344,7 @@ bool copy_file(path const& from, path const& to, unsigned int options, error_cod
23392344
if (BOOST_UNLIKELY(err != 0))
23402345
goto fail; // err already contains the error code
23412346

2342-
#if !defined(__wasm)
2347+
#if !defined(BOOST_FILESYSTEM_USE_WASI)
23432348
// If we created a new file with an explicitly added S_IWUSR permission,
23442349
// we may need to update its mode bits to match the source file.
23452350
if (to_mode != from_mode)
@@ -2775,7 +2780,7 @@ void create_symlink(path const& to, path const& from, error_code* ec)
27752780
BOOST_FILESYSTEM_DECL
27762781
path current_path(error_code* ec)
27772782
{
2778-
#if defined(UNDER_CE) || defined(__wasm)
2783+
#if defined(UNDER_CE) || defined(BOOST_FILESYSTEM_USE_WASI)
27792784
// Windows CE has no current directory, so everything's relative to the root of the directory tree.
27802785
// WASI also does not support current path.
27812786
emit_error(BOOST_ERROR_NOT_SUPPORTED, ec, "boost::filesystem::current_path");
@@ -2845,7 +2850,7 @@ path current_path(error_code* ec)
28452850
BOOST_FILESYSTEM_DECL
28462851
void current_path(path const& p, system::error_code* ec)
28472852
{
2848-
#if defined(UNDER_CE) || defined(__wasm)
2853+
#if defined(UNDER_CE) || defined(BOOST_FILESYSTEM_USE_WASI)
28492854
emit_error(BOOST_ERROR_NOT_SUPPORTED, p, ec, "boost::filesystem::current_path");
28502855
#else
28512856
error(!BOOST_SET_CURRENT_DIRECTORY(p.c_str()) ? BOOST_ERRNO : 0, p, ec, "boost::filesystem::current_path");
@@ -3349,7 +3354,7 @@ void permissions(path const& p, perms prms, system::error_code* ec)
33493354
if ((prms & add_perms) && (prms & remove_perms)) // precondition failed
33503355
return;
33513356

3352-
#if defined(__wasm)
3357+
#if defined(BOOST_FILESYSTEM_USE_WASI)
33533358
emit_error(BOOST_ERROR_NOT_SUPPORTED, p, ec, "boost::filesystem::permissions");
33543359
#elif defined(BOOST_POSIX_API)
33553360
error_code local_ec;
@@ -3613,7 +3618,7 @@ space_info space(path const& p, error_code* ec)
36133618
if (ec)
36143619
ec->clear();
36153620

3616-
#if defined(__wasm)
3621+
#if defined(BOOST_FILESYSTEM_USE_WASI)
36173622

36183623
emit_error(BOOST_ERROR_NOT_SUPPORTED, p, ec, "boost::filesystem::space");
36193624

0 commit comments

Comments
 (0)