|
1 # This file is a 'toolchain description file' for CMake. |
|
2 # It teaches CMake about the Emscripten compiler, so that CMake can generate makefiles |
|
3 # from CMakeLists.txt that invoke emcc. |
|
4 |
|
5 # To use this toolchain file with CMake, invoke CMake with the following command line parameters |
|
6 # cmake -DCMAKE_TOOLCHAIN_FILE=<EmscriptenRoot>/cmake/Modules/Platform/Emscripten.cmake |
|
7 # -DCMAKE_BUILD_TYPE=<Debug|RelWithDebInfo|Release|MinSizeRel> |
|
8 # -G "Unix Makefiles" (Linux and OSX) |
|
9 # -G "MinGW Makefiles" (Windows) |
|
10 # <path/to/CMakeLists.txt> # Note, pass in here ONLY the path to the file, not the filename 'CMakeLists.txt' itself. |
|
11 |
|
12 # After that, build the generated Makefile with the command 'make'. On Windows, you may download and use 'mingw32-make' instead. |
|
13 |
|
14 # The following variable describes the target OS we are building to. |
|
15 set(CMAKE_SYSTEM_NAME Emscripten) |
|
16 set(CMAKE_SYSTEM_VERSION 1) |
|
17 |
|
18 set(CMAKE_CROSSCOMPILING TRUE) |
|
19 |
|
20 # Advertise Emscripten as a 32-bit platform (as opposed to CMAKE_SYSTEM_PROCESSOR=x86_64 for 64-bit platform), |
|
21 # since some projects (e.g. OpenCV) use this to detect bitness. |
|
22 set(CMAKE_SYSTEM_PROCESSOR x86) |
|
23 |
|
24 # Tell CMake how it should instruct the compiler to generate multiple versions of an outputted .so library: e.g. "libfoo.so, libfoo.so.1, libfoo.so.1.4" etc. |
|
25 # This feature is activated if a shared library project has the property SOVERSION defined. |
|
26 set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-soname,") |
|
27 |
|
28 # In CMake, CMAKE_HOST_WIN32 is set when we are cross-compiling from Win32 to Emscripten: http://www.cmake.org/cmake/help/v2.8.12/cmake.html#variable:CMAKE_HOST_WIN32 |
|
29 # The variable WIN32 is set only when the target arch that will run the code will be WIN32, so unset WIN32 when cross-compiling. |
|
30 set(WIN32) |
|
31 |
|
32 # The same logic as above applies for APPLE and CMAKE_HOST_APPLE, so unset APPLE. |
|
33 set(APPLE) |
|
34 |
|
35 # And for UNIX and CMAKE_HOST_UNIX. However, Emscripten is often able to mimic being a Linux/Unix system, in which case a lot of existing CMakeLists.txt files can be configured for Emscripten while assuming UNIX build, so this is left enabled. |
|
36 set(UNIX 1) |
|
37 |
|
38 # Do a no-op access on the CMAKE_TOOLCHAIN_FILE variable so that CMake will not issue a warning on it being unused. |
|
39 if(CMAKE_TOOLCHAIN_FILE) |
|
40 endif() |
|
41 |
|
42 # In order for check_function_exists() detection to work, we must signal it to pass an additional flag, which causes the compilation |
|
43 # to abort if linking results in any undefined symbols. The CMake detection mechanism depends on the undefined symbol error to be raised. |
|
44 set(CMAKE_REQUIRED_FLAGS "-s ERROR_ON_UNDEFINED_SYMBOLS=1") |
|
45 |
|
46 # Locate where the Emscripten compiler resides in relative to this toolchain file. |
|
47 if("${EMSCRIPTEN_ROOT_PATH}" STREQUAL "") |
|
48 get_filename_component(GUESS_EMSCRIPTEN_ROOT_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE) |
|
49 if(EXISTS "${GUESS_EMSCRIPTEN_ROOT_PATH}/emranlib") |
|
50 set(EMSCRIPTEN_ROOT_PATH "${GUESS_EMSCRIPTEN_ROOT_PATH}") |
|
51 endif() |
|
52 endif() |
|
53 |
|
54 # If not found by above search, locate using the EMSCRIPTEN environment variable. |
|
55 if("${EMSCRIPTEN_ROOT_PATH}" STREQUAL "") |
|
56 set(EMSCRIPTEN_ROOT_PATH "$ENV{EMSCRIPTEN}") |
|
57 endif() |
|
58 |
|
59 # Abort if not found. |
|
60 if("${EMSCRIPTEN_ROOT_PATH}" STREQUAL "") |
|
61 message(FATAL_ERROR "Could not locate the Emscripten compiler toolchain directory! Either set the EMSCRIPTEN environment variable, or pass -DEMSCRIPTEN_ROOT_PATH=xxx to CMake to explicitly specify the location of the compiler! This usually matches EMSCRIPTEN_ROOT from your ~/.emscripten file.") |
|
62 endif() |
|
63 |
|
64 # Normalize, convert Windows backslashes to forward slashes or CMake will crash. |
|
65 get_filename_component(EMSCRIPTEN_ROOT_PATH "${EMSCRIPTEN_ROOT_PATH}" ABSOLUTE) |
|
66 |
|
67 if(NOT CMAKE_MODULE_PATH) |
|
68 set(CMAKE_MODULE_PATH "") |
|
69 endif() |
|
70 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${EMSCRIPTEN_ROOT_PATH}/cmake/Modules") |
|
71 |
|
72 set(CMAKE_FIND_ROOT_PATH "${EMSCRIPTEN_ROOT_PATH}/system") |
|
73 |
|
74 if(CMAKE_HOST_WIN32) |
|
75 set(EMCC_SUFFIX ".bat") |
|
76 else() |
|
77 set(EMCC_SUFFIX "") |
|
78 endif() |
|
79 |
|
80 # Specify the compilers to use for C and C++ |
|
81 if("${CMAKE_C_COMPILER}" STREQUAL "") |
|
82 set(CMAKE_C_COMPILER "${EMSCRIPTEN_ROOT_PATH}/emcc${EMCC_SUFFIX}") |
|
83 endif() |
|
84 if("${CMAKE_CXX_COMPILER}" STREQUAL "") |
|
85 set(CMAKE_CXX_COMPILER "${EMSCRIPTEN_ROOT_PATH}/em++${EMCC_SUFFIX}") |
|
86 endif() |
|
87 |
|
88 if("${CMAKE_AR}" STREQUAL "") |
|
89 set(CMAKE_AR "${EMSCRIPTEN_ROOT_PATH}/emar${EMCC_SUFFIX}" CACHE FILEPATH "Emscripten ar") |
|
90 endif() |
|
91 |
|
92 if("${CMAKE_RANLIB}" STREQUAL "") |
|
93 set(CMAKE_RANLIB "${EMSCRIPTEN_ROOT_PATH}/emranlib${EMCC_SUFFIX}" CACHE FILEPATH "Emscripten ranlib") |
|
94 endif() |
|
95 |
|
96 # Don't do compiler autodetection, since we are cross-compiling. |
|
97 include(CMakeForceCompiler) |
|
98 CMAKE_FORCE_C_COMPILER("${CMAKE_C_COMPILER}" Clang) |
|
99 CMAKE_FORCE_CXX_COMPILER("${CMAKE_CXX_COMPILER}" Clang) |
|
100 |
|
101 # To find programs to execute during CMake run time with find_program(), e.g. 'git' or so, we allow looking |
|
102 # into system paths. |
|
103 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) |
|
104 |
|
105 # Since Emscripten is a cross-compiler, we should never look at the system-provided directories like /usr/include and so on. |
|
106 # Therefore only CMAKE_FIND_ROOT_PATH should be used as a find directory. See http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_FIND_ROOT_PATH_MODE_INCLUDE.html |
|
107 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) |
|
108 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) |
|
109 set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) |
|
110 |
|
111 set(CMAKE_SYSTEM_INCLUDE_PATH "${EMSCRIPTEN_ROOT_PATH}/system/include") |
|
112 |
|
113 # We would prefer to specify a standard set of Clang+Emscripten-friendly common convention for suffix files, especially for CMake executable files, |
|
114 # but if these are adjusted, ${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake will fail, since it depends on being able to compile output files with predefined names. |
|
115 #SET(CMAKE_LINK_LIBRARY_SUFFIX "") |
|
116 #SET(CMAKE_STATIC_LIBRARY_PREFIX "") |
|
117 #SET(CMAKE_STATIC_LIBRARY_SUFFIX ".bc") |
|
118 #SET(CMAKE_SHARED_LIBRARY_PREFIX "") |
|
119 #SET(CMAKE_SHARED_LIBRARY_SUFFIX ".bc") |
|
120 SET(CMAKE_EXECUTABLE_SUFFIX ".js") |
|
121 #SET(CMAKE_FIND_LIBRARY_PREFIXES "") |
|
122 #SET(CMAKE_FIND_LIBRARY_SUFFIXES ".bc") |
|
123 |
|
124 SET(CMAKE_C_USE_RESPONSE_FILE_FOR_LIBRARIES 1) |
|
125 SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_LIBRARIES 1) |
|
126 SET(CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS 1) |
|
127 SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS 1) |
|
128 SET(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 1) |
|
129 SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 1) |
|
130 |
|
131 set(CMAKE_C_RESPONSE_FILE_LINK_FLAG "@") |
|
132 set(CMAKE_CXX_RESPONSE_FILE_LINK_FLAG "@") |
|
133 |
|
134 # Specify the program to use when building static libraries. Force Emscripten-related command line options to clang. |
|
135 set(CMAKE_C_CREATE_STATIC_LIBRARY "<CMAKE_AR> rc <TARGET> <LINK_FLAGS> <OBJECTS>") |
|
136 set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_AR> rc <TARGET> <LINK_FLAGS> <OBJECTS>") |
|
137 |
|
138 # Set a global EMSCRIPTEN variable that can be used in client CMakeLists.txt to detect when building using Emscripten. |
|
139 set(EMSCRIPTEN 1 CACHE BOOL "If true, we are targeting Emscripten output.") |
|
140 |
|
141 # Hardwire support for cmake-2.8/Modules/CMakeBackwardsCompatibilityC.cmake without having CMake to try complex things |
|
142 # to autodetect these: |
|
143 set(CMAKE_SKIP_COMPATIBILITY_TESTS 1) |
|
144 set(CMAKE_SIZEOF_CHAR 1) |
|
145 set(CMAKE_SIZEOF_UNSIGNED_SHORT 2) |
|
146 set(CMAKE_SIZEOF_SHORT 2) |
|
147 set(CMAKE_SIZEOF_INT 4) |
|
148 set(CMAKE_SIZEOF_UNSIGNED_LONG 4) |
|
149 set(CMAKE_SIZEOF_UNSIGNED_INT 4) |
|
150 set(CMAKE_SIZEOF_LONG 4) |
|
151 set(CMAKE_SIZEOF_VOID_P 4) |
|
152 set(CMAKE_SIZEOF_FLOAT 4) |
|
153 set(CMAKE_SIZEOF_DOUBLE 8) |
|
154 set(CMAKE_C_SIZEOF_DATA_PTR 4) |
|
155 set(CMAKE_CXX_SIZEOF_DATA_PTR 4) |
|
156 set(CMAKE_HAVE_LIMITS_H 1) |
|
157 set(CMAKE_HAVE_UNISTD_H 1) |
|
158 set(CMAKE_HAVE_PTHREAD_H 1) |
|
159 set(CMAKE_HAVE_SYS_PRCTL_H 1) |
|
160 set(CMAKE_WORDS_BIGENDIAN 0) |
|
161 set(CMAKE_DL_LIBS) |
|
162 |
|
163 set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2" CACHE STRING "Emscripten-overridden CMAKE_C_FLAGS_RELEASE") |
|
164 set(CMAKE_C_FLAGS_MINSIZEREL "-DNDEBUG -Os" CACHE STRING "Emscripten-overridden CMAKE_C_FLAGS_MINSIZEREL") |
|
165 set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2" CACHE STRING "Emscripten-overridden CMAKE_C_FLAGS_RELWITHDEBINFO") |
|
166 set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O2" CACHE STRING "Emscripten-overridden CMAKE_CXX_FLAGS_RELEASE") |
|
167 set(CMAKE_CXX_FLAGS_MINSIZEREL "-DNDEBUG -Os" CACHE STRING "Emscripten-overridden CMAKE_CXX_FLAGS_MINSIZEREL") |
|
168 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2" CACHE STRING "Emscripten-overridden CMAKE_CXX_FLAGS_RELWITHDEBINFO") |
|
169 |
|
170 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-O2" CACHE STRING "Emscripten-overridden CMAKE_EXE_LINKER_FLAGS_RELEASE") |
|
171 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "-Os" CACHE STRING "Emscripten-overridden CMAKE_EXE_LINKER_FLAGS_MINSIZEREL") |
|
172 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "-O2 -g" CACHE STRING "Emscripten-overridden CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO") |
|
173 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "-O2" CACHE STRING "Emscripten-overridden CMAKE_SHARED_LINKER_FLAGS_RELEASE") |
|
174 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "-Os" CACHE STRING "Emscripten-overridden CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL") |
|
175 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "-O2 -g" CACHE STRING "Emscripten-overridden CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO") |
|
176 set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "-O2" CACHE STRING "Emscripten-overridden CMAKE_MODULE_LINKER_FLAGS_RELEASE") |
|
177 set(CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL "-Os" CACHE STRING "Emscripten-overridden CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL") |
|
178 set(CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO "-O2 -g" CACHE STRING "Emscripten-overridden CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO") |
|
179 |
|
180 function(em_validate_asmjs_after_build target) |
|
181 add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo Validating build output for asm.js... COMMAND "python" ARGS "${EMSCRIPTEN_ROOT_PATH}/tools/validate_asmjs.py" "$<TARGET_FILE:${target}>") |
|
182 endfunction() |
|
183 |
|
184 # A global counter to guarantee unique names for js library files. |
|
185 set(link_js_counter 1) |
|
186 |
|
187 # Internal function: Do not call from user CMakeLists.txt files. Use one of em_link_js_library()/em_link_pre_js()/em_link_post_js() instead. |
|
188 function(em_add_tracked_link_flag target flagname) |
|
189 |
|
190 # User can input list of JS files either as a single list, or as variable arguments to this function, so iterate over varargs, and treat each |
|
191 # item in varargs as a list itself, to support both syntax forms. |
|
192 foreach(jsFileList ${ARGN}) |
|
193 foreach(jsfile ${jsFileList}) |
|
194 # If the user edits the JS file, we want to relink the emscripten application, but unfortunately it is not possible to make a link step |
|
195 # depend directly on a source file. Instead, we must make a dummy no-op build target on that source file, and make the project depend on |
|
196 # that target. |
|
197 |
|
198 # Sanitate the source .js filename to a good symbol name to use as a dummy filename. |
|
199 get_filename_component(jsname "${jsfile}" NAME) |
|
200 string(REGEX REPLACE "[/:\\\\.\ ]" "_" dummy_js_target ${jsname}) |
|
201 set(dummy_lib_name ${target}_${link_js_counter}_${dummy_js_target}) |
|
202 set(dummy_c_name "${CMAKE_BINARY_DIR}/${dummy_js_target}_tracker.c") |
|
203 |
|
204 # Create a new static library target that with a single dummy .c file. |
|
205 add_library(${dummy_lib_name} STATIC ${dummy_c_name}) |
|
206 # Make the dummy .c file depend on the .js file we are linking, so that if the .js file is edited, the dummy .c file, and hence the static library will be rebuild (no-op). This causes the main application to be relinked, which is what we want. |
|
207 # This approach was recommended by http://www.cmake.org/pipermail/cmake/2010-May/037206.html |
|
208 add_custom_command(OUTPUT ${dummy_c_name} COMMAND ${CMAKE_COMMAND} -E touch ${dummy_c_name} DEPENDS ${jsfile}) |
|
209 target_link_libraries(${target} ${dummy_lib_name}) |
|
210 |
|
211 # Link the js-library to the target |
|
212 # When a linked library starts with a "-" cmake will just add it to the linker command line as it is. |
|
213 # The advantage of doing it this way is that the js-library will also be automatically linked to targets |
|
214 # that depend on this target. |
|
215 get_filename_component(js_file_absolute_path "${jsfile}" ABSOLUTE ) |
|
216 target_link_libraries(${target} "${flagname} \"${js_file_absolute_path}\"") |
|
217 |
|
218 math(EXPR link_js_counter "${link_js_counter} + 1") |
|
219 endforeach() |
|
220 endforeach() |
|
221 endfunction() |
|
222 |
|
223 # This function links a (list of ) .js library file(s) to the given CMake project. |
|
224 # Example: em_link_js_library(my_executable "lib1.js" "lib2.js") |
|
225 # will result in emcc passing --js-library lib1.js --js-library lib2.js to the emscripten linker, as well as |
|
226 # tracking the modification timestamp between the linked .js files and the main project, so that editing the .js file |
|
227 # will cause the target project to be relinked. |
|
228 function(em_link_js_library target) |
|
229 em_add_tracked_link_flag(${target} "--js-library" ${ARGN}) |
|
230 endfunction() |
|
231 |
|
232 # This function is identical to em_link_js_library(), except the .js files will be added with '--pre-js file.js' command line flag, |
|
233 # which is generally used to add some preamble .js code to a generated output file. |
|
234 function(em_link_pre_js target) |
|
235 em_add_tracked_link_flag(${target} "--pre-js" ${ARGN}) |
|
236 endfunction() |
|
237 |
|
238 # This function is identical to em_link_js_library(), except the .js files will be added with '--post-js file.js' command line flag, |
|
239 # which is generally used to add some postamble .js code to a generated output file. |
|
240 function(em_link_post_js target) |
|
241 em_add_tracked_link_flag(${target} "--post-js" ${ARGN}) |
|
242 endfunction() |
|
243 |
|
244 # Experimental support for targeting generation of Visual Studio project files (vs-tool) of Emscripten projects for Windows. |
|
245 # To use this, pass the combination -G "Visual Studio 10" -DCMAKE_TOOLCHAIN_FILE=Emscripten.cmake |
|
246 if ("${CMAKE_GENERATOR}" MATCHES "^Visual Studio.*") |
|
247 # By default, CMake generates VS project files with a <GenerateManifest>true</GenerateManifest> directive. |
|
248 # This causes VS to attempt to invoke rc.exe during the build, which will fail since app manifests are meaningless for Emscripten. |
|
249 # To disable this, add the following linker flag. This flag will not go to emcc, since the Visual Studio CMake generator will swallow it. |
|
250 set(EMSCRIPTEN_VS_LINKER_FLAGS "/MANIFEST:NO") |
|
251 # CMake is hardcoded to write a ClCompile directive <ObjectFileName>$(IntDir)</ObjectFileName> in all VS project files it generates. |
|
252 # This makes VS pass emcc a -o param that points to a directory instead of a file, which causes emcc autogenerate the output filename. |
|
253 # CMake is hardcoded to assume all object files have the suffix .obj, so adjust the emcc-autogenerated default suffix name to match. |
|
254 set(EMSCRIPTEN_VS_LINKER_FLAGS "${EMSCRIPTEN_VS_LINKER_FLAGS} --default-obj-ext .obj") |
|
255 # Also hint CMake that it should not hardcode <ObjectFileName> generation. Requires a custom CMake build for this to work (ignored on others) |
|
256 # See http://www.cmake.org/Bug/view.php?id=14673 and https://github.com/juj/CMake |
|
257 set(CMAKE_VS_NO_DEFAULT_OBJECTFILENAME 1) |
|
258 |
|
259 # Apply and cache Emscripten Visual Studio IDE-specific linker flags. |
|
260 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMSCRIPTEN_VS_LINKER_FLAGS}" CACHE STRING "") |
|
261 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${EMSCRIPTEN_VS_LINKER_FLAGS}" CACHE STRING "") |
|
262 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${EMSCRIPTEN_VS_LINKER_FLAGS}" CACHE STRING "") |
|
263 endif() |
|
264 |
|
265 if(NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR) |
|
266 find_program(NODE_JS_EXECUTABLE NAMES nodejs node) |
|
267 if(NODE_JS_EXECUTABLE) |
|
268 set(CMAKE_CROSSCOMPILING_EMULATOR "${NODE_JS_EXECUTABLE}" CACHE FILEPATH "Path to the emulator for the target system.") |
|
269 endif() |
|
270 endif() |
|
271 |
|
272 # No-op on CMAKE_CROSSCOMPILING_EMULATOR so older versions of cmake do not |
|
273 # complain about unused CMake variable. |
|
274 if(CMAKE_CROSSCOMPILING_EMULATOR) |
|
275 endif() |