Skip to content

Commit cfecdac

Browse files
committed
Store CMAKE_AR and CMAKE_RANLIB in CMake CACHE so that the values persist to user CMake toolchain files. Also CACHE the variable Emscripten, in the hope of that fixing the visibility issue with variable. Some users have reported that in their CMakeLists.txt files the variable EMSCRIPTEN is not always visible, even though in the Emscripten cmake unit tests it always works. Fixes #2288. Thanks mhenschel!
1 parent ce58885 commit cfecdac

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

cmake/Platform/Emscripten.cmake

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ if ("${CMAKE_CXX_COMPILER}" STREQUAL "")
6666
endif()
6767

6868
if ("${CMAKE_AR}" STREQUAL "")
69-
set(CMAKE_AR "${EMSCRIPTEN_ROOT_PATH}/emar${EMCC_SUFFIX}")
69+
set(CMAKE_AR "${EMSCRIPTEN_ROOT_PATH}/emar${EMCC_SUFFIX}" CACHE FILEPATH "Emscripten ar")
7070
endif()
7171

7272
if ("${CMAKE_RANLIB}" STREQUAL "")
73-
set(CMAKE_RANLIB "${EMSCRIPTEN_ROOT_PATH}/emranlib${EMCC_SUFFIX}")
73+
set(CMAKE_RANLIB "${EMSCRIPTEN_ROOT_PATH}/emranlib${EMCC_SUFFIX}" CACHE FILEPATH "Emscripten ranlib")
7474
endif()
7575

7676
# Don't do compiler autodetection, since we are cross-compiling.
@@ -113,8 +113,7 @@ set(CMAKE_CXX_ARCHIVE_APPEND "${CMAKE_AR} r <TARGET> ${CMAKE_START_TEMP_FILE} <L
113113
set(CMAKE_C_ARCHIVE_APPEND "${CMAKE_AR} r <TARGET> ${CMAKE_START_TEMP_FILE} <LINK_FLAGS> <OBJECTS>${CMAKE_END_TEMP_FILE}")
114114

115115
# Set a global EMSCRIPTEN variable that can be used in client CMakeLists.txt to detect when building using Emscripten.
116-
# There seems to be some kind of bug with CMake, so you might need to define this manually on the command line with "-DEMSCRIPTEN=1".
117-
set(EMSCRIPTEN 1)
116+
set(EMSCRIPTEN 1 CACHE BOOL "If true, we are targeting Emscripten output.")
118117

119118
# We are cross-compiling, so unset the common CMake variables that represent the target platform. Leave UNIX define enabled, since Emscripten
120119
# mimics a Linux environment.

tests/cmake/target_html/CMakeLists.txt

+32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ else() # Either MinSizeRel, RelWithDebInfo or Release, all which run with optimi
1010
SET(linkFlags "-O2")
1111
endif()
1212

13+
if (NOT CMAKE_AR OR NOT EXISTS "${CMAKE_AR}")
14+
message(FATAL_ERROR "CMAKE_AR='${CMAKE_AR}' does not exist for Emscripten toolchain!")
15+
endif()
16+
17+
if (NOT CMAKE_RANLIB OR NOT EXISTS "${CMAKE_RANLIB}")
18+
message(FATAL_ERROR "CMAKE_RANLIB='${CMAKE_RANLIB}' does not exist for Emscripten toolchain!")
19+
endif()
20+
21+
if (NOT CMAKE_C_COMPILER OR NOT EXISTS "${CMAKE_C_COMPILER}")
22+
message(FATAL_ERROR "CMAKE_C_COMPILER='${CMAKE_C_COMPILER}' does not exist for Emscripten toolchain!")
23+
endif()
24+
25+
if (NOT CMAKE_CXX_COMPILER OR NOT EXISTS "${CMAKE_CXX_COMPILER}")
26+
message(FATAL_ERROR "CMAKE_CXX_COMPILER='${CMAKE_CXX_COMPILER}' does not exist for Emscripten toolchain!")
27+
endif()
28+
29+
if (WIN32)
30+
message(FATAL_ERROR "WIN32 should not be defined when cross-compiling!")
31+
endif()
32+
33+
if (APPLE)
34+
message(FATAL_ERROR "APPLE should not be defined when cross-compiling!")
35+
endif()
36+
37+
if (NOT EMSCRIPTEN)
38+
message(FATAL_ERROR "EMSCRIPTEN should be defined when cross-compiling!")
39+
endif()
40+
41+
if (NOT CMAKE_C_SIZEOF_DATA_PTR)
42+
message(FATAL_ERROR "CMAKE_C_SIZEOF_DATA_PTR was not defined!")
43+
endif()
44+
1345
SET(CMAKE_EXECUTABLE_SUFFIX ".html")
1446

1547
add_executable(hello_world_gles ${sourceFiles})

0 commit comments

Comments
 (0)