Emscripten.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #
  2. # Copyright (c) 2008-2017 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{EMSCRIPTEN})
  59. file (TO_CMAKE_PATH $ENV{EMSCRIPTEN} EMSCRIPTEN_ROOT_PATH)
  60. endif ()
  61. endif ()
  62. set (EMSCRIPTEN_ROOT_PATH ${EMSCRIPTEN_ROOT_PATH} CACHE STRING "Root path to Emscripten cross-compiler tools (Emscripten only)")
  63. if (NOT EXISTS ${EMSCRIPTEN_ROOT_PATH}/emcc${TOOL_EXT})
  64. message (FATAL_ERROR "Could not find Emscripten cross compilation tool. "
  65. "Use EMSCRIPTEN_ROOT_PATH environment variable or build option to specify the location of the toolchain. "
  66. "Or use the canonical EMSCRIPTEN environment variable by calling emsdk_env script.")
  67. endif ()
  68. endif ()
  69. if (NOT SAVED_EMSCRIPTEN_SYSROOT)
  70. if (NOT EMSCRIPTEN_SYSROOT)
  71. if (DEFINED ENV{EMSCRIPTEN_SYSROOT})
  72. file (TO_CMAKE_PATH $ENV{EMSCRIPTEN_SYSROOT} EMSCRIPTEN_SYSROOT)
  73. else ()
  74. set (EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_ROOT_PATH}/system)
  75. endif ()
  76. endif ()
  77. set (EMSCRIPTEN_SYSROOT ${EMSCRIPTEN_SYSROOT} CACHE PATH "Path to Emscripten system root (Emscripten only)")
  78. if (NOT EXISTS ${EMSCRIPTEN_SYSROOT})
  79. message (FATAL_ERROR "Could not find Emscripten system root. "
  80. "Use EMSCRIPTEN_SYSROOT environment variable or build option to specify the location of system root.")
  81. endif ()
  82. endif ()
  83. endif ()
  84. set (CMAKE_SYSROOT ${EMSCRIPTEN_SYSROOT})
  85. # Only search libraries and headers in sysroot
  86. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  87. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  88. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  89. # Cross compiler tools
  90. if (CMAKE_HOST_WIN32)
  91. set (TOOL_EXT .bat)
  92. endif ()
  93. if (NOT EMSCRIPTEN_EMCC_VERSION)
  94. 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)
  95. if (EXIT_CODE EQUAL 0)
  96. string (REGEX MATCH "[^ .]+\\.[^.]+\\.[^ ]+" EMSCRIPTEN_EMCC_VERSION "${EMSCRIPTEN_EMCC_VERSION}")
  97. else ()
  98. message (FATAL_ERROR "Could not determine the emcc version. Make sure you have installed and activated the Emscripten SDK correctly.")
  99. endif ()
  100. set (EMSCRIPTEN_EMCC_VERSION ${EMSCRIPTEN_EMCC_VERSION} CACHE INTERNAL "emcc version being used in this build tree")
  101. endif ()
  102. # ccache support could only be enabled for emcc prior to 1.31.3 when the CCACHE_CPP2 env var is also set to 1, newer emcc version could enable ccache support without this caveat (see https://github.com/kripken/emscripten/issues/3365 for more detail)
  103. # The CCACHE_CPP2 env var tells ccache to fallback to use original input source file instead of preprocessed one when passing on the compilation task to the compiler proper
  104. if (NOT EMSCRIPTEN_COMPILER_PATH)
  105. set (EMSCRIPTEN_COMPILER_PATH ${EMSCRIPTEN_ROOT_PATH})
  106. if ("$ENV{USE_CCACHE}" AND NOT CMAKE_HOST_WIN32 AND ("$ENV{CCACHE_CPP2}" OR NOT EMSCRIPTEN_EMCC_VERSION VERSION_LESS 1.31.3))
  107. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  108. if (CCACHE_SYMLINK AND EXISTS ${CCACHE_SYMLINK}/emcc AND EXISTS ${CCACHE_SYMLINK}/em++)
  109. set (EMSCRIPTEN_COMPILER_PATH ${CCACHE_SYMLINK})
  110. else ()
  111. # Fallback to create the ccache symlink in the build tree itself
  112. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  113. if (EXIT_CODE EQUAL 0 AND CCACHE)
  114. foreach (TOOL emcc em++)
  115. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${TOOL})
  116. endforeach ()
  117. set (EMSCRIPTEN_COMPILER_PATH ${CMAKE_BINARY_DIR})
  118. else ()
  119. message (WARNING "ccache may not have been installed on this host system. "
  120. "This is required to enable ccache support for Emscripten compiler toolchain. "
  121. "CMake has been configured to use the actual compiler toolchain instead of ccache. "
  122. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  123. endif ()
  124. endif ()
  125. if (NOT EMSCRIPTEN_COMPILER_PATH STREQUAL EMSCRIPTEN_ROOT_PATH AND NOT $ENV{PATH} MATCHES ${EMSCRIPTEN_ROOT_PATH})
  126. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${EMSCRIPTEN_ROOT_PATH}) has not been added in the PATH environment variable. "
  127. "This is required to enable ccache support for Emscripten compiler toolchain.")
  128. endif ()
  129. endif ()
  130. 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")
  131. endif ()
  132. set (CMAKE_C_COMPILER ${EMSCRIPTEN_COMPILER_PATH}/emcc${TOOL_EXT} CACHE PATH "C compiler")
  133. set (CMAKE_CXX_COMPILER ${EMSCRIPTEN_COMPILER_PATH}/em++${TOOL_EXT} CACHE PATH "C++ compiler")
  134. set (CMAKE_AR ${EMSCRIPTEN_ROOT_PATH}/emar${TOOL_EXT} CACHE PATH "archive")
  135. set (CMAKE_RANLIB ${EMSCRIPTEN_ROOT_PATH}/emranlib${TOOL_EXT} CACHE PATH "ranlib")
  136. set (CMAKE_LINKER ${EMSCRIPTEN_ROOT_PATH}/emlink.py CACHE PATH "linker")
  137. # Specific to Emscripten
  138. set (EMRUN ${EMSCRIPTEN_ROOT_PATH}/emrun${TOOL_EXT} CACHE PATH "emrun")
  139. set (EMPACKAGER python ${EMSCRIPTEN_ROOT_PATH}/tools/file_packager.py CACHE PATH "file_packager.py")
  140. set (EMBUILDER python ${EMSCRIPTEN_ROOT_PATH}/embuilder.py CACHE PATH "embuilder.py")
  141. # Still perform the compiler checks except for those stated otherwise below
  142. foreach (LANG C CXX)
  143. # Since currently CMake does not able to identify Emscripten compiler toolchain, set the compiler identification explicitly
  144. set (CMAKE_${LANG}_COMPILER_ID_RUN TRUE)
  145. set (CMAKE_${LANG}_COMPILER_ID Clang)
  146. set (CMAKE_${LANG}_COMPILER_VERSION ${EMSCRIPTEN_EMCC_VERSION})
  147. # 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
  148. set (CMAKE_${LANG}_ABI_COMPILED TRUE)
  149. set (CMAKE_${LANG}_SIZEOF_DATA_PTR 4) # Assume it is always 32-bit for now (we could have used our CheckCompilerToolChains.cmake module here)
  150. # 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
  151. set (CMAKE_EXECUTABLE_SUFFIX_${LANG} .js)
  152. set (CMAKE_SHARED_LIBRARY_SUFFIX_${LANG} .bc) # "linked" LLVM bitcode
  153. set (CMAKE_SHARED_MODULE_SUFFIX_${LANG} .js) # side module
  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)