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