Emscripten.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. file (STRINGS $ENV{EM_CONFIG} EMSCRIPTEN_ROOT_PATH REGEX "^EMSCRIPTEN_ROOT = '.*'$")
  60. string (REGEX REPLACE "^EMSCRIPTEN_ROOT = '(.*)'$" \\1 EMSCRIPTEN_ROOT_PATH "${EMSCRIPTEN_ROOT_PATH}") # Stringify to guard against empty variable
  61. endif ()
  62. endif ()
  63. set (EMSCRIPTEN_ROOT_PATH ${EMSCRIPTEN_ROOT_PATH} CACHE STRING "Root path to Emscripten cross-compiler tools (Emscripten only)")
  64. if (NOT EXISTS ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT})
  65. message (FATAL_ERROR "Could not find Emscripten cross compilation tool. "
  66. "Use EMSCRIPTEN_ROOT_PATH environment variable or build option to specify the location of the toolchain. "
  67. "Or use the canonical EMSCRIPTEN environment variable by calling emsdk_env script.")
  68. endif ()
  69. endif ()
  70. if (NOT SAVED_EMSCRIPTEN_SYSROOT)
  71. if (NOT EMSCRIPTEN_SYSROOT)
  72. if (DEFINED ENV{EMSCRIPTEN_SYSROOT})
  73. file (TO_CMAKE_PATH $ENV{EMSCRIPTEN_SYSROOT} EMSCRIPTEN_SYSROOT)
  74. else ()
  75. set (EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_ROOT_PATH}/system)
  76. endif ()
  77. endif ()
  78. set (EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_SYSROOT} CACHE PATH "Path to Emscripten system root (Emscripten only)")
  79. if (NOT EXISTS ${EMSCRIPTEN_SYSROOT})
  80. message (FATAL_ERROR "Could not find Emscripten system root. "
  81. "Use EMSCRIPTEN_SYSROOT environment variable or build option to specify the location of system root.")
  82. endif ()
  83. endif ()
  84. endif ()
  85. set (CMAKE_SYSROOT ${EMSCRIPTEN_SYSROOT})
  86. # Only search libraries and headers in sysroot
  87. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  88. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  89. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  90. # Cross compiler tools
  91. if (CMAKE_HOST_WIN32)
  92. set (TOOL_EXT .bat)
  93. endif ()
  94. if (NOT EMSCRIPTEN_EMCC_VERSION)
  95. 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)
  96. if (EXIT_CODE EQUAL 0)
  97. string (REGEX MATCH "[^ .]+\\.[^.]+\\.[^ ]+" EMSCRIPTEN_EMCC_VERSION "${EMSCRIPTEN_EMCC_VERSION}")
  98. else ()
  99. message (FATAL_ERROR "Could not determine the emcc version. Make sure you have installed and activated the Emscripten SDK correctly.")
  100. endif ()
  101. set (EMSCRIPTEN_EMCC_VERSION ${EMSCRIPTEN_EMCC_VERSION} CACHE INTERNAL "emcc version being used in this build tree")
  102. if (EMSCRIPTEN_EMCC_VERSION VERSION_LESS 1.39.0)
  103. message (FATAL_ERROR "Emscripten SDK 1.39.0 or later is required.")
  104. endif ()
  105. endif ()
  106. if (NOT EMSCRIPTEN_COMPILER_PATH)
  107. set (EMSCRIPTEN_COMPILER_PATH ${EMSCRIPTEN_ROOT_PATH})
  108. if ("$ENV{USE_CCACHE}" AND NOT CMAKE_HOST_WIN32)
  109. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  110. if (CCACHE_SYMLINK AND EXISTS ${CCACHE_SYMLINK}/emcc AND EXISTS ${CCACHE_SYMLINK}/em++)
  111. set (EMSCRIPTEN_COMPILER_PATH ${CCACHE_SYMLINK})
  112. else ()
  113. # Fallback to create the ccache symlink in the build tree itself
  114. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  115. if (EXIT_CODE EQUAL 0 AND CCACHE)
  116. foreach (TOOL emcc em++)
  117. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${TOOL})
  118. endforeach ()
  119. set (EMSCRIPTEN_COMPILER_PATH ${CMAKE_BINARY_DIR})
  120. else ()
  121. message (WARNING "ccache may not have been installed on this host system. "
  122. "This is required to enable ccache support for Emscripten compiler toolchain. "
  123. "CMake has been configured to use the actual compiler toolchain instead of ccache. "
  124. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  125. endif ()
  126. endif ()
  127. if (NOT EMSCRIPTEN_COMPILER_PATH STREQUAL EMSCRIPTEN_ROOT_PATH AND NOT $ENV{PATH} MATCHES ${EMSCRIPTEN_ROOT_PATH})
  128. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${EMSCRIPTEN_ROOT_PATH}) has not been added in the PATH environment variable. "
  129. "This is required to enable ccache support for Emscripten compiler toolchain.")
  130. endif ()
  131. endif ()
  132. 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")
  133. endif ()
  134. set (CMAKE_C_COMPILER ${EMSCRIPTEN_COMPILER_PATH}/emcc${TOOL_EXT} CACHE PATH "C compiler")
  135. set (CMAKE_CXX_COMPILER ${EMSCRIPTEN_COMPILER_PATH}/em++${TOOL_EXT} CACHE PATH "C++ compiler")
  136. set (CMAKE_AR ${EMSCRIPTEN_ROOT_PATH}/emar${TOOL_EXT} CACHE PATH "archive")
  137. set (CMAKE_RANLIB ${EMSCRIPTEN_ROOT_PATH}/emranlib${TOOL_EXT} CACHE PATH "ranlib")
  138. set (CMAKE_LINKER ${EMSCRIPTEN_ROOT_PATH}/emlink.py CACHE PATH "linker")
  139. # Specific to Emscripten
  140. set (EMRUN ${EMSCRIPTEN_ROOT_PATH}/emrun${TOOL_EXT} CACHE PATH "emrun")
  141. set (EMPACKAGER python ${EMSCRIPTEN_ROOT_PATH}/tools/file_packager.py CACHE PATH "file_packager.py")
  142. set (EMBUILDER python ${EMSCRIPTEN_ROOT_PATH}/embuilder.py CACHE PATH "embuilder.py")
  143. # Still perform the compiler checks except for those stated otherwise below
  144. foreach (LANG C CXX)
  145. # Since currently CMake does not able to identify Emscripten compiler toolchain, set the compiler identification explicitly
  146. set (CMAKE_${LANG}_COMPILER_ID_RUN TRUE)
  147. set (CMAKE_${LANG}_COMPILER_ID Clang)
  148. set (CMAKE_${LANG}_COMPILER_VERSION ${EMSCRIPTEN_EMCC_VERSION})
  149. # 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
  150. set (CMAKE_${LANG}_ABI_COMPILED TRUE)
  151. set (CMAKE_${LANG}_SIZEOF_DATA_PTR 4) # Assume it is always 32-bit for now (we could have used our CheckCompilerToolChains.cmake module here)
  152. # 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
  153. set (CMAKE_EXECUTABLE_SUFFIX_${LANG} .js)
  154. endforeach ()
  155. # 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
  156. set (CMAKE_REQUIRED_FLAGS "-s ERROR_ON_UNDEFINED_SYMBOLS=1")
  157. # Use response files on Windows host
  158. if (CMAKE_HOST_WIN32)
  159. foreach (lang C CXX)
  160. foreach (cat LIBRARIES OBJECTS INCLUDES)
  161. set (CMAKE_${lang}_USE_RESPONSE_FILE_FOR_${cat} 1)
  162. endforeach ()
  163. set (CMAKE_${lang}_CREATE_STATIC_LIBRARY "<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS>")
  164. endforeach ()
  165. endif ()
  166. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  167. if (NOT IN_TRY_COMPILE)
  168. get_cmake_property (CACHE_VARIABLES CACHE_VARIABLES)
  169. foreach (VAR ${CACHE_VARIABLES})
  170. if (VAR MATCHES ^EMSCRIPTEN_|CMAKE_CX*_COMPILER)
  171. set (ENV{${VAR}} ${${VAR}})
  172. list (APPEND VARS ${VAR})
  173. endif ()
  174. endforeach ()
  175. set (ENV{VARS} "${VARS}") # Stringify to keep the list together
  176. endif ()
  177. set (EMSCRIPTEN 1)