Skip to content

Commit 851cedc

Browse files
authored
cmake: Always check for EMSCRIPTEN_VERSION (#18027)
Even when running with EMSCRIPTEN_FORCE_COMPILERS it can be useful to have access to EMSCRIPTEN_VERSION.
1 parent deb5d86 commit 851cedc

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

cmake/Modules/Platform/Emscripten.cmake

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,21 @@ set(CMAKE_CXX_COMPILER_AR "${CMAKE_AR}" CACHE FILEPATH "Emscripten ar")
9696
set(CMAKE_C_COMPILER_RANLIB "${CMAKE_RANLIB}" CACHE FILEPATH "Emscripten ranlib")
9797
set(CMAKE_CXX_COMPILER_RANLIB "${CMAKE_RANLIB}" CACHE FILEPATH "Emscripten ranlib")
9898

99-
# Don't allow CMake to autodetect the compiler, since it does not understand
99+
# Capture the Emscripten version to EMSCRIPTEN_VERSION variable.
100+
if (NOT EMSCRIPTEN_VERSION)
101+
execute_process(COMMAND "${CMAKE_C_COMPILER}" "-v" RESULT_VARIABLE _cmake_compiler_result ERROR_VARIABLE _cmake_compiler_output OUTPUT_QUIET)
102+
if (NOT _cmake_compiler_result EQUAL 0)
103+
message(FATAL_ERROR "Failed to fetch Emscripten version information with command \"'${CMAKE_C_COMPILER}' -v\"! Process returned with error code ${_cmake_compiler_result}.")
104+
endif()
105+
string(REGEX MATCH "emcc \\(.*\\) ([0-9\\.]+)" _dummy_unused "${_cmake_compiler_output}")
106+
if (NOT CMAKE_MATCH_1)
107+
message(FATAL_ERROR "Failed to regex parse Emscripten compiler version from version string: ${_cmake_compiler_output}")
108+
endif()
109+
110+
set(EMSCRIPTEN_VERSION "${CMAKE_MATCH_1}")
111+
endif()
112+
113+
# Don't allow CMake to autodetect the compiler, since this is quite slow with
100114
# Emscripten.
101115
# Pass -DEMSCRIPTEN_FORCE_COMPILERS=OFF to disable (sensible mostly only for
102116
# testing/debugging purposes).
@@ -128,20 +142,6 @@ if (EMSCRIPTEN_FORCE_COMPILERS)
128142
endif()
129143
endif()
130144

131-
# Capture the Emscripten version to EMSCRIPTEN_VERSION variable.
132-
if (NOT EMSCRIPTEN_VERSION)
133-
execute_process(COMMAND "${CMAKE_C_COMPILER}" "-v" RESULT_VARIABLE _cmake_compiler_result ERROR_VARIABLE _cmake_compiler_output OUTPUT_QUIET)
134-
if (NOT _cmake_compiler_result EQUAL 0)
135-
message(FATAL_ERROR "Failed to fetch Emscripten version information with command \"'${CMAKE_C_COMPILER}' -v\"! Process returned with error code ${_cmake_compiler_result}.")
136-
endif()
137-
string(REGEX MATCH "emcc \\(.*\\) ([0-9\\.]+)" _dummy_unused "${_cmake_compiler_output}")
138-
if (NOT CMAKE_MATCH_1)
139-
message(FATAL_ERROR "Failed to regex parse Emscripten compiler version from version string: ${_cmake_compiler_output}")
140-
endif()
141-
142-
set(EMSCRIPTEN_VERSION "${CMAKE_MATCH_1}")
143-
endif()
144-
145145
set(CMAKE_C_COMPILER_ID_RUN TRUE)
146146
set(CMAKE_C_COMPILER_FORCED TRUE)
147147
set(CMAKE_C_COMPILER_WORKS TRUE)

test/test_other.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ def test_cmake_pthreads(self):
798798
# Tests that the CMake variable EMSCRIPTEN_VERSION is properly provided to user CMake scripts
799799
def test_cmake_emscripten_version(self):
800800
self.run_process([EMCMAKE, 'cmake', test_file('cmake/emscripten_version')])
801+
self.clear()
802+
self.run_process([EMCMAKE, 'cmake', test_file('cmake/emscripten_version'), '-DEMSCRIPTEN_FORCE_COMPILERS=OFF'])
801803

802804
def test_cmake_emscripten_system_processor(self):
803805
cmake_dir = test_file('cmake/emscripten_system_processor')

0 commit comments

Comments
 (0)