1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

Emscripten: Add platform modules

Issue: #18423
This commit is contained in:
Quinn Powell
2025-05-16 14:00:41 -04:00
parent 7b4a7a91eb
commit 96d9b94a98
14 changed files with 99 additions and 0 deletions

View File

@@ -704,3 +704,22 @@ the next rules to make device + simulator configuration work:
- Use :command:`find_package` only for libraries installed with
:variable:`CMAKE_IOS_INSTALL_COMBINED` feature
.. _`Cross Compiling for Emscripten`:
Cross Compiling for Emscripten
------------------------------
.. versionadded:: 4.2
A toolchain file may configure cross-compiling for `Emscripten`_ by
setting the :variable:`CMAKE_SYSTEM_NAME` variable to ``Emscripten``.
For example, a toolchain file might contain:
.. code-block:: cmake
set(CMAKE_SYSTEM_NAME Emscripten)
set(CMAKE_C_COMPILER /path/to/emcc)
set(CMAKE_CXX_COMPILER /path/to/em++)
.. _`Emscripten`: https://emscripten.org/

View File

@@ -0,0 +1,5 @@
emscripten-platform
-------------------
* CMake now supports :ref:`Cross Compiling for Emscripten` with simple
toolchain files.

View File

@@ -219,6 +219,11 @@ else()
list(PREPEND _CMAKE_LINKER_NAMES "armlink")
endif()
if(EMSCRIPTEN)
list(PREPEND _CMAKE_AR_NAMES "emar")
list(PREPEND _CMAKE_RANLIB_NAMES "emranlib")
endif()
list(APPEND _CMAKE_TOOL_VARS AR RANLIB STRIP LINKER NM OBJDUMP OBJCOPY READELF DLLTOOL ADDR2LINE TAPI)
endif()

View File

@@ -0,0 +1,3 @@
include(Platform/Emscripten-Clang)
__emscripten_clang(C)

View File

@@ -0,0 +1,3 @@
include(Platform/Emscripten-Clang)
__emscripten_clang(CXX)

View File

@@ -0,0 +1,17 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
include_guard()
macro(__emscripten_clang lang)
set(CMAKE_SHARED_LIBRARY_SONAME_${lang}_FLAG "-Wl,-soname,")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-sSIDE_MODULE")
set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_LIBRARIES 1)
set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_OBJECTS 1)
set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_INCLUDES 1)
set(CMAKE_${lang}_COMPILE_OBJECT
"<CMAKE_${lang}_COMPILER> -c <SOURCE> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -fPIC")
set(CMAKE_${lang}_LINK_EXECUTABLE
"<CMAKE_${lang}_COMPILER> <FLAGS> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> -sMAIN_MODULE")
endmacro()

View File

@@ -0,0 +1,2 @@
set(EMSCRIPTEN 1)
set(UNIX 1)

View File

@@ -0,0 +1,6 @@
set(CMAKE_SHARED_LIBRARY_LINK_C_WITH_RUNTIME_PATH ON)
set(CMAKE_SHARED_LIBRARY_SUFFIX ".wasm")
set(CMAKE_EXECUTABLE_SUFFIX ".js")
set(CMAKE_DL_LIBS "")

View File

@@ -0,0 +1,4 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
include(Platform/Linker/Emscripten-LLD-C)

View File

@@ -0,0 +1,4 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
include(Platform/Linker/Emscripten-LLD-CXX)

View File

@@ -0,0 +1,6 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
include(Platform/Linker/Emscripten-LLD)
__emscripten_linker_lld(C)

View File

@@ -0,0 +1,6 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
include(Platform/Linker/Emscripten-LLD)
__emscripten_linker_lld(CXX)

View File

@@ -0,0 +1,11 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
# This module is shared by multiple languages; use include blocker.
include_guard()
macro(__emscripten_linker_lld lang)
set(CMAKE_${lang}_LINK_LIBRARY_USING_WHOLE_ARCHIVE "-Wl,--whole-archive" "<LINK_ITEM>" "-Wl,--no-whole-archive")
set(CMAKE_${lang}_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED TRUE)
set(CMAKE_${lang}_LINK_LIBRARY_WHOLE_ARCHIVE_ATTRIBUTES LIBRARY_TYPE=STATIC DEDUPLICATION=YES OVERRIDE=DEFAULT)
endmacro()

View File

@@ -1386,6 +1386,14 @@ void cmCoreTryCompile::FindOutputFile(std::string const& targetName)
return;
}
if (cmHasLiteralSuffix(outputFileLocation, ".js")) {
std::string wasmOutputLocation = cmStrCat(
outputFileLocation.substr(0, outputFileLocation.length() - 3), ".wasm");
if (cmSystemTools::FileExists(wasmOutputLocation)) {
outputFileLocation = wasmOutputLocation;
}
}
this->OutputFile = cmSystemTools::CollapseFullPath(outputFileLocation);
}