Emscripten.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #
  2. # Copyright (c) 2008-2020 the Urho3D project.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  23. get_property (IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
  24. if (IN_TRY_COMPILE)
  25. foreach (VAR $ENV{VARS})
  26. set (${VAR} $ENV{${VAR}})
  27. endforeach ()
  28. else ()
  29. # Prevent critical variables from changing after the initial configuration
  30. if (CMAKE_CROSSCOMPILING)
  31. set (SAVED_EMSCRIPTEN_ROOT_PATH ${EMSCRIPTEN_ROOT_PATH} CACHE INTERNAL "Initial value for EMSCRIPTEN_ROOT_PATH")
  32. set (SAVED_EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_SYSROOT} CACHE INTERNAL "Initial value for EMSCRIPTEN_SYSROOT")
  33. # Save the initial values of CC and CXX environment variables
  34. set (SAVED_CC $ENV{CC} CACHE INTERNAL "Initial value for CC")
  35. set (SAVED_CXX $ENV{CXX} CACHE INTERNAL "Initial value for CXX")
  36. return ()
  37. elseif ((SAVED_EMSCRIPTEN_ROOT_PATH AND NOT SAVED_EMSCRIPTEN_ROOT_PATH STREQUAL EMSCRIPTEN_ROOT_PATH) OR (SAVED_EMSCRIPTEN_SYSROOT AND NOT SAVED_EMSCRIPTEN_SYSROOT STREQUAL EMSCRIPTEN_SYSROOT))
  38. set (EMSCRIPTEN_ROOT_PATH ${SAVED_EMSCRIPTEN_ROOT_PATH} CACHE STRING "Root path to Emscripten cross-compiler tools (Emscripten only)" FORCE)
  39. set (EMSCRIPTEN_SYSROOT ${SAVED_EMSCRIPTEN_SYSROOT} CACHE PATH "Path to Emscripten system root (Emscripten only)" FORCE)
  40. message (FATAL_ERROR "EMSCRIPTEN_ROOT_PATH and EMSCRIPTEN_SYSROOT cannot be changed after the initial configuration/generation. "
  41. "If you wish to change that then the build tree would have to be regenerated from scratch. Auto reverting to its initial value.")
  42. endif ()
  43. endif ()
  44. # Reference toolchain variable to suppress "unused variable" warning
  45. if (CMAKE_TOOLCHAIN_FILE)
  46. mark_as_advanced (CMAKE_TOOLCHAIN_FILE)
  47. endif ()
  48. # This one is important
  49. set (CMAKE_SYSTEM_NAME Linux)
  50. # This one not so much
  51. set (CMAKE_SYSTEM_VERSION 1)
  52. # System root
  53. if (NOT IN_TRY_COMPILE)
  54. if (NOT SAVED_EMSCRIPTEN_ROOT_PATH)
  55. if (NOT EMSCRIPTEN_ROOT_PATH)
  56. if (DEFINED ENV{EMSCRIPTEN_ROOT_PATH})
  57. file (TO_CMAKE_PATH $ENV{EMSCRIPTEN_ROOT_PATH} EMSCRIPTEN_ROOT_PATH)
  58. elseif (DEFINED ENV{EM_CONFIG})
  59. # Attempt to auto detect the Emscripten root path from the config file
  60. file (STRINGS $ENV{EM_CONFIG} EMSCRIPTEN_ROOT_PATH REGEX "^EMSCRIPTEN_ROOT = '.*'$")
  61. if (EMSCRIPTEN_ROOT_PATH)
  62. string (REGEX REPLACE "^EMSCRIPTEN_ROOT = '(.*)'$" \\1 EMSCRIPTEN_ROOT_PATH ${EMSCRIPTEN_ROOT_PATH})
  63. else ()
  64. # Newer config file requires Python to actually evaluate it, basically, `cat $EM_CONFIG <(echo 'print(EMSCRIPTEN_ROOT)') |python -`
  65. file (STRINGS $ENV{EM_CONFIG} EM_CONFIG NEWLINE_CONSUME)
  66. execute_process (COMMAND ${CMAKE_COMMAND} -E echo "${EM_CONFIG}\nprint(EMSCRIPTEN_ROOT)"
  67. COMMAND python - OUTPUT_VARIABLE EMSCRIPTEN_ROOT_PATH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  68. endif ()
  69. endif ()
  70. endif ()
  71. set (EMSCRIPTEN_ROOT_PATH ${EMSCRIPTEN_ROOT_PATH} CACHE STRING "Root path to Emscripten cross-compiler tools (Emscripten only)")
  72. if (NOT EXISTS ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT})
  73. message (FATAL_ERROR "Could not find Emscripten cross compilation tool. "
  74. "Use EMSCRIPTEN_ROOT_PATH environment variable or build option to specify the location of the toolchain. "
  75. "Or use the canonical EMSCRIPTEN environment variable by calling emsdk_env script.")
  76. endif ()
  77. endif ()
  78. if (NOT SAVED_EMSCRIPTEN_SYSROOT)
  79. if (NOT EMSCRIPTEN_SYSROOT)
  80. if (DEFINED ENV{EMSCRIPTEN_SYSROOT})
  81. file (TO_CMAKE_PATH $ENV{EMSCRIPTEN_SYSROOT} EMSCRIPTEN_SYSROOT)
  82. else ()
  83. set (EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_ROOT_PATH}/system)
  84. endif ()
  85. endif ()
  86. set (EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_SYSROOT} CACHE PATH "Path to Emscripten system root (Emscripten only)")
  87. if (NOT EXISTS ${EMSCRIPTEN_SYSROOT})
  88. message (FATAL_ERROR "Could not find Emscripten system root. "
  89. "Use EMSCRIPTEN_SYSROOT environment variable or build option to specify the location of system root.")
  90. endif ()
  91. endif ()
  92. endif ()
  93. set (CMAKE_SYSROOT ${EMSCRIPTEN_SYSROOT})
  94. # Only search libraries and headers in sysroot
  95. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  96. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  97. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  98. # Cross compiler tools
  99. if (CMAKE_HOST_WIN32)
  100. set (TOOL_EXT .bat)
  101. endif ()
  102. if (NOT EMSCRIPTEN_EMCC_VERSION)
  103. execute_process (COMMAND ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT} --version RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE EMSCRIPTEN_EMCC_VERSION ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  104. if (EXIT_CODE EQUAL 0)
  105. message (${EMSCRIPTEN_EMCC_VERSION})
  106. string (REGEX MATCH "[^ .]+\\.[^.]+\\.[^ ]+" EMSCRIPTEN_EMCC_VERSION "${EMSCRIPTEN_EMCC_VERSION}")
  107. else ()
  108. message (FATAL_ERROR "Could not determine the emcc version. Make sure you have installed and activated the Emscripten SDK correctly.")
  109. endif ()
  110. set (EMSCRIPTEN_EMCC_VERSION ${EMSCRIPTEN_EMCC_VERSION} CACHE INTERNAL "emcc version being used in this build tree")
  111. if (EMSCRIPTEN_EMCC_VERSION VERSION_LESS 1.39.0)
  112. message (FATAL_ERROR "Emscripten SDK 1.39.0 or later is required.")
  113. endif ()
  114. endif ()
  115. if (NOT EMSCRIPTEN_COMPILER_PATH)
  116. set (EMSCRIPTEN_COMPILER_PATH ${EMSCRIPTEN_ROOT_PATH})
  117. if ("$ENV{USE_CCACHE}" AND NOT CMAKE_HOST_WIN32)
  118. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  119. if (CCACHE_SYMLINK AND EXISTS ${CCACHE_SYMLINK}/emcc AND EXISTS ${CCACHE_SYMLINK}/em++)
  120. set (EMSCRIPTEN_COMPILER_PATH ${CCACHE_SYMLINK})
  121. else ()
  122. # Fallback to create the ccache symlink in the build tree itself
  123. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  124. if (EXIT_CODE EQUAL 0 AND CCACHE)
  125. foreach (TOOL emcc em++)
  126. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${TOOL})
  127. endforeach ()
  128. set (EMSCRIPTEN_COMPILER_PATH ${CMAKE_BINARY_DIR})
  129. else ()
  130. message (WARNING "ccache may not have been installed on this host system. "
  131. "This is required to enable ccache support for Emscripten compiler toolchain. "
  132. "CMake has been configured to use the actual compiler toolchain instead of ccache. "
  133. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  134. endif ()
  135. endif ()
  136. if (NOT EMSCRIPTEN_COMPILER_PATH STREQUAL EMSCRIPTEN_ROOT_PATH AND NOT $ENV{PATH} MATCHES ${EMSCRIPTEN_ROOT_PATH})
  137. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${EMSCRIPTEN_ROOT_PATH}) has not been added in the PATH environment variable. "
  138. "This is required to enable ccache support for Emscripten compiler toolchain.")
  139. endif ()
  140. endif ()
  141. set (EMSCRIPTEN_COMPILER_PATH ${EMSCRIPTEN_COMPILER_PATH} CACHE INTERNAL "Path to C/C++ compiler tool symlinks or to the actual tools if not using ccache")
  142. endif ()
  143. set (CMAKE_C_COMPILER ${EMSCRIPTEN_COMPILER_PATH}/emcc${TOOL_EXT} CACHE PATH "C compiler")
  144. set (CMAKE_CXX_COMPILER ${EMSCRIPTEN_COMPILER_PATH}/em++${TOOL_EXT} CACHE PATH "C++ compiler")
  145. set (CMAKE_AR ${EMSCRIPTEN_ROOT_PATH}/emar${TOOL_EXT} CACHE PATH "archive")
  146. set (CMAKE_RANLIB ${EMSCRIPTEN_ROOT_PATH}/emranlib${TOOL_EXT} CACHE PATH "ranlib")
  147. set (CMAKE_LINKER ${EMSCRIPTEN_ROOT_PATH}/emlink.py CACHE PATH "linker")
  148. # Specific to Emscripten
  149. set (EMRUN ${EMSCRIPTEN_ROOT_PATH}/emrun${TOOL_EXT} CACHE PATH "emrun")
  150. set (EMPACKAGER python ${EMSCRIPTEN_ROOT_PATH}/tools/file_packager.py CACHE PATH "file_packager.py")
  151. set (EMBUILDER python ${EMSCRIPTEN_ROOT_PATH}/embuilder.py CACHE PATH "embuilder.py")
  152. # Still perform the compiler checks except for those stated otherwise below
  153. foreach (LANG C CXX)
  154. # Since currently CMake does not able to identify Emscripten compiler toolchain, set the compiler identification explicitly
  155. set (CMAKE_${LANG}_COMPILER_ID_RUN TRUE)
  156. set (CMAKE_${LANG}_COMPILER_ID Clang)
  157. set (CMAKE_${LANG}_COMPILER_VERSION ${EMSCRIPTEN_EMCC_VERSION})
  158. # The ABI info could not be checked as per normal as CMake does not understand the test build output from Emscripten, so bypass it also
  159. set (CMAKE_${LANG}_ABI_COMPILED TRUE)
  160. set (CMAKE_${LANG}_SIZEOF_DATA_PTR 4) # Assume it is always 32-bit for now (we could have used our CheckCompilerToolChains.cmake module here)
  161. # We could not set CMAKE_EXECUTABLE_SUFFIX directly because CMake processes platform configuration files after the toolchain file and since we tell CMake that we are cross-compiling for 'Linux' platform (Emscripten is not a valid platform yet in CMake) via CMAKE_SYSTEM_NAME variable, as such CMake force initializes the CMAKE_EXECUTABLE_SUFFIX to empty string (as expected for Linux platform); To workaround it we have to use CMAKE_EXECUTABLE_SUFFIX_C and CMAKE_EXECUTABLE_SUFFIX_CXX instead, which are fortunately not being touched by platform configuration files
  162. set (CMAKE_EXECUTABLE_SUFFIX_${LANG} .js)
  163. endforeach ()
  164. # Set required compiler flags for various internal CMake checks which rely on the compiler/linker error to be occured for the check to be performed correctly
  165. set (CMAKE_REQUIRED_FLAGS "-s ERROR_ON_UNDEFINED_SYMBOLS=1")
  166. # Use response files on Windows host
  167. if (CMAKE_HOST_WIN32)
  168. foreach (lang C CXX)
  169. foreach (cat LIBRARIES OBJECTS INCLUDES)
  170. set (CMAKE_${lang}_USE_RESPONSE_FILE_FOR_${cat} 1)
  171. endforeach ()
  172. set (CMAKE_${lang}_CREATE_STATIC_LIBRARY "<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS>")
  173. endforeach ()
  174. endif ()
  175. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  176. if (NOT IN_TRY_COMPILE)
  177. get_cmake_property (CACHE_VARIABLES CACHE_VARIABLES)
  178. foreach (VAR ${CACHE_VARIABLES})
  179. if (VAR MATCHES ^EMSCRIPTEN_|CMAKE_CX*_COMPILER)
  180. set (ENV{${VAR}} ${${VAR}})
  181. list (APPEND VARS ${VAR})
  182. endif ()
  183. endforeach ()
  184. set (ENV{VARS} "${VARS}") # Stringify to keep the list together
  185. endif ()
  186. set (EMSCRIPTEN 1)